Skip to content

Commit

Permalink
Merge pull request #3892 from mathesar-foundation/exp_schema_oid
Browse files Browse the repository at this point in the history
Add `schema_oid` to `Explorations` model
  • Loading branch information
seancolsen authored Sep 26, 2024
2 parents 57e1df6 + 24c13da commit c9f2a6f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
18 changes: 18 additions & 0 deletions mathesar/migrations/0017_explorations_schema_oid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.11 on 2024-09-26 18:13

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('mathesar', '0016_alter_explorations_display_names'),
]

operations = [
migrations.AddField(
model_name='explorations',
name='schema_oid',
field=models.PositiveBigIntegerField(),
),
]
1 change: 1 addition & 0 deletions mathesar/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class Explorations(BaseModel):
database = models.ForeignKey('Database', on_delete=models.CASCADE)
name = models.CharField(max_length=128, unique=True)
base_table_oid = models.PositiveBigIntegerField()
schema_oid = models.PositiveBigIntegerField()
initial_columns = models.JSONField()
transformations = models.JSONField(null=True)
display_options = models.JSONField(null=True)
Expand Down
24 changes: 22 additions & 2 deletions mathesar/rpc/explorations.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ExplorationInfo(TypedDict):
database_id: The Django id of the database containing the exploration.
name: The name of the exploration.
base_table_oid: The OID of the base table of the exploration on the database.
schema_oid: The OID of the schema containing the base table of the exploration.
initial_columns: A list describing the columns to be included in the exploration.
transformations: A list describing the transformations to be made on the included columns.
display_options: A list describing metadata for the columns in the explorations.
Expand All @@ -39,6 +40,7 @@ class ExplorationInfo(TypedDict):
database_id: int
name: str
base_table_oid: int
schema_oid: int
initial_columns: list
transformations: Optional[list]
display_options: Optional[list]
Expand All @@ -52,6 +54,7 @@ def from_model(cls, model):
database_id=model.database.id,
name=model.name,
base_table_oid=model.base_table_oid,
schema_oid=model.schema_oid,
initial_columns=model.initial_columns,
transformations=model.transformations,
display_options=model.display_options,
Expand All @@ -68,6 +71,7 @@ class ExplorationDef(TypedDict):
database_id: The Django id of the database containing the exploration.
name: The name of the exploration.
base_table_oid: The OID of the base table of the exploration on the database.
schema_oid: The OID of the schema containing the base table of the exploration.
initial_columns: A list describing the columns to be included in the exploration.
transformations: A list describing the transformations to be made on the included columns.
display_options: A list describing metadata for the columns in the explorations.
Expand All @@ -77,6 +81,7 @@ class ExplorationDef(TypedDict):
database_id: int
name: str
base_table_oid: int
schema_oid: int
initial_columns: list
transformations: Optional[list]
display_options: Optional[list]
Expand Down Expand Up @@ -130,17 +135,18 @@ def from_dict(cls, e):
@rpc_method(name="explorations.list")
@http_basic_auth_login_required
@handle_rpc_exceptions
def list_(*, database_id: int, **kwargs) -> list[ExplorationInfo]:
def list_(*, database_id: int, schema_oid: int = None, **kwargs) -> list[ExplorationInfo]:
"""
List information about explorations for a database. Exposed as `list`.
Args:
database_id: The Django id of the database containing the explorations.
schema_oid: The OID of the schema containing the base table(s) of the exploration(s).(optional)
Returns:
A list of exploration details.
"""
explorations = list_explorations(database_id)
explorations = list_explorations(database_id, schema_oid)
return [ExplorationInfo.from_model(exploration) for exploration in explorations]


