Migration fails with: "TypeError: Cannot redefine property: then"
EmptySpace99 opened this issue ยท 4 comments
I created the following migration file:
import { Kysely, sql } from 'kysely'
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.createTable('person')
.addColumn('id', 'serial', (col) => col.primaryKey())
.addColumn('first_name', 'varchar', (col) => col.notNull())
.addColumn('last_name', 'varchar')
.addColumn('gender', 'varchar(50)', (col) => col.notNull())
.addColumn('created_at', 'timestamp', (col) =>
col.defaultTo(sql`now()`).notNull()
)
.execute()
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.dropTable('person').execute()
}
And then I tried to run the migration with npm run kysely migrate:up
but it failed with the following error message:
Migration failed with TypeError: Cannot redefine property: then
If I remove the following code and retry the migration, it completes successfully:
.addColumn('created_at', 'timestamp', (col) =>
col.defaultTo(sql`now()`).notNull()
)
In my kysely.config.ts file I have the following code:
import { Kysely, PostgresDialect } from "kysely";
import { defineConfig } from "kysely-ctl";
import { Pool } from 'pg'
const connectionString = process.env.DATABASE_URL ?? ''
const dialect = new PostgresDialect({
pool: new Pool({
connectionString: connectionString
})
})
export default defineConfig({
dialect: dialect,
migrations: {
migrationFolder: 'migrations',
},
});
It seems a bug to me but I'm not sure if I'm missing something else.
Hey ๐
Try adding up()
to and running this migration using npx tsx <filepath>
, does it still happen?
Can you provide a reproduction repository?
I created one: https://github.com/EmptySpace99/test-kysely-ctl
Thanks for the reproduction. I was able to debug and think that kysely-org/kysely#1031 should solve it - it did on my machine.
You should patch kysely
in your project for now following the changes in that PR using something like https://www.npmjs.com/package/patch-package OR https://pnpm.io/cli/patch.
This doesn't happen when turning the project to ESM ("type": "module"
in package.json
), so you could migrate instead.
Thank you so much, effectively turning the project to ESM fix the problem. Probably this is the easiest fix for now, hope that the real fix will be merged soon. ๐ช๐ป