-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Conversion from view to materialized view.
- Loading branch information
Showing
9 changed files
with
212 additions
and
63 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
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,73 @@ | ||
from pytest_mock_resources import create_postgres_fixture | ||
from sqlalchemy import Column, text, types | ||
from sqlalchemy.ext.declarative import declarative_base | ||
|
||
from sqlalchemy_declarative_extensions import ( | ||
Row, | ||
Rows, | ||
Schemas, | ||
View, | ||
declarative_database, | ||
register_sqlalchemy_events, | ||
register_view, | ||
) | ||
|
||
Base_ = declarative_base() | ||
|
||
|
||
@declarative_database | ||
class Base(Base_): | ||
__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", | ||
materialized=True, | ||
) | ||
|
||
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 | ||
) | ||
|
||
|
||
def test_create_view_postgresql(pg): | ||
pg.execute(text("CREATE SCHEMA fooschema")) | ||
pg.execute(text("CREATE TABLE fooschema.foo (id integer)")) | ||
|
||
pg.execute( | ||
text( | ||
"CREATE VIEW fooschema.bar AS (SELECT id FROM fooschema.foo WHERE id < 10)" | ||
) | ||
) | ||
|
||
Base.metadata.create_all(bind=pg.connection()) | ||
|
||
result = [f.id for f in pg.query(Foo).all()] | ||
assert result == [1, 2, 12, 13] | ||
|
||
pg.execute(text("refresh materialized view fooschema.bar")) | ||
result = [f.id for f in pg.execute(text("select * from fooschema.bar")).all()] | ||
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,46 @@ | ||
from pytest_mock_resources import create_postgres_fixture | ||
from sqlalchemy import Column, text, types | ||
from sqlalchemy.ext.declarative import declarative_base | ||
|
||
from sqlalchemy_declarative_extensions import ( | ||
Schemas, | ||
Views, | ||
declarative_database, | ||
register_sqlalchemy_events, | ||
) | ||
from sqlalchemy_declarative_extensions.view.compare import compare_views | ||
|
||
Base_ = declarative_base() | ||
|
||
|
||
@declarative_database | ||
class Base(Base_): | ||
__abstract__ = True | ||
|
||
schemas = Schemas().are("fooschema") | ||
views = Views(ignore_unspecified=True) | ||
|
||
|
||
class Foo(Base): | ||
__tablename__ = "foo" | ||
__table_args__ = {"schema": "fooschema"} | ||
|
||
id = Column(types.Integer(), primary_key=True) | ||
|
||
|
||
register_sqlalchemy_events(Base.metadata, schemas=True, views=True, rows=True) | ||
|
||
pg = create_postgres_fixture( | ||
scope="function", engine_kwargs={"echo": True}, session=True | ||
) | ||
|
||
|
||
def test_ignore_views(pg): | ||
Base.metadata.create_all(bind=pg.connection()) | ||
|
||
pg.execute(text("CREATE VIEW meow as (SELECT id from fooschema.foo)")) | ||
|
||
# Verify this no longer sees changes to make! Failing here would imply the autogenerate | ||
# is not fully normalizing the difference. | ||
result = compare_views(pg.connection(), views=Base.views) | ||
assert result == [] |
Oops, something went wrong.