mmkal / pgkit

PostgreSQL🤝TypeScript monorepo. SQL client/admin UI/smart migrator/type generator/schema inspector

Home Page:https://pgkit.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Correct way to do migrations with .ts files

Ugluth opened this issue · comments

When trying to create a .ts migration file, the default generated file contains the following:

import {Migration} from '@slonik/migrator'

export const up: Migration = async ({slonik, sql}) => {
  await slonik.query(sql`raise 'up migration not implemented'`)
}

export const down: Migration = async ({slonik, sql}) => {
  await slonik.query(sql`raise 'down migration not implemented'`)
}

However slonik and sql are marked as deprecated. The suggestion from the comments is to use context.connection and context.sql correspondingly. I had a look, however I can't really figure out from where exactly I should use those. Can you help out with this please?

Thanks in advance

Thanks for the report @Ugluth - I will update the template for future migrations. In the meantime, here's it should look with the non-deprecated parameters:

import {Migration} from '@slonik/migrator'

export const up: Migration = async ({context: {connection, sql}}) => {
  await connection.query(sql\`raise 'up migration not implemented'\`)
}

export const down: Migration = async ({context: {connection, sql}}) => {
  await connection.query(sql\`raise 'down migration not implemented'\`)
}

connection has all the same query helper methods as slonik had.