-
Notifications
You must be signed in to change notification settings - Fork 3
feat(cms): database migration tool #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,8 @@ | ||
| FROM python:slim | ||
|
|
||
| COPY requirements.txt . | ||
| RUN pip install -r requirements.txt | ||
|
|
||
| COPY . . | ||
|
|
||
| ENTRYPOINT [ "python", "migrate.py" ] |
This file contains hidden or 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 @@ | ||
| 1 |
This file contains hidden or 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,6 @@ | ||
| DROP TABLE IF EXISTS frontend CASCADE; | ||
| DROP TABLE IF EXISTS groups CASCADE; | ||
| DROP TABLE IF EXISTS person CASCADE; | ||
| DROP TABLE IF EXISTS filesystem CASCADE; | ||
|
|
||
| DROP TYPE IF EXISTS permissions_enum; |
This file contains hidden or 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,99 @@ | ||
| # Simple migration script to migrate the database in two phases | ||
| # - Phase 1: involves completely destroying the current state of the database (down) | ||
| # - Phase 2: involves recreating the entire database (up) | ||
|
|
||
| import os | ||
| import sys | ||
| import psycopg2 | ||
| import glob | ||
|
|
||
| # get_db acquires a connection to the database | ||
| def get_db(): | ||
| connection = psycopg2.connect( | ||
| host = os.environ["POSTGRES_HOST"], | ||
| database = os.environ["POSTGRES_DB"], | ||
| user = os.environ["POSTGRES_USER"], | ||
| password = os.environ["POSTGRES_PASSWORD"] | ||
| ) | ||
|
|
||
| connection.autocommit = False | ||
| return connection | ||
|
|
||
|
|
||
| # UpgradeJob represents a single attempt to upgrade the DB, it does everything within a transaction | ||
| class UpgradeJob: | ||
| def __init__(self): | ||
| self.connection = get_db() | ||
| self.cursor = self.connection.cursor() | ||
|
|
||
| def run_script(self, script: str): | ||
| self.cursor.execute(script) | ||
|
|
||
| def cancel_job(self): | ||
| self.cursor.close() | ||
| self.connection.rollback() | ||
|
|
||
| def finish_job(self): | ||
| self.cursor.execute("update migrations SET VersionID = VersionID + 1 WHERE MigrationID = 1;") | ||
| self.connection.commit() | ||
| self.cursor.close() | ||
|
|
||
|
|
||
| # Completely destroy the current state of the DB | ||
| def down(job: UpgradeJob): | ||
| down_script = open("down/down.sql", "r").read() | ||
| job.run_script(down_script) | ||
|
|
||
|
|
||
| # Recreate the database from the defined schema files | ||
| def up(job: UpgradeJob): | ||
| up_jobs = glob.glob('up/*.sql') | ||
| for script in sorted(up_jobs): | ||
| job.run_script(open(script, "r").read()) | ||
|
|
||
|
|
||
| # requires_update determines if the current database requires an update, it does so by querying an update table and comparing the result | ||
| # with the contents of the dbver.txt file | ||
| def get_db_versions(): | ||
| db = get_db() | ||
|
|
||
| git_version_file = open("dbver.txt", "r") | ||
| git_version = int(git_version_file.readline()) | ||
| container_version = 0 | ||
|
|
||
| # acquire the db version | ||
| cursor = db.cursor() | ||
| try: | ||
| cursor.execute("select VersionID from migrations where MigrationID = 1;") | ||
| migration_records = cursor.fetchall() | ||
| container_version = 0 if len(migration_records) == 0 else migration_records[0][0] | ||
| except Exception as e: | ||
| print(e) | ||
| pass | ||
| finally: | ||
| cursor.close() | ||
|
|
||
| return (container_version, git_version) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| (container_version, git_version) = get_db_versions() | ||
|
|
||
| if git_version <= container_version: | ||
| print("Container DB is up to date, skipping upgrade :)") | ||
| sys.exit() | ||
|
|
||
| # run the upgrade now :D | ||
| upgradeJob = UpgradeJob() | ||
| try: | ||
| down(upgradeJob) | ||
| up(upgradeJob) | ||
| except Exception as e: | ||
| print(f""" | ||
| Failed to upgrade the database from version {container_version} to {git_version}, check logs for additional information. | ||
| Database is currently on version {container_version}.""") | ||
| upgradeJob.cancel_job() | ||
| raise e | ||
|
|
||
| upgradeJob.finish_job() | ||
| print(f"Successfully updated DB from version {container_version} to {git_version}.") |
This file contains hidden or 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 @@ | ||
| psycopg2-binary==2.9.3 |
This file contains hidden or 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,11 @@ | ||
| CREATE TABLE IF NOT EXISTS migrations ( | ||
| MigrationID SERIAL PRIMARY KEY, | ||
| VersionID INTEGER default 0 | ||
| ); | ||
|
|
||
| DO LANGUAGE plpgsql $$ | ||
| BEGIN | ||
| IF NOT EXISTS (SELECT FROM migrations WHERE MigrationID = 1) THEN | ||
| INSERT INTO migrations (MigrationID, VersionID) VALUES (1, 0); | ||
| END IF; | ||
| END $$; |
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
postgres/01-create_groups_table.sql → postgres/up/03-create_groups_table.sql
This file contains hidden or 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.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.