This repository was archived by the owner on Jul 20, 2026. It is now read-only.
Database Management #23
TheRefraction
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Database Management
The following explain how the database is deployed and managed as we cannot work directly onto a remote database (this costs money...).
When starting the MySQL service, the database is created or opened if the volume
mysql_datahas not been unmounted/destroyed. Then migrations are applied accordingly ; each applied is tracked under theflyway_schema_historytable (do not delete it if you don't know what you're doing). Each change you make that is not put into a migration, is solely local. Keep that in mind when unmounting volumes.Migrations
Migrations are defined as the movement of data contained in a given database (for instance, schema objects, procedures, triggers, ...) from the latter to a new or updated database. In Docker, the Flyway service has already been configured and takes care of that. Migrations are applied when using the
managescript with themigrateparameter.File structure
Each migration should be placed under
db/migrations. Those migrations are committed to Git as they should be.Writing migrations is pretty straightforward: just create a
.sqlfile containing the exact SQL statements needed to evolve the database from one version to the next.This part is very important as if you do not follow the rules, Flyway will fail to register and apply migrations.
Versioned migrations
Each is applied exactly once, in version order. They should follow the following naming convention:
V<version>__<description>.sql. Please note that this name is case-sensitive. Also, there should be exactly two underscores as the separator. The version string is composed of numbers with underscores (for example1,1_2or2_0_1). The description string is pretty explicit although any space should be replaced by an underscore.A simple example:
Repeatable migrations
Used for views and procedures. They are applied every time the file changes. They follow the following naming convention
R__<description>.sql.Best practices
The following should also be applied to prevent having any kind of issue.
Immutable migrations
Warning
Never, ever modify a versioned migration script that has already been committed to version control, and thus applied to any shared environment.
Flyway tracks a checksum of each applied migration. Therefore if you modify a script after it's been applied, Flyway will fail to deploy any subsequent migration and return a checksum mismatch.
Therefore if you need to edit a migration script (for a typo for instance) or undo it, and it has already been committed, you'll need to write another migration that counteracts the first one.
Idempotency
Warning
Write migrations that can be safely run multiple times without causing errors or duplicate data.
During development, migrations might need to run multiple times for any reason (testing, disaster recovery, ...). Whence why each SQL migration should idempotent.
For instance, let's consider the following example:
First it may fail creating the table
settingsif it already exists. Then, the insertion can duplicate data on re-run.Finally, here is a better version:
Atomic migrations
Warning
Each migration should represent exactly one logical change to the database schema.
This is important for many reasons, one of which is better "debugging" and code review. Also it is easier to rollback if a migration fails: only one element will be lost.
However, there might multiple changes that are linked (for example, creating a trigger and its trigger function). In this case, it is completely fine to only write one migration.
Handle data migrations
Warning
Always separate schema changes from data migrations
File presentation
Warning
Always include a header with the name of the file as well as a description. Do include rollback logic in comments as well as comments when not trivial.
Testing migrations
In order to avoid having to troubleshoot your migration because Flyway failed, you should do the following to ensure data integrity:
exquisite_potassiumIt is easier to manage such a database than having to unmount and rebuild the containers every time, because the migration screwed up...
Troubleshooting
Flyway failed to apply the migration
This might be because of a SQL syntax error, or non-idempotency issue (trying to add a column that already exists). In this case, even though the script is transactional, MySQL will implicitely commit any changes to the database until the syntax error. This is why it's important not to have a huge migration, as it will be difficult to rollback.
You have several options:
Afterwards make sure to fix properly your migration, run the
managescript with therepairoption, and then migrate again!Migrations have failed validation
If you get that kind of message:
You need to run the
managescript with therepairoption. Then you might run your migration.Backups
Running the
managescript withbackupas the parameter will dump the current database underdb/backupsas a SQL file that can be imported later in PHPMyAdmin. Those files can be committed with no problem.All reactions