提交 6ebeeb65 编写于 作者: Tyl666's avatar Tyl666

【Liquibase逆向工程】第三版 精简代码

上级 e3d98dcf
......@@ -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"),
// }
......
......@@ -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.IDstDataSourceService;
import cn.ibizlab.core.lite.service.IDstLiquibaseService;
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.spring.GenerateLiquibaseChangeLog;
import cn.ibizlab.core.util.config.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;
......
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);
}
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;
}
}
package liquibase.integration.spring;
package cn.ibizlab.core.util.config;
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
@Component
@Configuration
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(fileFullPath, database, catalogName,liquibaseProp.getDefaultSchema(),StringUtils.trimToNull(generateField),
CommandLineUtils.doGenerateChangeLog(liquibaseChangeLogPath, database, catalogName,liquibaseProp.getDefaultSchema(),StringUtils.trimToNull(generateField),
name, context, dataDir, diffOutputControl);
SysModel sysModel = generateSysModel(fileFullPath);
SysModel sysModel = generateSysModel(liquibaseChangeLogPath);
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();
}
connection.close();
} catch (SQLException e) {
log.error("problem closing connection", e);
}
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());
}
}
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 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册