Expand Down Expand Up @@ -183,6 +189,11 @@ def run(*, exploration_def: ExplorationDef, limit: int = 100, offset: int = 0, *
Args:
exploration_def: A dict describing an exploration to run.
limit: The max number of rows to return.(default 100)
offset: The number of rows to skip.(default 0)
Returns:
The result of the exploration run.
"""
user = kwargs.get(REQUEST_KEY).user
with connect(exploration_def['database_id'], user) as conn:
Expand All @@ -201,6 +212,9 @@ def run_saved(*, exploration_id: int, limit: int = 100, offset: int = 0, **kwarg
exploration_id: The Django id of the exploration to run.
limit: The max number of rows to return.(default 100)
offset: The number of rows to skip.(default 0)
Returns:
The result of the exploration run.
"""
user = kwargs.get(REQUEST_KEY).user
exp_model = Explorations.objects.get(id=exploration_id)
Expand All @@ -218,6 +232,9 @@ def replace(*, new_exploration: ExplorationInfo) -> ExplorationInfo:
Args:
new_exploration: A dict describing the exploration to replace, including the updated fields.
Returns:
The exploration details for the replaced exploration.
"""
replaced_exp_model = replace_exploration(new_exploration)
return ExplorationInfo.from_model(replaced_exp_model)
Expand All @@ -232,6 +249,9 @@ def add(*, exploration_def: ExplorationDef) -> ExplorationInfo:
Args:
exploration_def: A dict describing the exploration to create.
Returns:
The exploration details for the newly created exploration.
"""
exp_model = create_exploration(exploration_def)
return ExplorationInfo.from_model(exp_model)
6 changes: 5 additions & 1 deletion mathesar/tests/rpc/test_explorations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_explorations_list(rf, monkeypatch):
request.user = User(username='alice', password='pass1234')
database_id = 11

def mock_exploration_info(_database_id):
def mock_exploration_info(_database_id, _schema_oid):
if _database_id != database_id:
raise AssertionError('incorrect parameters passed')
return [
Expand All @@ -24,6 +24,7 @@ def mock_exploration_info(_database_id):
database=Database(id=1),
name='page count',
base_table_oid=12375,
schema_oid=2200,
initial_columns=[
{'id': 51586, 'alias': 'Items_Acquisition Date'},
{'id': 51598, 'alias': 'Items_id'},
Expand All @@ -49,6 +50,7 @@ def mock_exploration_info(_database_id):
database=Database(id=1),
name='ISBN',
base_table_oid=12356,
schema_oid=2200,
initial_columns=[
{'id': 51594, 'alias': 'Publishers_Name'},
{'id': 51575, 'alias': 'Books_ISBN', 'jp_path': [[51593, 51579]]}
Expand Down Expand Up @@ -81,6 +83,7 @@ def mock_exploration_info(_database_id):
'database_id': 1,
'name': 'page count',
'base_table_oid': 12375,
'schema_oid': 2200,
'initial_columns': [
{'id': 51586, 'alias': 'Items_Acquisition Date'},
{'id': 51598, 'alias': 'Items_id'},
Expand All @@ -105,6 +108,7 @@ def mock_exploration_info(_database_id):
'database_id': 1,
'name': 'ISBN',
'base_table_oid': 12356,
'schema_oid': 2200,
'initial_columns': [
{'id': 51594, 'alias': 'Publishers_Name'},
{'id': 51575, 'alias': 'Books_ISBN', 'jp_path': [[51593, 51579]]}
Expand Down
6 changes: 5 additions & 1 deletion mathesar/utils/explorations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
from mathesar.state import get_cached_metadata


def list_explorations(database_id):
def list_explorations(database_id, schema_oid=None):
if schema_oid is not None:
return Explorations.objects.filter(database__id=database_id, schema_oid=schema_oid)
return Explorations.objects.filter(database__id=database_id)


Expand All @@ -27,6 +29,7 @@ def replace_exploration(new_exploration):
database=Database.objects.get(id=new_exploration["database_id"]),
name=new_exploration["name"],
base_table_oid=new_exploration["base_table_oid"],
schema_oid=new_exploration["schema_oid"],
initial_columns=new_exploration["initial_columns"],
transformations=new_exploration.get("transformations"),
display_options=new_exploration.get("display_options"),
Expand All @@ -41,6 +44,7 @@ def create_exploration(exploration_def):
database=Database.objects.get(id=exploration_def["database_id"]),
name=exploration_def["name"],
base_table_oid=exploration_def["base_table_oid"],
schema_oid=exploration_def["schema_oid"],
initial_columns=exploration_def["initial_columns"],
transformations=exploration_def.get("transformations"),
display_options=exploration_def.get("display_options"),
Expand Down

0 comments on commit c9f2a6f

Please sign in to comment.