Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial configuration for Flyway #178

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies {
implementation group: 'org.flywaydb', name: 'flyway-core', version: '7.15.0'
antsab20 marked this conversation as resolved.
Show resolved Hide resolved

// https://mvnrepository.com/artifact/com.googlecode.libphonenumber/libphonenumber
implementation group: 'com.googlecode.libphonenumber', name: 'libphonenumber', version: '8.13.27'
compileOnly group: 'com.googlecode.libphonenumber', name: 'libphonenumber', version: '8.13.25'

// https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml
compileOnly group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.14.0'
Expand Down Expand Up @@ -67,11 +67,6 @@ dependencies {
}

jar {
// Flyway migration need to also be included in jar
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from('src/main/resources') {
include '**/*.*'
}
archiveBaseName.set('postgresql-plugin')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,54 @@

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 org.jetbrains.annotations.TestOnly;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public final class FlywayMigration {

private FlywayMigration() {}

public static void startMigration(Start start) {
Logging.info(start, "Setting up Flyway.", true);
public static void startMigration(Start start) throws SQLException, StorageQueryException {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
Logging.info(start, "Starting migration.", true);
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
MigrationContextManager.putContext(start.getProcessId(), start);

try {
String baselineVersion = BaselineMigrationQueries.getBaselineMigrationVersion(start);

Flyway flyway = Flyway.configure()
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
.dataSource(ConnectionPool.getHikariDataSource(start))
.baselineOnMigrate(true)
.baselineVersion(baselineVersion)
.baselineVersion(BaselineMigrationQueries.getBaselineMigrationVersion(start))
.table(Config.getConfig(start).getFlywaySchemaHistory())
.locations("classpath:/io/supertokens/storage/postgresql/migrations")
.placeholders(getPlaceholders(start))
.load();
flyway.migrate();
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception e) {
Logging.error(start, "Error Setting up Flyway.", true);
Logging.error(start, e.toString(), true);
// TODO: Find all possible exception
}
}

@TestOnly
public static void executeTargetedMigration(Start start, String migrationTarget) {
try {
Flyway flyway = Flyway.configure()
.dataSource(ConnectionPool.getHikariDataSource(start))
.target(migrationTarget)
.locations("classpath:/io/supertokens/storage/postgresql/migrations")
.baselineOnMigrate(true)
.placeholders(getPlaceholders(start))
.load();
flyway.migrate();
} catch (Exception e) {
Logging.error(start, "Error Setting up Flyway.", true);
Logging.error(start, e.toString(), true);
// TODO: Find all possible exception
catch (Exception e) {
throw e;
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
} finally {
MigrationContextManager.removeContext(start.getProcessId());
}
}

private static Map<String, String> getPlaceholders(Start start) {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
Map<String, String> ph = new HashMap<>();
// TODO: Now that we have Start instance inside migration, be can use that instead of passing and maintaining
// this placeholders map.

ph.put("process_id", start.getProcessId());
ph.put("schema", Config.getConfig(start).getTableSchema());
ph.put("session_info_table", Config.getConfig(start).getSessionInfoTable());
ph.put("jwt_signing_keys_table", Config.getConfig(start).getJWTSigningKeysTable());
ph.put("session_access_token_signing_keys_table", Config.getConfig(start).getAccessTokenSigningKeysTable());
ph.put("access_token_signing_key_dynamic", "true");
ph.put("access_token_signing_key_dynamic", String.valueOf( Config.getConfig(start).getAccessTokenSigningKeyDynamic()));
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
ph.put("apps_table", Config.getConfig(start).getAppsTable());
ph.put("tenants_table", Config.getConfig(start).getTenantsTable());
ph.put("key_value_table", Config.getConfig(start).getKeyValueTable());
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public synchronized void clear() {
* DEADLOCK_FOUND: In case of a deadlock situation, we put this event
*/
public enum PROCESS_STATE {
CREATING_NEW_TABLE, DEADLOCK_FOUND, DEADLOCK_NOT_RESOLVED
CREATING_NEW_TABLE, STARTING_MIGRATION, DEADLOCK_FOUND, DEADLOCK_NOT_RESOLVED
}

public static class EventAndException {
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/io/supertokens/storage/postgresql/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,7 @@ public void initStorage(boolean shouldWait) throws DbInitException {
}
try {
ConnectionPool.initPool(this, shouldWait);
GeneralQueries.createTablesIfNotExists(this);
if (!isTesting) {
FlywayMigration.startMigration(this);
}
FlywayMigration.startMigration(this);
} catch (Exception e) {
throw new DbInitException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public class PostgreSQLConfig {
@IgnoreForAnnotationCheck
boolean isValidAndNormalised = false;

@JsonProperty
@IgnoreForAnnotationCheck
private boolean access_token_signing_key_dynamic = true;
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved

public static Set<String> getValidFields() {
PostgreSQLConfig config = new PostgreSQLConfig();
JsonObject configObj = new GsonBuilder().serializeNulls().create().toJsonTree(config).getAsJsonObject();
Expand Down Expand Up @@ -234,6 +238,10 @@ public String getThirdPartyUsersTable() {
return postgresql_thirdparty_users_table_name;
}

public boolean getAccessTokenSigningKeyDynamic() {
return access_token_signing_key_dynamic;
}

public String getThirdPartyUserToTenantTable() {
return addSchemaAndPrefixToTableName("thirdparty_user_to_tenant");
}
Expand Down Expand Up @@ -302,6 +310,10 @@ public String getTotpUsedCodesTable() {
return addSchemaAndPrefixToTableName("totp_used_codes");
}

public String getFlywaySchemaHistory() {
return addSchemaAndPrefixToTableName("flyway_schema_history");
}

private String addSchemaAndPrefixToTableName(String tableName) {
return addSchemaToTableName(postgresql_table_names_prefix + tableName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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);
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,32 @@

package io.supertokens.storage.postgresql.migrations;

import io.supertokens.storage.postgresql.MigrationContextManager;
import io.supertokens.storage.postgresql.Start;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

public class V1__plugin_version_3_0_0 extends BaseJavaMigration {
import static io.supertokens.storage.postgresql.ProcessState.PROCESS_STATE.STARTING_MIGRATION;
import static io.supertokens.storage.postgresql.ProcessState.getInstance;

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"));
getInstance(start).addState(STARTING_MIGRATION, null);

try (Statement statement = context.getConnection().createStatement()) {
// Add a new column with a default value
statement.execute("ALTER TABLE " + ph.get("session_info_table") + " ADD COLUMN use_static_key BOOLEAN NOT NULL DEFAULT" +
statement.execute("ALTER TABLE " + ph.get("session_info_table") + " 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 " + ph.get("session_info_table") + " ALTER COLUMN use_static_key DROP DEFAULT");
statement.execute("ALTER TABLE " + ph.get("session_info_table") + " ALTER COLUMN " +
"use_static_key DROP DEFAULT");

// Insert data into jwt_signing_keys from session_access_token_signing_keys
statement.execute("INSERT INTO " + ph.get("jwt_signing_keys_table") + " (key_id, key_string, algorithm, " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@

package io.supertokens.storage.postgresql.migrations;

import io.supertokens.storage.postgresql.MigrationContextManager;
import io.supertokens.storage.postgresql.Start;
import io.supertokens.storage.postgresql.utils.Utils;
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_4_0_0 extends BaseJavaMigration {
import static io.supertokens.storage.postgresql.ProcessState.PROCESS_STATE.STARTING_MIGRATION;
import static io.supertokens.storage.postgresql.ProcessState.getInstance;

public class V3__plugin_version_4_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"));
getInstance(start).addState(STARTING_MIGRATION, null);
try (Statement statement = context.getConnection().createStatement()) {
statement.execute("CREATE TABLE IF NOT EXISTS " + ph.get("apps_table") + " ( " +
" app_id VARCHAR(64) NOT NULL DEFAULT 'public', " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@

package io.supertokens.storage.postgresql.migrations;

import io.supertokens.storage.postgresql.MigrationContextManager;
import io.supertokens.storage.postgresql.Start;
import io.supertokens.storage.postgresql.utils.Utils;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;

import java.sql.Statement;
import java.util.Map;

public class V3__plugin_version_5_0_0 extends BaseJavaMigration {
import static io.supertokens.storage.postgresql.ProcessState.PROCESS_STATE.STARTING_MIGRATION;
import static io.supertokens.storage.postgresql.ProcessState.getInstance;

public class V4__plugin_version_5_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"));
getInstance(start).addState(STARTING_MIGRATION, null);

try (Statement statement = context.getConnection().createStatement()) {
statement.execute("ALTER TABLE " + ph.get("all_auth_recipe_users_table") +
" ADD COLUMN IF NOT EXISTS primary_or_recipe_user_id CHAR(36) NOT NULL DEFAULT '0';");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,25 @@

package io.supertokens.storage.postgresql.migrations;

import io.supertokens.storage.postgresql.MigrationContextManager;
import io.supertokens.storage.postgresql.Start;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;

import java.sql.Statement;
import java.util.Map;

public class V4__plugin_version_5_0_4 extends BaseJavaMigration {
import static io.supertokens.storage.postgresql.ProcessState.PROCESS_STATE.STARTING_MIGRATION;
import static io.supertokens.storage.postgresql.ProcessState.getInstance;

public class V5__plugin_version_5_0_4 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"));
getInstance(start).addState(STARTING_MIGRATION, null);

try (Statement statement = context.getConnection().createStatement()) {
statement.execute("CREATE INDEX IF NOT EXISTS app_id_to_user_id_primary_user_id_index ON " +
ph.get("app_id_to_user_id_table") +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.supertokens.storage.postgresql.migrations;

import io.supertokens.storage.postgresql.MigrationContextManager;
import io.supertokens.storage.postgresql.Start;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
import java.sql.Connection;
Expand All @@ -12,13 +14,19 @@
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;

public class V5__core_version_7_0_12 extends BaseJavaMigration {
import static io.supertokens.storage.postgresql.ProcessState.PROCESS_STATE.STARTING_MIGRATION;
import static io.supertokens.storage.postgresql.ProcessState.getInstance;

public class V6__core_version_7_0_12 extends BaseJavaMigration {

@Override
public void migrate(Context context) throws Exception {
try {
Connection connection = context.getConnection();
Map<String, String> ph = context.getConfiguration().getPlaceholders();
Start start = MigrationContextManager.getContext(ph.get("process_id"));
getInstance(start).addState(STARTING_MIGRATION, null);

updatePhoneNumbers(connection, ph.get("passwordless_users_table"));
updatePhoneNumbers(connection, ph.get("passwordless_devices_table"));
} catch (SQLException e) {
Expand Down
Loading