How to create script or Flyway can be configured to call it every time using a SQL callback? - flyway

How to create script or Flyway can be configured to call it every time using a SQL callback ??

flyway has already a lot of callbacks depending when you want your script to run, for exemple if you want your script to run after each successfull migration you can create a file named:
afterMigrate__my_script_to_run_after_each_migration.sql
Flyway will automatically call that script after the migration completes successfully.
You can find the list of flyway callbacks here, note that some of them are limited to the Flyway teams edition.
https://flywaydb.org/documentation/concepts/callbacks

Related

Flyway init new db

I have existing database postgres which not using Flyway and i need replicate it.
How to move existing database state to new empty database?
I don't have any migration sql.
So I am expecting command like generateChangelog in Liquibase but it seems in Flyway not existing command like that.
Flyway currently only manages the scripts that you create. It doesn't create scripts for you. So, in order to take an existing database and get it into Flyway processing, you'll need to generate the scripts for that database. You can use the methods outlined here to get the scripts for your database. Then, just rename them to the Flyway standard. You'll be off and running.

after migrate sql runs after reboot of the application with no migration performed

I'm using Flyway 6.5.7 and I have a afterMigrate.sql that truncates a table. I thought the script would run only if a migration occurred AND if the migration is successful. But it always runs (for example after restart of the application)
Schema "A_SCHEMA" is up to date. No migration necessary.
Parsing afterMigrate.sql ...
Executing SQL callback: afterMigrate -
Is it correct? If so, is there a way to achieve the described behavior?
Note: org.flywaydb.core.Flyway#migrate returns 0 after restart of the application with no migration
Thanks
The way it works is the afterMigrate callback runs after any migration that didn't have an error. A successful migration is defined that way. So, in your situation, you called the migrate command. It ran successfully. It didn't migrate anything. However, it ran without an error, so the afterMigrate callback ran.
There is also the afterEachMigrate event which you can attach a callback to. This runs after each successful execution of a migration script; it doesn't run if migrate finds nothing to do, but on the downside it will run more than once if migrate finds multiple scripts that it needs to run. Depending on how often you push out new scripts, this might be a better solution for you.

Flyway - Does it keep track of schema and object dependency

I have installed Flyway Software and trying to deploy code. I have a Scenario
I created a file V1_01_CREATE_TABLE.SQL to create a table.
Created a file V1_01_CREATE_PACKAGE.SQL - This package will hold code to insert a row into one of the columns in table created in step 1.
Created a file V1_01_02_ALTER_TABLE.SQL - This SQL will rename the column which was being referred in step2.
This will invalidate the package if I run 1, 2 & 3. How does FLYWAY handle such a situation? Does it understand object dependency?
As explained in the manual, Flyway simply passes your SQL scripts to the database to be executed, and records the success or failure of their execution.
Flyway has no interest or understanding of the content of your scripts. Flyway never looks at the content of those scripts. In that sense, there is no “intelligence” in Flyway.
Flyway is like the postal worker delivering your letters without opening the envelope. You are the author, and you take full responsibility for the logic and correctness of the SQL scripts. You are responsible for following the naming conventions so your scripts run in the correct order.
After initially creating its metadata table, Flyway makes very limited use of JDBC and SQL. Flyway does little more than make a connection to the database server, determine which of the scripts have yet to be run, and say to the database “Here, run this script, and this script, and then run this script.” while recording the success or failure of each run.
You should name your scripts in order they needs to be executed
V1.0.0_CREATE_TABLE.SQL
V1.0.1_CREATE_PACKAGE.SQL
V1.0.2_ALTER_TABLE.SQL
This will execute scripts in migrated

Flyway: how to execute a function after each migration

I'm working on a project where we use flyway to upgrade our DB schema.
I'm using flyway through the gradle plugin.
I need to execute a specific function every time a migration ends: we don't want to add the call to every migration manually.
The script will be exactly the same every time, so repeatable migration is a no go (it will be executed again only when the hash of the script changes).
I've tried to use sql callbacks, but they seem not working properly with the gradle plugin: if I run flyway on command line the callback sql is executed correctly (all migrations and callback are in sql dir within flyway distro) while it's not executed in gradle (migrations and callbacks are in directory src/main/db/migration set as filesystem:src/main/db/migration in flyway gradle configuration).
Can anyone help with the correct gradle plugin configuration for flyway or maybe suggest an alternative solution?
Many thanks
Use the afterMigrate callback. See https://flywaydb.org/documentation/callbacks

Populate SQLite only once upon installation of UWP app

I am developing UWP application using vs studio 2017 version 15.9.6.
I want to use Windows local SQLite database. I want to run an SQL script named mySql.txt when the user first time install the application. I dont want to run it every time when the user run the app as it contain insert statement, which will cause duplicate rows insertion. So I want to run that script only once, preferably in the installation time.
How can I do that? I am very new to UWP and .NET. Please guide me step-by-step if possible.
You can make sure the initialization/seeding is done only once for the app. For that you may utilize ApplicationDate.Current.LocalSettings.
These allow you to write simple data for your application which are bound to your app. Once the user uninstalls the app, these data will be removed as well. This fits your scenario exactly.
Suppose your database initialization code is in the method InitializeDb(). You could use the following to make sure the initialization is done only once:
if (!ApplicationData.Current.LocalSettings.Values.ContainsKey("DbInitialized"))
{
InitializeDb();
ApplicationData.Current.LocalSettings.Values["DbInitialized"] = true;
}
This code first checks if we have initialized the db previously and if not, performs the initialization and stores a flag into app settings to make sure the next time the initialization is skipped.
You can run this code during app initialization, for example in OnLaunched method, or on when the database service is first required.
This is of course the simplest implementation, so you can (and should) add some exception handling, so that if the initialization fails, it can be retried and so on. Also you may want to handle app updates and DB updates - in which case you can use ApplicationData.Current.Version which allows you to track the version of application data and can be used to keep track of DB version as well so you can perform appropriate migrations between versions.
Finally, for even better user convenience, there is also a way to perform the app update steps during updates. See this article for more info.

Resources