-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial configuration for Flyway (#178)
* Initial configuration for Flyway * Use flyway migration at runtime and also calculate baseline dynamically * Remove Flyway plugin configuration * Replace dummy migration with migrations from CHANGELOG * Replace dummy baseline script * Removed all migration written in sql and replace them with Java migration * Removed all migration written in sql and replace them with Java migration 2.0 * Made migration work without compilation errors + added initial testing * cleanup the build.gradle of previous config * moved init database as a migration script * breakdown migration into smaller subunit for V3 and started the preparation for V1 * V1 will create only necessary tables and columns without indexes * updated migrations * some missing indexes for V3 * clean up --------- Co-authored-by: Sabin Antohe <[email protected]>
- Loading branch information
Showing
31 changed files
with
2,383 additions
and
931 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/io/supertokens/storage/postgresql/FlywayMigration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (c) 2023, VRAI Labs and/or its affiliates. All rights reserved. | ||
* | ||
* This software is licensed under the Apache License, Version 2.0 (the | ||
* "License") as published by the Apache Software Foundation. | ||
* | ||
* You may not use this file except in compliance with the License. You may | ||
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.supertokens.storage.postgresql; | ||
|
||
import io.supertokens.pluginInterface.exceptions.StorageQueryException; | ||
import io.supertokens.storage.postgresql.config.Config; | ||
import io.supertokens.storage.postgresql.output.Logging; | ||
import io.supertokens.storage.postgresql.queries.BaselineMigrationQueries; | ||
import org.flywaydb.core.Flyway; | ||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public final class FlywayMigration { | ||
|
||
private static final String LOCATION = "classpath:/io/supertokens/storage/postgresql/migrations"; | ||
private FlywayMigration() {} | ||
|
||
public static void startMigration(Start start) throws SQLException, StorageQueryException { | ||
String baseline = BaselineMigrationQueries.getBaselineMigrationVersion(start); | ||
if (Integer.parseInt(baseline) >= BaselineMigrationQueries.LAST_MIGRATION) { | ||
return; | ||
} | ||
|
||
Logging.info(start, "Starting migration.", true); | ||
MigrationContextManager.putContext(start.getProcessId(), start); | ||
int maxRetries = 5; | ||
|
||
try { | ||
Flyway flyway = Flyway.configure() | ||
.dataSource(ConnectionPool.getHikariDataSource(start)) | ||
.baselineOnMigrate(true) | ||
.baselineVersion(baseline) | ||
.table(Config.getConfig(start).getFlywaySchemaHistory()) | ||
.connectRetries(maxRetries) | ||
.lockRetryCount(maxRetries) | ||
.locations(LOCATION) | ||
.placeholders(getPlaceholders(start)) | ||
.load(); | ||
flyway.migrate(); | ||
} finally { | ||
MigrationContextManager.removeContext(start.getProcessId()); | ||
} | ||
} | ||
|
||
private static Map<String, String> getPlaceholders(Start start) { | ||
Map<String, String> ph = new HashMap<>(); | ||
ph.put("process_id", start.getProcessId()); | ||
ph.put("access_token_signing_key_dynamic", String.valueOf( Config.getConfig(start).getAccessTokenSigningKeyDynamic())); | ||
return ph; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/io/supertokens/storage/postgresql/MigrationContextManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2024, VRAI Labs and/or its affiliates. All rights reserved. | ||
* | ||
* This software is licensed under the Apache License, Version 2.0 (the | ||
* "License") as published by the Apache Software Foundation. | ||
* | ||
* You may not use this file except in compliance with the License. You may | ||
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.supertokens.storage.postgresql; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class MigrationContextManager { | ||
private static Map<String, Start> contextMap = new ConcurrentHashMap<>(); | ||
|
||
public static void putContext(String key, Start start) { | ||
contextMap.put(key, start); | ||
} | ||
|
||
public static Start getContext(String key) { | ||
return contextMap.get(key); | ||
} | ||
|
||
public static void removeContext(String key) { | ||
contextMap.remove(key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/io/supertokens/storage/postgresql/migrations/V1__init_database.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2024, VRAI Labs and/or its affiliates. All rights reserved. | ||
* | ||
* This software is licensed under the Apache License, Version 2.0 (the | ||
* "License") as published by the Apache Software Foundation. | ||
* | ||
* You may not use this file except in compliance with the License. You may | ||
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.supertokens.storage.postgresql.migrations; | ||
|
||
import io.supertokens.storage.postgresql.MigrationContextManager; | ||
import io.supertokens.storage.postgresql.Start; | ||
import io.supertokens.storage.postgresql.queries.GeneralQueries; | ||
import org.flywaydb.core.api.migration.BaseJavaMigration; | ||
import org.flywaydb.core.api.migration.Context; | ||
|
||
import java.util.Map; | ||
|
||
|
||
public class V1__init_database extends BaseJavaMigration { | ||
|
||
@Override | ||
public void migrate(Context context) throws Exception { | ||
Map<String, String> ph = context.getConfiguration().getPlaceholders(); | ||
Start start = MigrationContextManager.getContext(ph.get("process_id")); | ||
GeneralQueries.createTablesIfNotExists(start); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/io/supertokens/storage/postgresql/migrations/V2__plugin_version_3_0_0.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2023, VRAI Labs and/or its affiliates. All rights reserved. | ||
* | ||
* This software is licensed under the Apache License, Version 2.0 (the | ||
* "License") as published by the Apache Software Foundation. | ||
* | ||
* You may not use this file except in compliance with the License. You may | ||
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.supertokens.storage.postgresql.migrations; | ||
|
||
import io.supertokens.storage.postgresql.MigrationContextManager; | ||
import io.supertokens.storage.postgresql.Start; | ||
import io.supertokens.storage.postgresql.config.Config; | ||
import org.flywaydb.core.api.migration.BaseJavaMigration; | ||
import org.flywaydb.core.api.migration.Context; | ||
import java.sql.Statement; | ||
import java.util.Map; | ||
|
||
public class V2__plugin_version_3_0_0 extends BaseJavaMigration { | ||
|
||
@Override | ||
public void migrate(Context context) throws Exception { | ||
Map<String, String> ph = context.getConfiguration().getPlaceholders(); | ||
Start start = MigrationContextManager.getContext(ph.get("process_id")); | ||
String sessionInfoTable = Config.getConfig(start).getSessionInfoTable(); | ||
String JWTSigningKeysTable = Config.getConfig(start).getJWTSigningKeysTable(); | ||
String accessTokenSigningKeysTable = Config.getConfig(start).getAccessTokenSigningKeysTable(); | ||
|
||
try (Statement statement = context.getConnection().createStatement()) { | ||
// Add a new column with a default value | ||
statement.execute("ALTER TABLE " + sessionInfoTable + " ADD COLUMN IF NOT EXISTS use_static_key BOOLEAN NOT NULL DEFAULT" + | ||
"(" + !Boolean.parseBoolean(ph.get("access_token_signing_key_dynamic")) + ")"); | ||
// Alter the column to drop the default value | ||
statement.execute("ALTER TABLE " + sessionInfoTable + " ALTER COLUMN " + | ||
"use_static_key DROP DEFAULT"); | ||
|
||
// Insert data into jwt_signing_keys from session_access_token_signing_keys | ||
statement.execute("INSERT INTO " + JWTSigningKeysTable + " (key_id, key_string, algorithm, " + | ||
"created_at) " + | ||
"SELECT CONCAT('s-', created_at_time) as key_id, value as key_string, 'RS256' as algorithm, created_at_time as created_at " + | ||
"FROM " + accessTokenSigningKeysTable); | ||
} | ||
} | ||
} |
Oops, something went wrong.