Skip to content

Commit f2d4870

Browse files
authored
Merge pull request #241 from UW-Macrostrat/schema-management
Schema management rework
2 parents 350050d + d900918 commit f2d4870

File tree

148 files changed

+12817
-6099
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+12817
-6099
lines changed

.idea/data_source_mapping.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/macrostrat.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sqldialects.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/swift-toolchain.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

base-images/database/Dockerfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
FROM imresamu/postgis:15-3.4
22
RUN apt-get update && apt-get install -y --no-install-recommends \
3-
postgresql-15-pgaudit
3+
build-essential make gcc python3 python3-venv zlib1g-dev \
4+
postgresql-server-dev-15 build-essential postgresql-15-pgaudit postgresql-15-pgvector \
5+
&& rm -rf /var/lib/apt/lists/*
6+
7+
RUN python3 -m venv /opt/pgxn-venv && \
8+
/opt/pgxn-venv/bin/pip install pgxnclient && \
9+
/opt/pgxn-venv/bin/pgxn install safeupdate
410

511
CMD docker-entrypoint.sh postgres -c shared_preload_libraries=pgaudit

cli/macrostrat/cli/database/__init__.py

Lines changed: 3 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from typer import Argument, Option
1111

1212
from macrostrat.core import MacrostratSubsystem, app
13-
from macrostrat.core.migrations import run_migrations
13+
from macrostrat.core.database import get_database
1414
from macrostrat.database import Database
1515
from macrostrat.database.transfer import pg_dump_to_file, pg_restore_from_file
1616
from macrostrat.database.transfer.utils import raw_database_url
@@ -20,9 +20,7 @@
2020

2121
from ._legacy import get_db
2222

23-
# First, register all migrations
2423
# NOTE: right now, this is quite implicit.
25-
from .migrations import load_migrations
2624
from .utils import engine_for_db_name, setup_postgrest_access
2725

2826
log = get_logger(__name__)
@@ -34,9 +32,6 @@
3432
DBCallable = Callable[[Database], None]
3533

3634

37-
load_migrations()
38-
39-
4035
class SubsystemSchemaDefinition(BaseModel):
4136
"""A schema definition managed by a Macrostrat subsystem"""
4237

@@ -116,52 +111,7 @@ def initialize(self):
116111

117112
db_app = typer.Typer(no_args_is_help=True)
118113

119-
120-
def update_schema(
121-
match: str = Option(None),
122-
subsystems: list[str] = Option(None),
123-
_all: bool = Option(None, "--all"),
124-
):
125-
"""Update the database schema"""
126-
from macrostrat.core.config import PG_DATABASE
127-
from macrostrat.database import Database
128-
129-
db_subsystem = app.subsystems.get("database")
130-
131-
if subsystems is None:
132-
subsystems = []
133-
134-
"""Create schema additions"""
135-
schema_dir = fixtures_dir
136-
# Loaded from env file
137-
db = Database(PG_DATABASE)
138-
139-
if match is not None and len(subsystems) == 0:
140-
# core is implicit
141-
subsystems = ["core"]
142-
143-
if not _all and len(subsystems) == 0:
144-
print("Please specify --all or --subsystems to update the schema")
145-
print("Available subsystems:")
146-
for hunk in db_subsystem.schema_hunks:
147-
print(f" {hunk.name}")
148-
return
149-
150-
# Run subsystem updates
151-
for hunk in db_subsystem.schema_hunks:
152-
if (
153-
subsystems is not None
154-
and len(subsystems) != 0
155-
and hunk.name not in subsystems
156-
):
157-
continue
158-
hunk.apply(db, match=match)
159-
160-
app.subsystems.run_hook("schema-update")
161-
162-
163114
db_app = db_subsystem.control_command()
164-
db_app.command(name="update", rich_help_panel="Schema management")(update_schema)
165115

166116

167117
@db_app.command(
@@ -230,7 +180,7 @@ def dump(
230180

231181
engine = engine_for_db_name(database)
232182

233-
if dumpfile == "-":
183+
if str(dumpfile) == "-":
234184
dumpfile = stdout
235185

236186
args = ctx.args
@@ -353,7 +303,7 @@ def check_constraints(self):
353303
@db_app.command(name="inspect", rich_help_panel="Helpers")
354304
def inspect_table(table: str):
355305
"""Inspect a table in the database"""
356-
db = get_db()
306+
db = get_database()
357307

358308
schema = None
359309
if "." in table:
@@ -374,44 +324,6 @@ def inspect_table(table: str):
374324
)
375325

376326

377-
@db_app.command(name="scripts", rich_help_panel="Schema management")
378-
def run_scripts(migration: str = Argument(None)):
379-
"""Ad-hoc database management scripts"""
380-
pth = Path(__file__).parent.parent / "data-scripts"
381-
files = list(pth.glob("*.sql")) + list(pth.glob("*.sh")) + list(pth.glob("*.py"))
382-
files.sort()
383-
if migration is None:
384-
print("[yellow bold]No script specified\n", file=stderr)
385-
print("[bold]Available scripts:", file=stderr)
386-
for f in files:
387-
print(f" {f.stem}[dim]{f.suffix}", file=stderr)
388-
exit(1)
389-
matching_migrations = [
390-
f for f in files if f.stem == migration or str(f) == migration
391-
]
392-
if len(matching_migrations) == 0:
393-
print(f"Script {migration} does not exist", file=stderr)
394-
exit(1)
395-
if len(matching_migrations) > 1:
396-
print(
397-
f"Ambiguous script name: {migration}",
398-
file=stderr,
399-
)
400-
print("Please specify the full file name")
401-
exit(1)
402-
migration = matching_migrations[0]
403-
if migration.suffix == ".py":
404-
run("python", str(migration))
405-
if migration.suffix == ".sh":
406-
run(str(migration))
407-
if migration.suffix == ".sql":
408-
db = get_db()
409-
db.run_sql(migration)
410-
411-
412-
db_app.command(name="migrations", rich_help_panel="Schema management")(run_migrations)
413-
414-
415327
def update_permissions():
416328
"""Setup permissions for the PostgREST API.
417329

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

Lines changed: 0 additions & 28 deletions
This file was deleted.

cli/macrostrat/cli/database/migrations/map_sources/01.1-maps-ingestion.sql

Lines changed: 0 additions & 5 deletions
This file was deleted.

cli/macrostrat/cli/database/migrations/map_sources/02-sources-add-columns.sql

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)