Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
45f85c6
Create basic helper to run pgschema
davenquinn Dec 25, 2025
6445c76
Basic usage of pgschema to apply ad-hoc updates
davenquinn Dec 29, 2025
61213e4
Prepare to dump schemas
davenquinn Dec 29, 2025
e064cf3
Created managed schema dumps
davenquinn Dec 29, 2025
1b5b01e
Switch schema management to PGSchema
davenquinn Dec 29, 2025
6a24ec0
Work with template database
davenquinn Dec 29, 2025
cb4502c
Improve database planning
davenquinn Dec 29, 2025
c9c9a4a
Updated schema dump
davenquinn Dec 30, 2025
ab1bf24
Updated schema dumps
davenquinn Dec 30, 2025
4d35368
Fixed schema generation and planning code
davenquinn Dec 30, 2025
b79cb46
planning etc works
davenquinn Dec 30, 2025
fd8a5e9
Get basic diffing working again
davenquinn Dec 30, 2025
fd3144a
Update schemas
davenquinn Dec 30, 2025
32e547a
Add some more schemas
davenquinn Dec 30, 2025
70c0e30
Update results database
davenquinn Dec 30, 2025
5fce479
Updated schema dumps
davenquinn Dec 30, 2025
8de224e
Fixed bad data type
davenquinn Dec 30, 2025
6a59ba6
Small changes to migrations
davenquinn Dec 31, 2025
38da476
Begin moving migrations around
davenquinn Dec 31, 2025
1ebe2e7
Separate schema management app from database portion
davenquinn Dec 31, 2025
556d9ab
Separate schema management 2
davenquinn Dec 31, 2025
14f3688
Better schema planning
davenquinn Jan 2, 2026
7e7c078
Remove unnecessary view
davenquinn Jan 2, 2026
5d2e219
Reorganize migrations to to be ordered SQL
davenquinn Jan 2, 2026
991a839
Removed an unused table
davenquinn Jan 2, 2026
a90ce1b
Perform enum_type migration in local environment
davenquinn Jan 2, 2026
4454bd5
Separated boundary_status migration
davenquinn Jan 2, 2026
8a45fc4
Improve syntax for generated column
davenquinn Jan 2, 2026
c7f8924
Greatly tersified schema files by removing automated comments
davenquinn Jan 2, 2026
6d7c65d
Updated API
davenquinn Jan 2, 2026
dcf3d5c
Start process of working with multiple environments
davenquinn Jan 2, 2026
9787b0f
Started sorting things by environment
davenquinn Jan 2, 2026
2824168
Made macrostrat_admin role the default
davenquinn Jan 2, 2026
3418df6
Move ecosystem schema to development
davenquinn Jan 2, 2026
657dc57
Hoist migrations up a level
davenquinn Jan 3, 2026
bdbf88d
Fix custom types a bit more
davenquinn Jan 3, 2026
af2fc98
Improve schema designs
davenquinn Jan 3, 2026
8f635bf
Deepen migrations a bit for development deployment
davenquinn Jan 3, 2026
932812a
Updated proposed changes
davenquinn Jan 3, 2026
8c70c35
Reorganized schema files a bit
davenquinn Jan 3, 2026
6a99997
Updated role changes for local
davenquinn Jan 3, 2026
33cd417
Updated schema management to better work with development db
davenquinn Jan 3, 2026
50ce8d0
Added some documentation
davenquinn Jan 4, 2026
29bf381
Added new schemas to management
davenquinn Jan 4, 2026
2a41f1a
Changed handling of tile layer setup
davenquinn Jan 4, 2026
84aeae3
Add pgvector to planning database
davenquinn Jan 4, 2026
a4c555a
Updated schema logging
davenquinn Jan 4, 2026
e57545e
Moved data scripts to the schema directory
davenquinn Jan 4, 2026
f3600fd
Moved postgres roles into schema directory
davenquinn Jan 4, 2026
3e7b0e7
Made schema changes idempotent
davenquinn Jan 4, 2026
5f8108a
Updated permissions for idempotency
davenquinn Jan 4, 2026
0b653ad
Updated schema management
davenquinn Jan 4, 2026
d900918
Format code and sort imports
davenquinn Jan 5, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/data_source_mapping.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/macrostrat.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/swift-toolchain.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion base-images/database/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
FROM imresamu/postgis:15-3.4
RUN apt-get update && apt-get install -y --no-install-recommends \
postgresql-15-pgaudit
build-essential make gcc python3 python3-venv zlib1g-dev \
postgresql-server-dev-15 build-essential postgresql-15-pgaudit postgresql-15-pgvector \
&& rm -rf /var/lib/apt/lists/*

RUN python3 -m venv /opt/pgxn-venv && \
/opt/pgxn-venv/bin/pip install pgxnclient && \
/opt/pgxn-venv/bin/pgxn install safeupdate

CMD docker-entrypoint.sh postgres -c shared_preload_libraries=pgaudit
94 changes: 3 additions & 91 deletions cli/macrostrat/cli/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typer import Argument, Option

from macrostrat.core import MacrostratSubsystem, app
from macrostrat.core.migrations import run_migrations
from macrostrat.core.database import get_database
from macrostrat.database import Database
from macrostrat.database.transfer import pg_dump_to_file, pg_restore_from_file
from macrostrat.database.transfer.utils import raw_database_url
Expand All @@ -20,9 +20,7 @@

from ._legacy import get_db

# First, register all migrations
# NOTE: right now, this is quite implicit.
from .migrations import load_migrations
from .utils import engine_for_db_name, setup_postgrest_access

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


load_migrations()


class SubsystemSchemaDefinition(BaseModel):
"""A schema definition managed by a Macrostrat subsystem"""

Expand Down Expand Up @@ -116,52 +111,7 @@ def initialize(self):

db_app = typer.Typer(no_args_is_help=True)


def update_schema(
match: str = Option(None),
subsystems: list[str] = Option(None),
_all: bool = Option(None, "--all"),
):
"""Update the database schema"""
from macrostrat.core.config import PG_DATABASE
from macrostrat.database import Database

db_subsystem = app.subsystems.get("database")

if subsystems is None:
subsystems = []

"""Create schema additions"""
schema_dir = fixtures_dir
# Loaded from env file
db = Database(PG_DATABASE)

if match is not None and len(subsystems) == 0:
# core is implicit
subsystems = ["core"]

if not _all and len(subsystems) == 0:
print("Please specify --all or --subsystems to update the schema")
print("Available subsystems:")
for hunk in db_subsystem.schema_hunks:
print(f" {hunk.name}")
return

# Run subsystem updates
for hunk in db_subsystem.schema_hunks:
if (
subsystems is not None
and len(subsystems) != 0
and hunk.name not in subsystems
):
continue
hunk.apply(db, match=match)

app.subsystems.run_hook("schema-update")


db_app = db_subsystem.control_command()
db_app.command(name="update", rich_help_panel="Schema management")(update_schema)


@db_app.command(
Expand Down Expand Up @@ -230,7 +180,7 @@ def dump(

engine = engine_for_db_name(database)

if dumpfile == "-":
if str(dumpfile) == "-":
dumpfile = stdout

args = ctx.args
Expand Down Expand Up @@ -353,7 +303,7 @@ def check_constraints(self):
@db_app.command(name="inspect", rich_help_panel="Helpers")
def inspect_table(table: str):
"""Inspect a table in the database"""
db = get_db()
db = get_database()

schema = None
if "." in table:
Expand All @@ -374,44 +324,6 @@ def inspect_table(table: str):
)


@db_app.command(name="scripts", rich_help_panel="Schema management")
def run_scripts(migration: str = Argument(None)):
"""Ad-hoc database management scripts"""
pth = Path(__file__).parent.parent / "data-scripts"
files = list(pth.glob("*.sql")) + list(pth.glob("*.sh")) + list(pth.glob("*.py"))
files.sort()
if migration is None:
print("[yellow bold]No script specified\n", file=stderr)
print("[bold]Available scripts:", file=stderr)
for f in files:
print(f" {f.stem}[dim]{f.suffix}", file=stderr)
exit(1)
matching_migrations = [
f for f in files if f.stem == migration or str(f) == migration
]
if len(matching_migrations) == 0:
print(f"Script {migration} does not exist", file=stderr)
exit(1)
if len(matching_migrations) > 1:
print(
f"Ambiguous script name: {migration}",
file=stderr,
)
print("Please specify the full file name")
exit(1)
migration = matching_migrations[0]
if migration.suffix == ".py":
run("python", str(migration))
if migration.suffix == ".sh":
run(str(migration))
if migration.suffix == ".sql":
db = get_db()
db.run_sql(migration)


db_app.command(name="migrations", rich_help_panel="Schema management")(run_migrations)


def update_permissions():
"""Setup permissions for the PostgREST API.

Expand Down
28 changes: 0 additions & 28 deletions cli/macrostrat/cli/database/migrations/__init__.py

This file was deleted.

This file was deleted.

This file was deleted.

19 changes: 0 additions & 19 deletions cli/macrostrat/cli/database/migrations/map_sources/__init__.py

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading