Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
StarWishsama committed Feb 11, 2025
2 parents f1d7ce2 + cc1da97 commit b728ecb
Show file tree
Hide file tree
Showing 21 changed files with 267 additions and 112 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/pr-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
name: Pull Request Checker

on:
pull_request:
pull_request_target:
types: [opened, synchronize, reopened]
paths:
- '.github/workflows/**'
- 'src/**'
Expand Down Expand Up @@ -34,7 +35,7 @@ jobs:
cache: 'maven'

- name: Codestyle Check
run: mvn -s .mvn/settings.xml -B spotless:check compile --errors
run: mvn -s .mvn/settings.xml -B spotless:check --errors

# Setup for the preview build
- name: Environment Setup
Expand All @@ -48,7 +49,7 @@ jobs:
sed -i "s/<version>UNOFFICIAL<\/version>/<version>$JAR_VERSION<\/version>/g" pom.xml
- name: Build with Maven
run: mvn -s .mvn/settings.xml package --errors
run: mvn -s .mvn/settings.xml package -Dversioning.disable --errors

- name: Upload the artifact
uses: actions/upload-artifact@v4
Expand Down
2 changes: 1 addition & 1 deletion .mvn/extensions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<extension>
<groupId>me.qoomon</groupId>
<artifactId>maven-git-versioning-extension</artifactId>
<version>9.6.6</version>
<version>9.8.1</version>
</extension>

</extensions>
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<!-- Dependency shading -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.1</version>
<version>3.6.0</version>

<configuration>
<!-- Relocate these to avoid clashes and conflicts -->
Expand Down Expand Up @@ -337,15 +337,15 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.3</version>
<version>42.7.4</version>
<scope>compile</scope>
</dependency>

<!-- Third party plugin integrations / soft dependencies -->
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-core</artifactId>
<version>7.2.19</version>
<version>7.3.6</version>
<scope>provided</scope>

<exclusions>
Expand All @@ -359,7 +359,7 @@
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>7.2.19</version>
<version>7.3.6</version>
<scope>provided</scope>

<exclusions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import java.util.List;

public interface IDataSourceAdapter<T> {
int DATABASE_VERSION = 1;
/**
* 当前数据库架构版本号
* 在数据库结构有变动时更新
*/
int DATABASE_VERSION = 2;

void prepare(T config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_PLAYER_UUID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_RESEARCH_KEY;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_SLIMEFUN_ID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_VERSION;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_METADATA_KEY;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_METADATA_VALUE;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_TRAITS;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_UUID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.METADATA_VERSION;

import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.IDataSourceAdapter;
import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlCommonAdapter;
Expand All @@ -26,6 +28,7 @@
import com.xzavier0722.mc.plugin.slimefun4.storage.common.RecordKey;
import com.xzavier0722.mc.plugin.slimefun4.storage.common.RecordSet;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import java.text.MessageFormat;
import java.util.List;

public class MysqlAdapter extends SqlCommonAdapter<MysqlConfig> {
Expand All @@ -51,8 +54,8 @@ public void initStorage(DataType type) {
}
}

tableInformationTable = SqlUtils.mapTable(DataScope.TABLE_INFORMATION, config.tablePrefix());
createTableInformationTable();
tableMetadataTable = SqlUtils.mapTable(DataScope.TABLE_METADATA, config.tablePrefix());
createTableMetadataTable();
}

@Override
Expand Down Expand Up @@ -388,18 +391,27 @@ private void createUniversalDataTable() {
+ ");");
}

private void createTableInformationTable() {
executeSql("CREATE TABLE IF NOT EXISTS "
+ tableInformationTable
+ "("
+ FIELD_TABLE_VERSION
+ " INT UNIQUE NOT NULL DEFAULT '0'"
+ ");");
private void createTableMetadataTable() {
executeSql(MessageFormat.format(
"""
CREATE TABLE IF NOT EXISTS {0}
(
{1} VARCHAR(100) UNIQUE NOT NULL,
{2} TEXT NOT NULL
);
""",
tableMetadataTable, FIELD_TABLE_METADATA_KEY, FIELD_TABLE_METADATA_VALUE));

if (Slimefun.isNewlyInstalled()) {
executeSql("INSERT INTO " + tableInformationTable + " (" + FIELD_TABLE_VERSION + ") SELECT '"
+ IDataSourceAdapter.DATABASE_VERSION + "' WHERE NOT EXISTS (SELECT 1 FROM " + tableInformationTable
+ ")");
executeSql(MessageFormat.format(
"""
INSERT INTO {0} ({1}, {2}) VALUES ({3}, {4});
""",
tableMetadataTable,
FIELD_TABLE_METADATA_KEY,
FIELD_TABLE_METADATA_VALUE,
METADATA_VERSION,
IDataSourceAdapter.DATABASE_VERSION));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_PLAYER_UUID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_RESEARCH_KEY;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_SLIMEFUN_ID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_VERSION;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_METADATA_KEY;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_METADATA_VALUE;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_TRAITS;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_UUID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.METADATA_VERSION;

import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.IDataSourceAdapter;
import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlCommonAdapter;
Expand All @@ -26,6 +28,7 @@
import com.xzavier0722.mc.plugin.slimefun4.storage.common.RecordKey;
import com.xzavier0722.mc.plugin.slimefun4.storage.common.RecordSet;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -53,8 +56,8 @@ public void initStorage(DataType type) {
}
}

tableInformationTable = SqlUtils.mapTable(DataScope.TABLE_INFORMATION, config.tablePrefix());
createTableInformationTable();
tableMetadataTable = SqlUtils.mapTable(DataScope.TABLE_METADATA, config.tablePrefix());
createTableMetadataTable();
}

@Override
Expand Down Expand Up @@ -404,18 +407,27 @@ private void createUniversalDataTable() {
+ ");");
}

