Product articles Redgate Flyway Tracking development changes
Finding Significant Differences in…

Finding Significant Differences in Flyway SQL Migration Code

This article demonstrates using PowerShell-based tokenization to compare two SQL migration files. It ignores non-functional changes like comments or formatting and pinpoints the first meaningful change in SQL logic, providing detailed feedback on its location and nature.

Guest post

This is a guest post from Phil Factor. Phil Factor (real name withheld to protect the guilty), aka Database Mole, has 30 years of experience with database-intensive applications.

Despite having once been shouted at by a furious Bill Gates at an exhibition in the early 1980s, he has remained resolutely anonymous throughout his career.

He is a regular contributor to Simple Talk and SQLServerCentral.

What caused the Flyway validation error?

A Flyway migration will fail with a validation error if one of the files has been altered since it was applied to the database. Flyway issues these ‘warnings’ because retrospective file changes can be problematic for several reasons. They can cause version inconsistency, where two copies of the same database, with the same Flyway schema version, are functionally different. Even minor changes intended to improve performance rather than functionally alter the code can have unintended consequences. They can cause errors in subsequent unaltered migration scripts that ran successfully, and they can cause difficulties when database branches are merged.

There are, on the other hand, some good and harmless changes you can make to a migration that has been successfully applied that will also cause Flyway validation errors. The formatting of the SQL may need to be changed to conform to team standards and to promote readability. The migration code is also the only sensible place to put the block comments and the end-of-line comments that explain the code. These ‘comments’ can always be improved, because one often needs to wade through migration code to determine what the developer intended to do.

When we’re responding to a validation error, scanning through code to work out what changed, we need an easy way to identify functional changes amidst formatting and documentation improvements. My previous article, Defusing Flyway Validation Errors using Smarter Checksum Comparisons used PowerShell-based tokenization to calculate a checksum of “just the code”, after excluding tokens for whitespace, formatting and comments. If a Flyway validation error was caused by retrospective cosmetic changes, developers could simply run Flyway Repair and move on, with minimal disruption to development and testing.

However, if the changes are not just cosmetic but rather edits to the SQL logic, then we can’t just “run repair and move on“. We need to investigate exactly what changed and why, so we can decide how best to handle it. This article builds on the concepts from my previous article. We use the same tokenization method but instead of just calculating a checksum, we compare the stream of code-only tokens (such as ‘select‘,’*‘, ‘from‘, ‘MySchema‘, ‘.‘, ‘MyTable‘), one by one, to identify exactly where in a script the first of the functional changes were made.

How does Flyway do the validation check?

When Flyway does a migration or executes the Validate command, it will access the database to read the Flyway Schema History table in the database you’ve specified (see The Flyway Migrate Command Explained Simply). It ascertains from it the current schema version of this database. It then it reads and sorts the migration files and validates any that were already applied. To do this, it uses the checksums of these files. The table stored the checksums that were calculated when the file was successfully applied, and so it then accesses each file, recalculating these checksums and compares the new checksum with the original to make sure no aspect of the previously applied migration sequence has been subsequently changed: no files have been subsequently altered, no new files injected into the previous sequence (see Flyway’s Validate Command Explained Simply).

When a Flyway raises a ‘checksum mismatch’ validation error, you can just smile indulgently and run my Get-SQLCodeHash function to see if the changes are purely decorative. It calculates a “metadata only” checksum for the ‘before’ and ‘after’ code. If they match, the changes are most likely a kind-hearted and conscientious reformatting or commenting of the file, and you can go ahead with a flyway repair. This allows Flyway to recalculate the stored checksum in the database’s flyway schema history table so that normal migrations can proceed.

If, however, the tokenized checksums still don’t match, then there is a change in the SQL, and you need to investigate. This is where this article comes in.

Why not just use source control and a differencing tool?

You can do very little beyond shrugging or looking bewildered when faced with a Flyway validation failure if you don’t have a copy of the version of the file that was used for the successful migration. We will assume that you do have, in your source control system, a copy of the previous version of the migration file, so why not use its built-in file comparison tool, or a specialist tool like Beyond Compare, to see what changed?

