Domanda

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();
    }
}
È stato utile?

Soluzione

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

Altri suggerimenti

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

insert into test_data (value) values ('$' || '{Email}')

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.)

try this properties:

        final var flyway = Flyway.configure()
                .dataSource(DataSourceProvider.getInstanceDataSource(instance.getInstanceId()))
                .locations(MIGRATION_PATH + "instance_db")
                .outOfOrder(true)
                .table("schema_version")
                .validateOnMigrate(false)
                .placeholderReplacement(false)
                .load();

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>...
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top