Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement data_modeling.move_columns RPC endpoint #3814

Merged
merged 4 commits into from
Sep 10, 2024
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
11 changes: 11 additions & 0 deletions db/tables/operations/move_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from sqlalchemy.dialects.postgresql import insert

from db import constants
from db.connection import exec_msar_func
from db.columns.base import MathesarColumn
from db.columns.operations.alter import batch_alter_table_drop_columns
from db.columns.operations.create import bulk_create_mathesar_column
Expand All @@ -10,6 +11,16 @@
from db.metadata import get_empty_metadata


def move_columns_to_referenced_table(conn, source_table_oid, target_table_oid, move_column_attnums):
exec_msar_func(
conn,
'move_columns_to_referenced_table',
source_table_oid,
target_table_oid,
move_column_attnums
)


def move_columns_between_related_tables(
source_table_oid,
target_table_oid,
Expand Down
1 change: 1 addition & 0 deletions docs/docs/api/rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ To use an RPC function:
- add_mapping_table
- suggest_types
- split_table
- move_columns
- MappingColumn

## Responses
Expand Down
33 changes: 32 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, split
from db.tables.operations import infer_types, split, move_columns as move_cols
from mathesar.rpc.exceptions.handlers import handle_rpc_exceptions
from mathesar.rpc.utils import connect

Expand Down Expand Up @@ -125,6 +125,7 @@ def split_table(
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.
database_id: The Django id of the database containing the table.
relationship_fk_column_name: The name to give the new foreign key column in the remainder table (optional)
"""
user = kwargs.get(REQUEST_KEY).user
Expand All @@ -136,3 +137,33 @@ def split_table(
extracted_table_name,
relationship_fk_column_name
)


@rpc_method(name="data_modeling.move_columns")
@http_basic_auth_login_required
@handle_rpc_exceptions
def move_columns(
*,
source_table_oid: int,
target_table_oid: int,
move_column_attnums: list[int],
database_id: int,
**kwargs
) -> None:
"""
Extract columns from a table to a referent table, linked by a foreign key.

Args:
source_table_oid: The OID of the source table whose column(s) we'll extract.
target_table_oid: The OID of the target table where the extracted column(s) will be added.
move_column_attnums: The list of attnum(s) to move from source table to the target table.
database_id: The Django id of the database containing the table.
"""
user = kwargs.get(REQUEST_KEY).user
with connect(database_id, user) as conn:
move_cols.move_columns_to_referenced_table(
conn,
source_table_oid,
target_table_oid,
move_column_attnums
)
52 changes: 48 additions & 4 deletions mathesar/tests/rpc/test_data_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from mathesar.models.users import User


def test_data_modeling_add_foreign_key_column(rf, monkeypatch):
def test_add_foreign_key_column(rf, monkeypatch):
_username = 'alice'
_password = 'pass1234'
_column_name = 'new_fkey_col'
Expand Down Expand Up @@ -56,7 +56,7 @@ def mock_add_fkey_col(
)


def test_data_modeling_add_mapping_table(rf, monkeypatch):
def test_add_mapping_table(rf, monkeypatch):
_username = 'alice'
_password = 'pass1234'
_schema_oid = 1234
Expand Down Expand Up @@ -103,7 +103,7 @@ def mock_add_mapping_table(
)


def test_data_modeling_suggest_types(rf, monkeypatch):
def test_suggest_types(rf, monkeypatch):
_username = 'alice'
_password = 'pass1234'
_table_oid = 12345
Expand Down Expand Up @@ -134,7 +134,7 @@ def mock_suggest_types(conn, table_oid):
)


def test_data_modeling_split_table(rf, monkeypatch):
def test_split_table(rf, monkeypatch):
_username = 'alice'
_password = 'pass1234'
_table_oid = 12345
Expand Down Expand Up @@ -180,3 +180,47 @@ def mock_split_table(
database_id=_database_id,
request=request
)


def test_move_columns(rf, monkeypatch):
_username = 'alice'
_password = 'pass1234'
_source_table_oid = 12345
_target_table_oid = 67891
_move_column_attnums = [2, 3, 4]
_database_id = 2
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_move_columns(
conn,
source_table_oid,
target_table_oid,
move_column_attnums
):
if (
source_table_oid != _source_table_oid
and target_table_oid != _target_table_oid
and move_column_attnums != _move_column_attnums
):
raise AssertionError('incorrect parameters passed')

monkeypatch.setattr(data_modeling, 'connect', mock_connect)
monkeypatch.setattr(data_modeling.move_cols, 'move_columns_to_referenced_table', mock_move_columns)
data_modeling.move_columns(
source_table_oid=_source_table_oid,
target_table_oid=_target_table_oid,
move_column_attnums=_move_column_attnums,
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 @@ -133,6 +133,11 @@
"data_modeling.split_table",
[user_is_authenticated]
),
(
data_modeling.move_columns,
"data_modeling.move_columns",
[user_is_authenticated]
),

(
databases.get,
Expand Down
Loading