private void createTableInformationTable() {
executeSql("CREATE TABLE IF NOT EXISTS "
+ tableInformationTable
+ "("
+ FIELD_TABLE_VERSION
+ " INT UNIQUE NOT NULL DEFAULT '0'"
+ ");");
private void createTableMetadataTable() {
executeSql(MessageFormat.format(
"""
CREATE TABLE IF NOT EXISTS {0}
(
{1} VARCHAR(100) UNIQUE NOT NULL,
{2} TEXT NOT NULL
);
""",
tableMetadataTable, FIELD_TABLE_METADATA_KEY, FIELD_TABLE_METADATA_VALUE));

if (Slimefun.isNewlyInstalled()) {
executeSql("INSERT INTO " + tableInformationTable + " (" + FIELD_TABLE_VERSION + ") SELECT '"
+ IDataSourceAdapter.DATABASE_VERSION + "' WHERE NOT EXISTS (SELECT 1 FROM " + tableInformationTable
+ ")");
executeSql(MessageFormat.format(
"""
INSERT INTO {0} ({1}, {2}) VALUES ({3}, {4});
""",
tableMetadataTable,
FIELD_TABLE_METADATA_KEY,
FIELD_TABLE_METADATA_VALUE,
METADATA_VERSION,
IDataSourceAdapter.DATABASE_VERSION));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon;

import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_METADATA_KEY;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_METADATA_VALUE;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_VERSION;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.METADATA_VERSION;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_TABLE_INFORMATION;

import city.norain.slimefun4.timings.entry.SQLEntry;
Expand All @@ -10,6 +13,7 @@
import com.xzavier0722.mc.plugin.slimefun4.storage.common.RecordSet;
import com.xzavier0722.mc.plugin.slimefun4.storage.patch.DatabasePatch;
import com.xzavier0722.mc.plugin.slimefun4.storage.patch.DatabasePatchV1;
import com.xzavier0722.mc.plugin.slimefun4.storage.patch.DatabasePatchV2;
import com.zaxxer.hikari.HikariDataSource;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import java.sql.SQLException;
Expand All @@ -26,7 +30,7 @@ public abstract class SqlCommonAdapter<T extends ISqlCommonConfig> implements ID
chunkDataTable,
blockInvTable,
universalInvTable;
protected String tableInformationTable;
protected String tableMetadataTable;
protected T config;

@Override
Expand Down Expand Up @@ -73,7 +77,7 @@ protected String mapTable(DataScope scope) {
case UNIVERSAL_INVENTORY -> universalInvTable;
case UNIVERSAL_RECORD -> universalRecordTable;
case UNIVERSAL_DATA -> universalDataTable;
case TABLE_INFORMATION -> tableInformationTable;
case TABLE_METADATA -> tableMetadataTable;
case NONE -> throw new IllegalArgumentException("NONE cannot be a storage data scope!");
};
}
Expand All @@ -93,28 +97,47 @@ public void shutdown() {
universalInvTable = null;
universalDataTable = null;
universalRecordTable = null;
tableMetadataTable = null;
}

public int getDatabaseVersion() {
var query = executeQuery("SELECT (" + FIELD_TABLE_VERSION + ") FROM "
+ (tableInformationTable == null ? TABLE_NAME_TABLE_INFORMATION : tableInformationTable));
if (Slimefun.isNewlyInstalled()) {
return IDataSourceAdapter.DATABASE_VERSION;
}

var query = executeQuery(String.format(
"SELECT (%s) FROM %s WHERE %s='%s';",
FIELD_TABLE_METADATA_VALUE, tableMetadataTable, FIELD_TABLE_METADATA_KEY, METADATA_VERSION));

if (query.isEmpty()) {
return 0;
try {
var prefix = config instanceof SqlCommonConfig sqc ? sqc.tablePrefix() : "";
var fallbackQuery = executeQuery(
"SELECT (" + FIELD_TABLE_VERSION + ") FROM " + (prefix + TABLE_NAME_TABLE_INFORMATION));

if (fallbackQuery.isEmpty()) {
return 0;
}

return fallbackQuery.getFirst().getInt(null);
} catch (Exception e) {
return 0;
}
} else {
return query.getFirst().getInt(FieldKey.TABLE_VERSION);
return query.getFirst().getInt(FieldKey.METADATA_VALUE);
}
}

@Override
public void patch() {
DatabasePatch patch = null;
var dbVer = getDatabaseVersion();

switch (getDatabaseVersion()) {
case 0: {
patch = new DatabasePatchV1();
break;
}
Slimefun.logger().log(Level.INFO, "当前数据库版本 {0}", new Object[] {dbVer});

switch (dbVer) {
case 0 -> patch = new DatabasePatchV1();
case 1 -> patch = new DatabasePatchV2();
}

if (patch == null) {
Expand All @@ -123,7 +146,9 @@ public void patch() {

try (var conn = ds.getConnection()) {
Slimefun.logger().log(Level.INFO, "正在更新数据库版本至 " + patch.getVersion() + ", 可能需要一段时间...");
patch.patch(conn.createStatement(), config);
var stmt = conn.createStatement();
patch.updateVersion(stmt, config);
patch.patch(stmt, config);
Slimefun.logger().log(Level.INFO, "更新完成. ");
} catch (SQLException e) {
Slimefun.logger().log(Level.SEVERE, "更新数据库时出现问题!", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ public interface SqlConstants {
String TABLE_NAME_UNIVERSAL_INVENTORY = "universal_inventory";
String TABLE_NAME_UNIVERSAL_RECORD = "universal_record";
String TABLE_NAME_UNIVERSAL_DATA = "universal_data";
/**
* @deprecated
* 由于设计不当,该表已被弃用
*/
String TABLE_NAME_TABLE_INFORMATION = "table_information";

String TABLE_NAME_TABLE_METADATA = "table_metadata";

String FIELD_PLAYER_UUID = "p_uuid";
String FIELD_PLAYER_NAME = "p_name";

Expand All @@ -38,5 +44,14 @@ public interface SqlConstants {

String FIELD_UNIVERSAL_TRAITS = "universal_traits";

/**
* @deprecated
* 由于设计不当,该字段已被弃用
*/
String FIELD_TABLE_VERSION = "table_version";

String FIELD_TABLE_METADATA_KEY = "table_metadata_key";
String FIELD_TABLE_METADATA_VALUE = "table_metadata_value";

String METADATA_VERSION = "version";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_PLAYER_UUID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_RESEARCH_KEY;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_SLIMEFUN_ID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_VERSION;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_METADATA_KEY;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_TABLE_METADATA_VALUE;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_TRAITS;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_UNIVERSAL_UUID;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_BACKPACK;
Expand All @@ -25,7 +26,7 @@
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_CHUNK_DATA;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_PLAYER_PROFILE;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_PLAYER_RESEARCH;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_TABLE_INFORMATION;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_TABLE_METADATA;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_UNIVERSAL_DATA;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_UNIVERSAL_INVENTORY;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.TABLE_NAME_UNIVERSAL_RECORD;
Expand Down Expand Up @@ -67,7 +68,8 @@ public class SqlUtils {
fieldMap.put(FieldKey.DATA_VALUE, FIELD_DATA_VALUE);
fieldMap.put(FieldKey.UNIVERSAL_UUID, FIELD_UNIVERSAL_UUID);
fieldMap.put(FieldKey.UNIVERSAL_TRAITS, FIELD_UNIVERSAL_TRAITS);
fieldMap.put(FieldKey.TABLE_VERSION, FIELD_TABLE_VERSION);
fieldMap.put(FieldKey.METADATA_KEY, FIELD_TABLE_METADATA_KEY);
fieldMap.put(FieldKey.METADATA_VALUE, FIELD_TABLE_METADATA_VALUE);
mapper = new FieldMapper<>(fieldMap);
}

Expand All @@ -84,7 +86,7 @@ public static String mapTable(DataScope scope) {
case UNIVERSAL_INVENTORY -> TABLE_NAME_UNIVERSAL_INVENTORY;
case UNIVERSAL_RECORD -> TABLE_NAME_UNIVERSAL_RECORD;
case UNIVERSAL_DATA -> TABLE_NAME_UNIVERSAL_DATA;
case TABLE_INFORMATION -> TABLE_NAME_TABLE_INFORMATION;
case TABLE_METADATA -> TABLE_NAME_TABLE_METADATA;
case NONE -> throw new IllegalArgumentException("NONE cannot be a storage data scope!");
};
}
Expand Down
Loading

0 comments on commit b728ecb

Please sign in to comment.