Reputation: 338876
The Flyway database migration tool includes a feature for placeholder replacement.
What is the purpose of the placeholder? In what kind of real-world practical scenarios might the placeholder be useful? Can you describe some simple examples to demonstrate usefulness?
The example at bottom of this doc page shows the use of a placeholder in SQL. How would we set the value to be inserted in place of that placeholder? Is the source of the placeholder's value defined as Environment Variables, Java variables, a Java .properties
file, or something else?
Does a placeholder hold only plain text, no other data types?
Can a placeholder be used only in an SQL file? Can a placeholder be used in the SQL strings within Java code?
Are there other places where a placeholder can be used?
I found this Question, How do placeholders work in Flyway?, but it does not give me a clear picture.
Upvotes: 1
Views: 2071
Reputation: 17171
Placeholder replacement is handled by PlaceholderReplacer
.
What is the purpose of the placeholder? In what kind of real-world practical scenarios might the placeholder be useful? Can you describe some simple examples to demonstrate usefulness?
Suppose I am a poor startup with a single database and use table prefixes to separate each environment named dev
, test
, and prod
. In my Flyway migrations I could choose to have a placeholder ${env}
for this variable. So now I can use the same migrations on all the environments using the placeholder and supplying a value at runtime.
Suppose I have a migration that creates a read-only user and password in the database. It would be bad practice to have the password in the script, so with placeholders you could pass in the password at runtime.
How would we set the value to be inserted in place of that placeholder?
Calling configure()
on an instance of the Flyway
class will parse placeholder replacements from the passed properties. It does it like so:
Map<String, String> placeholdersFromProps = new HashMap<String, String>(placeholders);
Iterator<Map.Entry<String, String>> iterator = props.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
String propertyName = entry.getKey();
if (propertyName.startsWith(PLACEHOLDERS_PROPERTY_PREFIX)
&& propertyName.length() > PLACEHOLDERS_PROPERTY_PREFIX.length()) {
String placeholderName = propertyName.substring(PLACEHOLDERS_PROPERTY_PREFIX.length());
String placeholderValue = entry.getValue();
placeholdersFromProps.put(placeholderName, placeholderValue);
iterator.remove();
}
}
setPlaceholders(placeholdersFromProps);
If you're using the CLI, check out the configuration section of the docs for guide to configuring Flyway.
Does a placeholder hold only plain text, no other data types?
Only strings. String placeholderValue = entry.getValue()
Can a placeholder be used only in an SQL file? Can a placeholder be used in the SQL strings within Java code?
Yes only a plain SQL file. I know this from inspecting references to PlaceholderReplacer
, but it also makes sense because Java migrations may not contain SQL, and it would require some sort of compiler plugin to work. Besides, Java migrations can do literally anything, so if it wants to use the same placeholders as the SQL scripts it can. Just write Java to load the properties and do string replacement. Hell you can even use PlaceholderReplacer
if you want.
Are there other places where a placeholder can be used?
I didn't find any in my inspection of the Flyway repo.
Upvotes: 5