-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for Snowflake schemas.
- Loading branch information
Showing
19 changed files
with
654 additions
and
203 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Empty file.
28 changes: 28 additions & 0 deletions
28
src/sqlalchemy_declarative_extensions/dialects/snowflake/query.py
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import annotations | ||
|
||
from sqlalchemy.engine.base import Connection | ||
from sqlalchemy.sql.expression import text | ||
|
||
|
||
def get_schemas_snowflake(connection: Connection): | ||
from sqlalchemy_declarative_extensions.schema.base import Schema | ||
|
||
schemas_query = text( | ||
"SELECT schema_name" | ||
" FROM information_schema.schemata" | ||
" WHERE schema_name NOT IN ('information_schema', 'pg_catalog', 'main')" | ||
) | ||
|
||
return { | ||
Schema(schema) for schema, *_ in connection.execute(schemas_query).fetchall() | ||
} | ||
|
||
|
||
def check_schema_exists_snowflake(connection: Connection, name: str) -> bool: | ||
schema_exists_query = text( | ||
"SELECT schema_name" | ||
" FROM information_schema.schemata" | ||
" WHERE schema_name = :schema" | ||
) | ||
row = connection.execute(schema_exists_query, {"schema": name}).scalar() | ||
return bool(row) |
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,6 +1,8 @@ | ||
from sqlalchemy_declarative_extensions.schema import compare | ||
from sqlalchemy_declarative_extensions.schema.base import Schema, Schemas | ||
|
||
__all__ = [ | ||
"Schema", | ||
"Schemas", | ||
"compare", | ||
] |
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,14 +1,18 @@ | ||
from sqlalchemy.schema import CreateSchema | ||
from __future__ import annotations | ||
|
||
from sqlalchemy_declarative_extensions.dialects import check_schema_exists | ||
from sqlalchemy_declarative_extensions.schema import Schema | ||
from sqlalchemy import MetaData | ||
from sqlalchemy.engine import Connection | ||
|
||
from sqlalchemy_declarative_extensions.schema import Schemas | ||
from sqlalchemy_declarative_extensions.schema.compare import compare_schemas | ||
|
||
def schema_ddl(schema: Schema): | ||
ddl = CreateSchema(schema.name) | ||
return ddl.execute_if(callable_=check_schema) | ||
|
||
def schema_ddl(metadata: MetaData, connection: Connection, **_): | ||
roles: Schemas | None = metadata.info.get("schemas") | ||
if not roles: # pragma: no cover | ||
return | ||
|
||
def check_schema(ddl, target, connection, **_): | ||
schema = ddl.element | ||
return check_schema_exists(connection, name=schema) | ||
result = compare_schemas(connection, roles) | ||
for op in result: | ||
statements = op.to_sql() | ||
connection.execute(statements) |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from pytest_mock_resources import create_postgres_fixture, create_sqlite_fixture | ||
from sqlalchemy.sql.ddl import CreateSchema | ||
|
||
from sqlalchemy_declarative_extensions import ( | ||
Schemas, | ||
declarative_database, | ||
register_sqlalchemy_events, | ||
) | ||
from sqlalchemy_declarative_extensions.dialects import check_schema_exists | ||
from sqlalchemy_declarative_extensions.sqlalchemy import declarative_base | ||
|
||
_Base = declarative_base() | ||
|
||
|
||
@declarative_database | ||
class Base(_Base): # type: ignore | ||
__abstract__ = True | ||
|
||
schemas = Schemas() | ||
|
||
|
||
register_sqlalchemy_events(Base.metadata, schemas=True) | ||
|
||
pg = create_postgres_fixture(scope="function", engine_kwargs={"echo": True}) | ||
sqlite = create_sqlite_fixture() | ||
|
||
|
||
def test_createall_schema_pg(pg): | ||
with pg.begin() as conn: | ||
conn.execute(CreateSchema("foo")) | ||
|
||
Base.metadata.create_all(bind=pg) | ||
|
||
with pg.connect() as conn: | ||
assert check_schema_exists(conn, "foo") is False | ||
|
||
|
||
def test_createall_schema_snowflake(snowflake): | ||
with snowflake.begin() as conn: | ||
conn.execute(CreateSchema("foo")) | ||
|
||
Base.metadata.create_all(bind=snowflake) | ||
|
||
with snowflake.connect() as conn: | ||
assert check_schema_exists(conn, "foo") is 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
Oops, something went wrong.