feat 数据库支持 mariadb

This commit is contained in:
bwcx_jzy 2024-04-01 19:03:50 +08:00
parent 8f3479d01c
commit 20c00acf5a
No known key found for this signature in database
GPG Key ID: 5E48E9372088B9E5
23 changed files with 486 additions and 6 deletions

View File

@ -2,6 +2,9 @@
## 2.11.3.1 ## 2.11.3.1
### 🐣 新增功能
1. 【server】新增 数据库支持 *mariadb*
### 🐞 解决BUG、优化功能 ### 🐞 解决BUG、优化功能

View File

@ -19,6 +19,7 @@
15. 隧道节点 15. 隧道节点
16. docker-compose sh 16. docker-compose sh
17. 监控通知模块优化支持更多(飞书) zx 17. 监控通知模块优化支持更多(飞书) zx
18. 数据库支持 mariadb
## 2.10.x ## 2.10.x

View File

@ -128,6 +128,12 @@
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.dromara.jpom.storage-module</groupId>
<artifactId>storage-module-mariadb</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>me.zhyd.oauth</groupId> <groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId> <artifactId>JustAuth</artifactId>

View File

@ -61,6 +61,8 @@ public class JpomServerApplication {
* --h2-migrate-mysql --h2-user=jpom --h2-pass=jpom h2 数据库迁移到 mysql * --h2-migrate-mysql --h2-user=jpom --h2-pass=jpom h2 数据库迁移到 mysql
* <p> * <p>
* --h2-migrate-postgresql --h2-user=jpom --h2-pass=jpom h2 数据库迁移到 postgresql * --h2-migrate-postgresql --h2-user=jpom --h2-pass=jpom h2 数据库迁移到 postgresql
* <p>
* --h2-migrate-mariadb --h2-user=jpom --h2-pass=jpom h2 数据库迁移到 mariadb
* *
* @param args 参数 * @param args 参数
* @throws Exception 异常 * @throws Exception 异常

View File

@ -306,6 +306,9 @@ public class InitDb implements DisposableBean, ILoadEvent {
Opt.ofNullable(environment.getProperty("h2-migrate-postgresql")).ifPresent(s -> { Opt.ofNullable(environment.getProperty("h2-migrate-postgresql")).ifPresent(s -> {
migrateOpr.accept(DbExtConfig.Mode.POSTGRESQL); migrateOpr.accept(DbExtConfig.Mode.POSTGRESQL);
}); });
Opt.ofNullable(environment.getProperty("h2-migrate-mariadb")).ifPresent(s -> {
migrateOpr.accept(DbExtConfig.Mode.MARIADB);
});
} }
private void importH2Sql(Environment environment, String importH2Sql) { private void importH2Sql(Environment environment, String importH2Sql) {

View File

@ -0,0 +1,21 @@
#
# Copyright (c) 2019 Of Him Code Technology Studio
# Jpom is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
# http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
#
jpom:
db:
# 数据库默认 支持 H2、MYSQL、MARIADB、POSTGRESQL
mode: MARIADB
url: jdbc:mysql://127.0.0.1:3309/jpom
# 数据库账号 默认 jpom
#user-name: jpom
user-name: root
# 数据库密码 默认 jpom 如果自行配置请保证密码强度
# user-pwd: jpom
user-pwd: jpom123456

View File

@ -10,7 +10,7 @@
jpom: jpom:
db: db:
# 数据库默认 支持 H2、MYSQL # 数据库默认 支持 H2、MYSQL、MARIADB、POSTGRESQL
mode: MYSQL mode: MYSQL
url: jdbc:mysql://127.0.0.1:3306/jpom url: jdbc:mysql://127.0.0.1:3306/jpom
# 数据库账号 默认 jpom # 数据库账号 默认 jpom

View File

@ -10,7 +10,7 @@
jpom: jpom:
db: db:
# 数据库默认 支持 H2、MYSQL # 数据库默认 支持 H2、MYSQL、MARIADB、POSTGRESQL
mode: POSTGRESQL mode: POSTGRESQL
url: jdbc:postgresql://localhost:5432/jpom url: jdbc:postgresql://localhost:5432/jpom
# 数据库账号 默认 jpom # 数据库账号 默认 jpom

View File

@ -72,7 +72,7 @@ jpom:
# 查看日志时初始读取最后多少行默认100不读取 # 查看日志时初始读取最后多少行默认100不读取
init-read-line: 10 init-read-line: 10
db: db:
# 数据库默认 支持 H2、MYSQL # 数据库默认 支持 H2、MYSQL、MARIADB、POSTGRESQL
mode: H2 mode: H2
# 日志存储条数,将自动清理旧数据,配置小于等于零则不清理 # 日志存储条数,将自动清理旧数据,配置小于等于零则不清理
log-storage-count: 10000 log-storage-count: 10000
@ -153,7 +153,9 @@ server:
spring: spring:
profiles: profiles:
active: mysql-1 # active: mysql-1
active: mariadb
web: web:
resources: resources:
static-locations: classpath:/dist/ static-locations: classpath:/dist/

View File

@ -72,7 +72,7 @@ jpom:
# 查看日志时初始读取最后多少行默认100不读取 # 查看日志时初始读取最后多少行默认100不读取
init-read-line: 10 init-read-line: 10
db: db:
# 数据库默认 支持 H2、MYSQL # 数据库默认 支持 H2、MYSQL、MARIADB、POSTGRESQL
mode: H2 mode: H2
# 日志存储条数,将自动清理旧数据,配置小于等于零则不清理 # 日志存储条数,将自动清理旧数据,配置小于等于零则不清理
log-storage-count: 10000 log-storage-count: 10000

View File

@ -25,6 +25,7 @@
<module>storage-module-h2</module> <module>storage-module-h2</module>
<module>storage-module-mysql</module> <module>storage-module-mysql</module>
<module>storage-module-postgresql</module> <module>storage-module-postgresql</module>
<module>storage-module-mariadb</module>
</modules> </modules>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<version>2.11.3.0</version> <version>2.11.3.0</version>

View File

@ -147,6 +147,10 @@ public class DbExtConfig implements InitializingBean {
/** /**
* postgresql * postgresql
*/ */
POSTGRESQL POSTGRESQL,
/**
* Mariadb
*/
MARIADB
} }
} }

