mirror of
https://github.com/supabase/supabase.git
synced 2026-10-18 07:39:07 +03:00
Use pg-meta formating functions
This commit is contained in:
1 parent
21c51ac868
commit
02d53d64db
6 files changed
+26
-14
No files matched your search
@@ -1,3 +1,5 @@
|
||||
import { ident } from "../../../pg-format"
|
||||
|
||||
export const getDropConstraintSQL = ({
|
||||
schema,
|
||||
table,
|
||||
@@ -6,4 +8,4 @@ export const getDropConstraintSQL = ({
|
||||
schema: string
|
||||
table: string
|
||||
name: string
|
||||
}) => `ALTER TABLE "${schema}"."${table}" DROP CONSTRAINT "${name}"`
|
||||
}) => `ALTER TABLE ${ident(schema)}.${ident(table)} DROP CONSTRAINT ${ident(name)}`
|
||||
@@ -1,3 +1,5 @@
|
||||
import { ident } from "../../../pg-format";
|
||||
|
||||
export const getAddForeignKeySQL = ({
|
||||
table,
|
||||
foreignKeys,
|
||||
@@ -28,9 +30,9 @@ export const getAddForeignKeySQL = ({
|
||||
const onDeleteSql = getOnDeleteSql(deletionAction)
|
||||
const onUpdateSql = getOnUpdateSql(updateAction)
|
||||
return `
|
||||
ALTER TABLE "${table.schema}"."${table.name}"
|
||||
ADD FOREIGN KEY (${relation.columns.map((column) => `"${column.source}"`).join(', ')})
|
||||
REFERENCES "${relation.schema}"."${relation.table}" (${relation.columns.map((column) => `"${column.target}"`).join(', ')})
|
||||
ALTER TABLE ${ident(table.schema)}.${ident(table.name)}
|
||||
ADD FOREIGN KEY (${relation.columns.map((column) => ident(column.source)).join(', ')})
|
||||
REFERENCES ${ident(relation.schema)}.${ident(relation.table)} (${relation.columns.map((column) => ident(column.target)).join(', ')})
|
||||
${onUpdateSql}
|
||||
${onDeleteSql}
|
||||
`
|
||||
@@ -52,8 +54,8 @@ export const getRemoveForeignKeySQL = ({
|
||||
foreignKeys
|
||||
.map((relation) =>
|
||||
`
|
||||
ALTER TABLE IF EXISTS "${table.schema}"."${table.name}"
|
||||
DROP CONSTRAINT IF EXISTS "${relation.name}"
|
||||
ALTER TABLE IF EXISTS ${ident(table.schema)}.${ident(table.name)}
|
||||
DROP CONSTRAINT IF EXISTS ${ident(relation.name)}
|
||||
`
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim()
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { ident } from "../../../pg-format"
|
||||
|
||||
export const getUpdateIdentitySequenceSQL = ({
|
||||
schema,
|
||||
table,
|
||||
@@ -7,7 +9,7 @@ export const getUpdateIdentitySequenceSQL = ({
|
||||
table: string
|
||||
column: string
|
||||
}) => {
|
||||
return `SELECT setval('"${schema}"."${table}_${column}_seq"', (SELECT COALESCE(MAX("${column}"), 1) FROM "${schema}"."${table}"))`
|
||||
return `SELECT setval('${ident(schema)}.${ident(`${table}_${column}_seq`)}', (SELECT COALESCE(MAX(${ident(column)}), 1) FROM ${ident(schema)}.${ident(table)}))`
|
||||
}
|
||||
|
||||
|
||||
@@ -22,5 +24,5 @@ export const getDuplicateIdentitySequenceSQL = ({
|
||||
sourceTableName: string
|
||||
sourceTableSchema: string
|
||||
}) => {
|
||||
return `SELECT setval('"${sourceTableSchema}"."${duplicatedTableName}_${columnName}_seq"', (SELECT MAX("${columnName}") FROM "${sourceTableSchema}"."${sourceTableName}"));`
|
||||
return `SELECT setval('${ident(sourceTableSchema)}.${ident(`${duplicatedTableName}_${columnName}_seq`)}', (SELECT MAX(${ident(columnName)}) FROM ${ident(sourceTableSchema)}.${ident(sourceTableName)}));`
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import { ident } from "../../../pg-format"
|
||||
|
||||
/**
|
||||
* The functions below are basically just queries but may be supported directly
|
||||
* from the pg-meta library in the future
|
||||
@@ -11,6 +13,6 @@ export const getAddPrimaryKeySQL = ({
|
||||
table: string
|
||||
columns: string[]
|
||||
}) => {
|
||||
const primaryKeyColumns = columns.map((col) => `"${col}"`).join(', ')
|
||||
return `ALTER TABLE "${schema}"."${table}" ADD PRIMARY KEY (${primaryKeyColumns})`
|
||||
const primaryKeyColumns = columns.map((col) => ident(col)).join(', ')
|
||||
return `ALTER TABLE ${ident(schema)}.${ident(table)} ADD PRIMARY KEY (${primaryKeyColumns})`
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import { ident } from "../../../pg-format";
|
||||
|
||||
export const getEnableRLSSQL = ({ schema, table }: { schema: string; table: string }) => {
|
||||
return `ALTER TABLE "${schema}"."${table}" ENABLE ROW LEVEL SECURITY`
|
||||
return `ALTER TABLE ${ident(schema)}.${ident(table)} ENABLE ROW LEVEL SECURITY`
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import { ident } from '../../../pg-format'
|
||||
|
||||
export const getDuplicateTableSQL = ({
|
||||
comment,
|
||||
duplicatedTableName,
|
||||
@@ -10,9 +12,9 @@ export const getDuplicateTableSQL = ({
|
||||
sourceTableSchema: string
|
||||
}) => {
|
||||
return [
|
||||
`CREATE TABLE "${sourceTableSchema}"."${duplicatedTableName}" (LIKE "${sourceTableSchema}"."${sourceTableName}" INCLUDING ALL);`,
|
||||
`CREATE TABLE ${ident(sourceTableSchema)}.${ident(duplicatedTableName)} (LIKE ${ident(sourceTableSchema)}.${ident(sourceTableName)} INCLUDING ALL);`,
|
||||
comment != undefined
|
||||
? `comment on table "${sourceTableSchema}"."${duplicatedTableName}" is '${comment}';`
|
||||
? `comment on table ${ident(sourceTableSchema)}.${ident(duplicatedTableName)} is '${comment}';`
|
||||
: '',
|
||||
].join('\n')
|
||||
}
|
||||
@@ -26,5 +28,5 @@ export const getDuplicateRowsSQL = ({
|
||||
sourceTableName: string
|
||||
sourceTableSchema: string
|
||||
}) => {
|
||||
return `INSERT INTO "${sourceTableSchema}"."${duplicatedTableName}" SELECT * FROM "${sourceTableSchema}"."${sourceTableName}";`
|
||||
return `INSERT INTO ${ident(sourceTableSchema)}.${ident(duplicatedTableName)} SELECT * FROM ${ident(sourceTableSchema)}.${ident(sourceTableName)};`
|
||||
}
|
||||
Reference in new issue
Block a user