Skip to content

Commit 3c14f82

Browse files
committed
fix: pg
1 parent cdb789a commit 3c14f82

File tree

1 file changed

+11
-12
lines changed
  • packages/drizzle-orm-browser-migrator/src/pg

1 file changed

+11
-12
lines changed

packages/drizzle-orm-browser-migrator/src/pg/index.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ export async function migrate<TSchema extends Record<string, unknown>>(db: Postg
2424
sql: string[]
2525
}[]) {
2626
const log = useLogg(packageJSON.name).withFormat(Format.Pretty)
27+
28+
await db.execute(sql`CREATE SCHEMA IF NOT EXISTS drizzle;`)
2729
const TABLE_NAME = sql.identifier('__drizzle_migrations')
2830

2931
await db.execute(
30-
sql`CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (
32+
sql`CREATE TABLE IF NOT EXISTS drizzle.${TABLE_NAME} (
3133
id bigserial PRIMARY KEY NOT NULL,
3234
hash text NOT NULL,
33-
tag text NOT NULL,
3435
created_at bigint NOT NULL
3536
);`,
3637
)
@@ -44,43 +45,41 @@ export async function migrate<TSchema extends Record<string, unknown>>(db: Postg
4445
id,
4546
hash,
4647
created_at
47-
FROM ${TABLE_NAME}
48+
FROM drizzle.${TABLE_NAME}
4849
ORDER BY created_at DESC
4950
LIMIT 1;`,
5051
)
5152

52-
const deployment = deployments.rows.at(0)
53+
const deployment = deployments[0]
5354
const migrations = bundledMigrations.filter((migration) => {
5455
const timestamp = deployment?.created_at ?? 0
5556
return !deployment || Number(timestamp) < migration.when
5657
})
5758
if (migrations.length === 0) {
5859
log.withField('tables', await listTables(db)).debug('no pending migrations')
59-
log.log('no pending migrations to apply')
60+
log.debug('no pending migrations to apply')
6061
return
6162
}
6263

6364
await db.transaction(async (tx) => {
6465
for (let i = 0; i < migrations.length; i++) {
6566
const migration = migrations[i]
6667

67-
log.log(`${i + 1}. Deploying migration:`)
68-
log.log(` tag => ${migration.tag}`)
69-
log.log(` hash => ${migration.hash}`)
68+
log.debug(`${i + 1}. Deploying migration:`)
69+
log.debug(` hash => ${migration.hash}`)
7070
for (const stmt of migration.sql) {
7171
await tx.execute(stmt)
7272
}
7373

7474
await tx.execute(sql`
75-
INSERT INTO ${TABLE_NAME} ("hash", "created_at", "tag") VALUES (
75+
INSERT INTO drizzle.${TABLE_NAME} ("hash", "created_at") VALUES (
7676
${sql.raw(`'${migration.hash}'`)},
77-
${sql.raw(`${migration.when}`)},
78-
${sql.raw(`'${migration.tag}'`)}
77+
${sql.raw(`${migration.when}`)}
7978
);
8079
`)
8180
}
8281
})
8382

8483
log.withField('tables', await listTables(db)).debug('migration successful')
85-
log.log(`all ${migrations.length} pending migrations applied!`)
84+
log.debug(`all ${migrations.length} pending migrations applied!`)
8685
}

0 commit comments

Comments
 (0)