From 1fc22129b4449209cffdb6c53875a63f7d9b78ff Mon Sep 17 00:00:00 2001 From: Damien Bargiacchi Date: Mon, 25 May 2015 14:51:27 -0700 Subject: [PATCH] Add db stuff --- db_create.py | 16 ++++++++++++++++ db_downgrade.py | 11 +++++++++++ db_migrate.py | 23 +++++++++++++++++++++++ db_upgrade.py | 9 +++++++++ migrations/README | 4 ++++ migrations/__init__.py | 0 migrations/manage.py | 5 +++++ migrations/migrate.cfg | 25 +++++++++++++++++++++++++ migrations/versions/__init__.py | 0 9 files changed, 93 insertions(+) create mode 100755 db_create.py create mode 100755 db_downgrade.py create mode 100755 db_migrate.py create mode 100755 db_upgrade.py create mode 100644 migrations/README create mode 100644 migrations/__init__.py create mode 100644 migrations/manage.py create mode 100644 migrations/migrate.cfg create mode 100644 migrations/versions/__init__.py diff --git a/db_create.py b/db_create.py new file mode 100755 index 0000000..96c68f3 --- /dev/null +++ b/db_create.py @@ -0,0 +1,16 @@ +#!env/bin/python +from migrate.versioning import api +from config import SQLALCHEMY_DATABASE_URI +from config import SQLALCHEMY_MIGRATE_REPO +from app import db + +import os.path + +db.create_all() + +# Either create the migration directory or use the existing one to create the database +if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): + api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository') + api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) +else: + api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO)) diff --git a/db_downgrade.py b/db_downgrade.py new file mode 100755 index 0000000..b1cb47d --- /dev/null +++ b/db_downgrade.py @@ -0,0 +1,11 @@ +#!env/bin/python +from migrate.versioning import api +from config import SQLALCHEMY_DATABASE_URI +from config import SQLALCHEMY_MIGRATE_REPO + +v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + +api.downgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, v - 1) + +v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) +print('Current database version: ' + str(v)) diff --git a/db_migrate.py b/db_migrate.py new file mode 100755 index 0000000..0aee3fc --- /dev/null +++ b/db_migrate.py @@ -0,0 +1,23 @@ +#!env/bin/python +import imp + +from migrate.versioning import api +from app import db +from config import SQLALCHEMY_DATABASE_URI +from config import SQLALCHEMY_MIGRATE_REPO + +v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + +migration = SQLALCHEMY_MIGRATE_REPO + ('/versions/%03d_migration.py' % (v+1)) +tmp_module = imp.new_module('old_model') +old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) +exec(old_model, tmp_module.__dict__) + +script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata) +open(migration, "wt").write(script) + +api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + +v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) +print('New migration saved as ' + migration) +print('Current database version: ' + str(v)) diff --git a/db_upgrade.py b/db_upgrade.py new file mode 100755 index 0000000..e56862e --- /dev/null +++ b/db_upgrade.py @@ -0,0 +1,9 @@ +#!env/bin/python +from migrate.versioning import api +from config import SQLALCHEMY_DATABASE_URI +from config import SQLALCHEMY_MIGRATE_REPO + +api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + +v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) +print('Current database version: ' + str(v)) diff --git a/migrations/README b/migrations/README new file mode 100644 index 0000000..6218f8c --- /dev/null +++ b/migrations/README @@ -0,0 +1,4 @@ +This is a database migration repository. + +More information at +http://code.google.com/p/sqlalchemy-migrate/ diff --git a/migrations/__init__.py b/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/migrations/manage.py b/migrations/manage.py new file mode 100644 index 0000000..554f89c --- /dev/null +++ b/migrations/manage.py @@ -0,0 +1,5 @@ +#!/usr/bin/env python +from migrate.versioning.shell import main + +if __name__ == '__main__': + main() diff --git a/migrations/migrate.cfg b/migrations/migrate.cfg new file mode 100644 index 0000000..0f4c729 --- /dev/null +++ b/migrations/migrate.cfg @@ -0,0 +1,25 @@ +[db_settings] +# Used to identify which repository this database is versioned under. +# You can use the name of your project. +repository_id=standings_repository + +# The name of the database table used to track the schema version. +# This name shouldn't already be used by your project. +# If this is changed once a database is under version control, you'll need to +# change the table name in each database too. +version_table=migrate_version + +# When committing a change script, Migrate will attempt to generate the +# sql for all supported databases; normally, if one of them fails - probably +# because you don't have that database installed - it is ignored and the +# commit continues, perhaps ending successfully. +# Databases in this list MUST compile successfully during a commit, or the +# entire commit will fail. List the databases your application will actually +# be using to ensure your updates to that database work properly. +# This must be a list; example: ['postgres','sqlite'] +required_dbs=['mysql'] + +# When creating new change scripts, Migrate will stamp the new script with +# a version number. By default this is latest_version + 1. You can set this +# to 'true' to tell Migrate to use the UTC timestamp instead. +use_timestamp_numbering=False diff --git a/migrations/versions/__init__.py b/migrations/versions/__init__.py new file mode 100644 index 0000000..e69de29