forked from trinodb/trino-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use flyway to perform database migrations on startup
- Loading branch information
Showing
19 changed files
with
663 additions
and
14 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
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
57 changes: 57 additions & 0 deletions
57
gateway-ha/src/main/java/io/trino/gateway/ha/persistence/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,57 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* 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.trino.gateway.ha.persistence; | ||
|
||
import io.airlift.log.Logger; | ||
import io.trino.gateway.ha.config.DataStoreConfiguration; | ||
import org.flywaydb.core.Flyway; | ||
import org.flywaydb.core.api.output.MigrateResult; | ||
|
||
import static java.lang.String.format; | ||
|
||
public class FlywayMigration | ||
{ | ||
private static final Logger log = Logger.get(FlywayMigration.class); | ||
|
||
private FlywayMigration() {} | ||
|
||
private static String getLocation(String configDbUrl) | ||
{ | ||
if (configDbUrl.startsWith("jdbc:postgresql")) { | ||
return "postgresql"; | ||
} | ||
if (configDbUrl.startsWith("jdbc:mysql")) { | ||
return "mysql"; | ||
} | ||
throw new IllegalArgumentException(format("Invalid JDBC URL: %s. Only PostgreSQL and MySQL are supported.", configDbUrl)); | ||
} | ||
|
||
public static void migrate(DataStoreConfiguration config) | ||
{ | ||
if (!config.isRunMigrationsEnabled()) { | ||
log.info("Skip migrations as automatic migrations are disabled"); | ||
return; | ||
} | ||
log.info("Performing migrations..."); | ||
Flyway flyway = Flyway.configure() | ||
.dataSource(config.getJdbcUrl(), config.getUser(), config.getPassword()) | ||
.locations(getLocation(config.getJdbcUrl())) | ||
.baselineOnMigrate(true) | ||
.baselineVersion("0") | ||
.load(); | ||
|
||
MigrateResult migrations = flyway.migrate(); | ||
log.info("Performed %s migrations", migrations.migrationsExecuted); | ||
} | ||
} |
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,78 @@ | ||
CREATE TABLE IF NOT EXISTS gateway_backend ( | ||
name VARCHAR(256) PRIMARY KEY, | ||
routing_group VARCHAR (256), | ||
backend_url VARCHAR (256), | ||
external_url VARCHAR (256), | ||
active BOOLEAN | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS query_history ( | ||
query_id VARCHAR(256) PRIMARY KEY, | ||
query_text VARCHAR (256), | ||
created bigint, | ||
backend_url VARCHAR (256), | ||
user_name VARCHAR(256), | ||
source VARCHAR(256) | ||
); | ||
CREATE INDEX query_history_created_idx ON query_history(created); | ||
|
||
CREATE TABLE IF NOT EXISTS resource_groups ( | ||
resource_group_id BIGINT NOT NULL AUTO_INCREMENT, | ||
name VARCHAR(250) NOT NULL UNIQUE, | ||
|
||
-- OPTIONAL POLICY CONTROLS | ||
parent BIGINT NULL, | ||
jmx_export BOOLEAN NULL, | ||
scheduling_policy VARCHAR(128) NULL, | ||
scheduling_weight INT NULL, | ||
|
||
-- REQUIRED QUOTAS | ||
soft_memory_limit VARCHAR(128) NOT NULL, | ||
max_queued INT NOT NULL, | ||
hard_concurrency_limit INT NOT NULL, | ||
|
||
-- OPTIONAL QUOTAS | ||
soft_concurrency_limit INT NULL, | ||
soft_cpu_limit VARCHAR(128) NULL, | ||
hard_cpu_limit VARCHAR(128) NULL, | ||
environment VARCHAR(128) NULL, | ||
|
||
PRIMARY KEY(resource_group_id), | ||
FOREIGN KEY (parent) REFERENCES resource_groups (resource_group_id) | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS selectors ( | ||
resource_group_id BIGINT NOT NULL, | ||
priority BIGINT NOT NULL, | ||
|
||
-- Regex fields -- these will be used as a regular expression pattern to | ||
-- match against the field of the same name on queries | ||
user_regex VARCHAR(512), | ||
source_regex VARCHAR(512), | ||
|
||
-- Selector fields -- these must match exactly. | ||
query_type VARCHAR(512), | ||
client_tags VARCHAR(512), | ||
selector_resource_estimate VARCHAR(1024), | ||
|
||
FOREIGN KEY (resource_group_id) REFERENCES resource_groups(resource_group_id) | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS resource_groups_global_properties ( | ||
name VARCHAR(128) NOT NULL PRIMARY KEY, | ||
value VARCHAR(512) NULL, | ||
CHECK (name in ('cpu_quota_period')) | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS exact_match_source_selectors ( | ||
resource_group_id VARCHAR(256) NOT NULL, | ||
update_time DATETIME NOT NULL, | ||
|
||
-- Selector fields which must exactly match a query | ||
source VARCHAR(512) NOT NULL, | ||
environment VARCHAR(128), | ||
query_type VARCHAR(512), | ||
|
||
PRIMARY KEY (environment, source(128), query_type), | ||
UNIQUE (source(128), environment, query_type(128), resource_group_id) | ||
); |
78 changes: 78 additions & 0 deletions
78
gateway-ha/src/main/resources/postgresql/V1__create_schema.sql
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,78 @@ | ||
CREATE TABLE IF NOT EXISTS gateway_backend ( | ||
name VARCHAR(256) PRIMARY KEY, | ||
routing_group VARCHAR (256), | ||
backend_url VARCHAR (256), | ||
external_url VARCHAR (256), | ||
active BOOLEAN | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS query_history ( | ||
query_id VARCHAR(256) PRIMARY KEY, | ||
query_text VARCHAR (256), | ||
created bigint, | ||
backend_url VARCHAR (256), | ||
user_name VARCHAR(256), | ||
source VARCHAR(256) | ||
); | ||
CREATE INDEX IF NOT EXISTS query_history_created_idx ON query_history(created); | ||
|
||
CREATE TABLE IF NOT EXISTS resource_groups ( | ||
resource_group_id SERIAL, | ||
name VARCHAR(250) NOT NULL UNIQUE, | ||
|
||
-- OPTIONAL POLICY CONTROLS | ||
parent BIGINT NULL, | ||
jmx_export BOOLEAN NULL, | ||
scheduling_policy VARCHAR(128) NULL, | ||
scheduling_weight INT NULL, | ||
|
||
-- REQUIRED QUOTAS | ||
soft_memory_limit VARCHAR(128) NOT NULL, | ||
max_queued INT NOT NULL, | ||
hard_concurrency_limit INT NOT NULL, | ||
|
||
-- OPTIONAL QUOTAS | ||
soft_concurrency_limit INT NULL, | ||
soft_cpu_limit VARCHAR(128) NULL, | ||
hard_cpu_limit VARCHAR(128) NULL, | ||
environment VARCHAR(128) NULL, | ||
|
||
PRIMARY KEY(resource_group_id), | ||
FOREIGN KEY (parent) REFERENCES resource_groups (resource_group_id) | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS selectors ( | ||
resource_group_id BIGINT NOT NULL, | ||
priority BIGINT NOT NULL, | ||
|
||
-- Regex fields -- these will be used as a regular expression pattern to | ||
-- match against the field of the same name on queries | ||
user_regex VARCHAR(512), | ||
source_regex VARCHAR(512), | ||
|
||
-- Selector fields -- these must match exactly. | ||
query_type VARCHAR(512), | ||
client_tags VARCHAR(512), | ||
selector_resource_estimate VARCHAR(1024), | ||
|
||
FOREIGN KEY (resource_group_id) REFERENCES resource_groups(resource_group_id) | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS resource_groups_global_properties ( | ||
name VARCHAR(128) NOT NULL PRIMARY KEY, | ||
value VARCHAR(512) NULL, | ||
CHECK (name in ('cpu_quota_period')) | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS exact_match_source_selectors ( | ||
resource_group_id VARCHAR(256) NOT NULL, | ||
update_time TIMESTAMP NOT NULL, | ||
|
||
-- Selector fields which must exactly match a query | ||
source VARCHAR(512) NOT NULL, | ||
environment VARCHAR(128), | ||
query_type VARCHAR(128), -- (reduced from 512) | ||
|
||
PRIMARY KEY (environment, source, query_type), | ||
UNIQUE (source, environment, query_type, resource_group_id) | ||
); |
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
Oops, something went wrong.