Migration support, for awesomeness
This commit is contained in:
@@ -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('/')
|
||||
|
||||
@@ -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()
|
||||
|
||||
0
couchpotato/core/migration/__init__.py
Executable file
0
couchpotato/core/migration/__init__.py
Executable file
4
couchpotato/core/migration/migrate.cfg
Normal file
4
couchpotato/core/migration/migrate.cfg
Normal file
@@ -0,0 +1,4 @@
|
||||
[db_settings]
|
||||
repository_id = CouchPotato
|
||||
version_table = migrate_version
|
||||
required_dbs = ['sqlite']
|
||||
30
couchpotato/core/migration/versions/__init__.py
Executable file
30
couchpotato/core/migration/versions/__init__.py
Executable file
@@ -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)
|
||||
|
||||
"""
|
||||
Reference in New Issue
Block a user