-
Notifications
You must be signed in to change notification settings - Fork 0
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 foldersrc/db/schemas
and make sure to replace{new_version}
with the valuen + 1
. - Create a new
DatabaseMigrator
class with the nameDatabaseMigratorV{current_version}ToV{new_version}.ts
and replace{current_version}
with the value ofn
and{new_version}
with the value ofn + 1
. This class should implement theDatabaseMigrator
-interface and is responsible for converting a project that's using schema versionn
to a project with schema versionn + 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 foldersrc/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!