Skip to content

Commit 5615030

Browse files
committed
add support for kingbase
Signed-off-by: dbgroupdlmu <wangxite@dlmu.edu.cn>
1 parent 83e4cb0 commit 5615030

28 files changed

+2829
-0
lines changed

flyway-database-kingbase/pom.xml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (C) Red Gate Software Ltd 2010-2024
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
<parent>
24+
<groupId>org.flywaydb</groupId>
25+
<artifactId>flyway-community-db-support</artifactId>
26+
<version>10.16.1</version>
27+
</parent>
28+
29+
<artifactId>flyway-database-kingbase</artifactId>
30+
<name>${project.artifactId}</name>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>${project.groupId}</groupId>
35+
<artifactId>flyway-core</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>${project.groupId}</groupId>
39+
<artifactId>flyway-database-postgresql</artifactId>
40+
<version>${version.flyway}</version>
41+
<exclusions>
42+
<exclusion>
43+
<groupId>org.flywaydb</groupId>
44+
<artifactId>flyway-core</artifactId>
45+
</exclusion>
46+
</exclusions>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.projectlombok</groupId>
50+
<artifactId>lombok</artifactId>
51+
<scope>provided</scope>
52+
</dependency>
53+
</dependencies>
54+
55+
<build>
56+
<resources>
57+
<resource>
58+
<directory>src/main/resources</directory>
59+
<filtering>true</filtering>
60+
</resource>
61+
</resources>
62+
<plugins>
63+
<plugin>
64+
<artifactId>maven-resources-plugin</artifactId>
65+
</plugin>
66+
<plugin>
67+
<artifactId>maven-jar-plugin</artifactId>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
</project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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;
36+
37+
import org.flywaydb.core.api.FlywayException;
38+
import org.flywaydb.core.extensibility.PluginMetadata;
39+
import org.flywaydb.core.internal.util.FileUtils;
40+
41+
import java.io.IOException;
42+
import java.nio.charset.StandardCharsets;
43+
44+
public class KingbaseDatabaseExtension implements PluginMetadata {
45+
public String getDescription() {
46+
return "Community-contributed kingbase database support extension " + readVersion() + " by Redgate";
47+
}
48+
49+
public static String readVersion() {
50+
try {
51+
return FileUtils.copyToString(
52+
KingbaseDatabaseExtension.class.getClassLoader().getResourceAsStream("org/flywaydb/community" +
53+
"/database/kingbase/version.txt"),
54+
StandardCharsets.UTF_8);
55+
} catch (IOException e) {
56+
throw new FlywayException("Unable to read extension version: " + e.getMessage(), e);
57+
}
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 org.flywaydb.core.internal.database.base.Schema;
38+
import org.flywaydb.core.internal.database.base.Table;
39+
import org.flywaydb.database.postgresql.PostgreSQLConnection;
40+
41+
import java.util.concurrent.Callable;
42+
43+
public class KingbaseMysqlConnection extends PostgreSQLConnection {
44+
45+
KingbaseMysqlConnection(KingbaseMysqlDatabase database, java.sql.Connection connection) {
46+
super(database, connection);
47+
}
48+
49+
@Override
50+
public Schema getSchema(String name) {
51+
return new KingbaseMysqlSchema(jdbcTemplate, (KingbaseMysqlDatabase) database, name);
52+
}
53+
54+
@Override
55+
public <T> T lock(Table table, Callable<T> callable) {
56+
return new KingbaseMysqlExecutionTemplate(jdbcTemplate, table.toString()).execute(callable);
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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

Comments
 (0)