Skip to content

Commit

Permalink
Merge pull request #3699 from mathesar-foundation/tables_patch
Browse files Browse the repository at this point in the history
Fix issues with `tables.patch` RPC method
  • Loading branch information
seancolsen authored Jul 22, 2024
2 parents 92a2656 + 798e08c commit 3a9e51c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
42 changes: 16 additions & 26 deletions db/sql/00_msar.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1171,55 +1171,45 @@ $$ 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
);
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;


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:
tab_id: the OID of the table whose name we want to change
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 --------------------------------------------------------------------------------

Expand Down
13 changes: 13 additions & 0 deletions db/sql/test_00_msar.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion db/tables/operations/alter.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]


Expand Down
8 changes: 7 additions & 1 deletion db/tests/tables/operations/test_alter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from unittest.mock import patch
import db.tables.operations.alter as tab_alter

Expand Down Expand Up @@ -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": {},
})

0 comments on commit 3a9e51c

Please sign in to comment.