Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore placeholder expressions for Flyway?

Tags:

java

mysql

flyway

I am using flyway version 2.3, I have an sql patch which inserts a varchar into a table having character sequence that Flyway treats as placeholders. I want to flyway to ignore placeholders and run the script as is.

The script file is

insert into test_data (value) values ("${Email}");

And the Java code is

package foobar;

import com.googlecode.flyway.core.Flyway;

public class App 
{
    public static void main( String[] args )
    {
        // Create the Flyway instance
        Flyway flyway = new Flyway();

        // Point it to the database
        flyway.setDataSource("jdbc:mysql://localhost:3306/flywaytest", "alpha", "beta");

        // Start the migration
        flyway.migrate();
    }
}
like image 417
user3356319 Avatar asked Feb 26 '14 14:02

user3356319


5 Answers

This can be done by splitting $ and { in the expression:

insert into test_data (value) values ('$' || '{Email}')
like image 195
Bordi Avatar answered Oct 06 '22 17:10

Bordi


In my MySQL migration script this worked:

I just escaped the first { characters, like this:

'...<p>\nProgram name: $\{programName}<br />\nStart of studies: $\{startOfStudies}<br />\n($\{semesterNote})\n</p>...'

This way flyway didn't recognize them as placeholders, and the string finally stored doesn't contain the escape character.

...<p>
Program name: ${programName}<br />
Start of studies: ${startOfStudies}<br />
(${semesterNote})
</p>...
like image 40
dtarnok Avatar answered Oct 06 '22 18:10

dtarnok


try this properties:

        final var flyway = Flyway.configure()
                .dataSource(DataSourceProvider.getInstanceDataSource())
                .locations("path")
                .outOfOrder(true)
                .validateOnMigrate(false)
                .placeholderReplacement(false)
                .load();
like image 25
Chânhéng Seang Avatar answered Oct 06 '22 17:10

Chânhéng Seang


You can change the value of the placeholder suffix or prefix to a different value and you should be OK.

like image 39
Axel Fontaine Avatar answered Oct 06 '22 18:10

Axel Fontaine


I had exactly the same problem, but the accepted answer didn't fit my requirements. So I solved the problem in another way and post this answer hoping that it'll be useful to other people coming here from Google search.

If you cannot change the placeholder suffix and prefix, you can trick Flyway into believing there are no placeholders by using an expression. E.g.:

INSERT INTO test_data(value) VALUES (REPLACE("#{Email}", "#{", "${"));

This is useful if you've already used placeholders in lots of previous migrations. (If you just change placeholder suffix and prefix, you'll have to change them in previous migration scripts, too. But then the migration script checksums won't match, Flyway will rightfully complain, and you'll have to change checksums in the schema_version table by calling Flyway#repair() or manually altering the table.)

like image 1
nvamelichev Avatar answered Oct 06 '22 17:10

nvamelichev