Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
EmptySpace99 opened this issue Jun 8, 2024 · 4 comments
Closed
Labels
actually kysely Actually related to Kysely, not CTL. bug Something isn't working cjs CommonJS related

Comments

@EmptySpace99
Copy link

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
Copy link
Member

igalklebanov commented Jun 8, 2024

Hey 👋

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

@EmptySpace99
Copy link
Author

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

@igalklebanov
Copy link
Member

igalklebanov commented Jun 9, 2024

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.

@EmptySpace99
Copy link
Author

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

@igalklebanov igalklebanov added bug Something isn't working actually kysely Actually related to Kysely, not CTL. cjs CommonJS related labels Jun 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actually kysely Actually related to Kysely, not CTL. bug Something isn't working cjs CommonJS related
Projects
None yet
Development

No branches or pull requests

2 participants