How to ignore placeholder expressions for Flyway?

不想你离开。 提交于 2020-07-20 07:33:20

问题


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();
    }
}

回答1:


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




回答2:


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

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



回答3:


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();



回答4:


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




回答5:


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



回答6:


Just add a property to your bootstrap.properties (or whatever you use)

flyway.placeholder-replacement = false


来源:https://stackoverflow.com/questions/22045108/how-to-ignore-placeholder-expressions-for-flyway

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!