View File

@ -86,6 +86,7 @@ public class DialectUtil {
case H2: case H2:
return getH2Dialect(); return getH2Dialect();
case MYSQL: case MYSQL:
case MARIADB:
return getMySqlDialect(); return getMySqlDialect();
case POSTGRESQL: case POSTGRESQL:
return getPostgresqlDialect(); return getPostgresqlDialect();

View File

@ -0,0 +1,3 @@
```shell
docker run -d --name jpom-mariadb -e MYSQL_ROOT_PASSWORD=jpom123456 -p 3309:3306 mariadb
```

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.dromara.jpom.storage-module</groupId>
<artifactId>jpom-storage-module-parent</artifactId>
<version>2.11.3.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>storage-module-mariadb</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.dromara.jpom.storage-module</groupId>
<artifactId>storage-module-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
<dependency>
<groupId>org.dromara.jpom</groupId>
<artifactId>common</artifactId>
<scope>provided</scope>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,95 @@
/*
* Copyright (c) 2019 Of Him Code Technology Studio
* Jpom is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.jpom.storage;
import cn.hutool.core.lang.Opt;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.setting.Setting;
import lombok.extern.slf4j.Slf4j;
import org.dromara.jpom.db.DbExtConfig;
import org.dromara.jpom.db.IStorageService;
import org.dromara.jpom.system.JpomRuntimeException;
import org.springframework.util.Assert;
/**
* @author bwcx_jzy
* @since 2024/4/1
*/
@Slf4j
public class MariadbStorageServiceImpl implements IStorageService {
private String dbUrl;
private DSFactory dsFactory;
@Override
public String dbUrl() {
Assert.hasText(this.dbUrl, "还没有初始化数据库");
return dbUrl;
}
@Override
public int getFetchSize() {
return Integer.MIN_VALUE;
}
@Override
public DbExtConfig.Mode mode() {
return DbExtConfig.Mode.MARIADB;
}
@Override
public DSFactory init(DbExtConfig dbExtConfig) {
Assert.isNull(this.dsFactory, "不要重复初始化数据库");
Assert.hasText(dbExtConfig.getUrl(), "没有配置数据库连接");
Setting setting = dbExtConfig.toSetting();
this.dsFactory = DSFactory.create(setting);
this.dbUrl = dbExtConfig.getUrl();
return this.dsFactory;
}
@Override
public DSFactory create(DbExtConfig dbExtConfig, String url, String user, String pass) {
Setting setting = this.createSetting(dbExtConfig, url, user, pass);
return DSFactory.create(setting);
}
@Override
public Setting createSetting(DbExtConfig dbExtConfig, String url, String user, String pass) {
String url2 = Opt.ofBlankAble(url).orElse(dbExtConfig.getUrl());
String user2 = Opt.ofBlankAble(user).orElse(dbExtConfig.getUserName());
String pass2 = Opt.ofBlankAble(pass).orElse(dbExtConfig.getUserPwd());
Setting setting = dbExtConfig.toSetting();
setting.set("user", user2);
setting.set("pass", pass2);
setting.set("url", url2);
return setting;
}
public DSFactory getDsFactory() {
Assert.notNull(this.dsFactory, "还没有初始化数据库");
return dsFactory;
}
@Override
public JpomRuntimeException warpException(Exception e) {
return new JpomRuntimeException("数据库异常", e);
}
@Override
public void close() throws Exception {
log.info("mysql db destroy");
if (this.dsFactory != null) {
dsFactory.destroy();
this.dsFactory = null;
}
}
}

View File

@ -0,0 +1,200 @@
/*
* Copyright (c) 2019 Of Him Code Technology Studio
* Jpom is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.jpom.storage;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.dromara.jpom.db.*;
import org.springframework.util.Assert;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author bwcx_jzy
* @since 2024/4/1
*/
public class MariadbTableBuilderImpl implements IStorageSqlBuilderService {
@Override
public DbExtConfig.Mode mode() {
return DbExtConfig.Mode.MARIADB;
}
@Override
public String generateIndexSql(List<TableViewIndexData> row) {
StringBuilder stringBuilder = new StringBuilder();
for (TableViewIndexData viewIndexData : row) {
String indexType = viewIndexData.getIndexType();
switch (indexType) {
case "ADD-UNIQUE": {
// ALTER TABLE `jpom`.`PROJECT_INFO`
//DROP INDEX `workspaceId`,
//ADD UNIQUE INDEX `workspaceId`(`workspaceId` ASC, `strike` ASC, `modifyUser`) USING BTREE;
String field = viewIndexData.getField();
List<String> fields = StrUtil.splitTrim(field, "+");
Assert.notEmpty(fields, "索引未配置字段");
stringBuilder.append("call drop_index_if_exists('").append(viewIndexData.getTableName()).append("','").append(viewIndexData.getName()).append("')").append(";").append(StrUtil.LF);
stringBuilder.append(this.delimiter()).append(StrUtil.LF);
stringBuilder.append("ALTER TABLE ").append(viewIndexData.getTableName()).append(" ADD UNIQUE INDEX ").append(viewIndexData.getName()).append(" (").append(CollUtil.join(fields, StrUtil.COMMA)).append(")");
break;
}
case "ADD": {
// ALTER TABLE `jpom`.`PROJECT_INFO`
//DROP INDEX `workspaceId`,
//ADD UNIQUE INDEX `workspaceId`(`workspaceId` ASC, `strike` ASC, `modifyUser`) USING BTREE;
String field = viewIndexData.getField();
List<String> fields = StrUtil.splitTrim(field, "+");
Assert.notEmpty(fields, "索引未配置字段");
stringBuilder.append("call drop_index_if_exists('").append(viewIndexData.getTableName()).append("','").append(viewIndexData.getName()).append("')").append(";").append(StrUtil.LF);
stringBuilder.append(this.delimiter()).append(StrUtil.LF);
stringBuilder.append("ALTER TABLE ").append(viewIndexData.getTableName()).append(" ADD INDEX ").append(viewIndexData.getName()).append(" (").append(CollUtil.join(fields, StrUtil.COMMA)).append(")");
break;
}
default:
throw new IllegalArgumentException("不支持的类型:" + indexType);
}
stringBuilder.append(";").append(StrUtil.LF);
stringBuilder.append(this.delimiter()).append(StrUtil.LF);
}
return stringBuilder.toString();
}
@Override
public String generateAlterTableSql(List<TableViewAlterData> row) {
StringBuilder stringBuilder = new StringBuilder();
for (TableViewAlterData viewAlterData : row) {
String alterType = viewAlterData.getAlterType();
switch (alterType) {
case "DROP":
// ALTER TABLE NODE_INFO DROP COLUMN IF EXISTS `cycle`;
stringBuilder.append("CALL drop_column_if_exists('").append(viewAlterData.getTableName()).append("', '").append(viewAlterData.getName()).append("')");
break;
case "ADD":
// ALTER TABLE PROJECT_INFO ADD IF NOT EXISTS triggerToken VARCHAR (100) comment '触发器token';
String columnSql = this.generateColumnSql(viewAlterData, true);
stringBuilder.append("CALL add_column_if_not_exists('").append(viewAlterData.getTableName()).append("','").append(viewAlterData.getName()).append("','").append(columnSql).append("')");
break;
case "ALTER":
// alter table table1 modify column column1 decimal(10,1) DEFAULT NULL COMMENT '注释';
stringBuilder.append("ALTER TABLE ").append(viewAlterData.getTableName()).append(" modify column ");
stringBuilder.append(this.generateColumnSql(viewAlterData));
break;
case "DROP-TABLE":
stringBuilder.append("drop table if exists ").append(viewAlterData.getTableName());
break;
default:
throw new IllegalArgumentException("不支持的类型:" + alterType);
}
stringBuilder.append(";").append(StrUtil.LF);
stringBuilder.append(this.delimiter()).append(StrUtil.LF);
}
return stringBuilder.toString();
}
/**
* CREATE TABLE IF NOT EXISTS USEROPERATELOGV1
* (
* id VARCHAR(50) not null comment 'id',
* reqId VARCHAR(50) COMMENT '请求ID',
* CONSTRAINT USEROPERATELOGV1_PK PRIMARY KEY (id)
* );
* COMMENT ON TABLE USEROPERATELOGV1 is '操作日志';
*
* @param name 表名
* @param desc 描述
* @param row 字段信息
* @return sql
*/
@Override
public String generateTableSql(String name, String desc, List<TableViewData> row) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("CREATE TABLE IF NOT EXISTS ").append(name).append(StrUtil.LF);
stringBuilder.append("(").append(StrUtil.LF);
for (TableViewData tableViewData : row) {
stringBuilder.append(StrUtil.TAB).append(this.generateColumnSql(tableViewData)).append(StrUtil.COMMA).append(StrUtil.LF);
}
// 主键
List<String> primaryKeys = row.stream()
.filter(tableViewData -> tableViewData.getPrimaryKey() != null && tableViewData.getPrimaryKey())
.map(TableViewRowData::getName)
.collect(Collectors.toList());
Assert.notEmpty(primaryKeys, "表没有主键");
stringBuilder.append(StrUtil.TAB).append("PRIMARY KEY (").append(CollUtil.join(primaryKeys, StrUtil.COMMA)).append(")").append(StrUtil.LF);
stringBuilder.append(") ").append("COMMENT=").append("'").append(desc).append("';");
return stringBuilder.toString();
}
@Override
public String generateColumnSql(TableViewRowData tableViewRowData) {
return generateColumnSql(tableViewRowData, false);
}
private String generateColumnSql(TableViewRowData tableViewRowData, boolean encode) {
// id VARCHAR(50) not null default '' comment 'id'
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("`").append(tableViewRowData.getName()).append("`").append(StrUtil.SPACE);
String type = tableViewRowData.getType();
Assert.hasText(type, "未正确配置数据类型");
type = type.toUpperCase();
switch (type) {
case "LONG":
stringBuilder.append("BIGINT").append(StrUtil.SPACE);
break;
case "STRING":
stringBuilder.append("VARCHAR(").append(ObjectUtil.defaultIfNull(tableViewRowData.getLen(), 255)).append(")").append(StrUtil.SPACE);
break;
case "TEXT":
stringBuilder.append("TEXT").append(StrUtil.SPACE);
break;
case "INTEGER":
stringBuilder.append("int").append(StrUtil.SPACE);
break;
case "TINYINT":
stringBuilder.append("TINYINT").append(StrUtil.SPACE);
break;
case "FLOAT":
stringBuilder.append("float").append(StrUtil.SPACE);
break;
case "DOUBLE":
stringBuilder.append("double").append(StrUtil.SPACE);
break;
default:
throw new IllegalArgumentException("不支持的数据类型:" + type);
}
//
Boolean notNull = tableViewRowData.getNotNull();
if (notNull != null && notNull) {
stringBuilder.append("not null").append(StrUtil.SPACE);
}
//
String defaultValue = tableViewRowData.getDefaultValue();
if (StrUtil.isNotEmpty(defaultValue)) {
stringBuilder.append("default '").append(defaultValue).append("'").append(StrUtil.SPACE);
}
stringBuilder.append("comment '").append(tableViewRowData.getComment()).append("'");
//
String columnSql = stringBuilder.toString();
if (encode) {
columnSql = StrUtil.replace(columnSql, "'", "\\'");
}
int length = StrUtil.length(columnSql);
Assert.state(length <= 180, "sql 语句太长啦");
return columnSql;
}
@Override
public String delimiter() {
return "-- mariadb delimiter";
}
}

