From 6520fdd156030bc3cfaeea69edf4ec1411305fdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Tvrzni=CC=81k?= Date: Tue, 30 May 2017 14:33:40 +0200 Subject: [PATCH] added config property for migration of all data sources. Useful for Grails Multitenancy with Multiple Databases (same db schema). --- src/docs/asciidoc/configuration.adoc | 7 +++++++ .../DatabaseMigrationGrailsPlugin.groovy | 20 +++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/docs/asciidoc/configuration.adoc b/src/docs/asciidoc/configuration.adoc index 18e0db85..a63b1a98 100644 --- a/src/docs/asciidoc/configuration.adoc +++ b/src/docs/asciidoc/configuration.adoc @@ -16,6 +16,7 @@ There are a few configuration options for the plugin. All configurations are pre |updateOnStartFileName |none |the file name (relative to `changelogLocation`) to run at startup if `updateOnStart` is `true` |updateOnStartDefaultSchema |none |the default schema to use when running auto-migrate on start |updateOnStartContexts |none |A comma-delimited list of http://www.liquibase.org/manual/contexts[context] names. If specified, only changesets tagged with one of the context names will be run +|updateAllOnStart |false |if `true` then changesets from the specified list of names will be run at startup for all dataSources. Useful for Grails Multitenancy with Multiple Databases (same db schema) |autoMigrateScripts |['RunApp'] |the scripts when running auto-migrate. Useful to run auto-migrate during test phase with: ['RunApp', 'TestApp'] |excludeObjects |none |A comma-delimited list of database object names to ignore while performing a dbm-gorm-diff or dbm-generate-gorm-changelog |includeObjects |none |A comma-delimited list of database object names to look for while performing a dbm-gorm-diff or dbm-generate-gorm-changelog @@ -45,3 +46,9 @@ The configuration for this data source would be: grails.plugin.databasemigration.reports.updateOnStart = true grails.plugin.databasemigration.reports.changelogFileName = changelog-reports.groovy ---- +The configuration for all data sources with same db schema would be: +[source,groovy] +---- +grails.plugin.databasemigration.updateAllOnStart = true +grails.plugin.databasemigration.changelogFileName = changelog.groovy +---- diff --git a/src/main/groovy/org/grails/plugins/databasemigration/DatabaseMigrationGrailsPlugin.groovy b/src/main/groovy/org/grails/plugins/databasemigration/DatabaseMigrationGrailsPlugin.groovy index 935018dd..6e7a4dd7 100644 --- a/src/main/groovy/org/grails/plugins/databasemigration/DatabaseMigrationGrailsPlugin.groovy +++ b/src/main/groovy/org/grails/plugins/databasemigration/DatabaseMigrationGrailsPlugin.groovy @@ -28,6 +28,9 @@ import org.springframework.context.ApplicationContext import javax.sql.DataSource class DatabaseMigrationGrailsPlugin extends Plugin { + + static final String CONFIG_MAIN_PREFIX = 'grails.plugin.databasemigration' + def grailsVersion = "3.0.0.BUILD-SNAPSHOT > *" def pluginExcludes = [ "**/testapp/**", @@ -40,7 +43,7 @@ class DatabaseMigrationGrailsPlugin extends Plugin { def description = 'Grails Database Migration Plugin' def documentation = "http://grails.org/plugin/database-migration" def license = "APACHE" - def scm = [url: "http://svn.codehaus.org/grails-plugins/"] + def scm = [url: "https://github.com/grails-plugins/grails-database-migration"] @Override Closure doWithSpring() { @@ -52,17 +55,22 @@ class DatabaseMigrationGrailsPlugin extends Plugin { void doWithApplicationContext() { def mainClassName = deduceApplicationMainClassName() - dataSourceNames.each { String dataSourceName -> - String configPrefix = isDefaultDataSource(dataSourceName) ? 'grails.plugin.databasemigration' : "grails.plugin.databasemigration.${dataSourceName}" + def updateAllOnStart = config.getProperty("${CONFIG_MAIN_PREFIX}.updateAllOnStart", Boolean, false) + dataSourceNames.each { String dataSourceName -> + String configPrefix = isDefaultDataSource(dataSourceName) ? CONFIG_MAIN_PREFIX : "${CONFIG_MAIN_PREFIX}.${dataSourceName}" def skipMainClasses = config.getProperty("${configPrefix}.skipUpdateOnStartMainClasses", List, ['grails.ui.command.GrailsApplicationContextCommandRunner']) if (skipMainClasses.contains(mainClassName)) { return } - def updateOnStart = config.getProperty("${configPrefix}.updateOnStart", Boolean, false) - if (!updateOnStart) { - return + if(!updateAllOnStart) { + def updateOnStart = config.getProperty("${configPrefix}.updateOnStart", Boolean, false) + if (!updateOnStart) { + return + } + } else { + configPrefix = CONFIG_MAIN_PREFIX } new DatabaseMigrationTransactionManager(applicationContext, dataSourceName).withTransaction {