13 changed files with 366 additions and 105 deletions
@ -0,0 +1,94 @@ |
|||
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.util.ZipUtil; |
|||
import io.sc.platform.jdbc.exporter.DataExporter; |
|||
import io.sc.platform.jdbc.exporter.support.DataExportConfigure; |
|||
import io.sc.platform.jdbc.exporter.support.ExportTable; |
|||
import io.sc.platform.jdbc.meta.support.Table; |
|||
import io.sc.platform.util.DateUtil; |
|||
import org.springframework.util.StringUtils; |
|||
|
|||
import javax.sql.DataSource; |
|||
import java.util.Arrays; |
|||
import java.util.Date; |
|||
import java.util.Locale; |
|||
import java.util.stream.Collectors; |
|||
|
|||
public class LiquibaseExporter implements DataExporter { |
|||
private static final String OUTPUT_PATH = DirectoryManager.getInstance().getByName("dir.work.web.export") + "/liquibase"; |
|||
|
|||
@Override |
|||
public void exportData(DataSource dataSource, DataExportConfigure configure, ProgressInfo progressInfo, Locale locale) throws Exception{ |
|||
String[] tableNames =ExportTable.getTableNames(configure.getTables()); |
|||
|
|||
progressInfo.setStatus(ProgressStatus.RUNNING); |
|||
progressInfo.setStartDatetime(new Date()); |
|||
progressInfo.setTotalWeight(tableNames.length); |
|||
|
|||
String catalogOrSchemaName =""; |
|||
if(StringUtils.hasText(configure.getCatalog())){ |
|||
catalogOrSchemaName ="/" + configure.getCatalog().trim(); |
|||
} |
|||
if(StringUtils.hasText(configure.getSchema())){ |
|||
catalogOrSchemaName ="/" + configure.getSchema().trim(); |
|||
} |
|||
|
|||
String outputDir =OUTPUT_PATH + catalogOrSchemaName; |
|||
|
|||
new CsvExporter().export(outputDir,dataSource,configure,progressInfo,locale); |
|||
new SchemaExporter().export(outputDir,dataSource,configure,progressInfo,locale); |
|||
new PluginExporter().export(outputDir,dataSource,configure,progressInfo,locale); |
|||
|
|||
ZipUtil.zip(outputDir); |
|||
|
|||
// 设置结果为导出的 zip 文件全路径
|
|||
progressInfo.setResult(outputDir + ".zip"); |
|||
|
|||
//执行完毕
|
|||
progressInfo.done(); |
|||
} |
|||
|
|||
public static String generateLiquibaseFileContent(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.9.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(); |
|||
} |
|||
|
|||
public static String generateImportCsvDataLiquiabaseChangeSet(Table table){ |
|||
StringBuilder sb =new StringBuilder(); |
|||
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"); |
|||
return sb.toString(); |
|||
} |
|||
|
|||
public static 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") + "__" + description; |
|||
} |
|||
} |
@ -0,0 +1,32 @@ |
|||
package io.sc.platform.jdbc.liquibase.exporter; |
|||
|
|||
import io.sc.platform.core.support.ProgressInfo; |
|||
import io.sc.platform.jdbc.exporter.support.DataExportConfigure; |
|||
import io.sc.platform.util.FileUtil; |
|||
|
|||
import javax.sql.DataSource; |
|||
import java.io.File; |
|||
import java.util.Locale; |
|||
|
|||
public class PluginExporter { |
|||
public void export(String outputBasePath, DataSource dataSource, DataExportConfigure configure, ProgressInfo progressInfo, Locale locale) throws Exception { |
|||
String outputDir =outputBasePath + "/src/main/resources/META-INF/platform/plugins"; |
|||
FileUtil.deldirs(outputDir); |
|||
new File(outputDir).mkdirs(); |
|||
|
|||
StringBuilder sb =new StringBuilder(); |
|||
sb.append("[").append("\n"); |
|||
sb.append(" {").append("\n"); |
|||
sb.append(" \"category\": \"install\"").append(",\n"); |
|||
sb.append(" \"order\": 100000").append(",\n"); |
|||
sb.append(" \"description\": \"\"").append(",\n"); |
|||
sb.append(" \"locations\": [").append("\n"); |
|||
sb.append(" \"").append("classpath:/liquibase/schema/").append(LiquibaseExporter.getLiquibaseChangeLogXmlFileName(configure)).append("_DDL.xml\"").append(",\n"); |
|||
sb.append(" \"").append("classpath:/liquibase/data/").append(LiquibaseExporter.getLiquibaseChangeLogXmlFileName(configure)).append("_DATA.xml\"").append("\n"); |
|||
sb.append(" ]").append("\n"); |
|||
sb.append(" }").append("\n"); |
|||
sb.append("]").append("\n"); |
|||
|
|||
FileUtil.writeString(outputDir + "/liquibase.json",sb.toString()); |
|||
} |
|||
} |
@ -0,0 +1,123 @@ |
|||
package io.sc.platform.jdbc.liquibase.exporter; |
|||
|
|||
import io.sc.platform.core.Environment; |
|||
import io.sc.platform.core.support.ProgressInfo; |
|||
import io.sc.platform.jdbc.exporter.support.DataExportConfigure; |
|||
import io.sc.platform.jdbc.exporter.support.ExportTable; |
|||
import io.sc.platform.jdbc.meta.MetaDataLoader; |
|||
import io.sc.platform.jdbc.meta.support.Column; |
|||
import io.sc.platform.jdbc.meta.support.Index; |
|||
import io.sc.platform.jdbc.meta.support.Table; |
|||
import io.sc.platform.util.FileUtil; |
|||
import io.sc.platform.util.WriterUtil; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
|
|||
import javax.sql.DataSource; |
|||
import javax.xml.stream.XMLStreamException; |
|||
import javax.xml.stream.XMLStreamWriter; |
|||
import java.io.File; |
|||
import java.util.List; |
|||
import java.util.Locale; |
|||
|
|||
public class SchemaExporter { |
|||
private static final Logger log = LoggerFactory.getLogger(SchemaExporter.class); |
|||
|
|||
public void export(String outputBasePath, DataSource dataSource, DataExportConfigure configure, ProgressInfo progressInfo, Locale locale) throws Exception{ |
|||
String outputDir =outputBasePath + "/src/main/resources/liquibase/schema"; |
|||
FileUtil.deldirs(outputDir); |
|||
new File(outputDir).mkdirs(); |
|||
|
|||
String[] tableNames = ExportTable.getTableNames(configure.getTables()); |
|||
|
|||
XMLStreamWriter writer = WriterUtil.xmlStreamWriter(outputDir + "/" + LiquibaseExporter.getLiquibaseChangeLogXmlFileName(configure) + "_DDL.xml"); |
|||
writeHeader(writer,LiquibaseExporter.getLiquibaseChangeLogXmlFileName(configure)); |
|||
List<Table> tables = MetaDataLoader.newInstance().getTablesWithDetail(dataSource,configure.getCatalog(),configure.getSchema(),tableNames); |
|||
for(Table table : tables){ |
|||
writeTable(table,writer); |
|||
} |
|||
writeTail(writer); |
|||
writer.flush(); |
|||
writer.close(); |
|||
} |
|||
|
|||
private void writeHeader(XMLStreamWriter writer,String changeSetId) throws XMLStreamException { |
|||
writer.writeStartDocument(Environment.DEFAULT_CHARSET_NAME,"1.0"); |
|||
|
|||
writer.writeStartElement("databaseChangeLog"); //start databaseChangeLog
|
|||
writer.writeAttribute("xmlns", "http://www.liquibase.org/xml/ns/dbchangelog"); |
|||
writer.writeAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); |
|||
writer.writeAttribute("xmlns:ext", "http://www.liquibase.org/xml/ns/dbchangelog-ext"); |
|||
writer.writeAttribute("xsi:schemaLocation", "http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.9.xsd http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd"); |
|||
|
|||
writer.writeStartElement("changeSet"); //start changeSet
|
|||
writer.writeAttribute("id", changeSetId); |
|||
writer.writeAttribute("author", "platform"); |
|||
} |
|||
|
|||
private void writeTail(XMLStreamWriter writer) throws XMLStreamException { |
|||
writer.writeEndElement(); //end changeSet
|
|||
writer.writeEndElement(); //end databaseChangeLog
|
|||
writer.writeEndDocument(); |
|||
} |
|||
|
|||
private void writeTable(Table table, XMLStreamWriter writer) throws XMLStreamException { |
|||
// create table
|
|||
writer.writeStartElement("createTable"); //start createTable
|
|||
writer.writeAttribute("tableName", table.getName().toUpperCase()); |
|||
writer.writeAttribute("remarks", table.getRemarks()); |
|||
|
|||
List<Column> columns =table.getColumns(); |
|||
for(Column column : columns){ |
|||
writer.writeStartElement("column"); //start column
|
|||
writer.writeAttribute("name",column.getName()); |
|||
writer.writeAttribute("type", getLiquibaseJavaSqlType(column)); |
|||
writer.writeAttribute("remarks", column.getRemarks()); |
|||
if(column.isPartOfPrimaryKey()) { |
|||
writer.writeStartElement("constraints"); |
|||
writer.writeAttribute("primaryKey", "true"); |
|||
writer.writeEndElement(); |
|||
} |
|||
writer.writeEndElement(); //end column
|
|||
} |
|||
writer.writeEndElement(); //end createTable
|
|||
|
|||
// create index
|
|||
List<Index> indexes =table.getIndexes(); |
|||
for(Index index : indexes){ |
|||
for(Column column : index.getColumns()){ |
|||
if(!column.isPartOfPrimaryKey()) { |
|||
writer.writeStartElement("createIndex"); //start createIndex
|
|||
writer.writeAttribute("tableName",table.getName()); |
|||
writer.writeAttribute("indexName",index.getName()); |
|||
writer.writeStartElement("column"); //start column
|
|||
writer.writeAttribute("name", column.getName()); |
|||
writer.writeEndElement(); //end column
|
|||
writer.writeEndElement(); //end createIndex
|
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
private String getLiquibaseJavaSqlType(Column column) { |
|||
String type =column.getSqlType(); |
|||
if("java.sql.Types.VARCHAR".equals(type) || "java.sql.Types.NVARCHAR".equals(type)) { |
|||
if(column.getSize()>=2000) { |
|||
type ="java.sql.Types.CLOB"; |
|||
}else { |
|||
type ="NVARCHAR(" + column.getSize() + ")"; |
|||
} |
|||
}else if("java.sql.Types.LONGVARCHAR".equals(type)){ |
|||
type ="CLOB"; |
|||
}else if("java.sql.Types.TIMESTAMP".equals(type)) { |
|||
type ="DATETIME"; |
|||
}else if("java.sql.Types.NUMERIC".equals(type)) { |
|||
type ="java.sql.Types.NUMERIC" + column.getWidth(); |
|||
}else if("java.sql.Types.DECIMAL".equals(type)) { |
|||
type ="java.sql.Types.DECIMAL" + column.getWidth(); |
|||
}else if("java.sql.Types.INTEGER".equals(type)){ |
|||
type ="INTEGER"; |
|||
} |
|||
return type; |
|||
} |
|||
} |
@ -1 +1 @@ |
|||
io.sc.platform.jdbc.liquibase.exporter.LiquibaseDataCsvExporter |
|||
io.sc.platform.jdbc.liquibase.exporter.LiquibaseExporter |
Loading…
Reference in new issue