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 8 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
11 changes: 11 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ dependencies {
// https://mvnrepository.com/artifact/org.postgresql/postgresql
implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.10'

// https://mvnrepository.com/artifact/org.flywaydb/flyway-core
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'
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved

// 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 @@ -61,6 +67,11 @@ dependencies {
}

jar {
// Flyway migration need to also be included in jar
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from('src/main/resources') {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
include '**/*.*'
}
antsab20 marked this conversation as resolved.
Show resolved Hide resolved
archiveBaseName.set('postgresql-plugin')
}

Expand Down
5 changes: 5 additions & 0 deletions implementationDependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
"jar": "https://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar",
"name": "SLF4j API 1.7.25",
"src": "https://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25-sources.jar"
},
{
"jar": "https://repo1.maven.org/maven2/org/flywaydb/flyway-core/7.15.0/flyway-core-7.15.0.jar",
"name": "Flyway Core 7.15.0",
"src": "https://repo1.maven.org/maven2/org/flywaydb/flyway-core/7.15.0/flyway-core-7.15.0-sources.jar"
}
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ public static Connection getConnection(Start start) throws SQLException {
return getInstance(start).hikariDataSource.getConnection();
}

public static HikariDataSource getHikariDataSource(Start start) throws SQLException {
if (getInstance(start) == null || getInstance(start).hikariDataSource == null ) {
throw new RuntimeException("Please call initPool before getHikariDataSource");
}
if (!start.enabled) {
throw new SQLException("Storage layer disabled");
}
return getInstance(start).hikariDataSource;
antsab20 marked this conversation as resolved.
Show resolved Hide resolved
}

static void close(Start start) {
if (getInstance(start) == null) {
return;
Expand Down
111 changes: 111 additions & 0 deletions src/main/java/io/supertokens/storage/postgresql/FlywayMigration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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.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.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);
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved

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)
.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) {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
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
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
}
}

private static Map<String, String> getPlaceholders(Start start) {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
Map<String, String> ph = new HashMap<>();
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("apps_table", Config.getConfig(start).getAppsTable());
ph.put("tenants_table", Config.getConfig(start).getTenantsTable());
ph.put("key_value_table", Config.getConfig(start).getKeyValueTable());
ph.put("app_id_to_user_id_table", Config.getConfig(start).getAppIdToUserIdTable());
ph.put("all_auth_recipe_users_table", Config.getConfig(start).getUsersTable());
ph.put("tenant_configs_table", Config.getConfig(start).getTenantConfigsTable());
ph.put("tenant_thirdparty_providers_table", Config.getConfig(start).getTenantThirdPartyProvidersTable());
ph.put("tenant_thirdparty_provider_clients_table", Config.getConfig(start).getTenantThirdPartyProviderClientsTable());
ph.put("emailverification_verified_emails_table", Config.getConfig(start).getEmailVerificationTable());
ph.put("emailverification_tokens_table", Config.getConfig(start).getEmailVerificationTokensTable());
ph.put("emailpassword_users_table", Config.getConfig(start).getEmailPasswordUsersTable());
ph.put("emailpassword_user_to_tenant_table", Config.getConfig(start).getEmailPasswordUserToTenantTable());
ph.put("emailpassword_pswd_reset_tokens_table", Config.getConfig(start).getPasswordResetTokensTable());
ph.put("passwordless_users_table", Config.getConfig(start).getPasswordlessUsersTable());
ph.put("passwordless_user_to_tenant_table", Config.getConfig(start).getPasswordlessUserToTenantTable());
ph.put("passwordless_devices_table", Config.getConfig(start).getPasswordlessDevicesTable());
ph.put("passwordless_codes_table", Config.getConfig(start).getPasswordlessCodesTable());
ph.put("thirdparty_users_table", Config.getConfig(start).getThirdPartyUsersTable());
ph.put("thirdparty_user_to_tenant_table", Config.getConfig(start).getThirdPartyUserToTenantTable());
ph.put("roles_table", Config.getConfig(start).getRolesTable());
ph.put("role_permissions_table", Config.getConfig(start).getUserRolesPermissionsTable());
ph.put("user_roles_table", Config.getConfig(start).getUserRolesTable());
ph.put("user_metadata_table", Config.getConfig(start).getUserMetadataTable());
ph.put("dashboard_users_table", Config.getConfig(start).getDashboardUsersTable());
ph.put("dashboard_user_sessions_table", Config.getConfig(start).getDashboardSessionsTable());
ph.put("totp_users_table", Config.getConfig(start).getTotpUsersTable());
ph.put("totp_user_devices_table", Config.getConfig(start).getTotpUserDevicesTable());
ph.put("totp_used_codes_table", Config.getConfig(start).getTotpUsedCodesTable());
ph.put("user_last_active_table", Config.getConfig(start).getUserLastActiveTable());
ph.put("userid_mapping_table", Config.getConfig(start).getUserIdMappingTable());

rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
return ph;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,4 @@ static int update(Connection con, String QUERY, PreparedStatementValueSetter set
return pst.executeUpdate();
}
}

}
3 changes: 3 additions & 0 deletions src/main/java/io/supertokens/storage/postgresql/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ public void initStorage(boolean shouldWait) throws DbInitException {
try {
ConnectionPool.initPool(this, shouldWait);
GeneralQueries.createTablesIfNotExists(this);
if (!isTesting) {
FlywayMigration.startMigration(this);
}
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception e) {
throw new DbInitException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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 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 {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved

@Override
public void migrate(Context context) throws Exception {
Map<String, String> ph = context.getConfiguration().getPlaceholders();
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" +
"(" + !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");

// 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, " +
"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 " + ph.get("session_access_token_signing_keys_table"));
}
}
}
Loading