Skip to content

Commit 90d1def

Browse files
committed
ignore creating the extension via migration
1 parent 639c1fc commit 90d1def

2 files changed

Lines changed: 37 additions & 37 deletions

File tree

alembic_osm/versions/a1b2c3d4e5f6_tasking_mvp_schema.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,26 @@ def _drop_enum_if_present(bind, name: str) -> None:
4747
bind.execute(text(f'DROP TYPE IF EXISTS "{name}"'))
4848

4949

50-
def _postgis_available(bind) -> bool:
51-
return bool(
50+
def _assert_postgis_installed(bind) -> None:
51+
"""Require the postgis extension to be installed in this database."""
52+
installed = bool(
5253
bind.execute(
53-
text("SELECT 1 FROM pg_available_extensions WHERE name = 'postgis'")
54+
text("SELECT 1 FROM pg_extension WHERE extname = 'postgis'")
5455
).scalar()
5556
)
57+
if not installed:
58+
raise RuntimeError(
59+
"postgis extension is not installed in this database. "
60+
"Run `CREATE EXTENSION IF NOT EXISTS postgis;` before migrations."
61+
)
5662

5763

5864
def upgrade() -> None:
5965
bind = op.get_bind()
6066
assert bind is not None
6167
insp = inspect(bind)
6268

63-
use_postgis = _postgis_available(bind)
64-
if use_postgis:
65-
op.execute("CREATE EXTENSION IF NOT EXISTS postgis")
69+
_assert_postgis_installed(bind)
6670

6771
# ---- teams / team_user -------------------------------------------
6872
#
@@ -173,12 +177,11 @@ def upgrade() -> None:
173177
sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
174178
)
175179

176-
if use_postgis:
177-
op.execute("ALTER TABLE tasking_projects DROP COLUMN aoi")
178-
op.execute(
179-
"ALTER TABLE tasking_projects "
180-
"ADD COLUMN aoi GEOMETRY(MultiPolygon, 4326)"
181-
)
180+
op.execute("ALTER TABLE tasking_projects DROP COLUMN aoi")
181+
op.execute(
182+
"ALTER TABLE tasking_projects "
183+
"ADD COLUMN aoi GEOMETRY(MultiPolygon, 4326)"
184+
)
182185

183186
# Unique project name per workspace among non-deleted rows.
184187
op.execute(
@@ -273,17 +276,14 @@ def upgrade() -> None:
273276
"project_id", "task_number", name="tasking_tasks_pn_unique"
274277
),
275278
)
276-
if use_postgis:
277-
op.execute(
278-
"ALTER TABLE tasking_tasks "
279-
"ADD COLUMN geometry GEOMETRY(Polygon, 4326) NOT NULL"
280-
)
281-
op.execute(
282-
"CREATE INDEX tasking_tasks_geometry_idx "
283-
"ON tasking_tasks USING GIST (geometry)"
284-
)
285-
else:
286-
op.execute("ALTER TABLE tasking_tasks " "ADD COLUMN geometry BYTEA")
279+
op.execute(
280+
"ALTER TABLE tasking_tasks "
281+
"ADD COLUMN geometry GEOMETRY(Polygon, 4326) NOT NULL"
282+
)
283+
op.execute(
284+
"CREATE INDEX tasking_tasks_geometry_idx "
285+
"ON tasking_tasks USING GIST (geometry)"
286+
)
287287
op.create_index("tasking_tasks_project_idx", "tasking_tasks", ["project_id"])
288288

289289
# ---- tasking_locks ------------------------------------------------

alembic_task/versions/c5121cbba124_initial_task_schema.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@
1212
depends_on: Union[str, Sequence[str], None] = None
1313

1414

15-
def _postgis_available(bind) -> bool:
16-
return bool(
15+
def _assert_postgis_installed(bind) -> None:
16+
"""Require the postgis extension to be installed in this database."""
17+
installed = bool(
1718
bind.execute(
18-
text("SELECT 1 FROM pg_available_extensions WHERE name = 'postgis'")
19+
text("SELECT 1 FROM pg_extension WHERE extname = 'postgis'")
1920
).scalar()
2021
)
22+
if not installed:
23+
raise RuntimeError(
24+
"postgis extension is not installed in this database. "
25+
"Run `CREATE EXTENSION IF NOT EXISTS postgis;` before migrations."
26+
)
2127

2228

2329
def upgrade() -> None:
2430
bind = op.get_bind()
2531
assert bind is not None
2632
insp = inspect(bind)
2733

28-
use_postgis = _postgis_available(bind)
29-
if use_postgis:
30-
op.execute("CREATE EXTENSION IF NOT EXISTS postgis")
34+
_assert_postgis_installed(bind)
3135

32-
geometry_column = (
33-
sa.Column(
34-
"geometry",
35-
Geometry(geometry_type="MULTIPOLYGON", srid=4326),
36-
nullable=True,
37-
)
38-
if use_postgis
39-
else sa.Column("geometry", sa.Text(), nullable=True)
36+
geometry_column = sa.Column(
37+
"geometry",
38+
Geometry(geometry_type="MULTIPOLYGON", srid=4326),
39+
nullable=True,
4040
)
4141

4242
# The TASK tree owns `workspaces` and `workspaces_*` only.

0 commit comments

Comments
 (0)