Skip to content

Commit

Permalink
Bump db version to 1.2, add setting for automatic version-rollback on…
Browse files Browse the repository at this point in the history
… dev builds
  • Loading branch information
JvstvsHD committed Dec 20, 2024
1 parent f5aed9f commit 7a5b2aa
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public class DataBaseData {
@JsonAlias("postgresSchema")
private final String postgresSchema;

@JsonProperty("enable-development-version-reset")
private final boolean enableDevelopmentVersionReset;

//TODO use Configurate

public DataBaseData(String host, String password, String username, String database, String port, String sqlType, int maxPoolSize, int minIdle, String postgresSchema) {
this.host = host;
this.password = password;
Expand All @@ -61,10 +66,24 @@ public DataBaseData(String host, String password, String username, String databa
this.maxPoolSize = maxPoolSize;
this.minIdle = minIdle;
this.postgresSchema = postgresSchema;
this.enableDevelopmentVersionReset = false;
}

public DataBaseData(String host, String password, String username, String database, String port, String sqlType, int maxPoolSize, int minIdle, String postgresSchema, boolean enableDevelopmentVersionReset) {
this.host = host;
this.password = password;
this.username = username;
this.database = database;
this.port = port;
this.sqlType = sqlType;
this.maxPoolSize = maxPoolSize;
this.minIdle = minIdle;
this.postgresSchema = postgresSchema;
this.enableDevelopmentVersionReset = enableDevelopmentVersionReset;
}

public DataBaseData() {
this("localhost", "password", "username", "database", "5432", "postgresql", 10, 5, "necrify");
this("localhost", "password", "username", "database", "5432", "postgresql", 10, 5, "necrify", false);
}

public String getHost() {
Expand Down Expand Up @@ -103,6 +122,10 @@ public String getPostgresSchema() {
return postgresSchema;
}

public boolean isEnableDevelopmentVersionReset() {
return enableDevelopmentVersionReset;
}

@SuppressWarnings("UnstableApiUsage")
public Database<?, ?> sqlType() {
return switch (sqlType.toLowerCase(Locale.ROOT)) {
Expand Down
6 changes: 5 additions & 1 deletion necrify-common/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ database:
max-pool-size: 5
# Determines how many idle connections should be kept open at minimum. As soon as the number of connections is below this
# number, HikariCP tries to open new connections as quickly and efficiently as possible.
min-idle: 2
min-idle: 2
# Determines whether the version of the database schema should be reset to the previous version. This avoids problems when
# a dev build is used and new additions to the database schema patch files are made afterwards and before the release. This
# will go only into effect when a dev build is used.
enable-development-version-reset: false
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ CREATE TABLE IF NOT EXISTS necrify_schema.punishment_log (
action VARCHAR(128) NOT NULL,
begins_at TIMESTAMP DEFAULT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)
);

UPDATE necrify_schema.necrify_user SET name = lower(name) WHERE name IS NOT NULL;
2 changes: 1 addition & 1 deletion necrify-common/src/main/resources/database/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1
1.2
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import de.jvstvshd.necrify.api.user.NecrifyUser;
import de.jvstvshd.necrify.api.user.UserManager;
import de.jvstvshd.necrify.common.AbstractNecrifyPlugin;
import de.jvstvshd.necrify.common.BuildParameters;
import de.jvstvshd.necrify.common.config.ConfigurationManager;
import de.jvstvshd.necrify.common.io.Adapters;
import de.jvstvshd.necrify.common.io.NecrifyDatabase;
Expand Down Expand Up @@ -291,7 +292,6 @@ private void updateDatabase() throws IOException, SQLException {
};
switch (configurationManager.getConfiguration().getDataBaseData().sqlType().name().toLowerCase(Locale.ROOT)) {
case "postgresql", "postgres" -> SqlUpdater.builder(dataSource, PostgreSql.get())
.setReplacements()
.setSchemas(configurationManager.getConfiguration().getDataBaseData().getPostgresSchema())
.setReplacements(new QueryReplacement("necrify_schema", configurationManager.getConfiguration().getDataBaseData().getPostgresSchema()))
.preUpdateHook(new SqlVersion(1, 1), preUpdateHook)
Expand All @@ -305,6 +305,20 @@ private void updateDatabase() throws IOException, SQLException {
default ->
getLogger().warn("Database type is not (yet) supported for automatic updates. Please update the database manually.");
}
//noinspection ConstantValue
if (BuildParameters.VERSION.contains("-") && configurationManager.getConfiguration().getDataBaseData().isEnableDevelopmentVersionReset()) {
var sqlVersion = SqlVersion.load();
getLogger().info("This is a development build. To ensure functionality, the last database patch is ran again. " +
"This might cause an error, but is necessary for development. This will also reset the version of the db to the last patch.");
if (sqlVersion.patch() <= 0) {
getLogger().warn("Could not load last patch version. Skipping re-run of last patch. Please update the " +
"database manually by executing SQL statements from the files in the resources after commit " + BuildParameters.GIT_COMMIT);
return;
}
Query.query("UPDATE necrify_schema.version SET patch = ?;")
.single(Call.of().bind(sqlVersion.patch() - 1))
.update();
}
}

@Override
Expand Down

0 comments on commit 7a5b2aa

Please sign in to comment.