|
| 1 | +/*- |
| 2 | + * ========================LICENSE_START================================= |
| 3 | + * flyway-database-yugabytedb |
| 4 | + * ======================================================================== |
| 5 | + * Copyright (C) 2010 - 2024 Red Gate Software Ltd |
| 6 | + * ======================================================================== |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * =========================LICENSE_END================================== |
| 19 | + */ |
| 20 | +/* |
| 21 | + * Copyright (C) Red Gate Software Ltd 2010-2024 |
| 22 | + * |
| 23 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 24 | + * you may not use this file except in compliance with the License. |
| 25 | + * You may obtain a copy of the License at |
| 26 | + * |
| 27 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 28 | + * |
| 29 | + * Unless required by applicable law or agreed to in writing, software |
| 30 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 31 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 32 | + * See the License for the specific language governing permissions and |
| 33 | + * limitations under the License. |
| 34 | + */ |
| 35 | +package org.flywaydb.community.database.kingbasemysql; |
| 36 | + |
| 37 | +import lombok.CustomLog; |
| 38 | +import org.flywaydb.core.api.configuration.Configuration; |
| 39 | +import org.flywaydb.core.internal.database.base.Table; |
| 40 | +import org.flywaydb.core.internal.exception.FlywaySqlException; |
| 41 | +import org.flywaydb.core.internal.jdbc.JdbcConnectionFactory; |
| 42 | +import org.flywaydb.core.internal.jdbc.StatementInterceptor; |
| 43 | +import org.flywaydb.database.postgresql.PostgreSQLDatabase; |
| 44 | + |
| 45 | +import java.sql.Connection; |
| 46 | +import java.sql.SQLException; |
| 47 | + |
| 48 | + |
| 49 | +@CustomLog |
| 50 | +public class KingbaseMysqlDatabase extends PostgreSQLDatabase { |
| 51 | + |
| 52 | + public static final String LOCK_TABLE_NAME = "YB_FLYWAY_LOCK_TABLE"; |
| 53 | + /** |
| 54 | + * This table is used to enforce locking through SELECT ... FOR UPDATE on a |
| 55 | + * token row inserted in this table. The token row is inserted with the name |
| 56 | + * of the Flyway's migration history table as a token for simplicity. |
| 57 | + */ |
| 58 | + private static final String CREATE_LOCK_TABLE_DDL = "CREATE TABLE IF NOT EXISTS " + LOCK_TABLE_NAME + " (table_name varchar PRIMARY KEY, locked bool)"; |
| 59 | + |
| 60 | + public KingbaseMysqlDatabase(Configuration configuration, JdbcConnectionFactory jdbcConnectionFactory, StatementInterceptor statementInterceptor) { |
| 61 | + super(configuration, jdbcConnectionFactory, statementInterceptor); |
| 62 | + createLockTable(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected KingbaseMysqlConnection doGetConnection(Connection connection) { |
| 67 | + return new KingbaseMysqlConnection(this, connection); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void ensureSupported(Configuration configuration) { |
| 72 | + // Checks the Postgres version |
| 73 | + ensureDatabaseIsRecentEnough("11.2"); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean supportsDdlTransactions() { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public String getRawCreateScript(Table table, boolean baseline) { |
| 83 | + return "CREATE TABLE IF NOT EXISTS " + table + " (\n" + |
| 84 | + " \"installed_rank\" INT NOT NULL PRIMARY KEY,\n" + |
| 85 | + " \"version\" VARCHAR(50),\n" + |
| 86 | + " \"description\" VARCHAR(200) NOT NULL,\n" + |
| 87 | + " \"type\" VARCHAR(20) NOT NULL,\n" + |
| 88 | + " \"script\" VARCHAR(1000) NOT NULL,\n" + |
| 89 | + " \"checksum\" INTEGER,\n" + |
| 90 | + " \"installed_by\" VARCHAR(100) NOT NULL,\n" + |
| 91 | + " \"installed_on\" TIMESTAMP NOT NULL DEFAULT now(),\n" + |
| 92 | + " \"execution_time\" INTEGER NOT NULL,\n" + |
| 93 | + " \"success\" BOOLEAN NOT NULL\n" + |
| 94 | + ");\n" + |
| 95 | + (baseline ? getBaselineStatement(table) + ";\n" : "") + |
| 96 | + "CREATE INDEX IF NOT EXISTS \"" + table.getName() + "_s_idx\" ON " + table + " (\"success\");"; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + @Override |
| 101 | + public boolean useSingleConnection() { |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public String getBooleanTrue() { |
| 107 | + return "1"; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String getBooleanFalse() { |
| 112 | + return "0"; |
| 113 | + } |
| 114 | + @Override |
| 115 | + public String getOpenQuote() { |
| 116 | + return "`"; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public String getCloseQuote() { |
| 121 | + return "`"; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public boolean catalogIsSchema() { |
| 126 | + return true; |
| 127 | + } |
| 128 | + |
| 129 | + private void createLockTable() { |
| 130 | + try { |
| 131 | + jdbcTemplate.execute(CREATE_LOCK_TABLE_DDL); |
| 132 | + } catch (SQLException e) { |
| 133 | + throw new FlywaySqlException("Unable to initialize the lock table", e); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments