Skip to content

Commit 6351024

Browse files
committed
Ensure that PostgREST API picks up additional lines_oriented attribute
1 parent 2cf0990 commit 6351024

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

cli/macrostrat/cli/database/migrations/maps_lines_oriented/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from macrostrat.core.migrations import Migration, has_columns
88

9+
_has_column = has_columns("maps", "sources", "lines_oriented")
10+
911

1012
class MapsLinesOriented(Migration):
1113
name = "maps-lines-oriented"
@@ -14,7 +16,7 @@ class MapsLinesOriented(Migration):
1416

1517
depends_on = ["baseline"]
1618

17-
postconditions = [has_columns("maps", "sources", "lines_oriented")]
19+
postconditions = [_has_column]
1820

1921
def apply(self, db: Database):
2022
db.run_sql("ALTER TABLE maps.sources ADD COLUMN lines_oriented boolean")
@@ -41,13 +43,17 @@ def matching_sources(
4143

4244

4345
def some_maps_are_unoriented(db: Database) -> bool:
46+
if not _has_column(db):
47+
return False
4448
ids = matching_sources(
4549
db, valid_maps + reversed_maps, "NOT coalesce(lines_oriented, false)"
4650
)
4751
return len(ids) > 0
4852

4953

5054
def all_maps_are_oriented(db: Database) -> bool:
55+
if not _has_column(db):
56+
return False
5157
_all_maps = valid_maps + reversed_maps
5258
ids = matching_sources(db, _all_maps, "coalesce(lines_oriented, false)")
5359
return len(ids) == len(_all_maps)

cli/macrostrat/cli/database/maps_api/01-maps-sources.sql renamed to cli/macrostrat/cli/database/migrations/maps_sources_api/01-maps-sources.sql

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
ALTER TABLE maps.sources ADD COLUMN IF NOT EXISTS raster_url text;
2-
ALTER TABLE maps.sources ADD COLUMN IF NOT EXISTS scale_denominator integer;
3-
ALTER TABLE maps.sources ADD COLUMN IF NOT EXISTS is_finalized boolean DEFAULT false;
4-
5-
DROP VIEW IF EXISTS macrostrat_api.sources_metadata CASCADE;
6-
DROP VIEW IF EXISTS maps.sources_metadata CASCADE;
7-
DROP VIEW IF EXISTS macrostrat_api.sources CASCADE;
1+
DROP VIEW IF EXISTS macrostrat_api.sources_metadata;
2+
DROP VIEW IF EXISTS maps.sources_metadata;
3+
DROP VIEW IF EXISTS macrostrat_api.sources;
84

95
CREATE OR REPLACE VIEW maps.sources_metadata AS
106
SELECT
@@ -26,7 +22,8 @@ SELECT
2622
status_code,
2723
raster_url,
2824
scale_denominator,
29-
is_finalized
25+
is_finalized,
26+
lines_oriented
3027
FROM maps.sources AS s
3128
ORDER BY source_id DESC;
3229

@@ -89,5 +86,6 @@ SELECT
8986
s.raster_url,
9087
s.web_geom envelope,
9188
s.is_finalized,
92-
s.scale_denominator
89+
s.scale_denominator,
90+
s.lines_oriented
9391
FROM maps.sources s;

cli/macrostrat/cli/database/maps_api/__init__.py renamed to cli/macrostrat/cli/database/migrations/maps_sources_api/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from macrostrat.core.migrations import Migration, has_columns, view_exists
22

33

4-
class MapsAPIMigration(Migration):
5-
name = "maps-sources"
6-
subsystem = "core"
4+
class MapsSourcesAPIMigration(Migration):
5+
name = "maps-sources-api"
6+
subsystem = "maps"
77
description = """
88
Create views for sources_metadata and ingest_process in the maps and macrostrat_api schemas
99
"""
@@ -20,5 +20,6 @@ class MapsAPIMigration(Migration):
2020
"is_finalized",
2121
"scale_denominator",
2222
"lines_oriented",
23+
allow_view=True,
2324
),
2425
]

core/macrostrat/core/migrations/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
from typing import Callable, Iterable
88

99
import docker
10-
from pydantic import BaseModel
11-
from rich import print
12-
1310
from macrostrat.database import Database
1411
from macrostrat.database.utils import OutputMode
1512
from macrostrat.dinosaur.upgrade_cluster.utils import database_cluster
13+
from pydantic import BaseModel
14+
from rich import print
1615

1716
from ..config import settings
1817
from ..database import get_database
@@ -58,11 +57,15 @@ def custom_type_exists(schema: str, *type_names: str) -> DbEvaluator:
5857
return lambda db: all(db.inspector.has_type(t, schema=schema) for t in type_names)
5958

6059

61-
def has_columns(schema: str, table: str, *fields: str) -> DbEvaluator:
60+
def has_columns(schema: str, table: str, *fields: str, allow_view=False) -> DbEvaluator:
6261
"""Return a function that evaluates to true when every given field in the given table exists"""
6362

6463
def _has_fields(db: Database) -> bool:
65-
if not db.inspector.has_table(table, schema=schema):
64+
_has_table = db.inspector.has_table(table, schema=schema)
65+
if not _has_table and not allow_view:
66+
return False
67+
_has_view = table in db.inspector.get_view_names(schema)
68+
if not _has_table and not _has_view:
6669
return False
6770
columns = db.inspector.get_columns(table, schema=schema)
6871
col_names = [c["name"] for c in columns]

0 commit comments

Comments
 (0)