Skip to content

Commit

Permalink
added config property for migration of all data sources. Useful for G…
Browse files Browse the repository at this point in the history
…rails Multitenancy with Multiple Databases (same db schema).
  • Loading branch information
tvrznikactis authored and puneetbehl committed Aug 27, 2018
1 parent 75fb112 commit 6520fdd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/docs/asciidoc/configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
----
Original file line number Diff line number Diff line change
Expand Up @@ -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/**",
Expand All @@ -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() {
Expand All @@ -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 {
Expand Down

0 comments on commit 6520fdd

Please sign in to comment.