Compare commits
4 Commits
69b45115d7
...
9cc735e7a5
Author | SHA1 | Date |
---|---|---|
an.guoyue | 9cc735e7a5 | |
an.guoyue | 116a18b97a | |
SAM2O2O | d98575d0d2 | |
SAM2O2O | 2ce34958b2 |
|
@ -11,4 +11,4 @@ target/
|
|||
windchat-server.jar
|
||||
site-file/
|
||||
site-logs/
|
||||
openzaly-server.config
|
||||
windchat-boot/openzaly-server.config
|
||||
|
|
52
README.md
52
README.md
|
@ -16,7 +16,7 @@
|
|||
简介
|
||||
----
|
||||
|
||||
WindChat 是一款开源免费私有IM聊天软件,原身是Akaxin即时通讯开源软件,由原开发者SAM2O2O个人维护,主要面向企业定制IM。
|
||||
WindChat 是一款开源免费私有IM聊天软件,原身是Akaxin即时通讯开源软件,由原开发者SAM2O2O个人维护,主要面向企业定制IM,全端开源,免费。
|
||||
|
||||
特性:
|
||||
|
||||
|
@ -40,40 +40,40 @@ WindChat 是一款开源免费私有IM聊天软件,原身是Akaxin即时通讯
|
|||
* git clone https://gitee.com/wind-chat/wind-im.git
|
||||
|
||||
|
||||
openzlay 0.10.6开始支持personal(个人版)与team版,默认状态下使用personal
|
||||
WindChat 开始支持personal(个人版)与team版,默认状态下使用personal
|
||||
|
||||
支持的启动参数:`java -jar windchat-server.jar -h`
|
||||
|
||||
openzaly Personal版本 命令:
|
||||
WindChat Personal版本 命令:
|
||||
|
||||
* 版本升级:`java -jar windchat-server.jar -upgrade` ,此命令在服务与sqlite数据库版本不一致时执行,正常情况无需执行
|
||||
|
||||
* 启动命令:`java -jar windchat-server.jar`
|
||||
|
||||
openzaly Team版本 命令:
|
||||
WindChat Team版本 命令:
|
||||
|
||||
* 生成Team版本所需配置模版:`java -jar windchat-server.jar -team`
|
||||
|
||||
* 修改配置文件: 上一步会生成 openzaly-server.config 使用mysql数据库需在openzaly-server.config配置文件中配置mysql参数:
|
||||
* 修改配置文件: 上一步会生成 windchat-server.config 使用mysql数据库需在[windchat-server.config]配置文件中配置mysql参数:
|
||||
主库(数据库编码需要设置utf8mb4):
|
||||
openzaly.mysql.host=localhost //数据库的地址
|
||||
openzaly.mysql.port=3306 //数据库端口
|
||||
openzaly.mysql.database=openzaly //数据库名称
|
||||
openzaly.mysql.username=root //mysql数据库访问用户
|
||||
openzaly.mysql.password=1234567890 //mysql数据库密码
|
||||
windchat.mysql.host=localhost //数据库的地址
|
||||
windchat.mysql.port=3306 //数据库端口
|
||||
windchat.mysql.database=openzaly //数据库名称
|
||||
windchat.mysql.username=root //mysql数据库访问用户
|
||||
windchat.mysql.password=1234567890 //mysql数据库密码
|
||||
|
||||
从库(如果需要使用主从模式,配置这里,不需要从库则不需要配置)数据库编码需要设置utf8mb4:
|
||||
openzaly.mysql.slave.host=localhost
|
||||
openzaly.mysql.slave.port=3306
|
||||
openzaly.mysql.slave.database=openzaly
|
||||
openzaly.mysql.slave.username=root
|
||||
openzaly.mysql.slave.password=1234567890
|
||||
windchat.mysql.slave.host=localhost
|
||||
windchat.mysql.slave.port=3306
|
||||
windchat.mysql.slave.database=openzaly
|
||||
windchat.mysql.slave.username=root
|
||||
windchat.mysql.slave.password=1234567890
|
||||
|
||||
其他mysql参数为使用mysql连接池的配置参数,如若涉及性能优化可开启配置项。
|
||||
|
||||
* 迁移数据库命令:openzaly支持使用者把Personal版本的sqlite中的数据迁移到Team版本的mysql数据库
|
||||
如果执行这一步需要在openzaly-server.config配置文件中配置:
|
||||
`openzaly.sqlite.url=openzalyDB.sqlite3` 这里指定sqlite数据库文件的位置
|
||||
* 迁移数据库命令:WindChat支持使用者把Personal版本的sqlite中的数据迁移到Team版本的mysql数据库
|
||||
如果执行这一步需要在windchat-server.config配置文件中配置:
|
||||
`windchat.sqlite.url=openzalyDB.sqlite3` 这里指定sqlite数据库文件的位置
|
||||
|
||||
继续执行迁移命令:
|
||||
`java -jar windchat-server.jar -migrate`
|
||||
|
@ -83,8 +83,9 @@ openzaly Team版本 命令:
|
|||
|
||||
**2. 下载客户端**
|
||||
|
||||
> * [iOS](https://itunes.apple.com/cn/app/%E9%98%BF%E5%8D%A1%E4%BF%A1/id1346971087?mt=8)
|
||||
> * [Android](https://www.akaxin.com)
|
||||
> * [Android 等待开源]()
|
||||
> * [iOS 等待开源]()
|
||||
|
||||
|
||||
**3. 访问站点**
|
||||
|
||||
|
@ -118,15 +119,8 @@ WindChat 具有灵活、强大的扩展机制 `(“管理平台” 就是一个
|
|||
|
||||
你的聊天服务器,将摇身一变,成为一个强大的社交软件平台。
|
||||
|
||||
> 扩展机制处于技术预览阶段,如果你希望在自己的业务中开发自己的扩展,可以联系我们( mail: hi@akaxin.xyz ),我们将免费提供文档与技术答疑。
|
||||
> 扩展机制处于技术预览阶段,如果你希望在自己的业务中开发自己的扩展,可以联系我们( mail: an.guoyue254@gmail.com ),我们将免费提供文档与技术答疑。
|
||||
|
||||
以下是我们开发的一个 “校园社交” 的扩展,截图如下:
|
||||
|
||||
<p align="center">
|
||||
<img align="center" src="https://raw.githubusercontent.com/akaxincom/faq/master/app_pic/plugin.1.jpeg" width="200" /> <img align="center" src="https://raw.githubusercontent.com/akaxincom/faq/master/app_pic/plugin.2.jpeg" width="200" /> <img align="center" src="https://raw.githubusercontent.com/akaxincom/faq/master/app_pic/plugin.3.jpeg" width="200" />
|
||||
</p>
|
||||
|
||||
> 大家可以去 demo.akaxin.com 体验。
|
||||
|
||||
|
||||
四、技术贡献者
|
||||
|
@ -142,4 +136,4 @@ WindChat 具有灵活、强大的扩展机制 `(“管理平台” 就是一个
|
|||
**向我们提问**
|
||||
|
||||
> 扫码加微信群:
|
||||
![输入图片说明](https://images.gitee.com/uploads/images/2019/1210/102554_efa90037_1566564.png "屏幕截图.png")
|
||||
<img width=200 src="https://images.gitee.com/uploads/images/2019/1231/170452_318770bc_1566564.png">
|
|
@ -20,7 +20,7 @@ import org.springframework.boot.SpringApplication;
|
|||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* springboot的支持,放在openzaly-boot中
|
||||
* springboot的支持
|
||||
*
|
||||
* @author Sam{@link an.guoyue254@gmail.com}
|
||||
* @since 2018-06-05 19:31:16
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
package com.windchat.im.boot.config;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sam{@link an.guoyue254@gmail.com}
|
||||
* @since 2018-03-26 19:07:30
|
||||
*/
|
||||
public interface AkxProject {
|
||||
String PROJECT_NAME = "openzaly-boot";
|
||||
|
||||
String PROJECT_LOG_NAME = "openzaly-boot";
|
||||
|
||||
String PLN = "[openzaly-boot]";
|
||||
}
|
|
@ -1,47 +1,47 @@
|
|||
/**
|
||||
/**
|
||||
* Copyright 2018-2028 Akaxin Group
|
||||
*
|
||||
* <p>
|
||||
* 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
|
||||
*
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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.
|
||||
*/
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.windchat.im.boot.config;
|
||||
|
||||
/**
|
||||
* 站点服务,相关配置字段
|
||||
*
|
||||
*
|
||||
* @author Sam{@link an.guoyue254@gmail.com}
|
||||
* @since 2018-01-24 19:49:19
|
||||
*/
|
||||
public interface ConfigKey {
|
||||
// 站点版本
|
||||
public String SITE_VERSION = "openzaly.version";
|
||||
// 站点服务地址,提供用户和站点之间连接使用
|
||||
public String SITE_ADDRESS = "site.address";
|
||||
// 站点服务监听端口
|
||||
public String SITE_PORT = "site.port";
|
||||
// 站点启动的http服务地址,内部扩展功能访问使用
|
||||
public String PLUGIN_API_ADDRESS = "pluginapi.address";
|
||||
// http服务监听端口
|
||||
public String PLUGIN_API_PORT = "pluginapi.port";
|
||||
// 站点管理扩展地址&&端口
|
||||
public String SITE_ADMIN_ADDRESS = "site.admin.address";
|
||||
public String SITE_ADMIN_PORT = "site.admin.port";
|
||||
// 站点管理员
|
||||
public String SITE_ADMINISTRATORS = "site.administrators";
|
||||
// 站点管理员首次登陆站点,设置的邀请码
|
||||
public String SITE_ADMIN_UIC = "site.uic";
|
||||
// 存放站点图片,音频相关文件路径
|
||||
public String SITE_BASE_DIR = "site.baseDir";
|
||||
// 最大成员人数
|
||||
public String GROUP_MEMBERS_COUNT = "group.members.count";
|
||||
// 站点版本
|
||||
public String SITE_VERSION = "windchat.version";
|
||||
// 站点服务地址,提供用户和站点之间连接使用
|
||||
public String SITE_ADDRESS = "windchat.address";
|
||||
// 站点服务监听端口
|
||||
public String SITE_PORT = "windchat.port";
|
||||
// 站点启动的http服务地址,内部扩展功能访问使用
|
||||
public String PLUGIN_API_ADDRESS = "windchat.api.address";
|
||||
// http服务监听端口
|
||||
public String PLUGIN_API_PORT = "windchat.api.port";
|
||||
// 站点管理扩展地址&&端口
|
||||
public String SITE_ADMIN_ADDRESS = "windchat.admin.address";
|
||||
public String SITE_ADMIN_PORT = "windchat.admin.port";
|
||||
// 站点管理员
|
||||
public String SITE_ADMINISTRATORS = "windchat.administrators";
|
||||
// 站点管理员首次登陆站点,设置的邀请码
|
||||
public String SITE_ADMIN_UIC = "windchat.uic";
|
||||
// 存放站点图片,音频相关文件路径
|
||||
public String SITE_BASE_DIR = "windchat.baseDir";
|
||||
// 最大成员人数
|
||||
public String GROUP_MEMBERS_COUNT = "group.members.count";
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package com.windchat.im.boot.config;
|
||||
|
||||
/**
|
||||
* @author Sam{@link an.guoyue254@gmail.com}
|
||||
* @since 2018-03-26 19:07:30
|
||||
*/
|
||||
public interface WindProject {
|
||||
String PLN = "[windchat-boot]";
|
||||
}
|
|
@ -35,7 +35,7 @@ import com.windchat.common.executor.AbstracteExecutor;
|
|||
import com.windchat.common.logs.AkxLog4jManager;
|
||||
import com.windchat.common.utils.StringHelper;
|
||||
import com.akaxin.proto.core.FileProto.FileType;
|
||||
import com.windchat.im.boot.config.AkxProject;
|
||||
import com.windchat.im.boot.config.WindProject;
|
||||
import com.windchat.im.boot.config.ConfigHelper;
|
||||
import com.windchat.im.boot.config.ConfigKey;
|
||||
import com.windchat.im.boot.config.ConfigListener;
|
||||
|
@ -219,7 +219,7 @@ public class Bootstrap {
|
|||
private static void setDefaultSystemLogLevel() {
|
||||
// 更新日志级别
|
||||
AkxLog4jManager.setLogLevel(Level.INFO);
|
||||
BootLog.info("{} set system log level={}", AkxProject.PLN, Level.INFO);
|
||||
BootLog.info("{} set system log level={}", WindProject.PLN, Level.INFO);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -247,7 +247,7 @@ public class Bootstrap {
|
|||
config.setAdminServerName(PluginArgs.SITE_ADMIN_NAME);
|
||||
config.setAdminIcon(getDefaultIcon(SiteDefaultIcon.DEFAULT_SITE_ADMIN_ICON));
|
||||
config.setParam(PluginArgs.FRIEND_SQUARE, getDefaultIcon(SiteDefaultIcon.DEFAULT_FRIEND_SQUARE_ICON));
|
||||
BootLog.info("{} init datasource config={}", AkxProject.PLN, config.toString());
|
||||
BootLog.info("{} init datasource config={}", WindProject.PLN, config.toString());
|
||||
DataSourceManager.init(config);
|
||||
}
|
||||
|
||||
|
@ -265,7 +265,7 @@ public class Bootstrap {
|
|||
}
|
||||
|
||||
}.start(address, port);
|
||||
BootLog.info("{} start http server {}:{} ok.", AkxProject.PLN, address, port);
|
||||
BootLog.info("{} start http server {}:{} ok.", WindProject.PLN, address, port);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -286,7 +286,7 @@ public class Bootstrap {
|
|||
}
|
||||
|
||||
}.start(address, port);
|
||||
BootLog.info("{} start netty server {}:{} ok.", AkxProject.PLN, address, port);
|
||||
BootLog.info("{} start netty server {}:{} ok.", WindProject.PLN, address, port);
|
||||
}
|
||||
|
||||
// websocket for web-im
|
||||
|
@ -309,7 +309,7 @@ public class Bootstrap {
|
|||
// add config listener,timing to update cached config value
|
||||
private static void addConfigListener() {
|
||||
ConfigListener.startListenning();
|
||||
BootLog.info("{} start listener to site-config", AkxProject.PLN);
|
||||
BootLog.info("{} start listener to site-config", WindProject.PLN);
|
||||
}
|
||||
|
||||
// get pic by base64
|
||||
|
@ -321,7 +321,7 @@ public class Bootstrap {
|
|||
FileType.SITE_PLUGIN, null);
|
||||
return fileId;
|
||||
} catch (Exception e) {
|
||||
BootLog.error(StringHelper.format("{} set openzaly-admin default icon error", AkxProject.PLN), e);
|
||||
BootLog.error(StringHelper.format("{} set openzaly-admin default icon error", WindProject.PLN), e);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
|
|
@ -1,16 +1,3 @@
|
|||
|
||||
/ \
|
||||
/ . \
|
||||
\ . \
|
||||
/ \ \ / _ _ __ _ __ __ ___ _ _
|
||||
/ . \ | / / \ / \ | |/ / / \ \ \/ / |_ _| | \ | |
|
||||
/ . _ \ / / . \ / _ \ | ' / / _ \ \ / | | | \| |
|
||||
\ / / \ - . / / ___ \ | . \ / ___ \ / \ | | | |\ |
|
||||
\ / / | \ / /_/ \_\ |_|\_\ /_/ \_\ /_/\_\ |___| |_| \_|
|
||||
/ . \ \ /
|
||||
\ . \
|
||||
\ /
|
||||
\ /
|
||||
|
||||
|
||||
Akaxin is an open source and free proprietary IM software,you can build private openzaly-server for everyone in any server.
|
||||
-----------------------------------------------------------------------------------------------------------------
|
||||
WindChat is an open source and free proprietary IM software,you can build private IM for everyone in any server.
|
||||
-----------------------------------------------------------------------------------------------------------------
|
|
@ -15,8 +15,8 @@
|
|||
#
|
||||
|
||||
########################################################
|
||||
### ***************Akaxin ***************
|
||||
### openzaly-server
|
||||
### *************** WindChat ***************
|
||||
### windchat-server
|
||||
########################################################
|
||||
|
||||
|
||||
|
@ -25,76 +25,77 @@
|
|||
## team/TEAM edition : use mysql database,support one master with several slaves
|
||||
## enterprise/ENTERPRISE edition : use mysql + redis database ,support im server distribution
|
||||
## ultimate edition : team or enterprise customization
|
||||
openzaly.edition=TEAM
|
||||
|
||||
windchat.edition=TEAM
|
||||
|
||||
|
||||
# openzaly version
|
||||
openzaly.version=1.1.7
|
||||
windchat.version=1.1.7
|
||||
|
||||
##### site tcp address,clients connect to site.address
|
||||
site.address=0.0.0.0
|
||||
windchat.address=0.0.0.0
|
||||
# site tcp port default 2021
|
||||
site.port=2021
|
||||
windchat.port=2021
|
||||
|
||||
# pluginapi server address
|
||||
pluginapi.address=0.0.0.0
|
||||
windchat.api.address=0.0.0.0
|
||||
# pluginapi server port
|
||||
pluginapi.port=8280
|
||||
windchat.api.port=8280
|
||||
|
||||
# default back-stage management address
|
||||
site.admin.address=127.0.0.1
|
||||
windchat.admin.address=127.0.0.1
|
||||
# default back-stage management port
|
||||
site.admin.port=8288
|
||||
windchat.admin.port=8288
|
||||
|
||||
# first user login site,use this user invite code(UIC)
|
||||
site.uic=000000
|
||||
windchat.uic=000000
|
||||
|
||||
# deposit pictures,audio,db default current path
|
||||
# site.baseDir=${pwd}
|
||||
# windchat.baseDir=${pwd}
|
||||
|
||||
## if openzaly.edition = personal/PERSONAL OR migrate sqlite to mysql
|
||||
##openzaly.sqlite.url=openzalyDB.sqlite3
|
||||
## if windchat.edition = personal/PERSONAL OR migrate sqlite to mysql
|
||||
##windchat.sqlite.url=windchatDB.sqlite3
|
||||
|
||||
|
||||
## if openzaly.edition = team/TEAM
|
||||
## if windchat.edition = team/TEAM
|
||||
##***************mysql master**************
|
||||
##openzaly.mysql.host=localhost
|
||||
##openzaly.mysql.port=3306
|
||||
##openzaly.mysql.database=openzaly
|
||||
##windchat.mysql.host=localhost
|
||||
##windchat.mysql.port=3306
|
||||
##windchat.mysql.database=windchat
|
||||
|
||||
##openzaly.mysql.username=root
|
||||
##openzaly.mysql.password=1234567890
|
||||
##windchat.mysql.username=root
|
||||
##windchat.mysql.password=1234567890
|
||||
|
||||
openzaly.mysql.initial-size=10
|
||||
openzaly.mysql.max-size=100
|
||||
openzaly.mysql.max-idle=60
|
||||
windchat.mysql.initial-size=10
|
||||
windchat.mysql.max-size=100
|
||||
windchat.mysql.max-idle=60
|
||||
|
||||
openzaly.mysql.useUnicode=true
|
||||
openzaly.mysql.characterEncoding=utf-8
|
||||
openzaly.mysql.verifyServerCertificate=false
|
||||
openzaly.mysql.useSSL=true
|
||||
windchat.mysql.useUnicode=true
|
||||
windchat.mysql.characterEncoding=utf-8
|
||||
windchat.mysql.verifyServerCertificate=false
|
||||
windchat.mysql.useSSL=true
|
||||
|
||||
##***************mysql slave**************
|
||||
##openzaly.mysql.slave.host=localhost
|
||||
##openzaly.mysql.slave.port=3306
|
||||
##openzaly.mysql.slave.database=openzaly
|
||||
##windchat.mysql.slave.host=localhost
|
||||
##windchat.mysql.slave.port=3306
|
||||
##windchat.mysql.slave.database=windchat
|
||||
|
||||
##openzaly.mysql.slave.username=root
|
||||
##openzaly.mysql.slave.password=1234567890
|
||||
##openzaly.mysql.slave.initial-size=10
|
||||
##openzaly.mysql.slave.max-size=100
|
||||
##openzaly.mysql.slave.max-idle=60
|
||||
##windchat.mysql.slave.username=root
|
||||
##windchat.mysql.slave.password=1234567890
|
||||
##windchat.mysql.slave.initial-size=10
|
||||
##windchat.mysql.slave.max-size=100
|
||||
##windchat.mysql.slave.max-idle=60
|
||||
|
||||
##openzaly.mysql.slave.useUnicode=true
|
||||
##openzaly.mysql.slave.characterEncoding=utf-8
|
||||
##openzaly.mysql.slave.verifyServerCertificate=false
|
||||
##openzaly.mysql.slave.useSSL=true
|
||||
##windchat.mysql.slave.useUnicode=true
|
||||
##windchat.mysql.slave.characterEncoding=utf-8
|
||||
##windchat.mysql.slave.verifyServerCertificate=false
|
||||
##windchat.mysql.slave.useSSL=true
|
||||
|
||||
|
||||
## if openzaly.edition = enterprise/ENTERPRISE
|
||||
## if windchat.edition = enterprise/ENTERPRISE
|
||||
#### support support im server distribution
|
||||
|
||||
## if openzaly.edition = ultimate edition
|
||||
#### openzaly support customed for team or enterprise
|
||||
## if windchat.edition = ultimate edition
|
||||
#### windchat support customed for team or enterprise
|
||||
|
||||
|
||||
|
|
|
@ -69,7 +69,7 @@ public class LogUtils extends LogCreater {
|
|||
public static void dbDebugLog(Logger logger, long startTime, Object result, String sql, Object... objects) {
|
||||
String messagePattern = sql.replace("?", "{}");
|
||||
FormattingTuple format = MessageFormatter.arrayFormat(messagePattern, objects);
|
||||
logger.debug("[openzaly-db] cost:{}ms result:{} sql:{}", System.currentTimeMillis() - startTime, result,
|
||||
logger.debug("[windchat-db] cost:{}ms result:{} sql:{}", System.currentTimeMillis() - startTime, result,
|
||||
format.getMessage());
|
||||
}
|
||||
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
/**
|
||||
/**
|
||||
* Copyright 2018-2028 Akaxin Group
|
||||
*
|
||||
* <p>
|
||||
* 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
|
||||
*
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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.
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.windchat.im.storage;
|
||||
|
||||
|
@ -43,101 +43,101 @@ import com.windchat.im.storage.util.MigrateUtils;
|
|||
|
||||
/**
|
||||
* 数据源初始化管理,不做具体操作对外提供方法
|
||||
*
|
||||
*
|
||||
* @author Sam{@link an.guoyue254@gmail.com}
|
||||
* @since 2018-01-31 12:15:15
|
||||
*/
|
||||
public class DataSourceManager {
|
||||
private static final Logger logger = LoggerFactory.getLogger(DataSourceManager.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(DataSourceManager.class);
|
||||
|
||||
private static final String OPENZALY_DATABASE_CONFIG = "openzaly-server.config";
|
||||
private static final String WINDCHAT_DATABASE_CONFIG = "windchat-server.config";
|
||||
|
||||
private DataSourceManager() {
|
||||
}
|
||||
private DataSourceManager() {
|
||||
}
|
||||
|
||||
// server启动,初始化数据库
|
||||
public static void init(DBConfig config)
|
||||
throws InitDatabaseException, UpgradeDatabaseException, NeedInitMysqlException {
|
||||
try {
|
||||
DBType dbType = DBType.PERSONAL;
|
||||
Properties pro = loadDatabaseConfig(OPENZALY_DATABASE_CONFIG);
|
||||
if (pro != null && pro.size() > 0) {
|
||||
// get edition from config file
|
||||
String edition = MysqlManager.trimToNull(pro, JdbcConst.OPENZALY_EDITION);
|
||||
if (StringUtils.isNotEmpty(edition)) {
|
||||
dbType = DBType.getDBType(edition);
|
||||
}
|
||||
}
|
||||
config.setDb(dbType);
|
||||
logger.info("load database config finish databaseType:{}", dbType);
|
||||
// server启动,初始化数据库
|
||||
public static void init(DBConfig config)
|
||||
throws InitDatabaseException, UpgradeDatabaseException, NeedInitMysqlException {
|
||||
try {
|
||||
DBType dbType = DBType.PERSONAL;
|
||||
Properties pro = loadDatabaseConfig(WINDCHAT_DATABASE_CONFIG);
|
||||
if (pro != null && pro.size() > 0) {
|
||||
// get edition from config file
|
||||
String edition = MysqlManager.trimToNull(pro, JdbcConst.WINDCHAT_EDITION);
|
||||
if (StringUtils.isNotEmpty(edition)) {
|
||||
dbType = DBType.getDBType(edition);
|
||||
}
|
||||
}
|
||||
config.setDb(dbType);
|
||||
logger.info("load database config finish databaseType:{}", dbType);
|
||||
|
||||
switch (dbType) {
|
||||
case PERSONAL:
|
||||
System.setProperty("database", dbType.getName());
|
||||
SQLiteJDBCManager.initSqliteDB(config);
|
||||
PrepareSiteConfigData.init(config);// 初始化数据库中初始数据
|
||||
break;
|
||||
case TEAM:
|
||||
System.setProperty("database", dbType.getName());
|
||||
MysqlManager.initMysqlDB(pro); // 初始化数据库以及数据库连接
|
||||
PrepareSiteConfigData.init(config);// 初始化数据库中初始数据
|
||||
break;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new InitDatabaseException("init database error", e);
|
||||
}
|
||||
}
|
||||
switch (dbType) {
|
||||
case PERSONAL:
|
||||
System.setProperty("database", dbType.getName());
|
||||
SQLiteJDBCManager.initSqliteDB(config);
|
||||
PrepareSiteConfigData.init(config);// 初始化数据库中初始数据
|
||||
break;
|
||||
case TEAM:
|
||||
System.setProperty("database", dbType.getName());
|
||||
MysqlManager.initMysqlDB(pro); // 初始化数据库以及数据库连接
|
||||
PrepareSiteConfigData.init(config);// 初始化数据库中初始数据
|
||||
break;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new InitDatabaseException("init database error", e);
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化,生成mysql的配置文件
|
||||
public static void initMysqlConfig() throws FileNotFoundException, IOException {
|
||||
// 生成配置文件
|
||||
File configFile = new File(OPENZALY_DATABASE_CONFIG);
|
||||
if (!configFile.exists()) {
|
||||
FileUtils.writeResourceToFile("/" + OPENZALY_DATABASE_CONFIG, configFile);
|
||||
}
|
||||
}
|
||||
// 初始化,生成mysql的配置文件
|
||||
public static void initMysqlConfig() throws FileNotFoundException, IOException {
|
||||
// 生成配置文件
|
||||
File configFile = new File(WINDCHAT_DATABASE_CONFIG);
|
||||
if (!configFile.exists()) {
|
||||
FileUtils.writeResourceToFile("/" + WINDCHAT_DATABASE_CONFIG, configFile);
|
||||
}
|
||||
}
|
||||
|
||||
// 手动升级数据库
|
||||
public static int upgradeDB(DBConfig config) throws UpgradeDatabaseException {
|
||||
try {
|
||||
switch (config.getDb()) {
|
||||
case PERSONAL:
|
||||
return SQLiteUpgrade.doUpgrade(config, true);
|
||||
case TEAM:
|
||||
throw new UpgradeDatabaseException("database upgrade can't support mysql");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new UpgradeDatabaseException("upgrade database error", e);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// 手动升级数据库
|
||||
public static int upgradeDB(DBConfig config) throws UpgradeDatabaseException {
|
||||
try {
|
||||
switch (config.getDb()) {
|
||||
case PERSONAL:
|
||||
return SQLiteUpgrade.doUpgrade(config, true);
|
||||
case TEAM:
|
||||
throw new UpgradeDatabaseException("database upgrade can't support mysql");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new UpgradeDatabaseException("upgrade database error", e);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 迁移数据库,把sqlite迁移至mysql数据库
|
||||
public static void migrateDB() throws MigrateDatabaseException, NeedInitMysqlException {
|
||||
// 加载配置文件中的数据库配置
|
||||
Properties prop = loadDatabaseConfig(OPENZALY_DATABASE_CONFIG);
|
||||
MigrateUtils.sqlite2Mysql(prop);
|
||||
}
|
||||
// 迁移数据库,把sqlite迁移至mysql数据库
|
||||
public static void migrateDB() throws MigrateDatabaseException, NeedInitMysqlException {
|
||||
// 加载配置文件中的数据库配置
|
||||
Properties prop = loadDatabaseConfig(WINDCHAT_DATABASE_CONFIG);
|
||||
MigrateUtils.sqlite2Mysql(prop);
|
||||
}
|
||||
|
||||
public static Properties loadDatabaseConfig(String configPath) {
|
||||
Properties properties = null;
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
properties = new Properties();
|
||||
inputStream = new FileInputStream(configPath);
|
||||
properties.load(inputStream);
|
||||
} catch (Exception e) {
|
||||
logger.error("load database config fail,openzaly will use sqlite database,cause={}", e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error("close db config inputstream error", e);
|
||||
}
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
public static Properties loadDatabaseConfig(String configPath) {
|
||||
Properties properties = null;
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
properties = new Properties();
|
||||
inputStream = new FileInputStream(configPath);
|
||||
properties.load(inputStream);
|
||||
} catch (Exception e) {
|
||||
logger.error("load database config fail,WindChat will use sqlite database,cause={}", e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error("close db config inputstream error", e);
|
||||
}
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,39 +8,39 @@ package com.windchat.im.storage.dao.config;
|
|||
*/
|
||||
public interface JdbcConst {
|
||||
|
||||
String OPENZALY_EDITION = "openzaly.edition";
|
||||
String WINDCHAT_EDITION = "windchat.edition";
|
||||
|
||||
String SQLITE_URL = "openzaly.sqlite.url";
|
||||
String SQLITE_URL = "windchat.sqlite.url";
|
||||
|
||||
// ----------------------MASTER--------------------
|
||||
String MYSQL_HOST = "openzaly.mysql.host";
|
||||
String MYSQL_PORT = "openzaly.mysql.port";
|
||||
String MYSQL_DB = "openzaly.mysql.database";
|
||||
String MYSQL_HOST = "windchat.mysql.host";
|
||||
String MYSQL_PORT = "windchat.mysql.port";
|
||||
String MYSQL_DB = "windchat.mysql.database";
|
||||
|
||||
String MYSQL_USER_NAME = "openzaly.mysql.username";
|
||||
String MYSQL_PASSWORD = "openzaly.mysql.password";
|
||||
String MYSQL_INITIAL_SIZE = "openzaly.mysql.initial-size";
|
||||
String MYSQL_MAX_SIZE = "openzaly.mysql.max-size";
|
||||
String MYSQL_MAX_IDLE = "openzaly.mysql.max-idle";
|
||||
String MYSQL_USER_NAME = "windchat.mysql.username";
|
||||
String MYSQL_PASSWORD = "windchat.mysql.password";
|
||||
String MYSQL_INITIAL_SIZE = "windchat.mysql.initial-size";
|
||||
String MYSQL_MAX_SIZE = "windchat.mysql.max-size";
|
||||
String MYSQL_MAX_IDLE = "windchat.mysql.max-idle";
|
||||
|
||||
String MYSQL_USE_UNICODE = "openzaly.mysql.useUnicode";
|
||||
String MYSQL_CHARACTER_ENCODING = "openzaly.mysql.characterEncoding";
|
||||
String MYSQL_VERIFY_SERVER_CERTIFICATE = "openzaly.mysql.verifyServerCertificate";
|
||||
String MYSQL_USE_SSL = "openzaly.mysql.useSSL";
|
||||
String MYSQL_USE_UNICODE = "windchat.mysql.useUnicode";
|
||||
String MYSQL_CHARACTER_ENCODING = "windchat.mysql.characterEncoding";
|
||||
String MYSQL_VERIFY_SERVER_CERTIFICATE = "windchat.mysql.verifyServerCertificate";
|
||||
String MYSQL_USE_SSL = "windchat.mysql.useSSL";
|
||||
|
||||
// ----------------------SLAVE--------------------
|
||||
String MYSQL_SLAVE_HOST = "openzaly.mysql.slave.host";
|
||||
String MYSQL_SLAVE_PORT = "openzaly.mysql.slave.port";
|
||||
String MYSQL_SLAVE_DB = "openzaly.mysql.slave.database";
|
||||
String MYSQL_SLAVE_HOST = "windchat.mysql.slave.host";
|
||||
String MYSQL_SLAVE_PORT = "windchat.mysql.slave.port";
|
||||
String MYSQL_SLAVE_DB = "windchat.mysql.slave.database";
|
||||
|
||||
String MYSQL_SLAVE_USER_NAME = "openzaly.mysql.slave.username";
|
||||
String MYSQL_SLAVE_PASSWORD = "openzaly.mysql.slave.password";
|
||||
String MYSQL_SLAVE_INITIAL_SIZE = "openzaly.mysql.slave.initial-size";
|
||||
String MYSQL_SLAVE_MAX_SIZE = "openzaly.mysql.slave.max-size";
|
||||
String MYSQL_SLAVE_MAX_IDLE = "openzaly.mysql.slave.max-idle";
|
||||
String MYSQL_SLAVE_USER_NAME = "windchat.mysql.slave.username";
|
||||
String MYSQL_SLAVE_PASSWORD = "windchat.mysql.slave.password";
|
||||
String MYSQL_SLAVE_INITIAL_SIZE = "windchat.mysql.slave.initial-size";
|
||||
String MYSQL_SLAVE_MAX_SIZE = "windchat.mysql.slave.max-size";
|
||||
String MYSQL_SLAVE_MAX_IDLE = "windchat.mysql.slave.max-idle";
|
||||
|
||||
String MYSQL_SLAVE_USE_UNICODE = "openzaly.mysql.slave.useUnicode";
|
||||
String MYSQL_SLAVE_CHARACTER_ENCODING = "openzaly.mysql.slave.characterEncoding";
|
||||
String MYSQL_SLAVE_VERIFY_SERVER_CERTIFICATE = "openzaly.mysql.slave.verifyServerCertificate";
|
||||
String MYSQL_SLAVE_USE_SSL = "openzaly.mysql.slave.useSSL";
|
||||
String MYSQL_SLAVE_USE_UNICODE = "windchat.mysql.slave.useUnicode";
|
||||
String MYSQL_SLAVE_CHARACTER_ENCODING = "windchat.mysql.slave.characterEncoding";
|
||||
String MYSQL_SLAVE_VERIFY_SERVER_CERTIFICATE = "windchat.mysql.slave.verifyServerCertificate";
|
||||
String MYSQL_SLAVE_USE_SSL = "windchat.mysql.slave.useSSL";
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ public class C3P0PoolManager extends AbstractPoolManager {
|
|||
int maxIdle = Integer.valueOf(trimToNull(pro, JdbcConst.MYSQL_MAX_IDLE, "60"));
|
||||
cpds.setMaxIdleTime(maxIdle);// 最大空闲时间
|
||||
|
||||
SqlLog.info("openzaly init mysql master connection pool cpds={}", cpds);
|
||||
SqlLog.info("windchat init mysql master connection pool cpds={}", cpds);
|
||||
}
|
||||
|
||||
public static java.sql.Connection getConnection() throws SQLException {
|
||||
|
|
|
@ -54,7 +54,7 @@ public class C3P0PoolSlaveManager extends AbstractPoolManager {
|
|||
|
||||
cpdsList.add(cpds);
|
||||
|
||||
SqlLog.info("openzaly init mysql slave connection pool cpds={}", cpds.toString());
|
||||
SqlLog.info("windchat init mysql slave connection pool cpds={}", cpds.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,96 +18,97 @@ import com.windchat.im.storage.util.SqlLog;
|
|||
|
||||
/**
|
||||
* 启动mysql的情形下,需要获取一个连接建数据库,建表操作,只有在初始化数据库情况下才使用
|
||||
*
|
||||
*
|
||||
* @author Sam{@link an.guoyue254@gmail.com}
|
||||
* @since 2018-06-07 16:18:11
|
||||
*/
|
||||
public class InitDatabaseConnection extends AbstractPoolManager {
|
||||
|
||||
private static final String OPENZALY_MYSQL_SQL = "openzaly-mysql.sql";
|
||||
private static Connection conn;
|
||||
private static final String WINDCHAT_MYSQL_SQL = "windchat-mysql.sql";
|
||||
private static Connection conn;
|
||||
|
||||
public static void init(Properties prop) throws Exception {
|
||||
try {
|
||||
initAndGetConnection(prop);
|
||||
} finally {
|
||||
closeInitConnection();
|
||||
}
|
||||
}
|
||||
public static void init(Properties prop) throws Exception {
|
||||
try {
|
||||
initAndGetConnection(prop);
|
||||
} finally {
|
||||
closeInitConnection();
|
||||
}
|
||||
}
|
||||
|
||||
public static Connection initAndGetConnection(Properties prop) throws Exception {
|
||||
Connection conn = initConnection(prop);
|
||||
String dbName = trimToNull(prop, JdbcConst.MYSQL_DB);
|
||||
createAndUseDatabase(conn, dbName);
|
||||
initDatabaseTable(conn);
|
||||
return conn;
|
||||
}
|
||||
public static Connection initAndGetConnection(Properties prop) throws Exception {
|
||||
Connection conn = initConnection(prop);
|
||||
String dbName = trimToNull(prop, JdbcConst.MYSQL_DB);
|
||||
createAndUseDatabase(conn, dbName);
|
||||
initDatabaseTable(conn);
|
||||
return conn;
|
||||
}
|
||||
|
||||
private static Connection initConnection(Properties pro) throws Exception {
|
||||
// "com.mysql.cj.jdbc.Driver"
|
||||
String jdbcUrl = getJdbcUrlWithoutDBName(pro);
|
||||
String userName = trimToNull(pro, JdbcConst.MYSQL_USER_NAME);
|
||||
String password = trimToNull(pro, JdbcConst.MYSQL_PASSWORD);
|
||||
Class.forName(MYSQL_JDBC_DRIVER);
|
||||
conn = DriverManager.getConnection(jdbcUrl, userName, password);
|
||||
conn.prepareStatement("SET NAMES utf8mb4;").executeUpdate();
|
||||
return conn;
|
||||
}
|
||||
private static Connection initConnection(Properties pro) throws Exception {
|
||||
// "com.mysql.cj.jdbc.Driver"
|
||||
String jdbcUrl = getJdbcUrlWithoutDBName(pro);
|
||||
String userName = trimToNull(pro, JdbcConst.MYSQL_USER_NAME);
|
||||
String password = trimToNull(pro, JdbcConst.MYSQL_PASSWORD);
|
||||
Class.forName(MYSQL_JDBC_DRIVER);
|
||||
conn = DriverManager.getConnection(jdbcUrl, userName, password);
|
||||
conn.prepareStatement("SET NAMES utf8mb4;").executeUpdate();
|
||||
return conn;
|
||||
}
|
||||
|
||||
// 获取单独一个连接,迁移数据库使用
|
||||
public static Connection getConnection(Properties pro) throws Exception {
|
||||
// "com.mysql.cj.jdbc.Driver"
|
||||
String jdbcUrl = getJdbcUrl(pro);
|
||||
String userName = trimToNull(pro, JdbcConst.MYSQL_USER_NAME);
|
||||
String password = trimToNull(pro, JdbcConst.MYSQL_PASSWORD);
|
||||
Class.forName(MYSQL_JDBC_DRIVER);
|
||||
conn = DriverManager.getConnection(jdbcUrl, userName, password);
|
||||
conn.prepareStatement("SET NAMES utf8mb4;").executeUpdate();
|
||||
return conn;
|
||||
}
|
||||
// 获取单独一个连接,迁移数据库使用
|
||||
public static Connection getConnection(Properties pro) throws Exception {
|
||||
// "com.mysql.cj.jdbc.Driver"
|
||||
String jdbcUrl = getJdbcUrl(pro);
|
||||
String userName = trimToNull(pro, JdbcConst.MYSQL_USER_NAME);
|
||||
String password = trimToNull(pro, JdbcConst.MYSQL_PASSWORD);
|
||||
Class.forName(MYSQL_JDBC_DRIVER);
|
||||
conn = DriverManager.getConnection(jdbcUrl, userName, password);
|
||||
conn.prepareStatement("SET NAMES utf8mb4;").executeUpdate();
|
||||
return conn;
|
||||
}
|
||||
|
||||
public static void closeInitConnection() {
|
||||
try {
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (Exception e) {
|
||||
SqlLog.error("close mysql init connection error", e);
|
||||
}
|
||||
}
|
||||
public static void closeInitConnection() {
|
||||
try {
|
||||
if (conn != null) {
|
||||
conn.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
SqlLog.error("close mysql init connection error", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void createAndUseDatabase(Connection conn, String dbName) throws SQLException {
|
||||
if (StringUtils.isEmpty(dbName)) {
|
||||
throw new SQLException("openzaly team need designated database name,but now it's " + dbName);
|
||||
}
|
||||
String createSql = "CREATE DATABASE IF NOT EXISTS " + dbName + " CHARACTER SET utf8mb4;";
|
||||
String useSql = "use " + dbName;
|
||||
conn.prepareStatement(createSql).executeUpdate();
|
||||
conn.prepareStatement(useSql).executeUpdate();
|
||||
}
|
||||
private static void createAndUseDatabase(Connection conn, String dbName) throws SQLException {
|
||||
if (StringUtils.isEmpty(dbName)) {
|
||||
throw new SQLException("windchat team need designated database name,but now it's " + dbName);
|
||||
}
|
||||
String createSql = "CREATE DATABASE IF NOT EXISTS " + dbName + " CHARACTER SET utf8mb4;";
|
||||
String useSql = "use " + dbName;
|
||||
conn.prepareStatement(createSql).executeUpdate();
|
||||
conn.prepareStatement(useSql).executeUpdate();
|
||||
}
|
||||
|
||||
private static void initDatabaseTable(Connection conn) throws SQLException {
|
||||
try {
|
||||
// 生成临时sql文件加载数据库sql执行脚本,
|
||||
File sqlFile = new File(OPENZALY_MYSQL_SQL);
|
||||
if (!sqlFile.exists()) {
|
||||
FileUtils.writeResourceToFile("/" + OPENZALY_MYSQL_SQL, sqlFile);
|
||||
}
|
||||
private static void initDatabaseTable(Connection conn) throws SQLException {
|
||||
try {
|
||||
// 生成临时sql文件加载数据库sql执行脚本,
|
||||
File sqlFile = new File(WINDCHAT_MYSQL_SQL);
|
||||
if (!sqlFile.exists()) {
|
||||
FileUtils.writeResourceToFile("/" + WINDCHAT_MYSQL_SQL, sqlFile);
|
||||
}
|
||||
|
||||
// 初始化数据库表
|
||||
File file = new File(OPENZALY_MYSQL_SQL);
|
||||
if (!file.exists()) {
|
||||
throw new FileNotFoundException("init mysql with sql script file is not exists");
|
||||
}
|
||||
// 初始化数据库表
|
||||
File file = new File(WINDCHAT_MYSQL_SQL);
|
||||
if (!file.exists()) {
|
||||
throw new FileNotFoundException("init mysql with sql script file is not exists");
|
||||
}
|
||||
|
||||
FileSystemResource rc = new FileSystemResource(file);
|
||||
EncodedResource encodeRes = new EncodedResource(rc, "GBK");
|
||||
ScriptUtils.executeSqlScript(conn, encodeRes);
|
||||
SqlLog.info("openzaly init mysql database with sql-script finish");
|
||||
FileSystemResource rc = new FileSystemResource(file);
|
||||
EncodedResource encodeRes = new EncodedResource(rc, "GBK");
|
||||
ScriptUtils.executeSqlScript(conn, encodeRes);
|
||||
SqlLog.info("windchat init mysql database with sql-script finish");
|
||||
|
||||
file.delete();
|
||||
} catch (Exception e) {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
}
|
||||
file.delete();
|
||||
} catch (Exception e) {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@ public class SQLiteJDBCManager {
|
|||
private static int SITE_DB_VERSION = SQLConst.SITE_DB_VERSION_10;
|
||||
private static String sqliteDriverName = "org.sqlite.JDBC";
|
||||
private static Connection sqlitConnection = null;
|
||||
private static final String DB_FILE_PATH = "openzalyDB.sqlite3";
|
||||
private static final String DB_FILE_PATH = "windchatDB.sqlite3";
|
||||
|
||||
private SQLiteJDBCManager() {
|
||||
|
||||
|
@ -61,7 +61,7 @@ public class SQLiteJDBCManager {
|
|||
loadDatabaseDriver(config.getDbDir());
|
||||
|
||||
if (getDbVersion() < SITE_DB_VERSION) {
|
||||
throw new UpgradeDatabaseException("openzaly-server need to upgrade before run it");
|
||||
throw new UpgradeDatabaseException("windchat-server need to upgrade before run it");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,297 +29,297 @@ import com.windchat.im.storage.util.SqlLog;
|
|||
|
||||
/**
|
||||
* sqlite 数据库升级
|
||||
*
|
||||
*
|
||||
* @author Sam{@link an.guoyue254@gmail.com}
|
||||
* @since 2018-06-28 11:47:07
|
||||
*/
|
||||
public class SQLiteUpgrade {
|
||||
private static final Logger logger = LoggerFactory.getLogger(SQLiteUpgrade.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(SQLiteUpgrade.class);
|
||||
|
||||
private static final String DB_FILE_PATH = "openzalyDB.sqlite3";
|
||||
private static final String OPENZALY_SQLITE_SQL = "openzaly-sqlite.sql";
|
||||
private static final String DB_FILE_PATH = "windchatDB.sqlite3";
|
||||
private static final String WINDCHAT_SQLITE_SQL = "windchat-sqlite.sql";
|
||||
|
||||
private SQLiteUpgrade() {
|
||||
}
|
||||
private SQLiteUpgrade() {
|
||||
}
|
||||
|
||||
public static int doUpgrade(DBConfig config) throws SQLException, UpgradeDatabaseException {
|
||||
return upgradeSqliteDB(config, false);
|
||||
}
|
||||
public static int doUpgrade(DBConfig config) throws SQLException, UpgradeDatabaseException {
|
||||
return upgradeSqliteDB(config, false);
|
||||
}
|
||||
|
||||
public static int doUpgrade(DBConfig config, boolean clear) throws SQLException, UpgradeDatabaseException {
|
||||
return upgradeSqliteDB(config, clear);
|
||||
}
|
||||
public static int doUpgrade(DBConfig config, boolean clear) throws SQLException, UpgradeDatabaseException {
|
||||
return upgradeSqliteDB(config, clear);
|
||||
}
|
||||
|
||||
// upgrade db,return current db user-version
|
||||
private static int upgradeSqliteDB(DBConfig config, boolean clear) throws SQLException, UpgradeDatabaseException {
|
||||
// 数据库文件
|
||||
File file = new File(config.getDbDir(), DB_FILE_PATH);
|
||||
// upgrade db,return current db user-version
|
||||
private static int upgradeSqliteDB(DBConfig config, boolean clear) throws SQLException, UpgradeDatabaseException {
|
||||
// 数据库文件
|
||||
File file = new File(config.getDbDir(), DB_FILE_PATH);
|
||||
|
||||
if (!file.exists()) {
|
||||
SqlLog.info("openzaly start with first init sqlite database");
|
||||
SQLiteJDBCManager.loadDatabaseDriver(config.getDbDir());
|
||||
doInitWork(SQLiteJDBCManager.getConnection());
|
||||
SQLiteJDBCManager.setDbVersion(SQLConst.SITE_DB_VERSION_11);
|
||||
} else {
|
||||
SQLiteJDBCManager.loadDatabaseDriver(config.getDbDir());
|
||||
if (clear) {
|
||||
SQLiteUnique.clearUnique(SQLiteJDBCManager.getConnection());
|
||||
}
|
||||
doUpgradeWork(config);
|
||||
}
|
||||
if (!file.exists()) {
|
||||
SqlLog.info("windchat start with first init sqlite database");
|
||||
SQLiteJDBCManager.loadDatabaseDriver(config.getDbDir());
|
||||
doInitWork(SQLiteJDBCManager.getConnection());
|
||||
SQLiteJDBCManager.setDbVersion(SQLConst.SITE_DB_VERSION_11);
|
||||
} else {
|
||||
SQLiteJDBCManager.loadDatabaseDriver(config.getDbDir());
|
||||
if (clear) {
|
||||
SQLiteUnique.clearUnique(SQLiteJDBCManager.getConnection());
|
||||
}
|
||||
doUpgradeWork(config);
|
||||
}
|
||||
|
||||
return SQLiteJDBCManager.getDbVersion();
|
||||
}
|
||||
return SQLiteJDBCManager.getDbVersion();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过sql脚本初始化数据库表
|
||||
*
|
||||
* @param conn
|
||||
* @throws SQLException
|
||||
*/
|
||||
private static void doInitWork(Connection conn) throws SQLException {
|
||||
try {
|
||||
// 生成临时sql文件加载数据库sql执行脚本,
|
||||
File sqlFile = new File(OPENZALY_SQLITE_SQL);
|
||||
if (!sqlFile.exists()) {
|
||||
FileUtils.writeResourceToFile("/" + OPENZALY_SQLITE_SQL, sqlFile);
|
||||
}
|
||||
/**
|
||||
* 通过sql脚本初始化数据库表
|
||||
*
|
||||
* @param conn
|
||||
* @throws SQLException
|
||||
*/
|
||||
private static void doInitWork(Connection conn) throws SQLException {
|
||||
try {
|
||||
// 生成临时sql文件加载数据库sql执行脚本,
|
||||
File sqlFile = new File(WINDCHAT_SQLITE_SQL);
|
||||
if (!sqlFile.exists()) {
|
||||
FileUtils.writeResourceToFile("/" + WINDCHAT_SQLITE_SQL, sqlFile);
|
||||
}
|
||||
|
||||
// 初始化数据库表
|
||||
File file = new File(OPENZALY_SQLITE_SQL);
|
||||
if (!file.exists()) {
|
||||
throw new FileNotFoundException("init mysql with sql script file is not exists");
|
||||
}
|
||||
// 初始化数据库表
|
||||
File file = new File(WINDCHAT_SQLITE_SQL);
|
||||
if (!file.exists()) {
|
||||
throw new FileNotFoundException("init mysql with sql script file is not exists");
|
||||
}
|
||||
|
||||
FileSystemResource rc = new FileSystemResource(file);
|
||||
EncodedResource encodeRes = new EncodedResource(rc, "GBK");
|
||||
ScriptUtils.executeSqlScript(conn, encodeRes);
|
||||
SqlLog.info("openzaly init sqlite with sql-script finish");
|
||||
FileSystemResource rc = new FileSystemResource(file);
|
||||
EncodedResource encodeRes = new EncodedResource(rc, "GBK");
|
||||
ScriptUtils.executeSqlScript(conn, encodeRes);
|
||||
SqlLog.info("windchat init sqlite with sql-script finish");
|
||||
|
||||
file.delete();
|
||||
} catch (Exception e) {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
}
|
||||
file.delete();
|
||||
} catch (Exception e) {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void doUpgradeWork(DBConfig config) throws UpgradeDatabaseException, SQLException {
|
||||
int times = 5;
|
||||
while (true) {
|
||||
// 做一个简单的频率控制,防止升级出现死循环
|
||||
if (times-- <= 0) {
|
||||
break;
|
||||
}
|
||||
private static void doUpgradeWork(DBConfig config) throws UpgradeDatabaseException, SQLException {
|
||||
int times = 5;
|
||||
while (true) {
|
||||
// 做一个简单的频率控制,防止升级出现死循环
|
||||
if (times-- <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
int dbVersion = SQLiteJDBCManager.getDbVersion();
|
||||
if (dbVersion < 9) {
|
||||
// 1.首先备份
|
||||
String fileName = backupDatabaseFile(config.getDbDir(), dbVersion);
|
||||
if (StringUtils.isEmpty(fileName)) {
|
||||
throw new UpgradeDatabaseException("backup database file before upgrade error");
|
||||
}
|
||||
// 2.升级
|
||||
if (upgrade0_9(dbVersion)) {
|
||||
SQLiteJDBCManager.setDbVersion(SQLConst.SITE_DB_VERSION_9);
|
||||
} else {
|
||||
logger.error("upgrade user-version {} -> {} error.", dbVersion, SQLConst.SITE_DB_VERSION_9);
|
||||
// db rename to original db file
|
||||
restoreDatabase(fileName);
|
||||
}
|
||||
continue;
|
||||
} else if (dbVersion == 9) {
|
||||
// 0.9.5 upgrade from 9 to 10
|
||||
// 1.首先备份
|
||||
String fileName = backupDatabaseFile(config.getDbDir(), dbVersion);
|
||||
if (StringUtils.isEmpty(fileName)) {
|
||||
throw new UpgradeDatabaseException("backup database file before upgrade error");
|
||||
}
|
||||
if (StringUtils.isEmpty(fileName)) {
|
||||
throw new UpgradeDatabaseException("backup database file before upgrade error");
|
||||
}
|
||||
// 2.升级
|
||||
if (upgrade9_10(dbVersion)) {
|
||||
SQLiteJDBCManager.setDbVersion(SQLConst.SITE_DB_VERSION_10);
|
||||
} else {
|
||||
logger.error("upgrade user-version {} -> {} error.", dbVersion, SQLConst.SITE_DB_VERSION_10);
|
||||
// db rename to original db file
|
||||
restoreDatabase(fileName);
|
||||
}
|
||||
int dbVersion = SQLiteJDBCManager.getDbVersion();
|
||||
if (dbVersion < 9) {
|
||||
// 1.首先备份
|
||||
String fileName = backupDatabaseFile(config.getDbDir(), dbVersion);
|
||||
if (StringUtils.isEmpty(fileName)) {
|
||||
throw new UpgradeDatabaseException("backup database file before upgrade error");
|
||||
}
|
||||
// 2.升级
|
||||
if (upgrade0_9(dbVersion)) {
|
||||
SQLiteJDBCManager.setDbVersion(SQLConst.SITE_DB_VERSION_9);
|
||||
} else {
|
||||
logger.error("upgrade user-version {} -> {} error.", dbVersion, SQLConst.SITE_DB_VERSION_9);
|
||||
// db rename to original db file
|
||||
restoreDatabase(fileName);
|
||||
}
|
||||
continue;
|
||||
} else if (dbVersion == 9) {
|
||||
// 0.9.5 upgrade from 9 to 10
|
||||
// 1.首先备份
|
||||
String fileName = backupDatabaseFile(config.getDbDir(), dbVersion);
|
||||
if (StringUtils.isEmpty(fileName)) {
|
||||
throw new UpgradeDatabaseException("backup database file before upgrade error");
|
||||
}
|
||||
if (StringUtils.isEmpty(fileName)) {
|
||||
throw new UpgradeDatabaseException("backup database file before upgrade error");
|
||||
}
|
||||
// 2.升级
|
||||
if (upgrade9_10(dbVersion)) {
|
||||
SQLiteJDBCManager.setDbVersion(SQLConst.SITE_DB_VERSION_10);
|
||||
} else {
|
||||
logger.error("upgrade user-version {} -> {} error.", dbVersion, SQLConst.SITE_DB_VERSION_10);
|
||||
// db rename to original db file
|
||||
restoreDatabase(fileName);
|
||||
}
|
||||
|
||||
} else if (dbVersion == 10) {
|
||||
doInitWork(SQLiteJDBCManager.getConnection());
|
||||
SQLiteJDBCManager.setDbVersion(SQLConst.SITE_DB_VERSION_11);
|
||||
}
|
||||
} else if (dbVersion == 10) {
|
||||
doInitWork(SQLiteJDBCManager.getConnection());
|
||||
SQLiteJDBCManager.setDbVersion(SQLConst.SITE_DB_VERSION_11);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static String backupDatabaseFile(String dbDir, int oldVersion) {
|
||||
String filePath = "./";
|
||||
try {
|
||||
String dbFileName = SQLiteJDBCManager.getDbFileName();
|
||||
if (StringUtils.isNotEmpty(dbDir)) {
|
||||
filePath = dbDir;
|
||||
}
|
||||
File file = new File(filePath, dbFileName);// original db file
|
||||
String bkFileName = dbFileName + ".backup." + oldVersion + "." + System.currentTimeMillis();
|
||||
File backupFile = new File(filePath, bkFileName);
|
||||
if (!backupFile.exists()) {
|
||||
new FileOutputStream(backupFile).close();
|
||||
}
|
||||
FileOutputStream fos = new FileOutputStream(backupFile);
|
||||
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
||||
try {
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesLen = 0;
|
||||
while ((bytesLen = bis.read(buffer)) != -1) {
|
||||
fos.write(buffer, 0, bytesLen);
|
||||
}
|
||||
} finally {
|
||||
if (bis != null) {
|
||||
bis.close();
|
||||
}
|
||||
if (fos != null) {
|
||||
fos.close();
|
||||
}
|
||||
}
|
||||
private static String backupDatabaseFile(String dbDir, int oldVersion) {
|
||||
String filePath = "./";
|
||||
try {
|
||||
String dbFileName = SQLiteJDBCManager.getDbFileName();
|
||||
if (StringUtils.isNotEmpty(dbDir)) {
|
||||
filePath = dbDir;
|
||||
}
|
||||
File file = new File(filePath, dbFileName);// original db file
|
||||
String bkFileName = dbFileName + ".backup." + oldVersion + "." + System.currentTimeMillis();
|
||||
File backupFile = new File(filePath, bkFileName);
|
||||
if (!backupFile.exists()) {
|
||||
new FileOutputStream(backupFile).close();
|
||||
}
|
||||
FileOutputStream fos = new FileOutputStream(backupFile);
|
||||
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
||||
try {
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesLen = 0;
|
||||
while ((bytesLen = bis.read(buffer)) != -1) {
|
||||
fos.write(buffer, 0, bytesLen);
|
||||
}
|
||||
} finally {
|
||||
if (bis != null) {
|
||||
bis.close();
|
||||
}
|
||||
if (fos != null) {
|
||||
fos.close();
|
||||
}
|
||||
}
|
||||
|
||||
return backupFile.getAbsolutePath();
|
||||
} catch (FileNotFoundException e) {
|
||||
logger.error("backup db file error,fileUrl=" + filePath, e);
|
||||
} catch (IOException e) {
|
||||
logger.error("backup db file IOException.fileUrl=" + filePath, e);
|
||||
}
|
||||
return backupFile.getAbsolutePath();
|
||||
} catch (FileNotFoundException e) {
|
||||
logger.error("backup db file error,fileUrl=" + filePath, e);
|
||||
} catch (IOException e) {
|
||||
logger.error("backup db file IOException.fileUrl=" + filePath, e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean upgrade0_9(int oldVersion) {
|
||||
String sql1 = "alter table " + SQLConst.SITE_USER_PROFILE + " RENAME TO "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_PROFILE);
|
||||
String sql2 = "alter table " + SQLConst.SITE_USER_FRIEND + " RENAME TO "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_FRIEND);
|
||||
String sql3 = "alter table " + SQLConst.SITE_USER_DEVICE + " RENAME TO "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_DEVICE);
|
||||
private static boolean upgrade0_9(int oldVersion) {
|
||||
String sql1 = "alter table " + SQLConst.SITE_USER_PROFILE + " RENAME TO "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_PROFILE);
|
||||
String sql2 = "alter table " + SQLConst.SITE_USER_FRIEND + " RENAME TO "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_FRIEND);
|
||||
String sql3 = "alter table " + SQLConst.SITE_USER_DEVICE + " RENAME TO "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_DEVICE);
|
||||
|
||||
boolean result = false;
|
||||
List<String> upgradeSqls = Arrays.asList(sql1, sql2, sql3);
|
||||
try {
|
||||
Connection conn = SQLiteJDBCManager.getConnection();
|
||||
conn.setAutoCommit(false);
|
||||
try {
|
||||
// 1.rename
|
||||
for (String sql : upgradeSqls) {
|
||||
PreparedStatement pst = conn.prepareStatement(sql);
|
||||
int res = pst.executeUpdate();
|
||||
logger.info("rename database table result={} sql:{}", res, sql);
|
||||
if (pst != null) {
|
||||
pst.close();
|
||||
}
|
||||
}
|
||||
// 2.check all tables
|
||||
doInitWork(conn);
|
||||
boolean result = false;
|
||||
List<String> upgradeSqls = Arrays.asList(sql1, sql2, sql3);
|
||||
try {
|
||||
Connection conn = SQLiteJDBCManager.getConnection();
|
||||
conn.setAutoCommit(false);
|
||||
try {
|
||||
// 1.rename
|
||||
for (String sql : upgradeSqls) {
|
||||
PreparedStatement pst = conn.prepareStatement(sql);
|
||||
int res = pst.executeUpdate();
|
||||
logger.info("rename database table result={} sql:{}", res, sql);
|
||||
if (pst != null) {
|
||||
pst.close();
|
||||
}
|
||||
}
|
||||
// 2.check all tables
|
||||
doInitWork(conn);
|
||||
|
||||
// 3. migrate
|
||||
String migSql1 = "INSERT INTO " + SQLConst.SITE_USER_PROFILE
|
||||
+ "(id,site_user_id,global_user_id,user_id_pubk,user_name,user_photo,phone_id,user_status,mute,register_time) select id,site_user_id,global_user_id,user_id_pubk,user_name,user_photo,phone_id,user_status,mute,register_time from "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_PROFILE);
|
||||
// 3. migrate
|
||||
String migSql1 = "INSERT INTO " + SQLConst.SITE_USER_PROFILE
|
||||
+ "(id,site_user_id,global_user_id,user_id_pubk,user_name,user_photo,phone_id,user_status,mute,register_time) select id,site_user_id,global_user_id,user_id_pubk,user_name,user_photo,phone_id,user_status,mute,register_time from "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_PROFILE);
|
||||
|
||||
String migSql2 = "INSERT INTO " + SQLConst.SITE_USER_FRIEND
|
||||
+ "(id,site_user_id,site_friend_id,relation,mute,add_time) select id,site_user_id,site_friend_id,relation,mute,add_time from "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_FRIEND);
|
||||
String migSql2 = "INSERT INTO " + SQLConst.SITE_USER_FRIEND
|
||||
+ "(id,site_user_id,site_friend_id,relation,mute,add_time) select id,site_user_id,site_friend_id,relation,mute,add_time from "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_FRIEND);
|
||||
|
||||
String migSql3 = "INSERT INTO " + SQLConst.SITE_USER_DEVICE
|
||||
+ "(id,site_user_id,device_id,user_device_pubk,user_token,device_name,active_time,add_time) SELECT id,site_user_id,device_id,user_device_pubk,user_token,device_name,active_time,add_time FROM "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_DEVICE);
|
||||
Map<Integer, String> sqlMap = new HashMap<Integer, String>() {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1438260552099272302L;
|
||||
String migSql3 = "INSERT INTO " + SQLConst.SITE_USER_DEVICE
|
||||
+ "(id,site_user_id,device_id,user_device_pubk,user_token,device_name,active_time,add_time) SELECT id,site_user_id,device_id,user_device_pubk,user_token,device_name,active_time,add_time FROM "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_DEVICE);
|
||||
Map<Integer, String> sqlMap = new HashMap<Integer, String>() {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1438260552099272302L;
|
||||
|
||||
{
|
||||
put(1, migSql1);
|
||||
put(2, migSql2);
|
||||
put(3, migSql3);
|
||||
}
|
||||
};
|
||||
for (int sqlIndex : sqlMap.keySet()) {
|
||||
String sql_mig = sqlMap.get(sqlIndex);
|
||||
PreparedStatement pst = conn.prepareStatement(sql_mig);
|
||||
int res = pst.executeUpdate();
|
||||
logger.info("migrate database table result={} sql:{}", res, sql_mig);
|
||||
if (pst != null) {
|
||||
pst.close();
|
||||
}
|
||||
}
|
||||
result = true;// 兼容失败情况,这里不能使用成功个数
|
||||
} catch (Exception e) {
|
||||
logger.error("upgrade to execute sql error", e);
|
||||
}
|
||||
conn.commit();
|
||||
conn.setAutoCommit(true);
|
||||
} catch (SQLException e) {
|
||||
logger.error("rename database table to upgrade error.", e);
|
||||
}
|
||||
{
|
||||
put(1, migSql1);
|
||||
put(2, migSql2);
|
||||
put(3, migSql3);
|
||||
}
|
||||
};
|
||||
for (int sqlIndex : sqlMap.keySet()) {
|
||||
String sql_mig = sqlMap.get(sqlIndex);
|
||||
PreparedStatement pst = conn.prepareStatement(sql_mig);
|
||||
int res = pst.executeUpdate();
|
||||
logger.info("migrate database table result={} sql:{}", res, sql_mig);
|
||||
if (pst != null) {
|
||||
pst.close();
|
||||
}
|
||||
}
|
||||
result = true;// 兼容失败情况,这里不能使用成功个数
|
||||
} catch (Exception e) {
|
||||
logger.error("upgrade to execute sql error", e);
|
||||
}
|
||||
conn.commit();
|
||||
conn.setAutoCommit(true);
|
||||
} catch (SQLException e) {
|
||||
logger.error("rename database table to upgrade error.", e);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static boolean upgrade9_10(int oldVersion) {
|
||||
String sql = "ALTER TABLE " + SQLConst.SITE_USER_MESSAGE + " RENAME TO "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_MESSAGE);
|
||||
private static boolean upgrade9_10(int oldVersion) {
|
||||
String sql = "ALTER TABLE " + SQLConst.SITE_USER_MESSAGE + " RENAME TO "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_MESSAGE);
|
||||
|
||||
boolean result = false;
|
||||
try {
|
||||
Connection conn = SQLiteJDBCManager.getConnection();
|
||||
// 1.rename
|
||||
PreparedStatement pst = conn.prepareStatement(sql);
|
||||
int res = pst.executeUpdate();
|
||||
logger.info("rename database table result={} sql:{}", res, sql);
|
||||
boolean result = false;
|
||||
try {
|
||||
Connection conn = SQLiteJDBCManager.getConnection();
|
||||
// 1.rename
|
||||
PreparedStatement pst = conn.prepareStatement(sql);
|
||||
int res = pst.executeUpdate();
|
||||
logger.info("rename database table result={} sql:{}", res, sql);
|
||||
|
||||
if (pst != null) {
|
||||
pst.close();
|
||||
}
|
||||
if (pst != null) {
|
||||
pst.close();
|
||||
}
|
||||
|
||||
// 2.check all tables
|
||||
doInitWork(conn);
|
||||
// 2.check all tables
|
||||
doInitWork(conn);
|
||||
|
||||
// 3. migrate
|
||||
String sql_mig = "INSERT INTO " + SQLConst.SITE_USER_MESSAGE
|
||||
+ "(id,site_user_id,msg_id,send_user_id,receive_user_id,msg_type,content,device_id,ts_key,msg_time) "
|
||||
+ "SELECT id,site_user_id,msg_id,send_user_id,site_user_id,msg_type,content,device_id,ts_key,msg_time FROM "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_MESSAGE);
|
||||
// 3. migrate
|
||||
String sql_mig = "INSERT INTO " + SQLConst.SITE_USER_MESSAGE
|
||||
+ "(id,site_user_id,msg_id,send_user_id,receive_user_id,msg_type,content,device_id,ts_key,msg_time) "
|
||||
+ "SELECT id,site_user_id,msg_id,send_user_id,site_user_id,msg_type,content,device_id,ts_key,msg_time FROM "
|
||||
+ getTempTable(oldVersion, SQLConst.SITE_USER_MESSAGE);
|
||||
|
||||
PreparedStatement pst2 = conn.prepareStatement(sql_mig);
|
||||
int res2 = pst2.executeUpdate();
|
||||
logger.info("migrate database table result={} sql:{}", res2, sql_mig);
|
||||
if (pst2 != null) {
|
||||
pst2.close();
|
||||
}
|
||||
result = true;// 兼容失败情况,这里不能使用成功个数
|
||||
} catch (SQLException e) {
|
||||
logger.error("execute database table to upgrade error.", e);
|
||||
}
|
||||
PreparedStatement pst2 = conn.prepareStatement(sql_mig);
|
||||
int res2 = pst2.executeUpdate();
|
||||
logger.info("migrate database table result={} sql:{}", res2, sql_mig);
|
||||
if (pst2 != null) {
|
||||
pst2.close();
|
||||
}
|
||||
result = true;// 兼容失败情况,这里不能使用成功个数
|
||||
} catch (SQLException e) {
|
||||
logger.error("execute database table to upgrade error.", e);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String getTempTable(int oldVersion, String tableName) {
|
||||
return "temp_" + oldVersion + "_" + tableName;
|
||||
}
|
||||
private static String getTempTable(int oldVersion, String tableName) {
|
||||
return "temp_" + oldVersion + "_" + tableName;
|
||||
}
|
||||
|
||||
private static void restoreDatabase(String filePath) throws UpgradeDatabaseException {
|
||||
File backupFile = new File(filePath);
|
||||
if (!backupFile.exists()) {
|
||||
throw new UpgradeDatabaseException("backup file cannot find");
|
||||
}
|
||||
String dbPath = backupFile.getParent();
|
||||
File dbFile = new File(dbPath, SQLiteJDBCManager.getDbFileName());
|
||||
if (dbFile.exists()) {
|
||||
dbFile.delete();
|
||||
}
|
||||
backupFile.renameTo(dbFile);
|
||||
}
|
||||
private static void restoreDatabase(String filePath) throws UpgradeDatabaseException {
|
||||
File backupFile = new File(filePath);
|
||||
if (!backupFile.exists()) {
|
||||
throw new UpgradeDatabaseException("backup file cannot find");
|
||||
}
|
||||
String dbPath = backupFile.getParent();
|
||||
File dbFile = new File(dbPath, SQLiteJDBCManager.getDbFileName());
|
||||
if (dbFile.exists()) {
|
||||
dbFile.delete();
|
||||
}
|
||||
backupFile.renameTo(dbFile);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue