|
@ -1,10 +1,6 @@ |
|
|
package io.sc.platform.jdbc.liquibase.exporter; |
|
|
package io.sc.platform.jdbc.liquibase.exporter; |
|
|
|
|
|
|
|
|
import io.sc.platform.core.DirectoryManager; |
|
|
|
|
|
import io.sc.platform.core.enums.ProgressStatus; |
|
|
|
|
|
import io.sc.platform.core.support.ProgressInfo; |
|
|
import io.sc.platform.core.support.ProgressInfo; |
|
|
import io.sc.platform.core.util.ZipUtil; |
|
|
|
|
|
import io.sc.platform.jdbc.exporter.DataExporter; |
|
|
|
|
|
import io.sc.platform.jdbc.exporter.support.DataExportConfigure; |
|
|
import io.sc.platform.jdbc.exporter.support.DataExportConfigure; |
|
|
import io.sc.platform.jdbc.exporter.support.ExportTable; |
|
|
import io.sc.platform.jdbc.exporter.support.ExportTable; |
|
|
import io.sc.platform.jdbc.liquibase.exporter.support.ParentChildRecord; |
|
|
import io.sc.platform.jdbc.liquibase.exporter.support.ParentChildRecord; |
|
@ -12,7 +8,6 @@ import io.sc.platform.jdbc.meta.MetaDataLoader; |
|
|
import io.sc.platform.jdbc.meta.support.Column; |
|
|
import io.sc.platform.jdbc.meta.support.Column; |
|
|
import io.sc.platform.jdbc.meta.support.Table; |
|
|
import io.sc.platform.jdbc.meta.support.Table; |
|
|
import io.sc.platform.jdbc.util.SqlTypeUtil; |
|
|
import io.sc.platform.jdbc.util.SqlTypeUtil; |
|
|
import io.sc.platform.util.DateUtil; |
|
|
|
|
|
import io.sc.platform.util.FileUtil; |
|
|
import io.sc.platform.util.FileUtil; |
|
|
import io.sc.platform.util.WriterUtil; |
|
|
import io.sc.platform.util.WriterUtil; |
|
|
import liquibase.repackaged.com.opencsv.CSVWriterBuilder; |
|
|
import liquibase.repackaged.com.opencsv.CSVWriterBuilder; |
|
@ -27,46 +22,39 @@ import java.io.BufferedWriter; |
|
|
import java.io.File; |
|
|
import java.io.File; |
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
import java.sql.*; |
|
|
import java.sql.*; |
|
|
import java.util.Date; |
|
|
import java.util.ArrayList; |
|
|
import java.util.*; |
|
|
import java.util.Base64; |
|
|
import java.util.stream.Collectors; |
|
|
import java.util.List; |
|
|
|
|
|
import java.util.Locale; |
|
|
public class LiquibaseDataCsvExporter implements DataExporter { |
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(LiquibaseDataCsvExporter.class); |
|
|
public class CsvExporter { |
|
|
private static final String OUTPUT_PATH = DirectoryManager.getInstance().getByName("dir.work.web.export") + "/liquibase"; |
|
|
private static final Logger log = LoggerFactory.getLogger(CsvExporter.class); |
|
|
|
|
|
|
|
|
@Override |
|
|
public void export(String outputBasePath,DataSource dataSource, DataExportConfigure configure, ProgressInfo progressInfo, Locale locale) throws Exception{ |
|
|
public void exportData(DataSource dataSource, DataExportConfigure configure, ProgressInfo progressInfo, Locale locale) throws Exception{ |
|
|
String outputDir =outputBasePath + "/src/main/resources/liquibase/data"; |
|
|
progressInfo.setStatus(ProgressStatus.RUNNING); |
|
|
FileUtil.deldirs(outputDir); |
|
|
progressInfo.setStartDatetime(new Date()); |
|
|
new File(outputDir).mkdirs(); |
|
|
|
|
|
String[] tableNames = ExportTable.getTableNames(configure.getTables()); |
|
|
String[] tableNames =ExportTable.getTableNames(configure.getTables()); |
|
|
// 获取需要操作的表信息
|
|
|
progressInfo.setTotalWeight(tableNames.length); |
|
|
List<Table> tables = MetaDataLoader.newInstance().getTablesWithDetail(dataSource,configure.getCatalog(),configure.getSchema(),tableNames); |
|
|
|
|
|
// 导出 csv 数据文件
|
|
|
List<Table> tables =MetaDataLoader.newInstance().getTablesWithDetail(dataSource,configure.getCatalog(),configure.getSchema(),tableNames); |
|
|
|
|
|
String outPutDir =OUTPUT_PATH + getPath(configure.getCatalog(),configure.getSchema()); |
|
|
|
|
|
FileUtil.deldirs(outPutDir); |
|
|
|
|
|
String dataDir =outPutDir + "/data"; |
|
|
|
|
|
new File(dataDir).mkdirs(); |
|
|
|
|
|
StringBuilder changeSetSb =new StringBuilder(); |
|
|
|
|
|
for(Table table : tables){ |
|
|
for(Table table : tables){ |
|
|
progressInfo.setMessageKey(table.getName()); |
|
|
progressInfo.setMessageKey(table.getName()); |
|
|
writeChangeSet(changeSetSb,table); |
|
|
writeTable(outputDir,dataSource,table,configure,progressInfo,locale); |
|
|
writeTable(dataSource,table,configure,progressInfo,locale); |
|
|
|
|
|
progressInfo.addWeight(1); |
|
|
progressInfo.addWeight(1); |
|
|
} |
|
|
} |
|
|
FileUtil.writeString(outPutDir + "/liquibase.xml",createLiquibaseFileContent(getLiquibaseChangeLogXmlFileName(configure),changeSetSb)); |
|
|
|
|
|
ZipUtil.zip(outPutDir); |
|
|
|
|
|
|
|
|
|
|
|
// 设置结果为导出的 zip 文件全路径
|
|
|
|
|
|
progressInfo.setResult(outPutDir + ".zip"); |
|
|
|
|
|
|
|
|
|
|
|
//执行完毕
|
|
|
// 导出 liquibase 用于导入 csv 数据文件的 changelog 文件
|
|
|
progressInfo.done(); |
|
|
StringBuilder changeSetSb =new StringBuilder(); |
|
|
|
|
|
for(Table table : tables){ |
|
|
|
|
|
changeSetSb.append(LiquibaseExporter.generateImportCsvDataLiquiabaseChangeSet(table)); |
|
|
|
|
|
} |
|
|
|
|
|
String xmlFileName =LiquibaseExporter.getLiquibaseChangeLogXmlFileName(configure) + "_DATA"; |
|
|
|
|
|
FileUtil.writeString(outputDir + "/" + xmlFileName + ".xml",LiquibaseExporter.generateLiquibaseFileContent(xmlFileName,changeSetSb)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void writeTable(DataSource dataSource,Table table,DataExportConfigure configure,ProgressInfo progressInfo, Locale locale) throws Exception{ |
|
|
private void writeTable(String outputDir,DataSource dataSource,Table table,DataExportConfigure configure,ProgressInfo progressInfo, Locale locale) throws Exception{ |
|
|
BufferedWriter writer = WriterUtil.bufferedWriter(OUTPUT_PATH + getPath(configure.getCatalog(),configure.getSchema()) + "/data/" + table.getName() + ".csv"); |
|
|
BufferedWriter writer = WriterUtil.bufferedWriter(outputDir + "/" + table.getName() + ".csv"); |
|
|
ICSVWriter csvWriter = new CSVWriterBuilder(writer).build(); |
|
|
ICSVWriter csvWriter = new CSVWriterBuilder(writer).build(); |
|
|
|
|
|
|
|
|
//写入字段定义信息===============================================================================
|
|
|
//写入字段定义信息===============================================================================
|
|
@ -209,45 +197,6 @@ public class LiquibaseDataCsvExporter implements DataExporter { |
|
|
log.info("[" + table.getName() + "] : " + count + " completed"); |
|
|
log.info("[" + table.getName() + "] : " + count + " completed"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private String createLiquibaseFileContent(String id,StringBuilder content){ |
|
|
|
|
|
StringBuilder sb =new StringBuilder(); |
|
|
|
|
|
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\n"); |
|
|
|
|
|
sb.append("<databaseChangeLog").append("\n"); |
|
|
|
|
|
sb.append("\t").append("xmlns=\"http://www.liquibase.org/xml/ns/dbchangelog\"").append("\n"); |
|
|
|
|
|
sb.append("\t").append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"").append("\n"); |
|
|
|
|
|
sb.append("\t").append("xmlns:ext=\"http://www.liquibase.org/xml/ns/dbchangelog-ext\"").append("\n"); |
|
|
|
|
|
sb.append("\t").append("xsi:schemaLocation=\"").append("\n"); |
|
|
|
|
|
sb.append("\t\t").append("http://www.liquibase.org/xml/ns/dbchangelog").append("\n"); |
|
|
|
|
|
sb.append("\t\t").append("http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd").append("\n"); |
|
|
|
|
|
sb.append("\t\t").append("http://www.liquibase.org/xml/ns/dbchangelog-ext").append("\n"); |
|
|
|
|
|
sb.append("\t\t").append("http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd").append("\n"); |
|
|
|
|
|
sb.append("\">").append("\n"); |
|
|
|
|
|
sb.append("\t").append("<changeSet id=\"").append(id).append("\" author=\"platform\">").append("\n"); |
|
|
|
|
|
sb.append(content); |
|
|
|
|
|
sb.append("\t").append("</changeSet>").append("\n"); |
|
|
|
|
|
sb.append("</databaseChangeLog>"); |
|
|
|
|
|
return sb.toString(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void writeChangeSet(StringBuilder sb,Table table){ |
|
|
|
|
|
sb.append("\t\t").append("<customChange class=\"io.sc.platform.jdbc.liquibase.task.CsvImportTaskChange\">").append("\n"); |
|
|
|
|
|
sb.append("\t\t\t").append("<param name=\"dataFile\" value=\"classpath:/liquibase/data/").append(table.getName()).append(".csv").append("\"/>").append("\n"); |
|
|
|
|
|
sb.append("\t\t").append("</customChange>").append("\n"); |
|
|
|
|
|
sb.append("\n"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getLiquibaseChangeLogXmlFileName(DataExportConfigure configure) { |
|
|
|
|
|
String[] tableNames = Arrays.stream(configure.getTables()).map(ExportTable::getName).collect(Collectors.toList()).toArray(new String[]{}); |
|
|
|
|
|
String description =null; |
|
|
|
|
|
if(tableNames.length==1) { |
|
|
|
|
|
description =tableNames[0].toUpperCase(); |
|
|
|
|
|
}else { |
|
|
|
|
|
description =tableNames[0].toUpperCase() + " And More " + (tableNames.length-1) + " Tables"; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return "LIQUIBASE_" + DateUtil.formatDate(new Date(),"yyyy.MM.dd_HH.mm.ss") + "__" + description; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private String transformString(String string){ |
|
|
private String transformString(String string){ |
|
|
if(string==null){ |
|
|
if(string==null){ |
|
|
return null; |
|
|
return null; |
|
@ -260,16 +209,4 @@ public class LiquibaseDataCsvExporter implements DataExporter { |
|
|
//string =string.replace("\r","\\\\r");
|
|
|
//string =string.replace("\r","\\\\r");
|
|
|
return string; |
|
|
return string; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private String getPath(String catalog,String schema){ |
|
|
|
|
|
String catalogOrSchemaName =""; |
|
|
|
|
|
if(StringUtils.hasText(catalog)){ |
|
|
|
|
|
catalogOrSchemaName ="/" + catalog; |
|
|
|
|
|
} |
|
|
|
|
|
if(StringUtils.hasText(schema)){ |
|
|
|
|
|
catalogOrSchemaName ="/" + schema; |
|
|
|
|
|
} |
|
|
|
|
|
return catalogOrSchemaName; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |