From 2fb578d4de0f6fab034117e6f7d2edd9f9025fd2 Mon Sep 17 00:00:00 2001 From: Ruud Date: Thu, 24 Feb 2011 08:37:35 +0100 Subject: [PATCH] Migration support, for awesomeness --- couchpotato/__init__.py | 2 +- couchpotato/cli.py | 19 +++++++++++- couchpotato/core/migration/__init__.py | 0 couchpotato/core/migration/migrate.cfg | 4 +++ .../core/migration/versions/__init__.py | 30 +++++++++++++++++++ 5 files changed, 53 insertions(+), 2 deletions(-) create mode 100755 couchpotato/core/migration/__init__.py create mode 100644 couchpotato/core/migration/migrate.cfg create mode 100755 couchpotato/core/migration/versions/__init__.py diff --git a/couchpotato/__init__.py b/couchpotato/__init__.py index 512b71c4..85ae33f4 100644 --- a/couchpotato/__init__.py +++ b/couchpotato/__init__.py @@ -22,7 +22,7 @@ def get_session(engine): return scoped_session(sessionmaker(transactional = True, bind = engine)) def get_engine(): - return create_engine('sqlite:///' + Env.get('db_path'), echo = False) + return create_engine(Env.get('db_path'), echo = False) @web.route('/') diff --git a/couchpotato/cli.py b/couchpotato/cli.py index 80dc17da..704ec6a9 100644 --- a/couchpotato/cli.py +++ b/couchpotato/cli.py @@ -47,7 +47,7 @@ def cmd_couchpotato(base_path, args): Env.get('settings').setFile(os.path.join(options.data_dir, 'settings.conf')) Env.set('app_dir', base_path) Env.set('data_dir', options.data_dir) - Env.set('db_path', os.path.join(options.data_dir, 'couchpotato.db')) + Env.set('db_path', 'sqlite:///' + os.path.join(options.data_dir, 'couchpotato.db')) Env.set('quiet', options.quiet) Env.set('daemonize', options.daemonize) Env.set('args', args) @@ -91,6 +91,23 @@ def cmd_couchpotato(base_path, args): settings_loader.run() + # Load migrations + from migrate.versioning.api import version_control, db_version, version, upgrade + db = Env.get('db_path') + repo = os.path.join('couchpotato', 'core', 'migration') + + latest_db_version = version(repo) + + try: + current_db_version = db_version(db, repo) + except: + version_control(db, repo, version = latest_db_version) + current_db_version = db_version(db, repo) + + if current_db_version < latest_db_version and not debug: + log.info('Doing database upgrade. From %d to %d' % (current_db_version, latest_db_version)) + upgrade(db, repo) + # Configure Database from elixir import setup_all, create_all setup_all() diff --git a/couchpotato/core/migration/__init__.py b/couchpotato/core/migration/__init__.py new file mode 100755 index 00000000..e69de29b diff --git a/couchpotato/core/migration/migrate.cfg b/couchpotato/core/migration/migrate.cfg new file mode 100644 index 00000000..f17e967a --- /dev/null +++ b/couchpotato/core/migration/migrate.cfg @@ -0,0 +1,4 @@ +[db_settings] +repository_id = CouchPotato +version_table = migrate_version +required_dbs = ['sqlite'] diff --git a/couchpotato/core/migration/versions/__init__.py b/couchpotato/core/migration/versions/__init__.py new file mode 100755 index 00000000..7e6e44bf --- /dev/null +++ b/couchpotato/core/migration/versions/__init__.py @@ -0,0 +1,30 @@ +""" + Examples + + Adding a column: + + from migrate import * + from migrate.changeset.schema import create_column + from sqlalchemy import * + + meta = MetaData() + + def upgrade(migrate_engine): + meta.bind = migrate_engine + + #print changeset.schema + path_column = Column('path', String) + resource = Table('resource', meta, path_column) + + create_column(path_column, resource) + + + + Adding Relation table: http://www.mail-archive.com/sqlelixir@googlegroups.com/msg02061.html + + person = Table('person', metadata, Column('id', Integer)) + person_column = Column('person_id', Integer, ForeignKey('person.id'), nullable=False) + movie = Table('movie', metadata, person_column) + person_constraint = ForeignKeyConstraint(['person_id'], ['person.id'], ondelete="restrict", table=movie) + +"""