Skip to content

Commit

Permalink
Merge pull request #3803 from mathesar-foundation/wire_column_extraction
Browse files Browse the repository at this point in the history
Implement `data_modeling.split_table` RPC functions
  • Loading branch information
mathemancer authored Sep 3, 2024
2 parents 6ceea75 + 2a065e3 commit 8933327
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
19 changes: 18 additions & 1 deletion db/tables/operations/split.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from db.connection import execute_msar_func_with_engine
from db.connection import execute_msar_func_with_engine, exec_msar_func


def extract_columns_from_table(
Expand All @@ -14,3 +14,20 @@ def extract_columns_from_table(
)
extracted_table_oid, new_fkey_attnum = curr.fetchone()[0]
return extracted_table_oid, old_table_oid, new_fkey_attnum


def split_table(
conn,
old_table_oid,
extracted_column_attnums,
extracted_table_name,
relationship_fk_column_name=None
):
exec_msar_func(
conn,
'extract_columns_from_table',
old_table_oid,
extracted_column_attnums,
extracted_table_name,
relationship_fk_column_name
)
1 change: 1 addition & 0 deletions docs/docs/api/rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ To use an RPC function:
- add_foreign_key_column
- add_mapping_table
- suggest_types
- split_table
- MappingColumn

## Responses
Expand Down
34 changes: 33 additions & 1 deletion mathesar/rpc/data_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from modernrpc.auth.basic import http_basic_auth_login_required

from db.links.operations import create as links_create
from db.tables.operations import infer_types
from db.tables.operations import infer_types, split
from mathesar.rpc.exceptions.handlers import handle_rpc_exceptions
from mathesar.rpc.utils import connect

Expand Down Expand Up @@ -104,3 +104,35 @@ def suggest_types(*, table_oid: int, database_id: int, **kwargs) -> dict:
user = kwargs.get(REQUEST_KEY).user
with connect(database_id, user) as conn:
return infer_types.infer_table_column_data_types(conn, table_oid)


@rpc_method(name="data_modeling.split_table")
@http_basic_auth_login_required
@handle_rpc_exceptions
def split_table(
*,
table_oid: int,
column_attnums: list,
extracted_table_name: str,
database_id: int,
relationship_fk_column_name: str = None,
**kwargs
) -> None:
"""
Extract columns from a table to create a new table, linked by a foreign key.
Args:
table_oid: The OID of the table whose columns we'll extract.
column_attnums: A list of the attnums of the columns to extract.
extracted_table_name: The name of the new table to be made from the extracted columns.
relationship_fk_column_name: The name to give the new foreign key column in the remainder table (optional)
"""
user = kwargs.get(REQUEST_KEY).user
with connect(database_id, user) as conn:
split.split_table(
conn,
table_oid,
column_attnums,
extracted_table_name,
relationship_fk_column_name
)
48 changes: 48 additions & 0 deletions mathesar/tests/rpc/test_data_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,51 @@ def mock_suggest_types(conn, table_oid):
database_id=_database_id,
request=request,
)


def test_data_modeling_split_table(rf, monkeypatch):
_username = 'alice'
_password = 'pass1234'
_table_oid = 12345
_database_id = 2
_column_attnums = [2, 3, 4]
_extracted_table_name = 'extracted_table'
_relationship_fk_column_name = 'fk_col'
request = rf.post('/api/rpc/v0/', data={})
request.user = User(username=_username, password=_password)

@contextmanager
def mock_connect(database_id, user):
if database_id == _database_id and user.username == _username:
try:
yield True
finally:
pass
else:
raise AssertionError('incorrect parameters passed')

def mock_split_table(
conn,
table_oid,
column_attnums,
extracted_table_name,
relationship_fk_column_name
):
if (
table_oid != _table_oid
and column_attnums != _column_attnums
and extracted_table_name != _extracted_table_name
and relationship_fk_column_name != _relationship_fk_column_name
):
raise AssertionError('incorrect parameters passed')

monkeypatch.setattr(data_modeling, 'connect', mock_connect)
monkeypatch.setattr(data_modeling.split, 'split_table', mock_split_table)
data_modeling.split_table(
table_oid=_table_oid,
column_attnums=_column_attnums,
extracted_table_name=_extracted_table_name,
relationship_fk_column_name=_relationship_fk_column_name,
database_id=_database_id,
request=request
)
5 changes: 5 additions & 0 deletions mathesar/tests/rpc/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@
"data_modeling.suggest_types",
[user_is_authenticated]
),
(
data_modeling.split_table,
"data_modeling.split_table",
[user_is_authenticated]
),

(
databases.get,
Expand Down

0 comments on commit 8933327

Please sign in to comment.