Skip to content

Commit 53e803c

Browse files
committed
fix: table existence check for multitenant
For the multitenant db, we use `public` as the schema
1 parent 1a60670 commit 53e803c

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/internal/database/migrations/migrate.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ export async function runMultitenantMigrations(): Promise<void> {
311311
await connectAndMigrate({
312312
databaseUrl: multitenantDatabaseUrl,
313313
migrationsDirectory: './migrations/multitenant',
314+
migrationsTableSchema: 'public',
314315
shouldCreateStorageSchema: false,
315316
waitForLock: true,
316317
})
@@ -347,6 +348,7 @@ export async function runMigrationsOnTenant({
347348
await connectAndMigrate({
348349
databaseUrl,
349350
migrationsDirectory: './migrations/tenant',
351+
migrationsTableSchema: 'storage',
350352
ssl: getSslSettings({ connectionString: databaseUrl, databaseSSLRootCert }),
351353
shouldCreateStorageSchema: true,
352354
tenantId,
@@ -502,6 +504,7 @@ async function connect(options: {
502504
async function connectAndMigrate(options: {
503505
databaseUrl: string | undefined
504506
migrationsDirectory: string
507+
migrationsTableSchema?: string
505508
ssl?: ClientConfig['ssl']
506509
shouldCreateStorageSchema?: boolean
507510
tenantId?: string
@@ -525,6 +528,7 @@ async function connectAndMigrate(options: {
525528
await migrate({
526529
client,
527530
migrationsDirectory,
531+
migrationsTableSchema: options.migrationsTableSchema,
528532
waitForLock: Boolean(waitForLock),
529533
shouldCreateStorageSchema,
530534
upToMigration: options.upToMigration,
@@ -537,6 +541,7 @@ async function connectAndMigrate(options: {
537541
interface MigrateOptions {
538542
client: BasicPgClient
539543
migrationsDirectory: string
544+
migrationsTableSchema?: string
540545
waitForLock: boolean
541546
shouldCreateStorageSchema?: boolean
542547
upToMigration?: keyof typeof DBMigration
@@ -552,6 +557,7 @@ interface MigrateOptions {
552557
export async function migrate({
553558
client,
554559
migrationsDirectory,
560+
migrationsTableSchema,
555561
waitForLock,
556562
shouldCreateStorageSchema,
557563
upToMigration,
@@ -561,6 +567,7 @@ export async function migrate({
561567
waitForLock,
562568
runMigrations({
563569
migrationsDirectory,
570+
migrationsTableSchema,
564571
shouldCreateStorageSchema,
565572
upToMigration,
566573
// Remove concurrent index creation if we're using oriole db as it does not support it currently
@@ -571,6 +578,7 @@ export async function migrate({
571578

572579
interface RunMigrationOptions {
573580
migrationsDirectory: string
581+
migrationsTableSchema?: string
574582
shouldCreateStorageSchema?: boolean
575583
upToMigration?: keyof typeof DBMigration
576584
transformers?: MigrationTransformer[]
@@ -584,6 +592,7 @@ interface RunMigrationOptions {
584592
*/
585593
function runMigrations({
586594
migrationsDirectory,
595+
migrationsTableSchema,
587596
shouldCreateStorageSchema,
588597
upToMigration,
589598
transformers = [],
@@ -607,7 +616,13 @@ function runMigrations({
607616
await client.query(`SET search_path TO ${searchPath.join(',')}`)
608617

609618
let appliedMigrations: Migration[] = []
610-
if (await doesTableExist(client, migrationTableName)) {
619+
if (
620+
await doesTableExist({
621+
client,
622+
schemaName: migrationsTableSchema,
623+
tableName: migrationTableName,
624+
})
625+
) {
611626
const selectQueryCurrentMigration = SQL`SELECT * FROM `
612627
.append(migrationTableName)
613628
.append(SQL` WHERE id <= ${lastMigrationId}`)
@@ -724,15 +739,26 @@ async function getDefaultAccessMethod(client: BasicPgClient): Promise<string> {
724739
* @param client
725740
* @param tableName
726741
*/
727-
async function doesTableExist(client: BasicPgClient, tableName: string) {
728-
const result = await client.query(SQL`SELECT EXISTS (
742+
async function doesTableExist({
743+
client,
744+
schemaName,
745+
tableName,
746+
}: {
747+
client: BasicPgClient
748+
schemaName?: string
749+
tableName: string
750+
}) {
751+
const result = await client.query(
752+
SQL`SELECT EXISTS (
729753
SELECT 1
730754
FROM pg_catalog.pg_class c
731755
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
732-
WHERE n.nspname = 'storage'
733-
AND c.relname = ${tableName}
756+
WHERE c.relname = ${tableName}
734757
AND c.relkind = 'r'
735-
);`)
758+
`
759+
.append(schemaName ? SQL`AND n.nspname = ${schemaName}` : '')
760+
.append(`);`)
761+
)
736762

737763
return result.rows.length > 0 && result.rows[0].exists
738764
}

0 commit comments

Comments
 (0)