K
Kysely•7mo ago
WhyDoThis

Is there an easy tool to convert my existing knex migrations to kysely migrations?

I have a bunch of knex migrations in following format:
.createTable("state", (table: Knex.TableBuilder) => {
table.bigIncrements("id").unsigned().primary();
table.string("abbreviation", 2).notNullable();
table.string("name", 100).notNullable();
})
.createTable("state", (table: Knex.TableBuilder) => {
table.bigIncrements("id").unsigned().primary();
table.string("abbreviation", 2).notNullable();
table.string("name", 100).notNullable();
})
Copilot can't seem to do this for me so wondering if anyone knows of a tool? Maybe the only way is by hand.
18 Replies
Igal (mobile)
Igal (mobile)•7mo ago
Hey 👋 Have you tried kysely-knex ? It allows you to postpone this effort. and use both for migrations.
WhyDoThis
WhyDoThisOP•7mo ago
Let me take a look I appreciate it igalk - I am a bit eager though to just rip out knex/objection completely if possible and move on haha.
Igal (mobile)
Igal (mobile)•7mo ago
i know, but this'll allow you to migrate incrementally
WhyDoThis
WhyDoThisOP•7mo ago
I am pre-release on this project so I think the cost to do this is lowest now then it will be but it is a good point
Igal (mobile)
Igal (mobile)•7mo ago
i see
WhyDoThis
WhyDoThisOP•7mo ago
I took a db backup so I can compare to make sure after I convert them they are 1:1 schema wise
Igal (mobile)
Igal (mobile)•7mo ago
well, it was built for brownfield projects in mind if it is not in production yet, you could simply start anew with kysely with an empty migration, and later fill it with queries representing the current schema
WhyDoThis
WhyDoThisOP•7mo ago
Yes I maybe phrased it poorly I don't have multiple migration files but all my tables in one knex migration file but knex of course uses things like bigIncrements/string, etc. which are not 1:1 with actual underlying db type names so it is a little tedious to rewrite them
Igal (mobile)
Igal (mobile)•7mo ago
yeah, part of one lib for many engines
WhyDoThis
WhyDoThisOP•7mo ago
(btw I am loving so far how much closer to actual pg sql kysely allows it to be) less obscurity
Igal (mobile)
Igal (mobile)•7mo ago
it's all about "what you see is what you get" and "0 magic" with kysely less overhead more trust
WhyDoThis
WhyDoThisOP•7mo ago
For sure - I appreciate you and the teams hard work on it!
Igal (mobile)
Igal (mobile)•7mo ago
sami made it all possible. i'm just lucky to be around. 😊
WhyDoThis
WhyDoThisOP•7mo ago
You are too humble haha - sorry to derail the original point of this thread but is there a way to dry run the migration that you know of? My current thought is the only way to test is by wiping the DB completely (or point to a different one) and re-backing it up to compare the schema of the rewritten kysely migration to the original schema created by the knex migration. I've always longed for something like Entity Framework how it can compare and show diffs before executing.
Igal (mobile)
Igal (mobile)•7mo ago
if its an engine that supports transactional ddl.. just throw in the end.
WhyDoThis
WhyDoThisOP•7mo ago
interesting thanks @Igal I finally got done mapping everything over and went to dry run - I throw at the end of the "up" but how do I see the sql it was going to use to make the changes? Or did I understand your recommendation incorrectly on that part?
Igal
Igal•7mo ago
Logging | Kysely
It is possible to set up logs for all queries using the log property when instantiating Kysely.
WhyDoThis
WhyDoThisOP•7mo ago
Thank you!

Did you find this page helpful?