Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
I
ibzlite
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
ibiz4jteam
ibzlite
提交
6ebeeb65
提交
6ebeeb65
编写于
12月 07, 2020
作者:
Tyl666
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
【Liquibase逆向工程】第三版 精简代码
上级
e3d98dcf
变更
5
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
165 行增加
和
50 行删除
+165
-50
DevBootApplication.java
...ite-boot/src/main/java/cn/ibizlab/DevBootApplication.java
+1
-1
DstDataSourceExService.java
...izlab/core/extensions/service/DstDataSourceExService.java
+4
-6
IDstLiquibaseService.java
...va/cn/ibizlab/core/lite/service/IDstLiquibaseService.java
+15
-0
DstLiquibaseServiceImpl.java
...izlab/core/lite/service/impl/DstLiquibaseServiceImpl.java
+47
-0
GenerateLiquibaseChangeLog.java
.../ibizlab/core/util/config/GenerateLiquibaseChangeLog.java
+98
-43
未找到文件。
ibzlite-boot/src/main/java/cn/ibizlab/DevBootApplication.java
浏览文件 @
6ebeeb65
...
...
@@ -25,7 +25,7 @@ import org.springframework.beans.factory.annotation.Autowired;
@SpringBootApplication
(
exclude
=
{
com
.
alibaba
.
druid
.
spring
.
boot
.
autoconfigure
.
DruidDataSourceAutoConfigure
.
class
})
@ComponentScan
(
basePackages
=
{
"cn.ibizlab"
,
"liquibase.integration.spring"
}
@ComponentScan
(
basePackages
=
{
"cn.ibizlab"
}
// ,excludeFilters = {
// @ComponentScan.Filter(type= org.springframework.context.annotation.FilterType.REGEX, pattern="cn.ibizlab.xxx.rest.xxx"),
// }
...
...
ibzlite-core/src/main/java/cn/ibizlab/core/extensions/service/DstDataSourceExService.java
浏览文件 @
6ebeeb65
...
...
@@ -3,18 +3,16 @@ package cn.ibizlab.core.extensions.service;
import
cn.ibizlab.core.lite.extensions.domain.Setting
;
import
cn.ibizlab.core.lite.extensions.domain.SysModel
;
import
cn.ibizlab.core.lite.extensions.service.LiteCoreService
;
import
cn.ibizlab.core.lite.service.IDst
DataSourc
eService
;
import
cn.ibizlab.core.lite.service.IDst
Liquibas
eService
;
import
cn.ibizlab.core.lite.service.impl.DstDataSourceServiceImpl
;
import
cn.ibizlab.util.domain.LiquibaseProp
;
import
cn.ibizlab.util.errors.BadRequestAlertException
;
import
com.alibaba.fastjson.JSONArray
;
import
com.baomidou.dynamic.datasource.DynamicRoutingDataSource
;
import
com.baomidou.dynamic.datasource.creator.BasicDataSourceCreator
;
import
com.baomidou.dynamic.datasource.creator.DataSourceCreator
;
import
com.baomidou.dynamic.datasource.creator.DruidDataSourceCreator
;
import
com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty
;
import
com.baomidou.mybatisplus.core.toolkit.Wrappers
;
import
liquibase.integration.sprin
g.GenerateLiquibaseChangeLog
;
import
cn.ibizlab.core.util.confi
g.GenerateLiquibaseChangeLog
;
import
lombok.extern.slf4j.Slf4j
;
import
cn.ibizlab.core.lite.domain.DstDataSource
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -47,7 +45,7 @@ public class DstDataSourceExService extends DstDataSourceServiceImpl {
public
LiteCoreService
liteCoreService
;
@Resource
public
GenerateLiquibaseChangeLog
generateLiquibaseChangeLog
;
public
IDstLiquibaseService
dstLiquibaseService
;
/**
* [BuildDS:buildDS] 行为扩展
...
...
@@ -171,7 +169,7 @@ public class DstDataSourceExService extends DstDataSourceServiceImpl {
lp
.
setUrl
(
datasourceMap
.
get
(
"url"
)
==
null
?
""
:
datasourceMap
.
get
(
"url"
).
toString
());
lp
.
setDefaultSchema
(
datasourceMap
.
get
(
"defaultSchema"
)
==
null
?
""
:
datasourceMap
.
get
(
"defaultSchema"
).
toString
());
// 调用liquibase逆向工程的逻辑,生成一个xml,并返回一个SysModel对象
SysModel
sysModel
=
generateLiquibaseChangeLog
.
liquibaseGenerateChangeLog
(
lp
,
ds
.
getDsId
());
SysModel
sysModel
=
dstLiquibaseService
.
generateLiquibase
(
lp
,
ds
.
getDsId
());
liteCoreService
.
syncSysModel
(
sysModel
);
return
et
;
...
...
ibzlite-core/src/main/java/cn/ibizlab/core/lite/service/IDstLiquibaseService.java
0 → 100644
浏览文件 @
6ebeeb65
package
cn
.
ibizlab
.
core
.
lite
.
service
;
import
cn.ibizlab.core.lite.extensions.domain.SysModel
;
import
cn.ibizlab.util.domain.LiquibaseProp
;
public
interface
IDstLiquibaseService
{
SysModel
generateLiquibase
(
LiquibaseProp
liquibaseProp
,
String
name
);
}
ibzlite-core/src/main/java/cn/ibizlab/core/lite/service/impl/DstLiquibaseServiceImpl.java
0 → 100644
浏览文件 @
6ebeeb65
package
cn
.
ibizlab
.
core
.
lite
.
service
.
impl
;
import
cn.ibizlab.core.lite.domain.DstConfig
;
import
cn.ibizlab.core.lite.extensions.domain.SysModel
;
import
cn.ibizlab.core.lite.extensions.service.LiteCoreService
;
import
cn.ibizlab.core.lite.filter.DstConfigSearchContext
;
import
cn.ibizlab.core.lite.mapper.DstConfigMapper
;
import
cn.ibizlab.core.lite.service.IDstConfigService
;
import
cn.ibizlab.core.lite.service.IDstLiquibaseService
;
import
cn.ibizlab.core.util.config.GenerateLiquibaseChangeLog
;
import
cn.ibizlab.util.domain.LiquibaseProp
;
import
cn.ibizlab.util.helper.CachedBeanCopier
;
import
com.alibaba.fastjson.JSONObject
;
import
com.baomidou.mybatisplus.core.conditions.Wrapper
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Lazy
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.PageImpl
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.util.ObjectUtils
;
import
javax.annotation.Resource
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Objects
;
@Slf4j
@Service
(
"DstLiquibaseServiceImpl"
)
public
class
DstLiquibaseServiceImpl
extends
ServiceImpl
<
DstConfigMapper
,
DstConfig
>
implements
IDstLiquibaseService
{
@Resource
public
GenerateLiquibaseChangeLog
generateLiquibaseChangeLog
;
@Override
public
SysModel
generateLiquibase
(
LiquibaseProp
liquibaseProp
,
String
name
)
{
SysModel
sysModel
=
generateLiquibaseChangeLog
.
liquibaseGenerateChangeLog
(
liquibaseProp
,
name
);
return
sysModel
;
}
}
ibzlite-core/src/main/java/
liquibase/integration/sprin
g/GenerateLiquibaseChangeLog.java
→
ibzlite-core/src/main/java/
cn/ibizlab/core/util/confi
g/GenerateLiquibaseChangeLog.java
浏览文件 @
6ebeeb65
package
liquibase
.
integration
.
sprin
g
;
package
cn
.
ibizlab
.
core
.
util
.
confi
g
;
import
cn.hutool.core.util.ObjectUtil
;
...
...
@@ -11,39 +11,46 @@ import cn.ibizlab.util.domain.LiquibaseProp;
import
com.alibaba.fastjson.JSON
;
import
liquibase.Liquibase
;
import
liquibase.database.Database
;
import
liquibase.database.DatabaseConnection
;
import
liquibase.database.DatabaseFactory
;
import
liquibase.database.OfflineConnection
;
import
liquibase.database.jvm.JdbcConnection
;
import
liquibase.diff.output.DiffOutputControl
;
import
liquibase.exception.DatabaseException
;
import
liquibase.integration.ant.type.ChangeLogOutputFile
;
import
liquibase.integration.commandline.CommandLineUtils
;
import
cn.ibizlab.core.util.config.LiquibaseGenerateConfiguration
;
import
liquibase.integration.spring.SpringLiquibase
;
import
liquibase.resource.ResourceAccessor
;
import
liquibase.util.StringUtils
;
import
lombok.SneakyThrows
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.io.IOUtils
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.
stereotype.Component
;
import
org.springframework.
context.annotation.Configuration
;
import
org.springframework.util.DigestUtils
;
import
javax.annotation.Resource
;
import
javax.sql.DataSource
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
import
java.util.concurrent.CopyOnWriteArrayList
;
@Slf4j
@Co
mponent
@Co
nfiguration
public
class
GenerateLiquibaseChangeLog
{
private
Set
<
ChangeLogOutputFile
>
changeLogOutputFiles
=
new
LinkedHashSet
<>();
private
DataSource
dataSource
;
private
boolean
ignoreClasspathPrefix
=
true
;
protected
String
contexts
;
protected
Map
<
String
,
String
>
parameters
;
protected
String
defaultSchema
;
protected
String
liquibaseSchema
;
protected
String
databaseChangeLogTable
;
protected
String
databaseChangeLogLockTable
;
protected
String
liquibaseTablespace
;
protected
boolean
dropFirst
;
@Value
(
"${ibiz.filePath:/app/file/}"
)
protected
String
liquibasePath
;
...
...
@@ -75,21 +82,22 @@ public class GenerateLiquibaseChangeLog{
@SneakyThrows
public
SysModel
liquibaseGenerateChangeLog
(
LiquibaseProp
liquibaseProp
,
String
name
){
// 根据指定数据源生成对象
String
liquibaseChangeLogPath
=
getChangeLog
();
SpringLiquibase
springLiquibase
=
liquibaseGenerateConfiguration
.
masterliquibase
(
liquibaseProp
);
Liquibase
liquibase
=
springLiquibase
.
createLiquibase
(
springLiquibase
.
getDataSource
().
getConnection
()
);
Liquibase
liquibase
=
createLiquibase
(
springLiquibase
,
liquibaseChangeLogPath
);
Database
database
=
liquibase
.
getDatabase
();
// 自定义属性参数
byte
[]
nameBytes
=
name
.
getBytes
(
"UTF-8"
);
String
fileid
=
DigestUtils
.
md5DigestAsHex
(
nameBytes
);
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyyMMddHHmmss"
);
String
fileName
=
sdf
.
format
(
new
Date
());
String
fileFullPath
=
this
.
liquibasePath
+
"ibizutil"
+
File
.
separator
+
fileid
+
File
.
separator
+
fileName
;
File
file
=
new
File
(
liquibaseChangeLogPath
);
if
(!
file
.
getParentFile
().
exists
()){
file
.
getParentFile
().
mkdirs
();
}
if
(!
file
.
exists
()){
file
.
createNewFile
();
}
DiffOutputControl
diffOutputControl
=
new
DiffOutputControl
(
includeCatalog
,
includeSchema
,
includeTablespace
,
null
);
CommandLineUtils
.
doGenerateChangeLog
(
fileFull
Path
,
database
,
catalogName
,
liquibaseProp
.
getDefaultSchema
(),
StringUtils
.
trimToNull
(
generateField
),
CommandLineUtils
.
doGenerateChangeLog
(
liquibaseChangeLog
Path
,
database
,
catalogName
,
liquibaseProp
.
getDefaultSchema
(),
StringUtils
.
trimToNull
(
generateField
),
name
,
context
,
dataDir
,
diffOutputControl
);
SysModel
sysModel
=
generateSysModel
(
fileFull
Path
);
SysModel
sysModel
=
generateSysModel
(
liquibaseChangeLog
Path
);
return
sysModel
;
}
/**
...
...
@@ -185,34 +193,81 @@ public class GenerateLiquibaseChangeLog{
return
null
;
}
@SneakyThrows
public
Database
getDataBase
(){
Connection
connection
=
null
;
Database
database
=
null
;
String
name
=
"unknown"
;
try
{
connection
=
getDataSource
().
getConnection
();
database
=
DatabaseFactory
.
getInstance
().
findCorrectDatabaseImplementation
(
new
JdbcConnection
(
connection
));
name
=
database
.
getDatabaseProductName
();
}
catch
(
SQLException
e
)
{
throw
new
DatabaseException
(
e
);
}
finally
{
if
(
database
!=
null
)
{
database
.
close
();
}
else
if
(
connection
!=
null
)
{
try
{
if
(!
connection
.
getAutoCommit
())
{
connection
.
rollback
();
protected
Liquibase
createLiquibase
(
SpringLiquibase
springLiquibase
,
String
changelogPath
){
SpringLiquibase
.
SpringResourceOpener
resourceAccessor
=
springLiquibase
.
new
SpringResourceOpener
(
changelogPath
);
Liquibase
liquibase
=
new
Liquibase
(
changelogPath
,
resourceAccessor
,
createDatabase
(
springLiquibase
.
getDataSource
().
getConnection
(),
resourceAccessor
));
liquibase
.
setIgnoreClasspathPrefix
(
isIgnoreClasspathPrefix
());
if
(
parameters
!=
null
)
{
for
(
Map
.
Entry
<
String
,
String
>
entry
:
parameters
.
entrySet
())
{
liquibase
.
setChangeLogParameter
(
entry
.
getKey
(),
entry
.
getValue
());
}
connection
.
close
();
}
catch
(
SQLException
e
)
{
log
.
error
(
"problem closing connection"
,
e
);
}
if
(
isDropFirst
())
{
liquibase
.
dropAll
();
}
return
liquibase
;
}
public
boolean
isDropFirst
()
{
return
dropFirst
;
}
public
boolean
isIgnoreClasspathPrefix
()
{
return
ignoreClasspathPrefix
;
}
protected
Database
createDatabase
(
Connection
c
,
ResourceAccessor
resourceAccessor
)
throws
DatabaseException
{
DatabaseConnection
liquibaseConnection
;
if
(
c
==
null
)
{
log
.
info
(
"Null connection returned by liquibase datasource. Using offline unknown database"
);
liquibaseConnection
=
new
OfflineConnection
(
"offline:unknown"
,
resourceAccessor
);
}
else
{
liquibaseConnection
=
new
JdbcConnection
(
c
);
}
Database
database
=
DatabaseFactory
.
getInstance
().
findCorrectDatabaseImplementation
(
liquibaseConnection
);
if
(
StringUtils
.
trimToNull
(
this
.
defaultSchema
)
!=
null
)
{
if
(
database
.
supportsSchemas
())
{
database
.
setDefaultSchemaName
(
this
.
defaultSchema
);
}
else
if
(
database
.
supportsCatalogs
())
{
database
.
setDefaultCatalogName
(
this
.
defaultSchema
);
}
}
if
(
StringUtils
.
trimToNull
(
this
.
liquibaseSchema
)
!=
null
)
{
if
(
database
.
supportsSchemas
())
{
database
.
setLiquibaseSchemaName
(
this
.
liquibaseSchema
);
}
else
if
(
database
.
supportsCatalogs
())
{
database
.
setLiquibaseCatalogName
(
this
.
liquibaseSchema
);
}
}
if
(
StringUtils
.
trimToNull
(
this
.
liquibaseTablespace
)
!=
null
&&
database
.
supportsTablespaces
())
{
database
.
setLiquibaseTablespaceName
(
this
.
liquibaseTablespace
);
}
if
(
StringUtils
.
trimToNull
(
this
.
databaseChangeLogTable
)
!=
null
)
{
database
.
setDatabaseChangeLogTableName
(
this
.
databaseChangeLogTable
);
}
if
(
StringUtils
.
trimToNull
(
this
.
databaseChangeLogLockTable
)
!=
null
)
{
database
.
setDatabaseChangeLogLockTableName
(
this
.
databaseChangeLogLockTable
);
}
return
database
;
}
public
DataSource
getDataSource
()
{
return
dataSource
;
@SneakyThrows
public
String
getChangeLog
()
{
// 自定义属性参数
String
uuid
=
String
.
valueOf
(
UUID
.
randomUUID
());
byte
[]
nameBytes
=
uuid
.
getBytes
(
"UTF-8"
);
String
fileid
=
DigestUtils
.
md5DigestAsHex
(
nameBytes
);
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyyMMddHHmmss"
);
String
fileName
=
sdf
.
format
(
new
Date
())+
".xml"
;
String
fileFullPath
=
this
.
liquibasePath
+
"ibizutil"
+
File
.
separator
+
fileid
+
File
.
separator
+
fileName
;
return
fileFullPath
;
}
}
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录