Håkon Pettersen
06/03/2023, 2:32 PMDatabase.Schema.migrate(
driver = driver,
oldVersion = 0,
newVersion = Database.Schema.version,
)
I assume that this won't work if the user is currently on version 2, and I am trying to migrate to version 3. I assume that I have to check which version of the database the user is currently on and only execute migration if it is lower than the latest version. If that's the case, how can I check the current database-verison that the user is on? (Also, If this is the wrong way to handle migrations, please elaborate).Matt Nelson
06/03/2023, 5:58 PMMichael Paus
06/03/2023, 6:36 PMDatabase.Schema.migrate(databaseDriver, config.localDatabaseVersion, Database.Schema.version)
Config is read from a configuration file which I update after the migration.Håkon Pettersen
06/03/2023, 6:37 PMMichael Paus
06/03/2023, 6:38 PMHåkon Pettersen
06/03/2023, 6:44 PMMichael Paus
06/03/2023, 7:49 PMHåkon Pettersen
06/03/2023, 7:51 PMJhaman das
06/05/2023, 12:38 PMJhaman das
06/05/2023, 12:39 PMHåkon Pettersen
06/06/2023, 12:29 PMMichael Paus
06/06/2023, 12:57 PMHåkon Pettersen
06/06/2023, 1:01 PMCREATE TABLE hockeyPlayer (
player_number INTEGER PRIMARY KEY NOT NULL,
full_name TEXT NOT NULL,
birthYear INTEGER NOT NULL
);
Add new field:
CREATE TABLE hockeyPlayer (
player_number INTEGER PRIMARY KEY NOT NULL,
full_name TEXT NOT NULL,
birthYear INTEGER NOT NULL,
birthMonth INTEGER NOT NULL
);
Add migration file 1.sqm
ALTER TABLE hockeyPlayer ADD COLUMN birthMonth INTEGER NOT NULL DEFAULT 12;
Create a new table:
CREATE TABLE hockeyTeam (
team_id INTEGER PRIMARY KEY NOT NULL,
team_Name TEXT NOT NULL
);
Add migration file 2.sqm:
CREATE TABLE hockeyTeam (
team_id INTEGER PRIMARY KEY NOT NULL,
team_Name TEXT NOT NULL
);
Verify migration: if schemaOutputDirectory.set(file("src/main/sqldelight/databases"))
is defined in build.gradle you can run ./gradlew generateDebugDatabaseSchema
to generate a .db
file. Once the .db
file has been generated you can run task ./gradlew verifySqlDelightMigration
that will verify that the migration is working as expected.
Image showing file-structure:Michael Paus
06/06/2023, 1:26 PM