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

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.

Related

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

How can a Symfony web app itself detect unapplied Doctrine Migrations?

The normal way of dealing with Doctrine Migrations is via the standard Commands - during development one runs the commands manually to e.g. run diffs and apply the migrations, and deployment typically involves applying them the by the same approach but automatically. Occasionally when working in a team on a local instance there are new migrations, but I've updated my source from version control rather than done a deployment, so I need to apply the new migrations manually, and I need to know that I need to do that! An improvement could be to display a warning on a rendered webpage that migrations are out of sync and action needs to be taken.
Is there a way to access the Migrations API directly in PHP/Symfony code, so that I could detect a mismatch between committed and applied migrations? I haven't found any documentation about that. I've had an initial poke around the code and it seems heavily skewed towards Commands (reasonably enough).
Firstly, updating your source code from version control, is a deployment too, and applying Doctrine Migrations should be part of that. You should create a check list of all the steps you need to do during a deployment, including rollbacks. Depending on the complexity of the application, many things could go wrong.
To answer you question, you can execute, in your code, a diff migration with the Process component and parse the output to determine if there're migrations to be applied.

EF Core running migrations from asp.net code throws exception

I am using EF core for the first time in my asp.net WebApi application (REST services),m using the SQLite provider. I have installed the Nuget package v2.0.1.
I have added my context , models etc, and everything seems to be working. I now wanted to try using Migrations. Following the documentation I have added my Migration, and all the expected files seem to have been created.
To test, I delete my SQLite database file and run the application,
however, when I call the `Migrate method in code, I get the following exception...
The weird thing is, when I close the debugger, the file and table is actually created. The migration table is present, but has no rows.
If I start again, ie delete the database, and then just run the migration from the Package Manager Console, then all works fine, ie I see...
PM> Update-Database
Applying migration '20180301030031_InitialCreate'.
Done.
PM>
an also the database is create, and this time the Migration table has the row of data I expect..
Any one have any ideas why this doesn't work when I just run it in code?
Thanks in advance for any help
[EDIT 1]
I get the same problem if I add a new Migration after just added a field. The first time I run, I gt the exception. If I then run again, it works!
I was considering just deleting this post, as the issue seemed to magically resolved itself.
I ran the same code (on the same database file) in a sample WPF application, and had no problems, so it seemed to be when running in the IIS process (and after doing iiresets to force the application to rerun the initialisation code which would then do the call to context.Database.Migrate();
At any rate, it now seems to work (but have no idea why it started working)

Using aotimport on server startup

I'm trying to set up one of my AX environments to have an XPO imported whenever the server process is started up. Because the environment is updated from production on a regular basis, and the code needs to be unique to this (non-production) environment, it seems the best option would be to use the AOTImport command on startup. However, I'm having trouble identifying the exact syntax/setup to get this to work.
Looking through the system code, it seems the syntax should be aotimport_[path to file]. Is this correct? The server does not seem to complain about this command, but the file in question does not get imported. I have also tried variations on this command, but have not seen it work.
I supose you're trying to execute a command on SysStartupCmd classes. If so, this methods are fired when AX client starts, not the AOS. It's docummented on this page:
http://msdn.microsoft.com/en-us/library/aa569641(v=ax.50).aspx
If you want to automate this import it can be done scheduling an execution of the AX client (ax32.exe) on your build workflow that run the import (it's recommended to run a full compilation after importing). This is discussed on other questions here on SO.

WF4 versioning - how do I cancel / terminate an invalid instance?

I've got a state machine workflow implemented using WorkflowFoundation 4.1. I'm using the SQL Server persistence store and the WorkflowApplication class for loading and running workflows.
I'm making changes to the state machine workflow model, and am finding that existing instances break very easily. I've written code that can replay the workflow back into the correct state, which is basically a migration, which works fine, however I need to be able to clear out the old workflow instance as well.
The main issue is that if the workflow is invalid, I can't even load it, so I can't terminate or cancel it either.
Is there a way to use the workflow API to remove a workflow without loading it (ie, some command on the SqlPersistenceStore), or do I have to clean the database manually?
The SqlWorkflowInstanceStore doesn't allow you to do so directly. You will need to go into the database and delete the record there. If you are using AppFabric there is actually a command to delete a workflow instance without loading it first for just that purpose. There should be a PowerShell command to do that using code.

Resources