提交 2839acef 编写于 作者: J Joram Barrez

Fix contentDbSchemaManager not using correct changelog

上级 ee4f943f
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flowable.cmmn.engine.impl.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import org.flowable.cmmn.engine.impl.db.CmmnDbSchemaManager;
import org.flowable.engine.common.impl.FlowableVersion;
import org.flowable.engine.common.impl.FlowableVersions;
import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseConnection;
import liquibase.database.DatabaseFactory;
import liquibase.database.core.DB2Database;
import liquibase.database.core.H2Database;
import liquibase.database.core.HsqlDatabase;
import liquibase.database.core.MSSQLDatabase;
import liquibase.database.core.MySQLDatabase;
import liquibase.database.core.OracleDatabase;
import liquibase.database.core.PostgresDatabase;
import liquibase.exception.DatabaseException;
import liquibase.exception.LiquibaseException;
/**
* When this class is executed, it generates
* - the create DDL script for the cmmn engine
* - the upgrade script for going from the previous version to the current version.
*
* @author Joram Barrez
*/
public class DbSchemaGenerator {
// The baseline version, i.e. the version at the time that the first sql script was shipped
private static final String BASELINE_VERSION = "6.2.0.0";
private static final String PREFIX_CHANGELOG = "src/main/resources/org/flowable/cmmn/db/liquibase/csv/changelog-";
private static final Map<String, String> DATABASES = new HashMap<>();
static {
DATABASES.put("db2", new DB2Database().getShortName());
DATABASES.put("h2", new H2Database().getShortName());
DATABASES.put("hsql", new HsqlDatabase().getShortName());
DATABASES.put("mssql", new MSSQLDatabase().getShortName());
DATABASES.put("mysql", new MySQLDatabase().getShortName());
DATABASES.put("oracle", new OracleDatabase().getShortName());
DATABASES.put("postgres", new PostgresDatabase().getShortName());
}
public static void main(String[] args) throws Exception {
for (String db : DATABASES.keySet()) {
generateCreateScript(db);
generateUpdateScript(db);
}
}
protected static void generateCreateScript(String db) throws Exception {
String changeLogFileName = PREFIX_CHANGELOG + "temp.csv";
File tempChangeLogFile = new File(changeLogFileName);
if (tempChangeLogFile.exists()) {
tempChangeLogFile.delete();
}
tempChangeLogFile.createNewFile();
Database database = determineDatabase(db, changeLogFileName);
generateSql(db, false, database);
tempChangeLogFile.delete();
}
protected static void generateUpdateScript(String db) throws IOException, DatabaseException, LiquibaseException, FileNotFoundException {
boolean isUpdate = !FlowableVersions.CURRENT_VERSION.equals(BASELINE_VERSION);
String changeLogFile = determineChangeLogFile(db, isUpdate);
Database database = determineDatabase(db, changeLogFile);
generateSql(db, isUpdate, database);
}
protected static String determineChangeLogFile(String db, boolean isUpdate) throws IOException {
String baseChangeLogFileName = PREFIX_CHANGELOG + db + "-";
String changeLogFileName = baseChangeLogFileName + cleanVersion(FlowableVersions.CURRENT_VERSION) + ".csv";
if (isUpdate) {
String previousChangeLogFileName = baseChangeLogFileName + cleanVersion(FlowableVersions.getPreviousVersion(FlowableVersions.CURRENT_VERSION).getMainVersion()) + ".csv";
File previousChangeLogFile = new File(previousChangeLogFileName);
File newChangeLogFile = new File(changeLogFileName);
if (newChangeLogFile.exists()) {
newChangeLogFile.delete();
}
Files.copy(previousChangeLogFile.toPath(), newChangeLogFile.toPath());
} else {
File changeLogFile = new File(changeLogFileName);
if (changeLogFile.exists()) {
changeLogFile.delete();
}
}
return changeLogFileName;
}
protected static Database determineDatabase(String db, String changeLogFile) throws DatabaseException {
DatabaseFactory databaseFactory = DatabaseFactory.getInstance();
DatabaseConnection databaseConnection = databaseFactory.openConnection("offline:" + DATABASES.get(db)
+ "?productName=" + db
+ "&outputLiquibaseSql=ALL"
+ "&changeLogFile=" + changeLogFile,
null, null, null, null);
return DatabaseFactory.getInstance().findCorrectDatabaseImplementation(databaseConnection);
}
protected static void generateSql(String db, boolean isUpdate, Database database) throws LiquibaseException, IOException, FileNotFoundException {
CmmnDbSchemaManager cmmnDbSchemaManager = new CmmnDbSchemaManager();
Liquibase liquibase = cmmnDbSchemaManager.createLiquibaseInstance(database);
File outputFile = null;
if (isUpdate) {
FlowableVersion previousVersion = FlowableVersions.getPreviousVersion(FlowableVersions.CURRENT_VERSION);
outputFile = new File("src/main/resources/org/flowable/cmmn/db/upgrade/flowable." + db + ".cmmn.upgradestep."
+ cleanVersion(previousVersion.getMainVersion()) + ".to." + cleanVersion(FlowableVersions.CURRENT_VERSION) + ".sql");
} else {
outputFile = new File("src/main/resources/org/flowable/cmmn/db/create/flowable." + db + ".cmmn.create.sql");
}
if (outputFile.exists()) {
outputFile.delete();
}
outputFile.createNewFile();
StringWriter stringWriter = new StringWriter();
liquibase.update("cmmn", stringWriter);
stringWriter.close();
String sqlScript = stringWriter.toString();
writeSqlScriptToFile(outputFile, sqlScript);
}
protected static void writeSqlScriptToFile(File file, String sqlScript) throws FileNotFoundException, IOException {
PrintWriter printWriter = new PrintWriter(new FileOutputStream(file));
BufferedReader reader = new BufferedReader(new StringReader(sqlScript));
String line = reader.readLine();
int nrOfLinesWritten = 0;
while (line != null) {
if (!line.startsWith("--")) {
printWriter.println(line);
nrOfLinesWritten++;
}
line = reader.readLine();
}
printWriter.flush();
printWriter.close();
// No need to generate empty upgrade script
if (nrOfLinesWritten == 0) {
file.delete();
}
}
protected static String cleanVersion(String version) {
return version.replaceAll("\\.", "");
}
}
"ID","AUTHOR","FILENAME","DATEEXECUTED","ORDEREXECUTED","EXECTYPE","MD5SUM","DESCRIPTION","COMMENTS","TAG","LIQUIBASE","CONTEXTS","LABELS"
"1","flowable","org/flowable/cmmn/db/liquibase/flowable-cmmn-db-changelog.xml","2017-09-03T22:40:04.992","2","EXECUTED","7:28e5931d36abab0185c189c584a7c2d0","createTable tableName=ACT_CMMN_RE_DEPLOYMENT; createTable tableName=ACT_CMMN_RE_DEPLOYMENT_RESOURCE; addForeignKeyConstraint baseTableName=ACT_CMMN_RE_DEPLOYMENT_RESOURCE, constraintName=ACT_FK_CMMN_RSRC_DPL, referencedTableName=ACT_CMMN_RE_DEPLOYMENT;...",,"","3.5.3","()","","4471204965"
"ID","AUTHOR","FILENAME","DATEEXECUTED","ORDEREXECUTED","EXECTYPE","MD5SUM","DESCRIPTION","COMMENTS","TAG","LIQUIBASE","CONTEXTS","LABELS"
"1","flowable","org/flowable/cmmn/db/liquibase/flowable-cmmn-db-changelog.xml","2017-09-03T22:40:05.217","2","EXECUTED","7:28e5931d36abab0185c189c584a7c2d0","createTable tableName=ACT_CMMN_RE_DEPLOYMENT; createTable tableName=ACT_CMMN_RE_DEPLOYMENT_RESOURCE; addForeignKeyConstraint baseTableName=ACT_CMMN_RE_DEPLOYMENT_RESOURCE, constraintName=ACT_FK_CMMN_RSRC_DPL, referencedTableName=ACT_CMMN_RE_DEPLOYMENT;...",,"","3.5.3","()","","4471205191"
"ID","AUTHOR","FILENAME","DATEEXECUTED","ORDEREXECUTED","EXECTYPE","MD5SUM","DESCRIPTION","COMMENTS","TAG","LIQUIBASE","CONTEXTS","LABELS"
"1","flowable","org/flowable/cmmn/db/liquibase/flowable-cmmn-db-changelog.xml","2017-09-03T22:40:05.116","2","EXECUTED","7:28e5931d36abab0185c189c584a7c2d0","createTable tableName=ACT_CMMN_RE_DEPLOYMENT; createTable tableName=ACT_CMMN_RE_DEPLOYMENT_RESOURCE; addForeignKeyConstraint baseTableName=ACT_CMMN_RE_DEPLOYMENT_RESOURCE, constraintName=ACT_FK_CMMN_RSRC_DPL, referencedTableName=ACT_CMMN_RE_DEPLOYMENT;...",,"","3.5.3","()","","4471205090"
"ID","AUTHOR","FILENAME","DATEEXECUTED","ORDEREXECUTED","EXECTYPE","MD5SUM","DESCRIPTION","COMMENTS","TAG","LIQUIBASE","CONTEXTS","LABELS"
"1","flowable","org/flowable/cmmn/db/liquibase/flowable-cmmn-db-changelog.xml","2017-09-03T22:40:05.479","2","EXECUTED","7:28e5931d36abab0185c189c584a7c2d0","createTable tableName=ACT_CMMN_RE_DEPLOYMENT; createTable tableName=ACT_CMMN_RE_DEPLOYMENT_RESOURCE; addForeignKeyConstraint baseTableName=ACT_CMMN_RE_DEPLOYMENT_RESOURCE, constraintName=ACT_FK_CMMN_RSRC_DPL, referencedTableName=ACT_CMMN_RE_DEPLOYMENT;...",,"","3.5.3","()","","4471205429"
"ID","AUTHOR","FILENAME","DATEEXECUTED","ORDEREXECUTED","EXECTYPE","MD5SUM","DESCRIPTION","COMMENTS","TAG","LIQUIBASE","CONTEXTS","LABELS"
"1","flowable","org/flowable/cmmn/db/liquibase/flowable-cmmn-db-changelog.xml","2017-09-03T22:40:05.328","2","EXECUTED","7:28e5931d36abab0185c189c584a7c2d0","createTable tableName=ACT_CMMN_RE_DEPLOYMENT; createTable tableName=ACT_CMMN_RE_DEPLOYMENT_RESOURCE; addForeignKeyConstraint baseTableName=ACT_CMMN_RE_DEPLOYMENT_RESOURCE, constraintName=ACT_FK_CMMN_RSRC_DPL, referencedTableName=ACT_CMMN_RE_DEPLOYMENT;...",,"","3.5.3","()","","4471205295"
"ID","AUTHOR","FILENAME","DATEEXECUTED","ORDEREXECUTED","EXECTYPE","MD5SUM","DESCRIPTION","COMMENTS","TAG","LIQUIBASE","CONTEXTS","LABELS"
"1","flowable","org/flowable/cmmn/db/liquibase/flowable-cmmn-db-changelog.xml","2017-09-03T22:40:04.856","2","EXECUTED","7:28e5931d36abab0185c189c584a7c2d0","createTable tableName=ACT_CMMN_RE_DEPLOYMENT; createTable tableName=ACT_CMMN_RE_DEPLOYMENT_RESOURCE; addForeignKeyConstraint baseTableName=ACT_CMMN_RE_DEPLOYMENT_RESOURCE, constraintName=ACT_FK_CMMN_RSRC_DPL, referencedTableName=ACT_CMMN_RE_DEPLOYMENT;...",,"","3.5.3","()","","4471204819"
"ID","AUTHOR","FILENAME","DATEEXECUTED","ORDEREXECUTED","EXECTYPE","MD5SUM","DESCRIPTION","COMMENTS","TAG","LIQUIBASE","CONTEXTS","LABELS"
"1","flowable","org/flowable/cmmn/db/liquibase/flowable-cmmn-db-changelog.xml","2017-09-03T22:40:05.557","2","EXECUTED","7:28e5931d36abab0185c189c584a7c2d0","createTable tableName=ACT_CMMN_RE_DEPLOYMENT; createTable tableName=ACT_CMMN_RE_DEPLOYMENT_RESOURCE; addForeignKeyConstraint baseTableName=ACT_CMMN_RE_DEPLOYMENT_RESOURCE, constraintName=ACT_FK_CMMN_RSRC_DPL, referencedTableName=ACT_CMMN_RE_DEPLOYMENT;...",,"","3.5.3","()","","4471205537"
......@@ -22,7 +22,6 @@ import java.util.Set;
import javax.sql.DataSource;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.TransactionFactory;
import org.flowable.content.api.ContentEngineConfigurationApi;
......@@ -45,7 +44,6 @@ import org.flowable.content.engine.impl.persistence.entity.TableDataManagerImpl;
import org.flowable.content.engine.impl.persistence.entity.data.ContentItemDataManager;
import org.flowable.content.engine.impl.persistence.entity.data.impl.MybatisContentItemDataManager;
import org.flowable.engine.common.AbstractEngineConfiguration;
import org.flowable.engine.common.api.FlowableException;
import org.flowable.engine.common.impl.cfg.BeansConfigurationHelper;
import org.flowable.engine.common.impl.db.DbSqlSessionFactory;
import org.flowable.engine.common.impl.interceptor.CommandInterceptor;
......@@ -59,13 +57,6 @@ import org.flowable.engine.common.runtime.Clock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseConnection;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.resource.ClassLoaderResourceAccessor;
public class ContentEngineConfiguration extends AbstractEngineConfiguration implements ContentEngineConfigurationApi {
protected static final Logger LOGGER = LoggerFactory.getLogger(ContentEngineConfiguration.class);
......@@ -222,37 +213,8 @@ public class ContentEngineConfiguration extends AbstractEngineConfiguration impl
}
public void initDbSchema() {
try {
DatabaseConnection connection = new JdbcConnection(dataSource.getConnection());
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
database.setDatabaseChangeLogTableName(LIQUIBASE_CHANGELOG_PREFIX + database.getDatabaseChangeLogTableName());
database.setDatabaseChangeLogLockTableName(LIQUIBASE_CHANGELOG_PREFIX + database.getDatabaseChangeLogLockTableName());
if (StringUtils.isNotEmpty(databaseSchema)) {
database.setDefaultSchemaName(databaseSchema);
database.setLiquibaseSchemaName(databaseSchema);
}
if (StringUtils.isNotEmpty(databaseCatalog)) {
database.setDefaultCatalogName(databaseCatalog);
database.setLiquibaseCatalogName(databaseCatalog);
}
Liquibase liquibase = new Liquibase("org/flowable/content/db/liquibase/flowable-content-db-changelog.xml", new ClassLoaderResourceAccessor(), database);
if (DB_SCHEMA_UPDATE_DROP_CREATE.equals(databaseSchemaUpdate)) {
LOGGER.debug("Dropping and creating schema CONTENT");
liquibase.dropAll();
liquibase.update("content");
} else if (DB_SCHEMA_UPDATE_TRUE.equals(databaseSchemaUpdate)) {
LOGGER.debug("Updating schema CONTENT");
liquibase.update("content");
} else if (DB_SCHEMA_UPDATE_FALSE.equals(databaseSchemaUpdate)) {
LOGGER.debug("Validating schema CONTENT");
liquibase.validate();
}
} catch (Exception e) {
throw new FlowableException("Error initialising content data schema", e);
if (dbSchemaManager != null) {
((ContentDbSchemaManager) dbSchemaManager).initSchema(this, databaseSchemaUpdate);
}
}
......
......@@ -19,6 +19,8 @@ import org.flowable.content.engine.impl.util.CommandContextUtil;
import org.flowable.engine.common.api.FlowableException;
import org.flowable.engine.common.impl.db.DbSchemaManager;
import org.flowable.engine.common.impl.interceptor.CommandContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import liquibase.Liquibase;
import liquibase.database.Database;
......@@ -29,23 +31,27 @@ import liquibase.resource.ClassLoaderResourceAccessor;
public class ContentDbSchemaManager implements DbSchemaManager {
private static final Logger LOGGER = LoggerFactory.getLogger(ContentDbSchemaManager.class);
public static String LIQUIBASE_CHANGELOG = "org/flowable/content/db/liquibase/flowable-content-db-changelog.xml";
@Override
public void dbSchemaCreate() {
Liquibase liquibase = createLiquibaseInstance();
Liquibase liquibase = createLiquibaseInstance(CommandContextUtil.getContentEngineConfiguration());
try {
liquibase.update("form");
liquibase.update("content");
} catch (Exception e) {
throw new FlowableException("Error creating form engine tables", e);
throw new FlowableException("Error creating content engine tables", e);
}
}
@Override
public void dbSchemaDrop() {
Liquibase liquibase = createLiquibaseInstance();
Liquibase liquibase = createLiquibaseInstance(CommandContextUtil.getContentEngineConfiguration());
try {
liquibase.dropAll();
} catch (Exception e) {
throw new FlowableException("Error dropping form engine tables", e);
throw new FlowableException("Error dropping content engine tables", e);
}
}
......@@ -55,13 +61,13 @@ public class ContentDbSchemaManager implements DbSchemaManager {
return null;
}
protected static Liquibase createLiquibaseInstance() {
protected static Liquibase createLiquibaseInstance(ContentEngineConfiguration configuration) {
try {
Connection jdbcConnection = null;
CommandContext commandContext = CommandContextUtil.getCommandContext();
if (commandContext == null) {
jdbcConnection = CommandContextUtil.getContentEngineConfiguration(commandContext).getDataSource().getConnection();
jdbcConnection = configuration.getDataSource().getConnection();
} else {
jdbcConnection = CommandContextUtil.getDbSqlSession(commandContext).getSqlSession().getConnection();
}
......@@ -74,12 +80,32 @@ public class ContentDbSchemaManager implements DbSchemaManager {
database.setDatabaseChangeLogTableName(ContentEngineConfiguration.LIQUIBASE_CHANGELOG_PREFIX + database.getDatabaseChangeLogTableName());
database.setDatabaseChangeLogLockTableName(ContentEngineConfiguration.LIQUIBASE_CHANGELOG_PREFIX + database.getDatabaseChangeLogLockTableName());
Liquibase liquibase = new Liquibase("org/flowable/form/db/liquibase/flowable-form-db-changelog.xml", new ClassLoaderResourceAccessor(), database);
Liquibase liquibase = new Liquibase(LIQUIBASE_CHANGELOG, new ClassLoaderResourceAccessor(), database);
return liquibase;
} catch (Exception e) {
throw new FlowableException("Error creating liquibase instance", e);
}
}
public void initSchema(ContentEngineConfiguration configuration, String databaseSchemaUpdate) {
try {
Liquibase liquibase = createLiquibaseInstance(configuration);
if (ContentEngineConfiguration.DB_SCHEMA_UPDATE_DROP_CREATE.equals(databaseSchemaUpdate)) {
LOGGER.debug("Dropping and creating schema Content");
liquibase.dropAll();
liquibase.update("content");
} else if (ContentEngineConfiguration.DB_SCHEMA_UPDATE_TRUE.equals(databaseSchemaUpdate)) {
LOGGER.debug("Updating schema Content");
liquibase.update("content");
} else if (ContentEngineConfiguration.DB_SCHEMA_UPDATE_FALSE.equals(databaseSchemaUpdate)) {
LOGGER.debug("Validating schema Content");
liquibase.validate();
}
} catch (Exception e) {
throw new FlowableException("Error initialising Content schema", e);
}
}
}
......@@ -38,6 +38,8 @@ public class DmnDbSchemaManager implements DbSchemaManager {
private static final Logger LOGGER = LoggerFactory.getLogger(DmnDbSchemaManager.class);
public static String LIQUIBASE_CHANGELOG = "org/flowable/dmn/db/liquibase/flowable-dmn-db-changelog.xml";
public void initSchema() {
initSchema(CommandContextUtil.getDmnEngineConfiguration());
}
......@@ -65,7 +67,7 @@ public class DmnDbSchemaManager implements DbSchemaManager {
}
}
protected Liquibase createLiquibaseInstance(DmnEngineConfiguration dmnEngineConfiguration)
public Liquibase createLiquibaseInstance(DmnEngineConfiguration dmnEngineConfiguration)
throws SQLException, DatabaseException, LiquibaseException {
Connection jdbcConnection = null;
......@@ -97,10 +99,10 @@ public class DmnDbSchemaManager implements DbSchemaManager {
database.setLiquibaseCatalogName(databaseCatalog);
}
Liquibase liquibase = new Liquibase("org/flowable/dmn/db/liquibase/flowable-dmn-db-changelog.xml", new ClassLoaderResourceAccessor(), database);
Liquibase liquibase = new Liquibase(LIQUIBASE_CHANGELOG, new ClassLoaderResourceAccessor(), database);
return liquibase;
}
@Override
public void dbSchemaCreate() {
try {
......
......@@ -30,6 +30,8 @@ import liquibase.resource.ClassLoaderResourceAccessor;
public class FormDbSchemaManager implements DbSchemaManager {
public static String LIQUIBASE_CHANGELOG = "org/flowable/form/db/liquibase/flowable-form-db-changelog.xml";
@Override
public void dbSchemaCreate() {
Liquibase liquibase = createLiquibaseInstance();
......@@ -85,7 +87,7 @@ public class FormDbSchemaManager implements DbSchemaManager {
database.setLiquibaseCatalogName(formEngineConfiguration.getDatabaseCatalog());
}
Liquibase liquibase = new Liquibase("org/flowable/form/db/liquibase/flowable-form-db-changelog.xml", new ClassLoaderResourceAccessor(), database);
Liquibase liquibase = new Liquibase(LIQUIBASE_CHANGELOG, new ClassLoaderResourceAccessor(), database);
return liquibase;
} catch (Exception e) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册