K
Kysely•2mo ago
tj1892

Transaction is necessary in the migration file?

Hi May I know if we need to wrap the migration in a transaction like the following?
export async function up(db: Kysely<any>): Promise<void> {
await db.transaction().execute(async (trx) => {
await db.schema.createTable(...)
})
...
}

export async function down(db: Kysely<any>): Promise<void> {
await db.transaction().execute(async (trx) => {
await db.schema.dropTable(...)
})
...
}
export async function up(db: Kysely<any>): Promise<void> {
await db.transaction().execute(async (trx) => {
await db.schema.createTable(...)
})
...
}

export async function down(db: Kysely<any>): Promise<void> {
await db.transaction().execute(async (trx) => {
await db.schema.dropTable(...)
})
...
}
or this is taken care by the migrator?
4 Replies
Igal (mobile)
Igal (mobile)•2mo ago
Hey 👋 Taken care of in the migrator, if you're using a database that supports transactional DDL.
tj1892
tj1892OP•2mo ago
@Igal (mobile) Uh ... it looks like MySQL 8 is not supporting transactional DDL, and I will have to add the transaction in the migration file. Thanks to point out this and this could be useful to mention in https://kysely.dev/docs/migrations too See also https://www.bytebase.com/blog/postgres-vs-mysql-ddl-transaction/
Migrations | Kysely
Migration files
Bytebase
Postgres vs. MySQL: DDL Transaction Difference
Database schema changes are critical operations that require careful planning and execution. The ability to perform these changes safely and reliably is a key c...
Igal (mobile)
Igal (mobile)•2mo ago
It won't help you. MySQL doesnt support transactional DDL. The trandaction will commit prematurely after the first DDL query you run with it.
tj1892
tj1892OP•2mo ago
You're right, I'm seeing the issue now.

Did you find this page helpful?