private String repeatableMigrationName(String scriptName) { return "R__" + scriptName.replace(' ', '_') + migrationConfig.getApplySuffix(); }
/** * Return true if the next pending drop changeSet should be generated as the next migration. */ private String generatePendingDrop() { String nextDrop = System.getProperty("ddl.migration.pendingDropsFor"); if (nextDrop != null) { return nextDrop; } return migrationConfig.getGeneratePendingDrop(); }
/** * Return the file path to write the xml and sql to. */ protected File getMigrationDirectory() { // path to src/main/resources in typical maven project File resourceRootDir = new File(pathToResources); String resourcePath = migrationConfig.getMigrationPath(); // expect to be a path to something like - src/main/resources/dbmigration/model File path = new File(resourceRootDir, resourcePath); if (!path.exists()) { if (!path.mkdirs()) { logger.debug("Unable to ensure migration directory exists at {}", path.getAbsolutePath()); } } return path; }
/** * Write the migration xml. */ protected boolean writeMigrationXml(Migration dbMigration, File resourcePath, String fullVersion) { String modelFile = fullVersion + migrationConfig.getModelSuffix(); File file = new File(resourcePath, modelFile); if (file.exists()) { return false; } String comment = migrationConfig.isIncludeGeneratedFileComment() ? GENERATED_COMMENT : null; MigrationXmlWriter xmlWriter = new MigrationXmlWriter(comment); xmlWriter.write(dbMigration, file); return true; }
/** * Return the full version for the migration being generated. * <p> * The full version can contain a comment suffix after a "__" double underscore. */ private String getFullVersion(MigrationModel migrationModel, String dropsFor) { String version = migrationConfig.getVersion(); if (version == null) { version = migrationModel.getNextVersion(initialVersion); } String fullVersion = migrationConfig.getApplyPrefix() + version; if (migrationConfig.getName() != null) { fullVersion += "__" + toUnderScore(migrationConfig.getName()); } else if (dropsFor != null) { fullVersion += "__" + toUnderScore("dropsFor_" + MigrationVersion.trim(dropsFor)); } else if (version.equals(initialVersion)) { fullVersion += "__initial"; } return fullVersion; }
/** * Run the DB migration against the DataSource. */ public DataSource runDbMigration(DataSource dataSource) { if (migrationConfig.isRunMigration()) { MigrationRunner runner = migrationConfig.createRunner(getClassLoadConfig().getClassLoader()); runner.run(dataSource); } return dataSource; }
/** * Create the MigrationRunner to run migrations if necessary. */ public MigrationRunner createRunner(ClassLoader classLoader) { MigrationConfig runnerConfig = new MigrationConfig(); runnerConfig.setMetaTable(metaTable); runnerConfig.setApplySuffix(applySuffix); runnerConfig.setMigrationPath(migrationPath); runnerConfig.setRunPlaceholderMap(runPlaceholderMap); runnerConfig.setRunPlaceholders(runPlaceholders); runnerConfig.setDbUsername(getDbUsername()); runnerConfig.setDbPassword(getDbPassword()); runnerConfig.setClassLoader(classLoader); return new MigrationRunner(runnerConfig); } }
/** * Return the model directory (relative to the migration directory). */ protected File getModelDirectory(File migrationDirectory) { String modelPath = migrationConfig.getModelPath(); if (modelPath == null || modelPath.isEmpty()) { return migrationDirectory; } File modelDir = new File(migrationDirectory, migrationConfig.getModelPath()); if (!modelDir.exists() && !modelDir.mkdirs()) { logger.debug("Unable to ensure migration model directory exists at {}", modelDir.getAbsolutePath()); } return modelDir; }
private Request() { this.migrationDir = getMigrationDirectory(); this.modelDir = getModelDirectory(migrationDir); this.migrationModel = new MigrationModel(modelDir, migrationConfig.getModelSuffix()); this.migrated = migrationModel.read(); this.currentModel = new CurrentModel(server, constraintNaming); this.current = currentModel.read(); }
/** * Write the ddl files. */ protected void writePlatformDdl(DdlWrite write, File resourcePath, String fullVersion) throws IOException { if (!write.isApplyEmpty()) { FileWriter applyWriter = createWriter(resourcePath, fullVersion, config.getApplySuffix()); try { writeApplyDdl(applyWriter, write); applyWriter.flush(); } finally { applyWriter.close(); } } }