Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions internal/plan/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,11 @@ WHERE c.relname = '%s';`, indexName)
// Helper functions

func getTableNameWithSchema(schema, table string) string {
quotedTable := ir.QuoteIdentifier(table)
if schema != "" && schema != "public" {
return fmt.Sprintf("%s.%s", schema, table)
return fmt.Sprintf("%s.%s", ir.QuoteIdentifier(schema), quotedTable)
}
return table
return quotedTable
}

func joinStrings(strs []string, sep string) string {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE "order"
ADD COLUMN tenant_id uuid CONSTRAINT "FK_order_tenant" REFERENCES tenant (id);

CREATE INDEX IF NOT EXISTS "IDX_order_tenant_order_number" ON "order" (tenant_id, order_number);
17 changes: 17 additions & 0 deletions testdata/diff/online/issue_286_reserved_keyword_quoting/new.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Test Case: Reserved keyword table names must be quoted in online rewrite DDL
-- Adding a column with FK and an index on a table named "order" (reserved keyword).

CREATE TABLE public.tenant (
id uuid NOT NULL,
name text NOT NULL,
CONSTRAINT tenant_pkey PRIMARY KEY (id)
);

CREATE TABLE public."order" (
id integer NOT NULL,
order_number text NOT NULL,
tenant_id uuid CONSTRAINT "FK_order_tenant" REFERENCES public.tenant (id),
CONSTRAINT order_pkey PRIMARY KEY (id)
);

CREATE INDEX "IDX_order_tenant_order_number" ON public."order" (tenant_id, order_number);
14 changes: 14 additions & 0 deletions testdata/diff/online/issue_286_reserved_keyword_quoting/old.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Test Case: Reserved keyword table names must be quoted in online rewrite DDL
-- The table "order" is a PostgreSQL reserved keyword and must be quoted everywhere.

CREATE TABLE public.tenant (
id uuid NOT NULL,
name text NOT NULL,
CONSTRAINT tenant_pkey PRIMARY KEY (id)
);

CREATE TABLE public."order" (
id integer NOT NULL,
order_number text NOT NULL,
CONSTRAINT order_pkey PRIMARY KEY (id)
);
44 changes: 44 additions & 0 deletions testdata/diff/online/issue_286_reserved_keyword_quoting/plan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"version": "1.0.0",
"pgschema_version": "1.7.0",
"created_at": "1970-01-01T00:00:00Z",
"source_fingerprint": {
"hash": "880c2cec11ce10f5b7868bde9b051d7559368a092d2071a5cd68a0b1d5456627"
},
"groups": [
{
"steps": [
{
"sql": "ALTER TABLE \"order\"\nADD COLUMN tenant_id uuid CONSTRAINT \"FK_order_tenant\" REFERENCES tenant (id);",
"type": "table.column",
"operation": "create",
"path": "public.order.tenant_id"
}
]
},
{
"steps": [
{
"sql": "CREATE INDEX CONCURRENTLY IF NOT EXISTS \"IDX_order_tenant_order_number\" ON \"order\" (tenant_id, order_number);",
"type": "table.index",
"operation": "create",
"path": "public.order.IDX_order_tenant_order_number"
}
]
},
{
"steps": [
{
"sql": "SELECT \n COALESCE(i.indisvalid, false) as done,\n CASE \n WHEN p.blocks_total > 0 THEN p.blocks_done * 100 / p.blocks_total\n ELSE 0\n END as progress\nFROM pg_class c\nLEFT JOIN pg_index i ON c.oid = i.indexrelid\nLEFT JOIN pg_stat_progress_create_index p ON c.oid = p.index_relid\nWHERE c.relname = 'IDX_order_tenant_order_number';",
"directive": {
"type": "wait",
"message": "Creating index IDX_order_tenant_order_number"
},
"type": "table.index",
"operation": "create",
"path": "public.order.IDX_order_tenant_order_number"
}
]
}
]
}
16 changes: 16 additions & 0 deletions testdata/diff/online/issue_286_reserved_keyword_quoting/plan.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ALTER TABLE "order"
ADD COLUMN tenant_id uuid CONSTRAINT "FK_order_tenant" REFERENCES tenant (id);

CREATE INDEX CONCURRENTLY IF NOT EXISTS "IDX_order_tenant_order_number" ON "order" (tenant_id, order_number);

-- pgschema:wait
SELECT
COALESCE(i.indisvalid, false) as done,
CASE
WHEN p.blocks_total > 0 THEN p.blocks_done * 100 / p.blocks_total
ELSE 0
END as progress
FROM pg_class c
LEFT JOIN pg_index i ON c.oid = i.indexrelid
LEFT JOIN pg_stat_progress_create_index p ON c.oid = p.index_relid
WHERE c.relname = 'IDX_order_tenant_order_number';
32 changes: 32 additions & 0 deletions testdata/diff/online/issue_286_reserved_keyword_quoting/plan.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Plan: 1 to modify.

Summary by type:
tables: 1 to modify

Tables:
~ order
+ tenant_id (column)
+ IDX_order_tenant_order_number (index)

DDL to be executed:
--------------------------------------------------

-- Transaction Group #1
ALTER TABLE "order"
ADD COLUMN tenant_id uuid CONSTRAINT "FK_order_tenant" REFERENCES tenant (id);

-- Transaction Group #2
CREATE INDEX CONCURRENTLY IF NOT EXISTS "IDX_order_tenant_order_number" ON "order" (tenant_id, order_number);

-- Transaction Group #3
-- pgschema:wait
SELECT
COALESCE(i.indisvalid, false) as done,
CASE
WHEN p.blocks_total > 0 THEN p.blocks_done * 100 / p.blocks_total
ELSE 0
END as progress
FROM pg_class c
LEFT JOIN pg_index i ON c.oid = i.indexrelid
LEFT JOIN pg_stat_progress_create_index p ON c.oid = p.index_relid
WHERE c.relname = 'IDX_order_tenant_order_number';