Skip to content

Commit c8455b4

Browse files
pjonssonomad
authored andcommitted
stores: use CreateSchema
There is already a DropSchema in stores, so put the CreateSchema in there as well. The previous code has some comment about being able to run things without create permissions, but the only caller is SummaryStore.init() whose documentation states that it requires create permissions, so use the if_not_exists parameter to CreateSchema instead of some function that probes the database.
1 parent 65c3686 commit c8455b4

File tree

3 files changed

+10
-28
lines changed

3 files changed

+10
-28
lines changed

cubedash/index/postgis/_schema.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
METADATA,
2929
REF_TABLE_METADATA,
3030
epsg_to_srid,
31-
has_schema,
3231
pg_create_index,
3332
)
3433

@@ -239,19 +238,10 @@ def get_srid_name(conn: Connection, srid: int):
239238
).scalar()
240239

241240

242-
def create_schema(conn: Connection, epsg_code: int):
241+
def create_after_schema(conn: Connection, epsg_code: int) -> None:
243242
"""
244-
Create any missing parts of the cubedash schema
243+
Create any missing parts once there is a cubedash schema
245244
"""
246-
# Create schema if needed.
247-
#
248-
# Note that we don't use the built-in "if not exists" because running it *always* requires
249-
# `create` permission.
250-
#
251-
# Doing it separately allows users to run this tool without `create` permission.
252-
#
253-
if not has_schema(conn):
254-
conn.execute(DDL(f"create schema {CUBEDASH_SCHEMA}"))
255245
# Add Postgis if needed
256246
#
257247
# Note that, as above, we deliberately don't use the built-in "if not exists"
@@ -346,7 +336,7 @@ def init_elements(conn: Connection, grouping_epsg_code: int):
346336
(Requires `create` permissions in the db)
347337
"""
348338
# Add any missing schema items or patches.
349-
create_schema(conn, epsg_code=grouping_epsg_code)
339+
create_after_schema(conn, epsg_code=grouping_epsg_code)
350340

351341
# If they specified an epsg code, make sure the existing schema uses it.
352342
srid = conn.execute(select(FOOTPRINT_SRID_EXPRESSION)).scalar()

cubedash/index/postgres/_schema.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
PleaseRefresh,
3737
SchemaNotRefreshableError,
3838
epsg_to_srid,
39-
has_schema,
4039
pg_add_column,
4140
pg_column_exists,
4241
pg_create_index,
@@ -263,20 +262,10 @@ def get_srid_name(conn: Connection, srid: int):
263262
).scalar()
264263

265264

266-
def create_schema(conn: Connection, epsg_code: int):
265+
def create_after_schema(conn: Connection, epsg_code: int) -> None:
267266
"""
268-
Create any missing parts of the cubedash schema
267+
Create any missing parts once there is a cubedash schema
269268
"""
270-
# Create schema if needed.
271-
#
272-
# Note that we don't use the built-in "if not exists" because running it *always* requires
273-
# `create` permission.
274-
#
275-
# Doing it separately allows users to run this tool without `create` permission.
276-
#
277-
if not has_schema(conn):
278-
conn.execute(DDL(f"create schema {CUBEDASH_SCHEMA}"))
279-
280269
# Add Postgis if needed
281270
#
282271
# Note that, as above, we deliberately don't use the built-in "if not exists"
@@ -462,7 +451,7 @@ def init_elements(conn: Connection, grouping_epsg_code: int):
462451
(Requires `create` permissions in the db)
463452
"""
464453
# Add any missing schema items or patches.
465-
create_schema(conn, epsg_code=grouping_epsg_code)
454+
create_after_schema(conn, epsg_code=grouping_epsg_code)
466455

467456
# If they specified an epsg code, make sure the existing schema uses it.
468457
srid = conn.execute(select(FOOTPRINT_SRID_EXPRESSION)).scalar()

cubedash/summary/_stores.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from sqlalchemy import Row, RowMapping, func, select
3030
from sqlalchemy.dialects.postgresql import TSTZRANGE
3131
from sqlalchemy.sql import Select
32-
from sqlalchemy.sql.ddl import DropSchema
32+
from sqlalchemy.sql.ddl import CreateSchema, DropSchema
3333

3434
try:
3535
from cubedash._version import version as explorer_version
@@ -300,6 +300,9 @@ def init(self, grouping_epsg_code: int | None = None) -> None:
300300
301301
(Requires `create` permissions in the db)
302302
"""
303+
self.e_index.execute_ddl(
304+
CreateSchema(_schema.CUBEDASH_SCHEMA, if_not_exists=True)
305+
)
303306
refresh_also = self.e_index.init_schema(grouping_epsg_code or DEFAULT_EPSG)
304307
if refresh_also:
305308
# Refresh product information after a schema update, plus the given kind of data.

0 commit comments

Comments
 (0)