From ecc9cdedcc3c6115a3d7552cc1f02d5131617502 Mon Sep 17 00:00:00 2001 From: Sean Colsen Date: Sun, 21 Jul 2024 14:04:35 -0400 Subject: [PATCH 1/4] Convert table patch data to JSON before send to psycopg --- db/tables/operations/alter.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/db/tables/operations/alter.py b/db/tables/operations/alter.py index d1d554bc29..72c72ca514 100644 --- a/db/tables/operations/alter.py +++ b/db/tables/operations/alter.py @@ -1,4 +1,6 @@ """The functions in this module wrap SQL functions that use `ALTER TABLE`.""" +import json + from db import constants from db import connection as db_conn from db.columns.operations.alter import batch_update_columns @@ -66,7 +68,7 @@ def alter_table_on_database(table_oid, table_data_dict, conn): } """ return db_conn.exec_msar_func( - conn, 'alter_table', table_oid, table_data_dict + conn, 'alter_table', table_oid, json.dumps(table_data_dict) ).fetchone()[0] From f66bc48cf934c8a09abf3a82360fcd7cdae05e45 Mon Sep 17 00:00:00 2001 From: Sean Colsen Date: Sun, 21 Jul 2024 16:32:58 -0400 Subject: [PATCH 2/4] Refactor __msar out of rename_table DB functions --- db/sql/00_msar.sql | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/db/sql/00_msar.sql b/db/sql/00_msar.sql index 1c73adc5db..da6bd5da3c 100644 --- a/db/sql/00_msar.sql +++ b/db/sql/00_msar.sql @@ -1170,24 +1170,25 @@ $$ LANGUAGE plpgsql RETURNS NULL ON NULL INPUT; -- Rename table ------------------------------------------------------------------------------------ +DROP FUNCTION IF EXISTS msar.rename_table(text, text, text); CREATE OR REPLACE FUNCTION -__msar.rename_table(old_tab_name text, new_tab_name text) RETURNS text AS $$/* +msar.rename_table(sch_name text, old_tab_name text, new_tab_name text) RETURNS void AS $$/* Change a table's name, returning the command executed. Args: - old_tab_name: properly quoted, qualified table name - new_tab_name: properly quoted, unqualified table name + sch_name: unquoted schema name where the table lives + old_tab_name: unquoted, unqualified original table name + new_tab_name: unquoted, unqualified new table name */ BEGIN - RETURN __msar.exec_ddl( - 'ALTER TABLE %s RENAME TO %s', old_tab_name, new_tab_name - ); + EXECUTE format('ALTER TABLE %I.%I RENAME TO %I', sch_name, old_tab_name, new_tab_name); END; $$ LANGUAGE plpgsql RETURNS NULL ON NULL INPUT; +DROP FUNCTION IF EXISTS msar.rename_table(oid, text); CREATE OR REPLACE FUNCTION -msar.rename_table(tab_id oid, new_tab_name text) RETURNS text AS $$/* +msar.rename_table(tab_id oid, new_tab_name text) RETURNS void AS $$/* Change a table's name, returning the command executed. Args: @@ -1195,30 +1196,15 @@ Args: new_tab_name: unquoted, unqualified table name */ BEGIN - RETURN __msar.rename_table( - __msar.get_qualified_relation_name_or_null(tab_id), - quote_ident(new_tab_name) + PERFORM msar.rename_table( + msar.get_relation_schema_name(tab_id), + msar.get_relation_name(tab_id), + new_tab_name ); END; $$ LANGUAGE plpgsql RETURNS NULL ON NULL INPUT; -CREATE OR REPLACE FUNCTION -msar.rename_table(sch_name text, old_tab_name text, new_tab_name text) RETURNS text AS $$/* -Change a table's name, returning the command executed. - -Args: - sch_name: unquoted schema name where the table lives - old_tab_name: unquoted, unqualified original table name - new_tab_name: unquoted, unqualified new table name -*/ -DECLARE fullname text; -BEGIN - fullname := __msar.build_qualified_name_sql(sch_name, old_tab_name); - RETURN __msar.rename_table(fullname, quote_ident(new_tab_name)); -END; -$$ LANGUAGE plpgsql RETURNS NULL ON NULL INPUT; - -- Comment on table -------------------------------------------------------------------------------- From da89d221c04404b5c96f2d0d7460a838a8a40198 Mon Sep 17 00:00:00 2001 From: Sean Colsen Date: Sun, 21 Jul 2024 16:49:42 -0400 Subject: [PATCH 3/4] Fix error on rename table to same name --- db/sql/00_msar.sql | 4 ++++ db/sql/test_00_msar.sql | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/db/sql/00_msar.sql b/db/sql/00_msar.sql index da6bd5da3c..3522f5cd8d 100644 --- a/db/sql/00_msar.sql +++ b/db/sql/00_msar.sql @@ -1181,6 +1181,10 @@ Args: new_tab_name: unquoted, unqualified new table name */ BEGIN + IF old_tab_name = new_tab_name THEN + -- Return early if the names are the same. This avoids an error from Postgres. + RETURN; + END IF; EXECUTE format('ALTER TABLE %I.%I RENAME TO %I', sch_name, old_tab_name, new_tab_name); END; $$ LANGUAGE plpgsql RETURNS NULL ON NULL INPUT; diff --git a/db/sql/test_00_msar.sql b/db/sql/test_00_msar.sql index 91efd314ab..9bfaff637d 100644 --- a/db/sql/test_00_msar.sql +++ b/db/sql/test_00_msar.sql @@ -1370,6 +1370,19 @@ END; $$ LANGUAGE plpgsql; +CREATE OR REPLACE FUNCTION test_rename_table_with_same_name() RETURNS SETOF TEXT AS $$ +BEGIN + PERFORM __setup_alter_table(); + PERFORM msar.rename_table( + sch_name =>'public', + old_tab_name => 'alter_this_table', + new_tab_name => 'alter_this_table' + ); + RETURN NEXT has_table('alter_this_table'); +END; +$$ LANGUAGE plpgsql; + + CREATE OR REPLACE FUNCTION test_rename_table_using_oid() RETURNS SETOF TEXT AS $$ BEGIN PERFORM __setup_alter_table(); From 798e08ccfe256ecf6963e2878f137763388573d0 Mon Sep 17 00:00:00 2001 From: Sean Colsen Date: Mon, 22 Jul 2024 10:43:56 -0400 Subject: [PATCH 4/4] Fix failing test --- db/tests/tables/operations/test_alter.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/db/tests/tables/operations/test_alter.py b/db/tests/tables/operations/test_alter.py index 754a588956..af1f0b9b0c 100644 --- a/db/tests/tables/operations/test_alter.py +++ b/db/tests/tables/operations/test_alter.py @@ -1,3 +1,5 @@ +import json + from unittest.mock import patch import db.tables.operations.alter as tab_alter @@ -42,4 +44,8 @@ def test_alter_table(): assert call_args[0] == "conn" assert call_args[1] == "alter_table" assert call_args[2] == 12345 - assert call_args[3] == {"name": "newname", "description": "this is a comment", "columns": {}} + assert call_args[3] == json.dumps({ + "name": "newname", + "description": "this is a comment", + "columns": {}, + })