Add flyway migrate tool
This commit is contained in:
Executable
@@ -1,26 +1,4 @@
|
|||||||
DROP TABLE IF EXISTS `t_messages`;
|
|
||||||
CREATE TABLE `t_messages` (
|
|
||||||
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
|
||||||
`_mid` bigint(20) NOT NULL,
|
|
||||||
`_from` varchar(64) NOT NULL,
|
|
||||||
`_type` tinyint NOT NULL DEFAULT 0,
|
|
||||||
`_target` varchar(64) NOT NULL,
|
|
||||||
`_line` int(11) NOT NULL DEFAULT 0,
|
|
||||||
`_data` BLOB NOT NULL,
|
|
||||||
`_searchable_key` TEXT DEFAULT NULL,
|
|
||||||
`_dt` DATETIME NOT NULL DEFAULT NOW(),
|
|
||||||
UNIQUE INDEX `message_uid_index` (`_mid` ASC)
|
|
||||||
);
|
|
||||||
|
|
||||||
DROP TABLE IF EXISTS `t_user_messages`;
|
|
||||||
CREATE TABLE `t_user_messages` (
|
|
||||||
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
|
||||||
`_mid` bigint(20) NOT NULL,
|
|
||||||
`_uid` varchar(64) NOT NULL,
|
|
||||||
`_seq` bigint(20) NOT NULL,
|
|
||||||
`_dt` DATETIME NOT NULL DEFAULT NOW(),
|
|
||||||
UNIQUE INDEX `message_seq_uid_index` (`_uid` DESC, `_seq` DESC)
|
|
||||||
);
|
|
||||||
|
|
||||||
DROP TABLE IF EXISTS `t_group`;
|
DROP TABLE IF EXISTS `t_group`;
|
||||||
CREATE TABLE `t_group` (
|
CREATE TABLE `t_group` (
|
||||||
@@ -220,17 +198,3 @@ CREATE TABLE `t_sensitiveword` (
|
|||||||
`_word` TEXT DEFAULT NULL
|
`_word` TEXT DEFAULT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
insert into t_chatroom (`_cid`, `_title`, `_desc`,`_dt`) values ('chatroom1','火信聊天室1','火信测试聊天室1,用来演示聊天室功能', 1);
|
|
||||||
|
|
||||||
insert into t_chatroom (`_cid`, `_title`, `_desc`,`_dt`) values ('chatroom2','火信聊天室2','火信测试聊天室2,用来演示聊天室功能', 1);
|
|
||||||
|
|
||||||
insert into t_chatroom (`_cid`, `_title`, `_desc`,`_dt`) values ('chatroom3','火信聊天室3','火信测试聊天室3,用来演示聊天室功能', 1);
|
|
||||||
|
|
||||||
insert into t_user (`_uid`,`_name`,`_display_name`,`_portrait`,`_type`,`_dt`) values ('FireRobot','FireRobot','小火','http://cdn2.wildfirechat.cn/robot.png',1,1);
|
|
||||||
|
|
||||||
insert into t_robot (`_uid`,`_owner`,`_secret`,`_callback`,`_state`,`_dt`) values ('FireRobot', 'FireRobot', '123456', 'http://127.0.0.1:8883/robot/recvmsg', 0, 1);
|
|
||||||
|
|
||||||
|
|
||||||
alter table t_friend add column `_alias` varchar(64) DEFAULT NULL;
|
|
||||||
+24
@@ -0,0 +1,24 @@
|
|||||||
|
|
||||||
|
DROP TABLE IF EXISTS `t_messages`;
|
||||||
|
CREATE TABLE `t_messages` (
|
||||||
|
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||||
|
`_mid` bigint(20) NOT NULL,
|
||||||
|
`_from` varchar(64) NOT NULL,
|
||||||
|
`_type` tinyint NOT NULL DEFAULT 0,
|
||||||
|
`_target` varchar(64) NOT NULL,
|
||||||
|
`_line` int(11) NOT NULL DEFAULT 0,
|
||||||
|
`_data` BLOB NOT NULL,
|
||||||
|
`_searchable_key` TEXT DEFAULT NULL,
|
||||||
|
`_dt` DATETIME NOT NULL DEFAULT NOW(),
|
||||||
|
UNIQUE INDEX `message_uid_index` (`_mid` ASC)
|
||||||
|
);
|
||||||
|
|
||||||
|
DROP TABLE IF EXISTS `t_user_messages`;
|
||||||
|
CREATE TABLE `t_user_messages` (
|
||||||
|
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||||
|
`_mid` bigint(20) NOT NULL,
|
||||||
|
`_uid` varchar(64) NOT NULL,
|
||||||
|
`_seq` bigint(20) NOT NULL,
|
||||||
|
`_dt` DATETIME NOT NULL DEFAULT NOW(),
|
||||||
|
UNIQUE INDEX `message_seq_uid_index` (`_uid` DESC, `_seq` DESC)
|
||||||
|
);
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
insert into t_chatroom (`_cid`, `_title`, `_desc`,`_dt`) values ('chatroom1','火信聊天室1','火信测试聊天室1,用来演示聊天室功能', 1);
|
||||||
|
|
||||||
|
insert into t_chatroom (`_cid`, `_title`, `_desc`,`_dt`) values ('chatroom2','火信聊天室2','火信测试聊天室2,用来演示聊天室功能', 1);
|
||||||
|
|
||||||
|
insert into t_chatroom (`_cid`, `_title`, `_desc`,`_dt`) values ('chatroom3','火信聊天室3','火信测试聊天室3,用来演示聊天室功能', 1);
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
insert into t_user (`_uid`,`_name`,`_display_name`,`_portrait`,`_type`,`_dt`) values ('FireRobot','FireRobot','小火','http://cdn2.wildfirechat.cn/robot.png',1,1);
|
||||||
|
|
||||||
|
insert into t_robot (`_uid`,`_owner`,`_secret`,`_callback`,`_state`,`_dt`) values ('FireRobot', 'FireRobot', '123456', 'http://127.0.0.1:8883/robot/recvmsg', 0, 1);
|
||||||
Executable
+2
@@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
alter table t_friend add column `_alias` varchar(64) DEFAULT NULL;
|
||||||
Executable
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
DROP TABLE IF EXISTS `t_messages`;
|
DROP TABLE IF EXISTS `t_messages`;
|
||||||
CREATE TABLE `t_messages` (
|
CREATE TABLE `t_messages` (
|
||||||
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||||
+2353
File diff suppressed because it is too large
Load Diff
@@ -257,6 +257,13 @@
|
|||||||
<version>3.13.1</version>
|
<version>3.13.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.flywaydb/flyway-core -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.flywaydb</groupId>
|
||||||
|
<artifactId>flyway-core</artifactId>
|
||||||
|
<version>5.2.4</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
CREATE DATABASE IF NOT EXISTS wfchat CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
show_help_info(){
|
|
||||||
echo "### Usage: ./initial_db.sh -u{user} -p{password}"
|
|
||||||
echo "### 初始化数据库。"
|
|
||||||
echo "###"
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for i in "$@"
|
|
||||||
do
|
|
||||||
PFLAG=`echo $i|cut -b1-2`
|
|
||||||
PPARAM=`echo $i|cut -b3-`
|
|
||||||
if [ $PFLAG = "-u" ]; then
|
|
||||||
USER=${PPARAM}
|
|
||||||
elif [ $PFLAG = "-p" ]; then
|
|
||||||
PASSWORD=${PPARAM}
|
|
||||||
elif [ $PFLAG = "-h" ]; then
|
|
||||||
show_help_info
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
echo
|
|
||||||
|
|
||||||
if [ "${USER}" = "" ]; then
|
|
||||||
show_help_info
|
|
||||||
exit 1
|
|
||||||
elif [ "${PASSWORD}" = "" ]; then
|
|
||||||
show_help_info
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
mysql -u${USER} -p${PASSWORD} -e"source 1_create_database.sql;"
|
|
||||||
mysql -u${USER} -p${PASSWORD} -e"use wfchat; source 2_create_table.sql; source 3_create_default_chatroom.sql; source 4_create_default_robot.sql; source 5_add_friend_alias.sql;"
|
|
||||||
|
|
||||||
for((i=0;i<36;i++));
|
|
||||||
do
|
|
||||||
create_message_tbl_sql="USE wfchat;
|
|
||||||
DROP TABLE IF EXISTS \`t_messages_${i}\`;
|
|
||||||
CREATE TABLE \`t_messages_${i}\` (
|
|
||||||
\`id\` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
|
||||||
\`_mid\` bigint(20) NOT NULL,
|
|
||||||
\`_from\` varchar(64) NOT NULL,
|
|
||||||
\`_type\` tinyint NOT NULL DEFAULT 0,
|
|
||||||
\`_target\` varchar(64) NOT NULL,
|
|
||||||
\`_line\` int(11) NOT NULL DEFAULT 0,
|
|
||||||
\`_data\` BLOB NOT NULL,
|
|
||||||
\`_searchable_key\` TEXT DEFAULT NULL,
|
|
||||||
\`_dt\` DATETIME NOT NULL,
|
|
||||||
INDEX \`message_uid_index\` (\`_mid\` DESC)
|
|
||||||
)
|
|
||||||
ENGINE = InnoDB
|
|
||||||
DEFAULT CHARACTER SET = utf8mb4
|
|
||||||
COLLATE = utf8mb4_unicode_ci;
|
|
||||||
"
|
|
||||||
|
|
||||||
echo "create table t_messages_$i"
|
|
||||||
mysql -u${USER} -p${PASSWORD} -e"${create_message_tbl_sql}"
|
|
||||||
done
|
|
||||||
|
|
||||||
|
|
||||||
for((i=0;i<128;i++));
|
|
||||||
do
|
|
||||||
create_user_message_tbl_sql="USE wfchat;
|
|
||||||
DROP TABLE IF EXISTS \`t_user_messages_${i}\`;
|
|
||||||
CREATE TABLE \`t_user_messages_${i}\` (
|
|
||||||
\`id\` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
|
||||||
\`_mid\` bigint(20) NOT NULL,
|
|
||||||
\`_uid\` varchar(64) NOT NULL,
|
|
||||||
\`_seq\` bigint(20) NOT NULL,
|
|
||||||
\`_dt\` DATETIME NOT NULL DEFAULT NOW(),
|
|
||||||
INDEX \`message_seq_uid_index\` (\`_uid\` DESC, \`_seq\` DESC)
|
|
||||||
)
|
|
||||||
ENGINE = InnoDB
|
|
||||||
DEFAULT CHARACTER SET = utf8mb4
|
|
||||||
COLLATE = utf8mb4_unicode_ci;
|
|
||||||
"
|
|
||||||
|
|
||||||
echo "create table t_user_messages_$i"
|
|
||||||
mysql -u${USER} -p${PASSWORD} -e"${create_user_message_tbl_sql}"
|
|
||||||
done
|
|
||||||
@@ -370,7 +370,8 @@ public class Server {
|
|||||||
throw new IllegalArgumentException("nodeId error: " + nodeIdStr);
|
throw new IllegalArgumentException("nodeId error: " + nodeIdStr);
|
||||||
}
|
}
|
||||||
if (nodeIdSet != null && nodeIdSet.contains(nodeId)){
|
if (nodeIdSet != null && nodeIdSet.contains(nodeId)){
|
||||||
throw new IllegalArgumentException("nodeId conflict " + nodeId + ", 请修改wildfirechat.conf文件,是各个节点的nodeId不冲突");
|
LOG.error("只允许一个实例运行,多个实例会引起冲突,进程终止");
|
||||||
|
System.exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
MessageShardingUtil.setNodeId(nodeId);
|
MessageShardingUtil.setNodeId(nodeId);
|
||||||
|
|||||||
@@ -12,9 +12,7 @@ package win.liyufan.im;
|
|||||||
import java.beans.PropertyVetoException;
|
import java.beans.PropertyVetoException;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.sql.ResultSet;
|
import java.sql.*;
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -22,11 +20,11 @@ import com.hazelcast.util.StringUtil;
|
|||||||
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||||
import io.moquette.BrokerConstants;
|
import io.moquette.BrokerConstants;
|
||||||
import io.moquette.server.config.IConfig;
|
import io.moquette.server.config.IConfig;
|
||||||
|
import org.flywaydb.core.Flyway;
|
||||||
|
import org.flywaydb.core.api.configuration.FlywayConfiguration;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
public class DBUtil {
|
public class DBUtil {
|
||||||
@@ -53,9 +51,10 @@ public class DBUtil {
|
|||||||
LOG.info("Use mysql database");
|
LOG.info("Use mysql database");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (comboPooledDataSource == null) {
|
if (comboPooledDataSource == null) {
|
||||||
|
String migrateLocation;
|
||||||
if (IsEmbedDB) {
|
if (IsEmbedDB) {
|
||||||
|
migrateLocation = "filesystem:./migrate/h2";
|
||||||
comboPooledDataSource = new ComboPooledDataSource();
|
comboPooledDataSource = new ComboPooledDataSource();
|
||||||
|
|
||||||
comboPooledDataSource.setJdbcUrl( "jdbc:h2:./h2db/wfchat;AUTO_SERVER=TRUE;MODE=MySQL" );
|
comboPooledDataSource.setJdbcUrl( "jdbc:h2:./h2db/wfchat;AUTO_SERVER=TRUE;MODE=MySQL" );
|
||||||
@@ -74,48 +73,34 @@ public class DBUtil {
|
|||||||
} catch (PropertyVetoException e) {
|
} catch (PropertyVetoException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Utility.printExecption(LOG, e);
|
Utility.printExecption(LOG, e);
|
||||||
}
|
System.exit(-1);
|
||||||
|
|
||||||
ResultSet rs = null;
|
|
||||||
Connection connection = null;
|
|
||||||
try {
|
|
||||||
connection = comboPooledDataSource.getConnection();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Utility.printExecption(LOG, e);
|
|
||||||
}
|
|
||||||
Statement stmt = null;
|
|
||||||
try {
|
|
||||||
connection = DBUtil.getConnection();
|
|
||||||
rs = connection.getMetaData().getTables(null, null, "t_user_setting", null );
|
|
||||||
if (!rs.next()) {
|
|
||||||
List<String> creatsqls = getCreateSql();
|
|
||||||
for (String createsql : creatsqls) {
|
|
||||||
if (StringUtil.isNullOrEmpty(createsql)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
stmt = connection.createStatement();
|
|
||||||
if (0 != stmt.executeLargeUpdate(createsql)) {
|
|
||||||
LOG.error("创建表失败! {}", createsql);
|
|
||||||
// System.exit(-1);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
stmt.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Utility.printExecption(LOG, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Utility.printExecption(LOG, e);
|
|
||||||
} finally {
|
|
||||||
DBUtil.closeDB(connection, null, rs);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
migrateLocation = "filesystem:./migrate/mysql";
|
||||||
comboPooledDataSource = new ComboPooledDataSource("mysql");
|
comboPooledDataSource = new ComboPooledDataSource("mysql");
|
||||||
|
try {
|
||||||
|
String url01 = comboPooledDataSource.getJdbcUrl().substring(0,comboPooledDataSource.getJdbcUrl().indexOf("?"));
|
||||||
|
|
||||||
|
String url02 = url01.substring(0,url01.lastIndexOf("/"));
|
||||||
|
|
||||||
|
String datasourceName = url01.substring(url01.lastIndexOf("/")+1);
|
||||||
|
// 连接已经存在的数据库,如:mysql
|
||||||
|
Connection connection = DriverManager.getConnection(url02, comboPooledDataSource.getUser(), comboPooledDataSource.getPassword());
|
||||||
|
Statement statement = connection.createStatement();
|
||||||
|
|
||||||
|
// 创建数据库
|
||||||
|
statement.executeUpdate("CREATE DATABASE IF NOT EXISTS `" + datasourceName + "` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;");
|
||||||
|
|
||||||
|
statement.close();
|
||||||
|
connection.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Flyway flyway = Flyway.configure().dataSource(comboPooledDataSource).locations(migrateLocation).baselineOnMigrate(true).load();
|
||||||
|
flyway.migrate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,18 @@
|
|||||||
<includeBaseDirectory>false
|
<includeBaseDirectory>false
|
||||||
</includeBaseDirectory> <!-- disable the creation of root's distribution dir in the archive -->
|
</includeBaseDirectory> <!-- disable the creation of root's distribution dir in the archive -->
|
||||||
|
|
||||||
|
<fileSets>
|
||||||
|
<fileSet>
|
||||||
|
<directory>../broker/migrate/mysql/</directory>
|
||||||
|
<outputDirectory>/migrate/mysql</outputDirectory>
|
||||||
|
</fileSet>
|
||||||
|
|
||||||
|
<fileSet>
|
||||||
|
<directory>../broker/migrate/h2/</directory>
|
||||||
|
<outputDirectory>/migrate/h2</outputDirectory>
|
||||||
|
</fileSet>
|
||||||
|
</fileSets>
|
||||||
|
|
||||||
<files>
|
<files>
|
||||||
<!-- executables scripts-->
|
<!-- executables scripts-->
|
||||||
<file>
|
<file>
|
||||||
@@ -46,47 +58,11 @@
|
|||||||
<source>src/main/resources/README.txt</source>
|
<source>src/main/resources/README.txt</source>
|
||||||
</file>
|
</file>
|
||||||
|
|
||||||
<file>
|
|
||||||
<source>../broker/sql/1_create_database.sql</source>
|
|
||||||
<outputDirectory>sql/</outputDirectory>
|
|
||||||
</file>
|
|
||||||
|
|
||||||
<file>
|
|
||||||
<source>../broker/sql/2_create_table.sql</source>
|
|
||||||
<outputDirectory>sql/</outputDirectory>
|
|
||||||
</file>
|
|
||||||
|
|
||||||
<file>
|
|
||||||
<source>../broker/sql/3_create_default_chatroom.sql</source>
|
|
||||||
<outputDirectory>sql/</outputDirectory>
|
|
||||||
</file>
|
|
||||||
|
|
||||||
<file>
|
|
||||||
<source>../broker/sql/4_create_default_robot.sql</source>
|
|
||||||
<outputDirectory>sql/</outputDirectory>
|
|
||||||
</file>
|
|
||||||
|
|
||||||
<file>
|
|
||||||
<source>../broker/sql/5_add_friend_alias.sql</source>
|
|
||||||
<outputDirectory>sql/</outputDirectory>
|
|
||||||
</file>
|
|
||||||
|
|
||||||
<file>
|
|
||||||
<source>../broker/sql/initialize_db.sh</source>
|
|
||||||
<outputDirectory>sql/</outputDirectory>
|
|
||||||
</file>
|
|
||||||
|
|
||||||
<file>
|
<file>
|
||||||
<source>src/main/resources/git.properties</source>
|
<source>src/main/resources/git.properties</source>
|
||||||
<outputDirectory>config/</outputDirectory>
|
<outputDirectory>config/</outputDirectory>
|
||||||
</file>
|
</file>
|
||||||
|
|
||||||
|
|
||||||
<file>
|
|
||||||
<source>../broker/h2/create_table.sql</source>
|
|
||||||
<outputDirectory>h2/</outputDirectory>
|
|
||||||
</file>
|
|
||||||
|
|
||||||
<file>
|
<file>
|
||||||
<source>../broker/target/moquette-broker-${project.version}.jar</source>
|
<source>../broker/target/moquette-broker-${project.version}.jar</source>
|
||||||
<outputDirectory>lib/</outputDirectory>
|
<outputDirectory>lib/</outputDirectory>
|
||||||
|
|||||||
Reference in New Issue
Block a user