I'm having issues setting up migrations in sqldeli...
# multiplatform
r
I'm having issues setting up migrations in sqldelight (
1.5.5
) i have a table
media
and i want to alter it to add a column (
broadcast_date
) so i have 1.sqm
Copy code
ALTER TABLE media ADD COLUMN broadcast_date TEXT DEFAULT NULL;
i have turned on
deriveSchemaFromMigrations = true
in
build.gradle.kts
but when building, it tries to run the alter table command before the schema in the .sq files? (error output in thread) just wondering if thats the best way to do migrations? source is here: https://github.com/sentinelweb/cuer/tree/feature/196-upcoming-videos/database/src/commonMain/sqldelight/uk/co/sentinelweb/cuer/database
Copy code
> Task :database:generateCommonMainDatabaseInterface FAILED

/Users/robmunro/repos/cuer/database/src/commonMain/sqldelight/uk/co/sentinelweb/cuer/database/migrations/1.sqm line 1:12 - Attempting to alter something that is not a table.
1    ALTER TABLE media ADD COLUMN broadcast_date TEXT DEFAULT NULL
                 ^^^^^

/Users/robmunro/repos/cuer/database/src/commonMain/sqldelight/uk/co/sentinelweb/cuer/database/migrations/1.sqm line 1:12 - No table found with name media
1    ALTER TABLE media ADD COLUMN broadcast_date TEXT DEFAULT NULL
                 ^^^^^

/Users/robmunro/repos/cuer/database/src/commonMain/sqldelight/uk/co/sentinelweb/cuer/database/entity/MediaEntity.sq line 3:13 - Table already defined with name media
03    CREATE TABLE media (
                   ^^^^^
04        id TEXT NOT NULL PRIMARY KEY,
05        flags INTEGER NOT NULL DEFAULT 0,
06        type TEXT AS uk.co.sentinelweb.cuer.domain.MediaDomain.MediaTypeDomain NOT NULL,
07        url TEXT NOT NULL,
08        title TEXT,
09        duration INTEGER,
10        position INTEGER,
11        date_last_played TEXT AS kotlinx.datetime.Instant,
12        description TEXT,
13        platform TEXT AS uk.co.sentinelweb.cuer.domain.PlatformDomain NOT NULL,
14        platform_id TEXT NOT NULL ,
15        published TEXT AS kotlinx.datetime.LocalDateTime,
16        channel_id TEXT,
17        thumb_id TEXT,
18        image_id TEXT,
19    --    broadcast_date TEXT AS kotlinx.datetime.LocalDateTime,
20        FOREIGN KEY (channel_id) REFERENCES channel(id)
21    )
also if i set:
Copy code
deriveSchemaFromMigrations = false
then the above error is gone - but when I try to generate the verification db (using
database:generateCommonMainDatabaseSchema
) - then the migrations arent included in the generated db file. (in this case
broadcast_date
is not in the
media
table) - hence
verifySqlelightMigration
fails.
l
Together with the migration you also must update the table definitions.
r
yes thanks, i have found that out today... i have the verify working now but i was running the migrations using the following code:
Copy code
Database.Schema.migrate(
     driver = driver,
     oldVersion = prefs.dbVersion,
     newVersion = Database.Schema.version,
).also {
     prefs.dbVersion = Database.Schema.version
}
But it seems that the migration is run automatically when the db is created? the
Database.Schema.version
is already
2
Do i NOT need to call
Database.Schema.migrate()
manually at all?
l
if you have a migration defined and a database in an older version in android, it will migrate automatically for you before any read/write is done
r
thanks for the feedback
472 Views