https://kotlinlang.org logo
#squarelibraries
Title
# squarelibraries
j

Josh Feinberg

10/10/2023, 4:03 AM
i'm having trouble figuring out whats going on with my migrations for a jvm project using sqldelight
i have a table defined in my
Main.sq
file
Copy code
CREATE TABLE holding (
    accountId TEXT NOT NULL,
    securityId TEXT NOT NULL,
    costBasis REAL NOT NULL
)
i then created a migration
1.sqm
file that has
Copy code
ALTER TABLE holding ADD COLUMN timestamp INTEGER AS Instant NOT NULL DEFAULT 0;
however i get a compile error
if i have
deriveSchemaFromMigrations
set to
true
, i get an error in the
.sq
that "Table already defined with name holding"
if i have it set to
false
then i get an error in the
.sqm
that "No table found with name holding"
h

hfhbd

10/10/2023, 5:36 AM
You should either move all table definitions in you sq files or use deriveSchemaFromMigrations and declare all tables in sqm files only, eg 0.sqm for initial schema.
r

Rafs

10/10/2023, 6:51 AM
I'm getting a similar error as well. I'm just trying to use the sample table but I get the
no such table
error when I try to execute the
selectAll
query.
d

dhia chemingui

10/10/2023, 12:43 PM
I have same problem Too who can help us Or who find a solution
@Josh Feinberg do u fix the problem
j

Josh Feinberg

10/10/2023, 3:51 PM
@hfhbd all table definitions are in my sq files but i'm trying to build a migration to alter that table. i'm also adding the column
timestamp
to the sq file but i still get those errors. will try to move all table definitions to 0.sqm but not sure if thats a great fix
d

dhia chemingui

10/10/2023, 4:13 PM
@hfhbd why I should add all table definitions in the file migrations . I will do that in each migration ??
r

Rafs

10/10/2023, 4:14 PM
I fixed it by adding the table definitions to
1.sqm
and adding the other queries into my
.sq
file
j

Josh Feinberg

10/10/2023, 4:15 PM
if you add the table definitions to your migration file doesn't it try to recreate them when you run the migration?
r

Rafs

10/10/2023, 4:15 PM
IF NOT EXISTS
will skip table creation if it exists
1
d

dhia chemingui

10/10/2023, 4:16 PM
yes me too but I dont wanna add this in each migration and its rejected in code review
r

Rafs

10/10/2023, 4:16 PM
You don't have to add it in every migration. Only the first migration
1.sqm
d

dhia chemingui

10/10/2023, 4:17 PM
emm okay will see thnx
I have a question it is logic to add the a column in the new migration and add this colum to the creation query hhh .
h

hfhbd

10/10/2023, 7:59 PM
The schema files (sqm) are evaluated before the query files because it is needed to evaluate the queries. Otherwise the queries are not resolved correctly. You don't need to add
IF NOT EXISTS
, you should just conditionally run the
schema.create
function (or use
pragma version
).
j

Josh Feinberg

10/10/2023, 8:02 PM
the sample app doesn't seem to do this to handle migrations (https://github.com/cashapp/sqldelight/tree/master/sample/common/src/commonMain/sqldelight). it does look like the sample app hasn't been updated to 2.0.0 though so was this a breaking change? the docs don't seem to say anything about this changing (https://cashapp.github.io/sqldelight/2.0.0/jvm_sqlite/migrations/)
😅 1
👍 1
h

hfhbd

10/10/2023, 8:04 PM
Well the migration of the sample does not do any useful things, so yeah, we should update it.
❤️ 1
j

Josh Feinberg

10/10/2023, 8:11 PM
yeah, looks like the docs then should be updated as well. the table
hockeyPlayer
in the docs in defined in
Player.sq
then the alter function is defined in
1.sqm
which is exactly what i'm trying to do so it seems like you are saying that is wrong?
2 Views