Skip to content

Commit

Permalink
Merge branch 'develop' into records_list_filter
Browse files Browse the repository at this point in the history
  • Loading branch information
mathemancer authored Jul 25, 2024
2 parents 79631b0 + 8688414 commit 2cadd69
Show file tree
Hide file tree
Showing 64 changed files with 463 additions and 1,778 deletions.
59 changes: 26 additions & 33 deletions db/sql/00_msar.sql
Original file line number Diff line number Diff line change
Expand Up @@ -838,13 +838,16 @@ Each returned JSON object in the array will have the form:
Args:
sch_id: The OID or name of the schema.
*/
SELECT jsonb_agg(
jsonb_build_object(
'oid', pgc.oid::bigint,
'name', pgc.relname,
'schema', pgc.relnamespace::bigint,
'description', msar.obj_description(pgc.oid, 'pg_class')
)
SELECT coalesce(
jsonb_agg(
jsonb_build_object(
'oid', pgc.oid::bigint,
'name', pgc.relname,
'schema', pgc.relnamespace::bigint,
'description', msar.obj_description(pgc.oid, 'pg_class')
)
),
'[]'::jsonb
)
FROM pg_catalog.pg_class AS pgc
LEFT JOIN pg_catalog.pg_namespace AS pgn ON pgc.relnamespace = pgn.oid
Expand Down Expand Up @@ -1171,55 +1174,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
15 changes: 14 additions & 1 deletion 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 Expand Up @@ -2683,7 +2696,7 @@ BEGIN

-- Test table info for schema 'alice' that contains no tables
RETURN NEXT is(
alice_table_info, null
alice_table_info, '[]'::jsonb
);
END;
$$ LANGUAGE plpgsql;
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": {},
})
7 changes: 4 additions & 3 deletions docs/docs/api/rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ To use an RPC function:
- add
- patch
- delete
- list_with_metadata
- ColumnInfo
- ColumnListReturn
- CreatableColumnInfo
Expand All @@ -148,9 +149,9 @@ To use an RPC function:
options:
members:
- list_
- patch
- ColumnMetaData
- SettableColumnMetaData
- set_
- ColumnMetaDataRecord
- ColumnMetaDataBlob

## Types

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Generated by Django 4.2.11 on 2024-07-24 10:53

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('mathesar', '0012_merge_20240718_0628'),
]

operations = [
migrations.AlterField(
model_name='columnmetadata',
name='bool_false',
field=models.CharField(null=True),
),
migrations.AlterField(
model_name='columnmetadata',
name='bool_input',
field=models.CharField(choices=[('dropdown', 'dropdown'), ('checkbox', 'checkbox')], null=True),
),
migrations.AlterField(
model_name='columnmetadata',
name='bool_true',
field=models.CharField(null=True),
),
migrations.AlterField(
model_name='columnmetadata',
name='date_format',
field=models.CharField(null=True),
),
migrations.AlterField(
model_name='columnmetadata',
name='duration_max',
field=models.CharField(max_length=255, null=True),
),
migrations.AlterField(
model_name='columnmetadata',
name='duration_min',
field=models.CharField(max_length=255, null=True),
),
migrations.AlterField(
model_name='columnmetadata',
name='duration_show_units',
field=models.BooleanField(null=True),
),
migrations.AlterField(
model_name='columnmetadata',
name='mon_currency_location',
field=models.CharField(choices=[('after-minus', 'after-minus'), ('end-with-space', 'end-with-space')], null=True),
),
migrations.AlterField(
model_name='columnmetadata',
name='mon_currency_symbol',
field=models.CharField(null=True),
),
migrations.AlterField(
model_name='columnmetadata',
name='num_max_frac_digits',
field=models.PositiveIntegerField(null=True),
),
migrations.AlterField(
model_name='columnmetadata',
name='num_min_frac_digits',
field=models.PositiveIntegerField(null=True),
),
migrations.AlterField(
model_name='columnmetadata',
name='num_show_as_perc',
field=models.BooleanField(null=True),
),
migrations.AlterField(
model_name='columnmetadata',
name='time_format',
field=models.CharField(null=True),
),
]
26 changes: 13 additions & 13 deletions mathesar/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,23 @@ class ColumnMetaData(BaseModel):
attnum = models.SmallIntegerField()
bool_input = models.CharField(
choices=[("dropdown", "dropdown"), ("checkbox", "checkbox")],
blank=True
null=True
)
bool_true = models.CharField(default='True')
bool_false = models.CharField(default='False')
num_min_frac_digits = models.PositiveIntegerField(default=0)
num_max_frac_digits = models.PositiveIntegerField(default=20)
num_show_as_perc = models.BooleanField(default=False)
mon_currency_symbol = models.CharField(default="$")
bool_true = models.CharField(null=True)
bool_false = models.CharField(null=True)
num_min_frac_digits = models.PositiveIntegerField(null=True)
num_max_frac_digits = models.PositiveIntegerField(null=True)
num_show_as_perc = models.BooleanField(null=True)
mon_currency_symbol = models.CharField(null=True)
mon_currency_location = models.CharField(
choices=[("after-minus", "after-minus"), ("end-with-space", "end-with-space")],
default="after-minus"
null=True
)
time_format = models.CharField(blank=True)
date_format = models.CharField(blank=True)
duration_min = models.CharField(max_length=255, blank=True)
duration_max = models.CharField(max_length=255, blank=True)
duration_show_units = models.BooleanField(default=True)
time_format = models.CharField(null=True)
date_format = models.CharField(null=True)
duration_min = models.CharField(max_length=255, null=True)
duration_max = models.CharField(max_length=255, null=True)
duration_show_units = models.BooleanField(null=True)

class Meta:
constraints = [
Expand Down
24 changes: 24 additions & 0 deletions mathesar/rpc/columns/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
from db.columns.operations.create import add_columns_to_table
from db.columns.operations.drop import drop_columns_from_table
from db.columns.operations.select import get_column_info_for_table
from mathesar.rpc.columns.metadata import ColumnMetaDataBlob
from mathesar.rpc.exceptions.handlers import handle_rpc_exceptions
from mathesar.rpc.utils import connect
from mathesar.utils.columns import get_columns_meta_data


class TypeOptions(TypedDict, total=False):
Expand Down Expand Up @@ -282,3 +284,25 @@ def delete(
user = kwargs.get(REQUEST_KEY).user
with connect(database_id, user) as conn:
return drop_columns_from_table(table_oid, column_attnums, conn)


@rpc_method(name="columns.list_with_metadata")
@http_basic_auth_login_required
@handle_rpc_exceptions
def list_with_metadata(*, table_oid: int, database_id: int, **kwargs) -> list:
"""
List information about columns for a table, along with the metadata associated with each column.
Args:
table_oid: Identity of the table in the user's database.
database_id: The Django id of the database containing the table.
Returns:
A list of column details.
"""
user = kwargs.get(REQUEST_KEY).user
with connect(database_id, user) as conn:
column_info = get_column_info_for_table(table_oid, conn)
column_metadata = get_columns_meta_data(table_oid, database_id)
metadata_map = {
c.attnum: ColumnMetaDataBlob.from_model(c) for c in column_metadata
}
return [col | {"metadata": metadata_map.get(col["id"])} for col in column_info]
Loading

0 comments on commit 2cadd69

Please sign in to comment.