-
-
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.
Merge pull request #57 from DanCardin/dc/sqlite-views
fix: View declaration for sqlite.
- Loading branch information
Showing
7 changed files
with
183 additions
and
25 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
32 changes: 12 additions & 20 deletions
32
src/sqlalchemy_declarative_extensions/dialects/sqlite/schema.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 |
---|---|---|
@@ -1,27 +1,19 @@ | ||
from typing import Optional | ||
|
||
from sqlalchemy import column, literal, table | ||
from sqlalchemy import bindparam, text | ||
|
||
from sqlalchemy_declarative_extensions.sqlalchemy import select | ||
|
||
|
||
def make_sqlite_schema(schema: Optional[str] = None): | ||
tablename = "sqlite_schema" | ||
def views_query(schema: Optional[str] = None): | ||
tablename = "sqlite_master" | ||
if schema: | ||
tablename = f"{schema}.{tablename}" | ||
|
||
return table( | ||
tablename, | ||
column("type"), | ||
column("name"), | ||
column("sql"), | ||
) | ||
|
||
|
||
def views_query(schema: Optional[str] = None): | ||
sqlite_schema = make_sqlite_schema(schema) | ||
return select( | ||
literal(None), | ||
sqlite_schema.c.name.label("name"), | ||
sqlite_schema.c.sql.label("definition"), | ||
).where(sqlite_schema.c.type == "view") | ||
return text( | ||
"SELECT" # noqa: S608 | ||
" :schema AS schema," | ||
" name AS name," | ||
" sql AS definition," | ||
" false as materialized" | ||
f" FROM {tablename}" | ||
" WHERE type == 'view'", | ||
).bindparams(bindparam("schema", schema)) |
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,69 @@ | ||
from pytest_mock_resources import ( | ||
create_postgres_fixture, | ||
create_sqlite_fixture, | ||
) | ||
from sqlalchemy import Column, text, types | ||
|
||
from sqlalchemy_declarative_extensions import ( | ||
Row, | ||
Rows, | ||
View, | ||
declarative_database, | ||
register_sqlalchemy_events, | ||
register_view, | ||
) | ||
from sqlalchemy_declarative_extensions.sqlalchemy import declarative_base | ||
|
||
_Base = declarative_base() | ||
|
||
|
||
@declarative_database | ||
class Base(_Base): # type: ignore | ||
__abstract__ = True | ||
|
||
rows = Rows().are( | ||
Row("foo", id=1), | ||
Row("foo", id=2), | ||
Row("foo", id=12), | ||
Row("foo", id=13), | ||
) | ||
|
||
|
||
class Foo(Base): | ||
__tablename__ = "foo" | ||
|
||
id = Column(types.Integer(), primary_key=True) | ||
|
||
|
||
view = View("bar", "select id from foo where id < 10") | ||
register_view(Base.metadata, view) | ||
|
||
|
||
register_sqlalchemy_events(Base.metadata, schemas=True, views=True, rows=True) | ||
|
||
pg = create_postgres_fixture( | ||
scope="function", engine_kwargs={"echo": True}, session=True | ||
) | ||
sqlite = create_sqlite_fixture(scope="function", session=True) | ||
|
||
|
||
def test_create_view_postgresql(pg): | ||
run_test(pg) | ||
|
||
|
||
def test_create_view_sqlite(sqlite): | ||
run_test(sqlite) | ||
|
||
|
||
def run_test(session): | ||
session.execute(text("CREATE TABLE foo (id integer)")) | ||
session.execute(text("CREATE VIEW bar AS SELECT id FROM foo WHERE id = 1")) | ||
session.execute(text("INSERT INTO foo (id) VALUES (1), (2), (12), (13)")) | ||
|
||
result = [f.id for f in session.execute(text("SELECT id from bar")).fetchall()] | ||
assert result == [1] | ||
|
||
Base.metadata.create_all(bind=session.connection()) | ||
|
||
result = [f.id for f in session.execute(text("SELECT id from bar")).fetchall()] | ||
assert result == [1, 2] |
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,87 @@ | ||
from pytest_mock_resources import ( | ||
create_postgres_fixture, | ||
create_sqlite_fixture, | ||
) | ||
from sqlalchemy import Column, text, types | ||
|
||
from sqlalchemy_declarative_extensions import ( | ||
Row, | ||
Rows, | ||
Schemas, | ||
View, | ||
declarative_database, | ||
register_sqlalchemy_events, | ||
register_view, | ||
) | ||
from sqlalchemy_declarative_extensions.sqlalchemy import declarative_base | ||
|
||
_Base = declarative_base() | ||
|
||
|
||
@declarative_database | ||
class Base(_Base): # type: ignore | ||
__abstract__ = True | ||
|
||
schemas = Schemas().are("fooschema") | ||
rows = Rows().are( | ||
Row("fooschema.foo", id=1), | ||
Row("fooschema.foo", id=2), | ||
Row("fooschema.foo", id=12), | ||
Row("fooschema.foo", id=13), | ||
) | ||
|
||
|
||
class Foo(Base): | ||
__tablename__ = "foo" | ||
__table_args__ = {"schema": "fooschema"} | ||
|
||
id = Column(types.Integer(), primary_key=True) | ||
|
||
|
||
# Register imperitively | ||
view = View( | ||
"bar", | ||
"select id from fooschema.foo where id < 10", | ||
schema="fooschema", | ||
) | ||
|
||
register_view(Base.metadata, view) | ||
|
||
|
||
register_sqlalchemy_events(Base.metadata, schemas=True, views=True, rows=True) | ||
|
||
pg = create_postgres_fixture( | ||
scope="function", engine_kwargs={"echo": True}, session=True | ||
) | ||
sqlite = create_sqlite_fixture(scope="function", session=True) | ||
|
||
|
||
def test_create_view_postgresql(pg): | ||
pg.execute(text("CREATE SCHEMA fooschema")) | ||
run_test(pg) | ||
|
||
|
||
def test_create_view_sqlite(sqlite): | ||
sqlite.execute(text("ATTACH DATABASE ':memory:' AS fooschema")) | ||
run_test(sqlite) | ||
|
||
|
||
def run_test(session): | ||
session.execute(text("CREATE TABLE fooschema.foo (id integer)")) | ||
session.execute( | ||
text("CREATE VIEW fooschema.bar AS SELECT id FROM fooschema.foo WHERE id = 1") | ||
) | ||
session.execute(text("INSERT INTO fooschema.foo (id) VALUES (1), (2), (12), (13)")) | ||
session.commit() | ||
|
||
result = [ | ||
f.id for f in session.execute(text("SELECT id from fooschema.bar")).fetchall() | ||
] | ||
assert result == [1] | ||
|
||
Base.metadata.create_all(bind=session.connection()) | ||
|
||
result = [ | ||
f.id for f in session.execute(text("SELECT id from fooschema.bar")).fetchall() | ||
] | ||
assert result == [1, 2] |