Skip to content

Commit

Permalink
remove views
Browse files Browse the repository at this point in the history
  • Loading branch information
alex75 committed Aug 28, 2023
1 parent 6ec1583 commit 9e48b85
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
35 changes: 35 additions & 0 deletions alembic/versions/875f93f41d79_remove_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""remove views.
Revision ID: 875f93f41d79
Revises: fe3054fbc892
Create Date: 2023-08-28 10:29:58.886117
"""

from alembic import op

# revision identifiers, used by Alembic.
revision = "875f93f41d79"
down_revision = "fe3054fbc892"
branch_labels = None
depends_on = None


def upgrade() -> None:
op.execute("DROP VIEW c3s_resources")
op.execute("DROP VIEW cams_resources")


def downgrade() -> None:
sql1 = """
CREATE VIEW c3s_resources AS
SELECT * FROM resources
WHERE portal = 'c3s'
"""
op.execute(sql1)
sql2 = """
CREATE VIEW cams_resources AS
SELECT * FROM resources
WHERE portal = 'cams'
"""
op.execute(sql2)
17 changes: 0 additions & 17 deletions cads_catalogue/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,23 +278,6 @@ class Licence(BaseModel):
)


# create views
c3s_view_stmt = sa.select(Resource).filter_by(portal="c3s")
cams_view_stmt = sa.select(Resource).filter_by(portal="cams")


class C3SView(BaseModel):
"""C3S resources view."""

__table__ = sqlalchemy_utils.create_view("c3s_resources", c3s_view_stmt, metadata) # type: ignore


class CAMSView(BaseModel):
"""CAMS resources view."""

__table__ = sqlalchemy_utils.create_view("cams_resources", cams_view_stmt, metadata) # type: ignore


def ensure_session_obj(session_obj: sa.orm.sessionmaker | None) -> sa.orm.sessionmaker:
"""If `session_obj` is None, create a new session object.
Expand Down
4 changes: 1 addition & 3 deletions tests/test_02_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ def test_init_database(postgresql: Connection[str]) -> None:
"SELECT table_name FROM information_schema.tables WHERE table_schema='public'"
)
expected_tables_at_beginning: set[str] = set()
expected_tables_complete = set(database.metadata.tables).union(
{"alembic_version", "c3s_resources", "cams_resources"}
)
expected_tables_complete = set(database.metadata.tables).union({"alembic_version"})
assert set(conn.execute(query).scalars()) == expected_tables_at_beginning # type: ignore

database.init_database(connection_string, force=True)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_90_entry_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_init_db(postgresql: Connection[str]) -> None:

assert result.exit_code == 0
assert set(conn.execute(query).scalars()) == set(database.metadata.tables).union(
{"alembic_version", "c3s_resources", "cams_resources"}
{"alembic_version"}
)


Expand Down

0 comments on commit 9e48b85

Please sign in to comment.