Unverified Commit 6801c4fc authored by Zzm0809's avatar Zzm0809 Committed by GitHub

[Feature] Support Flyway (#3468)

parent 7fa870fb
......@@ -336,7 +336,14 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
</dependency>
</dependencies>
<build>
......
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.springframework.jdbc.datasource.init;
import java.sql.Connection;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.util.Assert;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.db.Db;
/**
* Utility methods for executing a {@link DatabasePopulator}.
*
* @author Juergen Hoeller
* @author Oliver Gierke
* @author Sam Brannen
* @since 3.1
*/
public abstract class DatabasePopulatorUtils {
private static boolean isInit = true;
/**
* Execute the given {@link DatabasePopulator} against the given {@link DataSource}.
* <p>As of Spring Framework 5.3.11, the {@link Connection} for the supplied
* {@code DataSource} will be {@linkplain Connection#commit() committed} if
* it is not configured for {@link Connection#getAutoCommit() auto-commit} and
* is not {@linkplain DataSourceUtils#isConnectionTransactional transactional}.
*
* @param populator the {@code DatabasePopulator} to execute
* @param dataSource the {@code DataSource} to execute against
* @throws DataAccessException if an error occurs, specifically a {@link ScriptException}
* @see DataSourceUtils#isConnectionTransactional(Connection, DataSource)
*/
public static void execute(DatabasePopulator populator, DataSource dataSource) throws DataAccessException {
Assert.notNull(populator, "DatabasePopulator must not be null");
Assert.notNull(dataSource, "DataSource must not be null");
try {
// Override this method and do not run it if the table exists(重写此方法,如果有表存在,则不运行)
if (isInit && CollUtil.isNotEmpty(Db.use(dataSource).query("show tables"))) {
return;
}
isInit = false;
Connection connection = DataSourceUtils.getConnection(dataSource);
try {
populator.populate(connection);
if (!connection.getAutoCommit() && !DataSourceUtils.isConnectionTransactional(connection, dataSource)) {
connection.commit();
}
} finally {
DataSourceUtils.releaseConnection(connection, dataSource);
}
} catch (ScriptException ex) {
throw ex;
} catch (Throwable ex) {
throw new UncategorizedScriptException("Failed to execute database script", ex);
}
}
}
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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
#
# https://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.
#
spring:
flyway:
enabled: true # Is it enabled
group: true # Enable grouping
locations:
- classpath:db/migration/${spring.profiles.active}
table: _dinky_flyway_schema_history
placeholder-replacement: false # Placeholder Replacement
baseline-on-migrate: true # Baseline during migration
baseline-version: 1.0.2 # Baseline version
validate-on-migrate: false # Verify during migration
placeholder-prefix: '##{'
placeholder-suffix: '}##'
fail-on-missing-locations: true # Does the non-existent migration file throw an exception
......@@ -27,9 +27,4 @@ spring:
h2:
console:
enabled: true
path: /api/h2
sql:
init:
schema-locations: classpath:db/db-h2-ddl.sql
data-locations: classpath:db/db-h2-dml.sql
mode: always
\ No newline at end of file
path: /api/h2
\ No newline at end of file
......@@ -17,7 +17,9 @@ spring:
# If you use the h2 database, please configure the h2 database connection information in application-h2.yml,
# note: the h2 database is only for experience use, and the related data that has been created cannot be migrated, please use it with caution
active: ${DB_ACTIVE:h2} #[h2,mysql,pgsql]
include: jmx
include:
- jmx
- flyway
lifecycle:
timeout-per-shutdown-phase: 30s
......
This diff is collapsed.
# Chinese
## 前置要求
- 数据库版本: MySQL 5.7+
- 必须有 mysql/postgresql 连接包
## 数据库脚本规则
- V 开头的代表发布版本; 规则: `V{版本号}__{描述}.sql`
- R 开头的代表回滚版本; 规则: `R{版本号}__{描述}.sql`
## 命名规则
- V{版本号}_{描述}.sql eg: V1.0.2__modify-ddl.sql
- R{版本号}_{描述}.sql eg: R1.0.2__modify-ddl.sql
**注意:**
- V{版本号}__{描述}.sql 中间是两个下划线,固定规则,不符合规则将无法执行
- 每个版本只能有一个 V{版本号}__{描述}.sql 文件,否则将无法执行, 不管是 DDL 还是 DML 统一放在一个文件中
# English
## Pre requirements
- Database version: MySQL 5.7+
- Must have MySQL/postgreSQL connection package
## Database Script Rules
- V represents the released version; Rule: `V{Version Number}__{Description}.SQL`
- R represents the rolled back version; Rule: `R{Version Number}__{Description}.SQL`
## Naming rules
- V {version number} _ {description}. SQL eg: V1.0.2_modify ddl. SQL
- R {version number} _ {description}. SQL eg: R1.0.2_modify ddl. SQL
**Attention:**
- V{version number}__{description}.SQL has two underscores in the middle, which are fixed rules. If they do not comply with the rules, they cannot be executed
- Each version can only have one V {version number} _ {description}. SQL file, otherwise it will not be executed, whether it is DDL or DML, it will be placed in one file
\ No newline at end of file
update dinky_sys_menu
set `path`='/registration/alert/rule',
`component`='./RegCenter/Alert/AlertRule',
`perms`='registration:alert:rule',
`parent_id`=12
where `id` = 116;
update dinky_sys_menu
set `path`='/registration/alert/rule/add',
`perms`='registration:alert:rule:add'
where `id` = 117;
update dinky_sys_menu
set `path`='/registration/alert/rule/delete',
`perms`='registration:alert:rule:delete'
where `id` = 118;
update dinky_sys_menu
set `path`='/registration/alert/rule/edit',
`perms`='registration:alert:rule:edit'
where `id` = 119;
-- Increase class_name column's length from 50 to 100.
ALTER TABLE dinky_udf_manage CHANGE COLUMN class_name class_name VARCHAR(100) null DEFAULT null COMMENT 'Complete class name';
ALTER TABLE dinky_task
add COLUMN `first_level_owner` int DEFAULT NULL comment 'primary responsible person id';
ALTER TABLE dinky_task
add COLUMN `second_level_owners` varchar(128) DEFAULT NULL comment 'list of secondary responsible persons ids';
update dinky_task set first_level_owner = creator;
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
update dinky_sys_menu
set `path`='/registration/alert/rule',
`component`='./RegCenter/Alert/AlertRule',
`perms`='registration:alert:rule',
`parent_id`=12
where `id` = 116;
update dinky_sys_menu
set `path`='/registration/alert/rule/add',
`perms`='registration:alert:rule:add'
where `id` = 117;
update dinky_sys_menu
set `path`='/registration/alert/rule/delete',
`perms`='registration:alert:rule:delete'
where `id` = 118;
update dinky_sys_menu
set `path`='/registration/alert/rule/edit',
`perms`='registration:alert:rule:edit'
where `id` = 119;
-- Increase class_name column's length from 50 to 100.
ALTER TABLE dinky_udf_manage CHANGE COLUMN class_name class_name VARCHAR(100) null DEFAULT null COMMENT 'Complete class name';
ALTER TABLE dinky_task
add COLUMN `first_level_owner` int DEFAULT NULL comment 'primary responsible person id';
ALTER TABLE dinky_task
add COLUMN `second_level_owners` varchar(128) DEFAULT NULL comment 'list of secondary responsible persons ids';
update dinky_task set first_level_owner = creator;
ALTER TABLE dinky_alert_template MODIFY COLUMN `name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'template name';
SET FOREIGN_KEY_CHECKS = 1;
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment