-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from mwestphall/feature/move-fixtures-to-migra…
…tions Feature/move fixtures to migrations
- Loading branch information
Showing
35 changed files
with
50,425 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from ..base import Migration, exists | ||
|
||
class BaselineMigration(Migration): | ||
name = "api-v3" | ||
subsystem = "core" | ||
description = """ | ||
Apply the schema changes from https://github.com/UW-Macrostrat/api-v3 to the database | ||
""" | ||
|
||
depends_on = ['map-source-slug'] | ||
|
||
# Confirm that the tables created by the API v3 migrations are present | ||
postconditions = [ | ||
exists("storage","object_group","object"), | ||
exists("maps_metadata","ingest_process","ingest_process_tag"), | ||
exists("macrostrat_auth","user","group"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,88 @@ | ||
from macrostrat.database import Database | ||
from pathlib import Path | ||
import inspect | ||
from typing import Callable | ||
from enum import Enum | ||
|
||
""" Higher-order functions that return a function that evaluates whether a condition is met on the database """ | ||
DbEvaluator = Callable[[Database], bool] | ||
|
||
|
||
def exists(schema: str, *table_names: str) -> DbEvaluator: | ||
""" Return a function that evaluates to true when every given table in the given schema exists """ | ||
return lambda db: all(db.inspector.has_table(t, schema=schema) for t in table_names) | ||
|
||
def not_exists(schema: str, *table_names: str) -> DbEvaluator: | ||
""" Return a function that evaluates to true when every given table in the given schema doesn't exist """ | ||
return lambda db: all(not db.inspector.has_table(t, schema=schema) for t in table_names) | ||
|
||
def schema_exists(schema: str) -> DbEvaluator: | ||
""" Return a function that evaluates to true when the given schema exists """ | ||
return lambda db: db.inspector.has_schema(schema) | ||
|
||
def view_exists(schema: str, *view_names: str) -> DbEvaluator: | ||
""" Return a function that evaluates to true when every given view in the given schema exists """ | ||
return lambda db: all(v in db.inspector.get_view_names(schema) for v in view_names) | ||
|
||
def has_fks(schema: str, *table_names: str) -> DbEvaluator: | ||
""" Return a function that evaluates to true when every given table in the given schema has at least one foreign key """ | ||
return lambda db: all( | ||
db.inspector.has_table(t, schema=schema) and | ||
len(db.inspector.get_foreign_keys(t, schema=schema)) for t in table_names) | ||
|
||
class ApplicationStatus(Enum): | ||
""" Enum for the possible """ | ||
|
||
# The preconditions for this migration aren't met, so it can't be applied | ||
CANT_APPLY = "cant_apply" | ||
|
||
# The preconditions for this migration are met but the postconditions aren't met, so it can be applied | ||
CAN_APPLY = "can_apply" | ||
|
||
# The postconditions for this migration are met, so it doesn't need to be applied | ||
APPLIED = "applied" | ||
|
||
class Migration: | ||
"""This will eventually be merged with the migration system in macrostrat.dinosaur""" | ||
""" Class defining a set of SQL changes to be applied to the database, as well as checks for | ||
whether the migration can be applied to the current state of the database | ||
""" | ||
|
||
# Unique name for the migration | ||
name: str | ||
|
||
# Short description for the migration | ||
description: str | ||
|
||
# Portion of the database to which this migration applies | ||
subsystem: str | ||
|
||
def should_apply(self, database: Database): | ||
raise NotImplementedError | ||
# List of migration names that must | ||
depends_on: list[str] = [] | ||
|
||
# List of checks on the database that must all evaluate to true before the migration can be run | ||
preconditions: list[DbEvaluator] = [] | ||
|
||
def apply(self, database: Database): | ||
raise NotImplementedError | ||
# List of checks on the database that should all evaluate to true after the migration has run successfully | ||
postconditions: list[DbEvaluator] = [] | ||
|
||
# Flag for whether running this migration will cause data changes in the database in addition to | ||
# schema changes | ||
destructive: bool = False | ||
|
||
def is_satisfied(self, database: Database): | ||
"""In some cases, we may want to note that a migration does not need to be run | ||
(e.g. if the database is already in the correct state) without actually running it. | ||
""" | ||
return not self.should_apply(database) | ||
def should_apply(self, database: Database) -> ApplicationStatus: | ||
""" Determine whether this migration can run, or has already run. """ | ||
# If all post-conditions are met, the migration is already applied | ||
if all([cond(database) for cond in self.postconditions]): | ||
return ApplicationStatus.APPLIED | ||
# Else if all pre-conditions are met, the migration can be applied | ||
elif all([cond(database) for cond in self.preconditions]): | ||
return ApplicationStatus.CAN_APPLY | ||
# Else, can't apply | ||
else: | ||
return ApplicationStatus.CANT_APPLY | ||
|
||
def apply(self, database: Database): | ||
""" Apply the migrations defined by this class. By default, run every sql file | ||
in the same directory as the class definition. """ | ||
child_cls_dir = Path(inspect.getfile(self.__class__)).parent | ||
database.run_fixtures(child_cls_dir) |
Oops, something went wrong.