Skip to content

Updating database schema

Pieter Verschaffelt edited this page Mar 8, 2021 · 1 revision

Sometimes changes have to be made to the desktop app's database schema. Every version of the database schema has a specific version identifier that should be incremented every time a change is made to the database schema. In order to make sure that projects that were created with older versions of the application keep working with the most recent schema, a DatabaseMigrator is present. Once loaded, the migrator will check the current schema version of a project and migrate the project to the most recent version (if required).

Now, if you decide to update the database schema, you need to follow these steps to make sure that old projects can still be opened by the latest version of our application:

  • Take note of the most recent version number of the database. Assume that is n in this example.
  • Create a new schema definition file schema_v{new_version}.sql in the folder src/db/schemas and make sure to replace {new_version} with the value n + 1.
  • Create a new DatabaseMigrator class with the name DatabaseMigratorV{current_version}ToV{new_version}.ts and replace {current_version} with the value of n and {new_version} with the value of n + 1. This class should implement the DatabaseMigrator-interface and is responsible for converting a project that's using schema version n to a project with schema version n + 1.
  • In most cases, a SQL-migration should be created to accomplish this goal. Store this migration in a file with the name v{current_version}_to_v{new_version}.sql in the folder src/db/migrations.
  • Once done, increment the schema version number in the file src/filesystem/database/Schema.ts. This reference is used to keep track of which database version should be used by the application. Make sure not to forget this!
Clone this wiki locally