Skip to content

Commit

Permalink
Add schema replacement for PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
JvstvsHD committed Dec 20, 2024
1 parent 5c4bd8e commit f5aed9f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class DataBaseData {
@JsonAlias("minIdle")
private final int minIdle;

@JsonProperty("postgres-schema")
@JsonAlias("postgresSchema")
private final String postgresSchema;

public DataBaseData(String host, String password, String username, String database, String port, String sqlType, int maxPoolSize, int minIdle, String postgresSchema) {
Expand All @@ -62,7 +64,7 @@ public DataBaseData(String host, String password, String username, String databa
}

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

public String getHost() {
Expand Down
42 changes: 23 additions & 19 deletions necrify-common/src/main/resources/database/postgresql/1/patch_1.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
-- noinspection SqlResolveForFile @ column/"name"

-- noinspection SqlResolveForFile @ table/"necrify_whitelist"

CREATE TABLE IF NOT EXISTS punishment.necrify_user
(
uuid UUID PRIMARY KEY,
name VARCHAR(16)
);

ALTER TABLE punishment.necrify_punishment
ALTER TABLE necrify_schema.necrify_punishment
ALTER COLUMN uuid TYPE UUID USING uuid :: uuid;
ALTER TABLE punishment.necrify_punishment
ALTER TABLE necrify_schema.necrify_punishment
ALTER COLUMN punishment_id TYPE uuid USING punishment_id:: uuid;
ALTER TABLE punishment.necrify_whitelist
ALTER TABLE necrify_schema.necrify_whitelist
ALTER COLUMN uuid TYPE uuid USING uuid :: uuid;
ALTER TABLE punishment.necrify_punishment
ALTER TABLE necrify_schema.necrify_punishment
ALTER COLUMN type TYPE INTEGER USING CASE
WHEN (type = 'BAN') THEN 1
WHEN (type = 'PERMANENT_BAN') THEN 2
Expand All @@ -22,33 +26,33 @@ ALTER TABLE punishment.necrify_punishment

INSERT INTO punishment.necrify_user (uuid, name)
SELECT DISTINCT uuid, name
FROM punishment.necrify_punishment
FROM necrify_schema.necrify_punishment
ON CONFLICT
DO NOTHING;
INSERT INTO punishment.necrify_user (uuid, name)
SELECT DISTINCT uuid, NULL
FROM punishment.necrify_whitelist
FROM necrify_schema.necrify_whitelist
ON CONFLICT
DO NOTHING;
ALTER TABLE punishment.necrify_punishment
ALTER TABLE necrify_schema.necrify_punishment
DROP CONSTRAINT IF EXISTS fk_punishment_user;
ALTER TABLE punishment.necrify_punishment
ALTER TABLE necrify_schema.necrify_punishment
ADD CONSTRAINT fk_punishment_user FOREIGN KEY (uuid) REFERENCES punishment.necrify_user (uuid) ON DELETE CASCADE;
ALTER TABLE punishment.necrify_whitelist
ALTER TABLE necrify_schema.necrify_whitelist
DROP CONSTRAINT IF EXISTS fk_whitelist_user;
ALTER TABLE punishment.necrify_whitelist
ALTER TABLE necrify_schema.necrify_whitelist
ADD CONSTRAINT fk_whitelist_user FOREIGN KEY (uuid) REFERENCES punishment.necrify_user (uuid) ON DELETE CASCADE;
ALTER TABLE punishment.necrify_user
ALTER TABLE necrify_schema.necrify_user
ADD COLUMN IF NOT EXISTS whitelisted BOOLEAN DEFAULT FALSE;
ALTER TABLE punishment.necrify_punishment
ALTER TABLE necrify_schema.necrify_punishment
DROP COLUMN IF EXISTS name;
UPDATE punishment.necrify_user
UPDATE necrify_schema.necrify_user
SET whitelisted = TRUE
WHERE uuid IN (SELECT uuid FROM punishment.necrify_whitelist);
DROP TABLE punishment.necrify_whitelist;
ALTER TABLE punishment.necrify_punishment ADD COLUMN IF NOT EXISTS issued_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE punishment.necrify_punishment
WHERE uuid IN (SELECT uuid FROM necrify_schema.necrify_whitelist);
DROP TABLE necrify_schema.necrify_whitelist;
ALTER TABLE necrify_schema.necrify_punishment ADD COLUMN IF NOT EXISTS issued_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE necrify_schema.necrify_punishment
ADD CONSTRAINT unique_punishment_id UNIQUE (punishment_id);

ALTER TABLE punishment.necrify_punishment
ADD COLUMN IF NOT EXISTS successor UUID DEFAULT NULL REFERENCES punishment.necrify_punishment (punishment_id) ON DELETE SET NULL;
ALTER TABLE necrify_schema.necrify_punishment
ADD COLUMN IF NOT EXISTS successor UUID DEFAULT NULL REFERENCES necrify_schema.necrify_punishment (punishment_id) ON DELETE SET NULL;
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
CREATE TABLE IF NOT EXISTS punishment_log (
id SERIAL PRIMARY KEY,
CREATE TABLE IF NOT EXISTS necrify_schema.punishment_log (
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
punishment_id UUID,
player_id UUID REFERENCES necrify_users (uuid) ON DELETE SET NULL,
message TEXT,
expiration TIMESTAMP,
reason TEXT,
player_id UUID REFERENCES necrify_schema.necrify_user (uuid) ON DELETE SET NULL,
message TEXT DEFAULT NULL,
expiration TIMESTAMP DEFAULT NULL,
reason TEXT DEFAULT NULL,
predecessor UUID DEFAULT NULL,
successor UUID DEFAULT NULL,
action VARCHAR(128),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
action VARCHAR(128) NOT NULL,
begins_at TIMESTAMP DEFAULT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE IF NOT EXISTS necrify_punishment
CREATE TABLE IF NOT EXISTS necrify_schema.necrify_punishment
(
uuid VARCHAR(36),
name VARCHAR(16),
Expand All @@ -8,7 +8,7 @@ CREATE TABLE IF NOT EXISTS necrify_punishment
punishment_id VARCHAR(36)
);

CREATE TABLE IF NOT EXISTS necrify_whitelist
CREATE TABLE IF NOT EXISTS necrify_schema.necrify_whitelist
(
uuid VARCHAR(36)
);
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@
import de.chojo.sadu.mysql.databases.MySql;
import de.chojo.sadu.postgresql.databases.PostgreSql;
import de.chojo.sadu.queries.api.call.Call;
import de.chojo.sadu.queries.api.configuration.QueryConfiguration;
import de.chojo.sadu.queries.api.query.Query;
import de.chojo.sadu.queries.configuration.QueryConfiguration;
import de.chojo.sadu.updater.QueryReplacement;
import de.chojo.sadu.updater.SqlUpdater;
import de.jvstvshd.necrify.api.event.EventDispatcher;
import de.jvstvshd.necrify.api.event.Slf4jLogger;
Expand Down Expand Up @@ -242,7 +243,8 @@ private HikariDataSource createDataSource() {
.port(dbData.getPort())
.database(dbData.getDatabase())
.user(dbData.getUsername())
.password(dbData.getPassword()))
.password(dbData.getPassword())
.currentSchema(dbData.getPostgresSchema()))
.create();

default -> DataSourceCreator
Expand Down Expand Up @@ -288,10 +290,12 @@ private void updateDatabase() throws IOException, SQLException {
getLogger().info("Updated {} reasons to minimessage format.", updatedReasons.size());
};
switch (configurationManager.getConfiguration().getDataBaseData().sqlType().name().toLowerCase(Locale.ROOT)) {
case "postgresql", "postgres" ->
SqlUpdater.builder(dataSource, PostgreSql.get()).setSchemas(configurationManager.getConfiguration().getDataBaseData().getPostgresSchema())
.preUpdateHook(new SqlVersion(1, 1), preUpdateHook)
.execute();
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)
.execute();
case "mariadb" -> SqlUpdater.builder(dataSource, MariaDb.get())
.preUpdateHook(new SqlVersion(1, 1), preUpdateHook)
.execute();
Expand Down

0 comments on commit f5aed9f

Please sign in to comment.