kysely-org / kysely-ctl

Command-line tool for Kysely
MIT License
46 stars 1 forks source link

Migration fails with: "TypeError: Cannot redefine property: then" #26

Closed EmptySpace99 closed 1 month ago

EmptySpace99 commented 1 month ago

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.

igalklebanov commented 1 month ago

Hey 👋

Try adding up() to and running this migration using npx tsx <filepath>, does it still happen? Can you provide a reproduction repository?

EmptySpace99 commented 1 month ago

I created one: https://github.com/EmptySpace99/test-kysely-ctl

igalklebanov commented 1 month ago

Thanks for the reproduction. I was able to debug and think that https://github.com/kysely-org/kysely/pull/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.

EmptySpace99 commented 1 month ago

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. 💪🏻