Skip to content

Commit 556b8ed

Browse files
nachomglzr1tsuu
andauthored
fix(db-postgres): added missing quotes and schema name to sql statement in v2-v3 migration (#10322)
### What? * Added missing schema name to the SQL statement * Added missing quotes to the `parent_id`'s in the SQL statement ### Why? * Migrations script isn't working for databases that don't use the `public` schema * They were missing, causing the database to throw an error. ### Fixes: - [Discord Discussion](https://discord.com/channels/967097582721572934/1319313926885736448) - [Discord Discussion](https://discord.com/channels/967097582721572934/1324352204915609661) - [Discord Discussion](https://discord.com/channels/967097582721572934/1323445259606429716) --------- Co-authored-by: Sasha <64744993+r1tsuu@users.noreply.github.com>
1 parent 0ce7c66 commit 556b8ed

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

packages/drizzle/src/postgres/predefinedMigrations/v2-v3/groupUpSQLStatements.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ export type Groups =
1414
*/
1515
function convertAddColumnToAlterColumn(sql) {
1616
// Regular expression to match the ADD COLUMN statement with its constraints
17-
const regex = /ALTER TABLE ("[^"]+") ADD COLUMN ("[^"]+") [\w\s]+ NOT NULL;/
17+
const regex = /ALTER TABLE ("[^"]+")\.(".*?") ADD COLUMN ("[^"]+") [\w\s]+ NOT NULL;/
1818

1919
// Replace the matched part with "ALTER COLUMN ... SET NOT NULL;"
20-
return sql.replace(regex, 'ALTER TABLE $1 ALTER COLUMN $2 SET NOT NULL;')
20+
return sql.replace(regex, 'ALTER TABLE $1.$2 ALTER COLUMN $3 SET NOT NULL;')
2121
}
2222

2323
export const groupUpSQLStatements = (list: string[]): Record<Groups, string[]> => {

packages/drizzle/src/postgres/predefinedMigrations/v2-v3/migrateRelationships.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { PgSchema } from 'drizzle-orm/pg-core'
12
import type { FlattenedField, Payload, PayloadRequest } from 'payload'
23

34
import { sql } from 'drizzle-orm'
@@ -42,14 +43,16 @@ export const migrateRelationships = async ({
4243

4344
let paginationResult
4445

46+
const schemaName = (adapter.pgSchema as PgSchema).schemaName ?? 'public'
47+
4548
const where = Array.from(pathsToQuery).reduce((statement, path, i) => {
4649
return (statement += `
47-
"${tableName}${adapter.relationshipsSuffix}"."path" LIKE '${path}'${pathsToQuery.size !== i + 1 ? ' OR' : ''}
50+
"${schemaName}"."${tableName}${adapter.relationshipsSuffix}"."path" LIKE '${path}'${pathsToQuery.size !== i + 1 ? ' OR' : ''}
4851
`)
4952
}, '')
5053

5154
while (typeof paginationResult === 'undefined' || paginationResult.rows.length > 0) {
52-
const paginationStatement = `SELECT DISTINCT parent_id FROM ${tableName}${adapter.relationshipsSuffix} WHERE
55+
const paginationStatement = `SELECT DISTINCT parent_id FROM "${schemaName}"."${tableName}${adapter.relationshipsSuffix}" WHERE
5356
${where} ORDER BY parent_id LIMIT 500 OFFSET ${offset * 500};
5457
`
5558

@@ -61,8 +64,8 @@ export const migrateRelationships = async ({
6164

6265
offset += 1
6366

64-
const statement = `SELECT * FROM ${tableName}${adapter.relationshipsSuffix} WHERE
65-
(${where}) AND parent_id IN (${paginationResult.rows.map((row) => row.parent_id).join(', ')});
67+
const statement = `SELECT * FROM "${schemaName}"."${tableName}${adapter.relationshipsSuffix}" WHERE
68+
(${where}) AND parent_id IN (${paginationResult.rows.map((row) => `'${row.parent_id}'`).join(', ')});
6669
`
6770
if (debug) {
6871
payload.logger.info('FINDING ROWS TO MIGRATE')
@@ -99,7 +102,7 @@ export const migrateRelationships = async ({
99102
})
100103
}
101104

102-
const deleteStatement = `DELETE FROM ${tableName}${adapter.relationshipsSuffix} WHERE ${where}`
105+
const deleteStatement = `DELETE FROM "${adapter.schemaName}"."${tableName}${adapter.relationshipsSuffix}" WHERE ${where}`
103106
if (debug) {
104107
payload.logger.info('DELETING ROWS')
105108
payload.logger.info(deleteStatement)

0 commit comments

Comments
 (0)