-
-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3620 from mathesar-foundation/schemas_add
Implement `schemas.add` RPC method
- Loading branch information
Showing
14 changed files
with
176 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,53 @@ | ||
from db.schemas.operations.alter import comment_on_schema | ||
from db.connection import execute_msar_func_with_engine | ||
from db.connection import execute_msar_func_with_engine, exec_msar_func | ||
|
||
|
||
def create_schema(schema_name, engine, comment=None, if_not_exists=False): | ||
def create_schema_via_sql_alchemy(schema_name, engine, description=None): | ||
""" | ||
Creates a schema. | ||
Creates a schema using a SQLAlchemy engine. | ||
Args: | ||
schema_name: Name of the schema to create. | ||
engine: SQLAlchemy engine object for connecting. | ||
comment: The new comment. Any quotes or special characters must | ||
be escaped. | ||
if_not_exists: Whether to ignore an error if the schema does | ||
exist. | ||
description: A new description to set on the schema. | ||
If a schema already exists with the given name, this function will raise an error. | ||
Returns: | ||
Returns a string giving the command that was run. | ||
The integer oid of the newly created schema. | ||
""" | ||
result = execute_msar_func_with_engine( | ||
engine, 'create_schema', schema_name, if_not_exists | ||
return execute_msar_func_with_engine( | ||
engine, 'create_schema', schema_name, description | ||
).fetchone()[0] | ||
|
||
if comment: | ||
comment_on_schema(schema_name, engine, comment) | ||
return result | ||
|
||
def create_schema_if_not_exists_via_sql_alchemy(schema_name, engine): | ||
""" | ||
Ensure that a schema exists using a SQLAlchemy engine. | ||
Args: | ||
schema_name: Name of the schema to create. | ||
engine: SQLAlchemy engine object for connecting. | ||
Returns: | ||
The integer oid of the newly created schema. | ||
""" | ||
return execute_msar_func_with_engine( | ||
engine, 'create_schema_if_not_exists', schema_name | ||
).fetchone()[0] | ||
|
||
|
||
def create_schema(schema_name, conn, description=None): | ||
""" | ||
Create a schema using a psycopg connection. | ||
Args: | ||
schema_name: Name of the schema to create. | ||
conn: a psycopg connection | ||
description: A new description to set on the schema. | ||
If a schema already exists with the given name, this function will raise an error. | ||
Returns: | ||
The integer oid of the newly created schema. | ||
""" | ||
return exec_msar_func(conn, 'create_schema', schema_name, description).fetchone()[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,16 @@ | ||
import pytest | ||
from unittest.mock import patch | ||
import db.schemas.operations.create as sch_create | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"if_not_exists", [(True), (False), (None)] | ||
) | ||
def test_create_schema(engine_with_schema, if_not_exists): | ||
def test_create_schema_via_sql_alchemy(engine_with_schema): | ||
engine = engine_with_schema | ||
with patch.object(sch_create, 'execute_msar_func_with_engine') as mock_exec: | ||
sch_create.create_schema( | ||
sch_create.create_schema_via_sql_alchemy( | ||
schema_name='new_schema', | ||
engine=engine, | ||
comment=None, | ||
if_not_exists=if_not_exists | ||
description=None, | ||
) | ||
call_args = mock_exec.call_args_list[0][0] | ||
assert call_args[0] == engine | ||
assert call_args[1] == "create_schema" | ||
assert call_args[2] == "new_schema" | ||
assert call_args[3] == if_not_exists or False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ To use an RPC function: | |
options: | ||
members: | ||
- list_ | ||
- add | ||
- delete | ||
- SchemaInfo | ||
|
||
|
Oops, something went wrong.