Laxmi Salunkhe
Laxmi Salunkhe

Reputation: 509

How to update already run database migration files using Grails DBMigration plugin?

I have migration file named test-plugin-migration-1.2.groovy which nedds to be only renamed as migrations listed in it are already run and some other custom migrations.

test-plugin-migration-1.2.groovy - > test-plugin-migration-1-2.groovy

I tried adding a custom migration for DATABASECHANGELOG table in new file named rename-migration.groovy

rename-migration.groovy

changeSet(author: "Laxmi Salunkhe", id: "12345-1") {
    grailsChange {
        change {
            sql.execute("""update DATABASECHANGELOG set 
                filename='test-plugin-migration-1-2.groovy' where
                filename='test-plugin-migration-1.2.groovy'""")
        }
    }
}

changelog.groovy

databaseChangeLog = {

    // Some Old Migrations

    include file: 'rename-migration.groovy'

    // Previously it was test-plugin-migration-1.2.groovy
    include file: 'test-plugin-migration-1-2.groovy' 

    include file: 'new-plugin-migration.groovy'
}

It still runs renamed file migrations again.

After going through liquibase documentation for update, It explains

Liquibase executes the databaseChangeLog, it reads the changeSets in order and, for each one, checks the “databasechangelog” table to see if the combination of id/author/filepath has been run.

What should I do to avoid renamed files change set getting applied on database?

Upvotes: 1

Views: 1402

Answers (2)

Laxmi Salunkhe
Laxmi Salunkhe

Reputation: 509

I figured out workaround for this problem. I have added one groovy script rename-file.groovy which rename my invalid file names in DATABASECHANGELOG table and then migration run's sucessfully.

rename-file.groovy

import groovy.sql.Sql

@GrabConfig(systemClassLoader = true)
@Grab(group = "mysql", module = "mysql-connector-java", version = "5.1.29")

// Get instance of MYSQL database of old system
Sql sql = Sql.newInstance("jdbc:mysql://localhost:3306/causecode", "root", "sql", "com.mysql.jdbc.Driver")

String oldFileName = "test-plugin-migration-1.2"
String newFileName = oldFileName.replace('.', '_') + '.groovy'
oldFileName = oldFileName + '.groovy'
String query = """
    Update DATABASECHANGELOG
    set filename = "$newFileName", MD5SUM = null
    where filename = "$oldFileName"
"""
sql.executeUpdate query


// Close the connections
sql.close()

and then run migration against database.

Upvotes: 1

Sudhir N
Sudhir N

Reputation: 4096

If you are not making any changes to existing changesets, you can use command dbm-changelog-sync it will mark all changesets as executed so when you run-app again, your changelogs will not execute again and will be treated as they are already applied

See dbm-changelog-sync

Upvotes: 2

Related Questions