The problem is that this sort of comparison is purely text-based rather than semantic, so you are back to a process that can produce plenty of false positives, especially with reformatting and changed documentation. A file that has been commented, formatted and documented will be wildly different textually, yet contain the same SQL. See that sea of red? It is distracted by cosmetic changes. The SQL hasn’t changed, but it’ll take a while to detect that.

a sea of red

The script I’ll provide in this article, by contrast, does SQL-specific analysis. It does a token-by-token comparison highlighting differences in keywords, operators, or structure, while ignoring any changes due to comments or whitespace. It alerts you to the first semantic difference.

C’mon: show us the code!

OK! All the scripts are on GitHub in my Flyway Teamwork repository, which I know you all read. The Compare-SQLTokens cmdlet processes the original (source) and changed (target) code using a regex ($parserRegex) to extract the text of all the SQL tokens by category, such as BlockComment, EndOfLineComment, String, Identifier, Operator, and so on. Cosmetic categories of tokens such as comments are then filtered out leaving just the text of the meaningful tokens.

It uses the same tokenizer as used by the Get-SQLCodeHash cmdlet from the previous article. However, where that function just calculated and compared checksums for the ‘metadata only’ code, Compare-SQLTokens extracts the tokens from the source and target SQL files by comparing each token in sequence. The first time the token in the source doesn’t match the corresponding token in the target, the function calculates the exact line number and column where the difference occurs. It also retrieves nearby tokens for context.

Trying it out: detecting where a SQL change occurred

It is sometimes very useful to detect a SQL change in a file, using a script like this, before applying a migration, especially when working within a development team. Based on the result, you can go straight to the workflow that can approve the change via Flyway repair, or redo all migrations via Flyway clean, or condemn the change entirely without attempting a migration.

Together with Get-SQLCodeHash, the intent is to make it easier for developers to manage justifiable changes in Flyway migration scripts, by giving them notifications instead of nasty surprises within team-based development.

finding significant SQL difference in two versions of a migration file

Comparing SQL Expressions

First, we’ll use Compare-SQLTokens to compare two SQL Expressions that, apart from formatting differences, contain just one small change to the Stocklevel logic.

It picks out the change immediately with a warning:

WARNING: At line 4, column 49: 'then stocklevel + 4' - '4' is different to '5' in the code "'...0 THEN Stocklevel + 4 
      ...".No further search attempted..

Comparing Flyway migration files

Now let’s try it out on ‘before’ and ‘after’ versions of the same Flyway migration file:

Nothing returned means that there are only cosmetic differences, which are whitespace changes in this case. So, let’s sneakily make a single, subtle change. Nothing drastic or easily detectable:

retrospective change to a Flyway migration

We’ll change that 7 to a 6 (heh heh). That is a change that could be embarrassing if not spotted. Now we rerun the comparison.

Yes, it picks it up and gives us a warning:

WARNING: At line 40, column 45: 'not null constraint [df__employee__hire_d__29572725]' - '[df__employee__hire_d__29572725]' is diff
erent to '[df__employee__hire_d__29562725]' in the code "'...NOT NULL CONSTRAINT [DF__emplo...".No further search attempted..

Just make that into a ‘write-error’ and it can be made to stop a process dead. By comparison, an excellent differencing utility like Beyond Compare detects it too, but it’s much harder to spot amongst the sea of red caused by formatting changes. Can you run this check as part of a scripted process?

beyond compare

Conclusions

It’s nice to find plenty of uses for a script. As well as helping to find differences in SQL in Flyway migration files, this code is more generally good at ensuring that, when annotating and formatting someone else’s code, one doesn’t accidentally change the actual code. This can be particularly embarrassing when it causes the code to generate an error, but the subtle changes can cause a lot of head-scratching otherwise.

Obviously, this code can be improved and made more generic, but that will take more resources than I have. One could improve it to the point where it would find more than the first difference (a task that can become all-consuming) but that misses the point that a single accidental difference is enough to make a big problem for database development.

 

Tools in this post

Redgate Flyway

DevOps for the Database

Find out more