Skip to content

Commit

Permalink
init-db --force fixed (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex75 authored Jan 23, 2024
1 parent ec0657f commit aa32761
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 3 additions & 1 deletion cads_broker/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,9 @@ def init_database(connection_string: str, force: bool = False) -> sa.engine.Engi
if not sqlalchemy_utils.database_exists(engine.url):
sqlalchemy_utils.create_database(engine.url)
# cleanup and create the schema
BaseModel.metadata.drop_all(engine)
cacholote.database.Base.metadata.drop_all(engine)
cacholote.database.Base.metadata.create_all(engine)
BaseModel.metadata.drop_all(engine)
BaseModel.metadata.create_all(engine)
alembic.command.stamp(alembic_cfg, "head")
else:
Expand All @@ -608,6 +608,8 @@ def init_database(connection_string: str, force: bool = False) -> sa.engine.Engi
if force:
# cleanup and create the schema
BaseModel.metadata.drop_all(engine)
cacholote.database.Base.metadata.drop_all(engine)
cacholote.database.Base.metadata.create_all(engine)
BaseModel.metadata.create_all(engine)
alembic.command.stamp(alembic_cfg, "head")
else:
Expand Down
12 changes: 8 additions & 4 deletions tests/test_02_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,10 @@ def test_init_database(postgresql: Connection[str]) -> None:

# verify create structure
db.init_database(connection_string, force=True)
expected_tables_complete = set(db.BaseModel.metadata.tables).union(
{"alembic_version"}
expected_tables_complete = (
set(db.BaseModel.metadata.tables)
.union({"alembic_version"})
.union(set(cacholote.database.Base.metadata.tables))
)
assert set(conn.execute(query).scalars()) == expected_tables_complete # type: ignore

Expand Down Expand Up @@ -779,8 +781,10 @@ def test_init_database_with_password(postgresql2: Connection[str]) -> None:

# verify create structure
db.init_database(connection_string, force=True)
expected_tables_complete = set(db.BaseModel.metadata.tables).union(
{"alembic_version"}
expected_tables_complete = (
set(db.BaseModel.metadata.tables)
.union({"alembic_version"})
.union(set(cacholote.database.Base.metadata.tables))
)
assert set(conn.execute(query).scalars()) == expected_tables_complete # type: ignore

Expand Down
3 changes: 2 additions & 1 deletion tests/test_90_entry_points.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any

import cacholote
import sqlalchemy as sa
from psycopg import Connection
from typer.testing import CliRunner
Expand Down Expand Up @@ -40,5 +41,5 @@ def test_init_db(postgresql: Connection[str], mocker) -> None:
)
assert set(conn.execute(query).scalars()) == set(
database.BaseModel.metadata.tables
).union({"alembic_version"})
).union({"alembic_version"}).union(set(cacholote.database.Base.metadata.tables))
conn.close()

0 comments on commit aa32761

Please sign in to comment.