View File

@ -0,0 +1 @@
org.dromara.jpom.storage.MariadbStorageServiceImpl

View File

@ -0,0 +1 @@
org.dromara.jpom.storage.MariadbTableBuilderImpl

View File

@ -0,0 +1,88 @@
--
-- Copyright (c) 2019 Of Him Code Technology Studio
-- Jpom is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at:
-- http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.
--
DROP FUNCTION IF EXISTS column_exists;
-- mariadb delimiter
CREATE FUNCTION column_exists1(
tname VARCHAR(64),
cname VARCHAR(64)
)
RETURNS BOOLEAN
READS SQL DATA
BEGIN
RETURN 0 < (SELECT COUNT(*)
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA` = SCHEMA()
AND `TABLE_NAME` = tname
AND `COLUMN_NAME` = cname);
END
-- mariadb delimiter
DROP PROCEDURE IF EXISTS drop_column_if_exists;
-- mariadb delimiter
CREATE PROCEDURE drop_column_if_exists(
tname VARCHAR(64),
cname VARCHAR(64)
)
BEGIN
IF column_exists1(tname, cname)
THEN
SET @drop_column_if_exists = CONCAT('ALTER TABLE `', tname, '` DROP COLUMN `', cname, '`');
PREPARE drop_query FROM @drop_column_if_exists;
EXECUTE drop_query;
END IF;
END
-- mariadb delimiter
DROP PROCEDURE IF EXISTS add_column_if_not_exists;
-- mariadb delimiter
CREATE PROCEDURE add_column_if_not_exists(
tname VARCHAR(64),
cname VARCHAR(64),
columninfo VARCHAR(200)
)
BEGIN
IF column_exists1(tname, cname)
THEN
SET @add_column_sql = '';
else
SET @add_column_sql = CONCAT('ALTER TABLE `', tname, '` ADD COLUMN ', columninfo);
PREPARE execute_query FROM @add_column_sql;
EXECUTE execute_query;
END IF;
END
-- mariadb delimiter
DROP PROCEDURE IF EXISTS drop_index_if_exists;
-- mariadb delimiter
create procedure drop_index_if_exists(
p_tablename varchar(200),
p_idxname VARCHAR(200)
)
begin
select count(*) into @cnt from information_schema.statistics where `TABLE_SCHEMA` = SCHEMA() and table_name = p_tablename and index_name = p_idxname;
if @cnt > 0 then
set @str = concat('drop index ', p_idxname, ' on ', p_tablename);
PREPARE execute_query FROM @str;
EXECUTE execute_query;
end if;
end;

View File

@ -0,0 +1,7 @@
```shell
docker run --name jpom-postgres \
-e POSTGRES_USER=jpom \
-e POSTGRES_PASSWORD=jpom123456 \
-e POSTGRES_DB=jpom \
-p 5432:5432 -d postgres
```