def migrate(args): """ Create DB migrations for current version """ # Get the configruation config = get_config() DB_URI = config.DATABASE_URI M_REPO = config.MIGRATIONS v = api.db_version(DB_URI, M_REPO) m = os.path.join(M_REPO, 'versions', '%03d_migration.py' % (v + 1)) tmpmod = imp.new_module('old_model') oldmod = api.create_model(DB_URI, M_REPO) exec(oldmod, tmpmod.__dict__) script = api.make_update_script_for_model( DB_URI, M_REPO, tmpmod.meta, elmr.db.metadata ) with open(m, 'wt') as f: f.write(script) v = api.db_version(DB_URI, M_REPO) return "New migration saved as %s\nCurrent database version: %d" % (m, v)
def migratedb(): """ Updates the database and SQLAlchemy-migrate repository to a new version containing all of the tables defined in the SQLAlchemy data models. """ # Obtain Current Verison ver = api.db_version(app.config['SQLALCHEMY_DATABASE_URI'], app.config['SQLALCHEMY_MIGRATE_REPO']) # Create Migration Script To Apply Model Changes mgr = app.config['SQLALCHEMY_MIGRATE_REPO'] +\ ('/versions/%03d_migration.py' % (ver+1)) tmp_module = imp.new_module('old_model') old_model = api.create_model(app.config['SQLALCHEMY_DATABASE_URI'], app.config['SQLALCHEMY_MIGRATE_REPO']) exec(old_model, tmp_module.__dict__) script = api.make_update_script_for_model( app.config['SQLALCHEMY_DATABASE_URI'], app.config['SQLALCHEMY_MIGRATE_REPO'], tmp_module.meta, db.metadata) open(mgr, "wt").write(script) # Update Database With Migration Script api.upgrade(app.config['SQLALCHEMY_DATABASE_URI'], app.config['SQLALCHEMY_MIGRATE_REPO']) # Obtain & Display Current Version & Migration ver = api.db_version(app.config['SQLALCHEMY_DATABASE_URI'], app.config['SQLALCHEMY_MIGRATE_REPO']) print('New migration saved as: ' + mgr) print('Current databse version: ' + str(ver))
def db_version(engine, abs_path, init_version): """Show the current version of the repository. :param engine: SQLAlchemy engine instance for a given database :param abs_path: Absolute path to migrate repository :param version: Initial database version """ repository = _find_migrate_repo(abs_path) try: return versioning_api.db_version(engine, repository) except versioning_exceptions.DatabaseNotControlledError: meta = sqlalchemy.MetaData() meta.reflect(bind=engine) tables = meta.tables if len(tables) == 0 or "alembic_version" in tables: db_version_control(engine, abs_path, version=init_version) return versioning_api.db_version(engine, repository) else: raise exception.DbMigrationError( message=_( "The database is not under version control, but has " "tables. Please stamp the current version of the schema " "manually." ) )
def downgrade_db(*opts): ''' Downgrades the Database 1 rev. ''' v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) api.downgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, v - 1) print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
def wrapper(self, *args, **kwargs): try: api.db_version(self.sqlalchemy_database_uri, self.sqlalchemy_migration_path) except InvalidRepositoryError: print('You have no database under version control. ' 'Try to "init" it first') return command(self, *args, **kwargs)
def test_dbSync_withException(self): self.mox.StubOutWithMock(versioning_api, "db_version") versioning_api.db_version(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndRaise( versioning_exceptions.DatabaseNotControlledError ) self.mox.ReplayAll() self.assertRaises(Exception, migration.db_sync, "0") self.mox.UnsetStubs()
def migrate_database(): migration = c.SQLALCHEMY_MIGRATE_REPO + '/versions/%03d_migration.py' % (api.db_version(c.SQLALCHEMY_DATABASE_URI, c.SQLALCHEMY_MIGRATE_REPO) + 1) tmp_module = imp.new_module('old_model') old_model = api.create_model(c.SQLALCHEMY_DATABASE_URI, c.SQLALCHEMY_MIGRATE_REPO) exec old_model in tmp_module.__dict__ script = api.make_update_script_for_model(c.SQLALCHEMY_DATABASE_URI, c.SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata) open(migration, "wt").write(script) api.upgrade(c.SQLALCHEMY_DATABASE_URI, c.SQLALCHEMY_MIGRATE_REPO) print 'New migration saved as ' + migration print 'Current database version: ' + str(api.db_version(c.SQLALCHEMY_DATABASE_URI, c.SQLALCHEMY_MIGRATE_REPO))
def upgrade(self, version=None): import migrate.versioning.api as mig self.setup_migration_version_control(version) version_before = mig.db_version(self.metadata.bind, self.migrate_repository) mig.upgrade(self.metadata.bind, self.migrate_repository, version=version) version_after = mig.db_version(self.metadata.bind, self.migrate_repository) if version_after != version_before: print 'CKAN database version upgraded: %s -> %s' % (version_before, version_after) else: print 'CKAN database version remains as: %s' % version_after
def downgraddb(): v = api.db_version(Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_REPO) api.downgrade(Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_REPO, v - 1) v = api.db_version(Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_REPO) print 'Current database version: ' + str(v)
def create_db(): """ Creates the test DB """ try: db_version(URI, REPO) except DatabaseNotControlledError: version_control(URI, REPO) upgrade(URI, REPO)
def migrate_db(): import imp 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, Base.metadata) open(migration, "wt").write(script) api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
def migrate_db(): migration = os.path.join(MG_REPO, 'versions', '%03d_migration.py' % (api.db_version(DB_URI, MG_REPO) + 1)) tmp_module = imp.new_module('old_model') old_model = api.create_model(DB_URI, MG_REPO) exec(old_model, tmp_module.__dict__) script = api.make_update_script_for_model(DB_URI, MG_REPO, tmp_module.meta, db.metadata) # ? open(migration, 'wt').write(script) api.upgrade(DB_URI, MG_REPO) print('New migration saved as ' + migration) print('Current database version: ' + str(api.db_version(DB_URI, MG_REPO)))
def migrate(): from migrate.versioning import api migration = SQLALCHEMY_MIGRATE_REPO + '/versions/%03d_migration.py' % (api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + 1) tmp_module = imp.new_module('old_model') old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) exec old_model in 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) print u'Миграция успешна: ' + migration print u'Версия базы данных: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
def db_version(engine): repository = _find_migrate_repo() try: return versioning_api.db_version(engine, repository) except versioning_exceptions.DatabaseNotControlledError: meta = sqlalchemy.MetaData() meta.reflect(bind=engine) tables = meta.tables if not tables: db_version_control(engine, 0) return versioning_api.db_version(engine, repository)
def db_version(engine, init_version=INIT_VERSION): """Show the current version of the repository. :param engine: SQLAlchemy engine instance for a given database :param init_version: Initial database version """ repository = _find_migrate_repo() try: return versioning_api.db_version(engine, repository) except versioning_exceptions.DatabaseNotControlledError: db_version_control(engine, init_version) return versioning_api.db_version(engine, repository)
def db_version(): repository = _find_migrate_repo() try: return versioning_api.db_version(get_engine(), repository) except versioning_exceptions.DatabaseNotControlledError: meta = sqlalchemy.MetaData() engine = get_engine() meta.reflect(bind=engine) tables = meta.tables if len(tables) == 0: db_version_control(0) return versioning_api.db_version(get_engine(), repository)
def db_downgrade(version=None): # This is used to downgrade the database schema to a certain version or to one version before. # If you know exactly the version you wish to use then you can directly downgrade to that version. if not version: current_version = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) downgrade_version = current_version - 1 else: downgrade_version = version api.downgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, downgrade_version) print 'Database downgrade completed!' print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
def migrate_db(): 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))
def db_migrate(): import imp migration = SQLALCHEMY_MIGRATE_REPO + '/versions/%03d_migration.py' % (api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + 1) tmp_module = imp.new_module('old_model') old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) #exec old_model in tmp_module.__dict__ # python2 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) print ('New migration saved as ' + migration) print ('Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)))
def main(url, repository): # Check if database is under version control try: db_version(url, repository) except DatabaseNotControlledError: # put database under version control version_control(url, repository) kwargs = {'url': url, 'repository': repository } shell.main(**kwargs)
def db_migrate(): # This is used for database migration. Newly created database should go through this as well. migration = SQLALCHEMY_MIGRATE_REPO + '/versions/%03d_migration.py' % (api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + 1) tmp_module = imp.new_module('old_model') old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) exec old_model in 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) print 'New migration saved as ' + migration print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)) + '\n'
def migrate(): migration = SQLALCHEMY_MIGRATE_REPO + "/versions/%03d_migration.py" % ( api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + 1 ) tmp_module = imp.new_module("old_model") old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) exec old_model in 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) print "New migration saved as " + migration print "Current database version: " + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
def migrate_db(uri, **kwargs): # Need to apply migrate-versions repo_path = repo.Repository( os.path.abspath(os.path.dirname(opencenter_repo.__file__))) try: db_ver = migrate_api.db_version(uri, repo_path) except DatabaseNotControlledError: migrate_api.version_control(uri, repo_path) db_ver = migrate_api.db_version(uri, repo_path) # Find the current version in the repo latest = migrate_api.version(str(repo_path)) if db_ver < latest: migrate_api.upgrade(uri, repo_path)
def downgrade(args): """ Restores the database one previous version, run multiple times to go back multiple versions if needed. """ # Get the configruation config = get_config() DB_URI = config.DATABASE_URI M_REPO = config.MIGRATIONS v = api.db_version(DB_URI, M_REPO) api.downgrade(DB_URI, M_REPO, v - 1) v = api.db_version(DB_URI, M_REPO) return "Current database version: %i" % v
def db_version(): repository = _find_migrate_repo() try: return versioning_api.db_version(get_engine(), repository) except versioning_exceptions.DatabaseNotControlledError as exc: # If we aren't version controlled there may be an existing, # non-version controlled database present. meta = sqlalchemy.MetaData() engine = get_engine() meta.reflect(bind=engine) tables = meta.tables if len(tables): raise exc db_version_control(INIT_VERSION) return versioning_api.db_version(get_engine(), repository)
def db_version(): repository = _find_migrate_repo() try: return versioning_api.db_version(get_engine(), repository) except versioning_exceptions.DatabaseNotControlledError: meta = sqlalchemy.MetaData() engine = get_engine() meta.reflect(bind=engine) tables = meta.tables if len(tables) == 0: db_version_control(migration.INIT_VERSION) return versioning_api.db_version(get_engine(), repository) else: # Some pre-Essex DB's may not be version controlled. # Require them to upgrade using Essex first. raise exception.Error(_("Upgrade DB using Essex release first."))
def execute(self, parsed_args): url = cfg.CONF['storage:sqlalchemy'].database_connection if not os.path.exists(REPOSITORY): raise Exception('Migration Respository Not Found') try: target_version = int(parsed_args.to_version) \ if parsed_args.to_version else None current_version = versioning_api.db_version(url=url, repository=REPOSITORY) except DatabaseNotControlledError: raise Exception('Database not yet initialized') LOG.info("Attempting to synchronize database from version '%s' to '%s'" % (current_version, target_version)) if target_version and target_version < current_version: versioning_api.downgrade(url=url, repository=REPOSITORY, version=parsed_args.to_version) else: versioning_api.upgrade(url=url, repository=REPOSITORY, version=parsed_args.to_version) LOG.info('Database synchronized sucessfully')
def db_version(): repo_path = _find_migrate_repo() try: return versioning_api.db_version( CONF.sql.connection, repo_path) except versioning_exceptions.DatabaseNotControlledError: return db_version_control(0)
def migrateDB(): repo = app.config['SQLALCHEMY_MIGRATE_REPO'] uri = app.config['SQLALCHEMY_DATABASE_URI'] v = api.db_version(uri, repo) migration = repo + ('/versions/%03d_migration.py' % (v+1)) tmp_module = imp.new_module('old_model') old_model = api.create_model(uri, repo) exec(old_model, tmp_module.__dict__) script = api.make_update_script_for_model(uri, repo, tmp_module.meta, db.metadata) open(migration, "wt").write(script) api.upgrade(uri, repo) v = api.db_version(uri, repo) print('New migration saved as ' + migration) print('Current database version: ' + str(v))
def db_version(): repository = _find_migrate_repo() try: return versioning_api.db_version(get_engine(), repository) except versioning_exceptions.DatabaseNotControlledError: # If we aren't version controlled we may already have the database # in the state from before we started version control, check for that # and set up version_control appropriately meta = sqlalchemy.MetaData() engine = get_engine() meta.reflect(bind=engine) try: for table in ('auth_tokens', 'zones', 'export_devices', 'fixed_ips', 'floating_ips', 'instances', 'key_pairs', 'networks', 'projects', 'quotas', 'security_group_instance_association', 'security_group_rules', 'security_groups', 'services', 'migrations', 'users', 'user_project_association', 'user_project_role_association', 'user_role_association', 'virtual_storage_arrays', 'volumes', 'volume_metadata', 'volume_types', 'volume_type_extra_specs'): assert table in meta.tables return db_version_control(1) except AssertionError: return db_version_control(0)
import imp from migrate.versioning import api from settings import SQLALCHEMY_DB_URI, SQLALCHEMY_MIGRATE_REPO from stats import db, Base migration = SQLALCHEMY_MIGRATE_REPO + '/versions/%03d_migration.py' % ( api.db_version(SQLALCHEMY_DB_URI, SQLALCHEMY_MIGRATE_REPO) + 1) tmp_module = imp.new_module('old_model') old_model = api.create_model(SQLALCHEMY_DB_URI, SQLALCHEMY_MIGRATE_REPO) exec old_model in tmp_module.__dict__ script = api.make_update_script_for_model(SQLALCHEMY_DB_URI, SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, Base.metadata) open(migration, "wt").write(script) a = api.upgrade(SQLALCHEMY_DB_URI, SQLALCHEMY_MIGRATE_REPO) print 'New migration saved as ' + migration print 'Current DB version: ' + str( api.db_version(SQLALCHEMY_DB_URI, SQLALCHEMY_MIGRATE_REPO))
#!flask/Scripts/python # -*- coding: utf-8 -*- #数据库版本回退脚本 from migrate .versioning import api from config import SQLALCHEMY_DATABASE_URL from config import SQLALCHEMY_MIGRATE_REPO v = api.db_version(SQLALCHEMY_DATABASE_URL,SQLALCHEMY_MIGRATE_REPO) api.downgrade(SQLALCHEMY_DATABASE_URL,SQLALCHEMY_MIGRATE_REPO,v - 1) print "Current database version:" + str(api.db_version(SQLALCHEMY_DATABASE_URL,SQLALCHEMY_MIGRATE_REPO))
#!/usr/bin/env python # Original concept from # https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database import imp from migrate.versioning import api from app import db from config import Config as cfg db_version = api.db_version(cfg.SQLALCHEMY_DATABASE_URI, cfg.SQLALCHEMY_MIGRATE_REPO) migration = cfg.SQLALCHEMY_MIGRATE_REPO + ('/versions/%03d_migration.py' % (db_version + 1)) tmp_module = imp.new_module('old_model') old_model = api.create_model(cfg.SQLALCHEMY_DATABASE_URI, cfg.SQLALCHEMY_MIGRATE_REPO) exec(old_model, tmp_module.__dict__) script = api.make_update_script_for_model(cfg.SQLALCHEMY_DATABASE_URI, cfg.SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata) open(migration, "wt").write(script) api.upgrade(cfg.SQLALCHEMY_DATABASE_URI, cfg.SQLALCHEMY_MIGRATE_REPO) new_db_version = api.db_version(cfg.SQLALCHEMY_DATABASE_URI, cfg.SQLALCHEMY_MIGRATE_REPO) print('New migration saved as ' + migration) print('Current Database Version ' + str(new_db_version))
def version(self): try: return int(api.db_version(self.engine, self.db_repository)) except DatabaseNotControlledError: return 0
def downgrade_db(): db_ver = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) api.downgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, db_ver - 1) print "Current database version is: {0}".format( str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)))
def current_version(self): try: return migrate.db_version(self.url, self.repository) except: return None
import imp from migrate.versioning import api from app import db from config import Config migration = Config.SQLALCHEMY_MIGRATE_REPO + '/versions/%03d_migration.py' % (api.db_version( Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_REPO) + 1) tmp_module = imp.new_module('old_model') old_model = api.create_model( Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_REPO) exec(old_model, tmp_module.__dict__) script = api.make_update_script_for_model( Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata) open(migration, "wt").write(script) api.upgrade( Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_REPO) print( 'New migration saved as ' + migration) print('Current database version: ' + str(api.db_version( Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_REPO)))
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) print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI,SQLALCHEMY_MIGRATE_REPO)
def db_downgrade(): 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))
# -*- coding: utf-8 -*- import imp from migrate.versioning import api from config import SQLALCHEMY_DATABASE_URI from config import SQLALCHEMY_MIGRATE_REPO from app import db migration = SQLALCHEMY_MIGRATE_REPO + "/versions/%03d_migration.py" % (api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + 1) tmp_module = imp.new_module('old_module') old_module = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) exec old_module in 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) print 'New migration saved as ' + migration print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
#!/usr/bin/env python import os.path import sys cwd = os.path.dirname(os.path.realpath(__file__)) sys.path.insert(0, os.path.abspath(cwd + '/../')) from migrate.versioning import api from app import app api.upgrade(app.config['SQLALCHEMY_DATABASE_URI'], app.config['SQLALCHEMY_MIGRATE_REPO']) print 'Current database version: ' + str(api.db_version(app.config['SQLALCHEMY_DATABASE_URI'], app.config['SQLALCHEMY_MIGRATE_REPO']))
def _is_database_under_migrate_control(engine, repository): try: migrate_api.db_version(engine, repository) return True except migrate_exceptions.DatabaseNotControlledError: return False
def get_version(self): """Get the current version of the versioned DB. Raises DatabaseNotControlledError if the DB is not initialized.""" return db_version(self._db_url, self._repository_path)
# script = api.make_update_script_for_model(UserConfig.SQLALCHEMY_DATABASE_URI, # MigrateConfig.SQLALCHEMY_MIGRATE_JEFF, # tmp_module.meta, db.metadata) # open(migration, "wt").write(script) # api.upgrade(UserConfig.SQLALCHEMY_DATABASE_URI, MigrateConfig.SQLALCHEMY_MIGRATE_JEFF) # v = api.db_version(UserConfig.SQLALCHEMY_DATABASE_URI, MigrateConfig.SQLALCHEMY_MIGRATE_JEFF) # print('New migration saved as ' + migration) # print('Current database version: ' + str(v)) import imp from migrate.versioning import api from app import db from config import Config # from config import MigrateConfig # from config import UserConfig v = api.db_version(Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_JEFF) migration = Config.SQLALCHEMY_MIGRATE_JEFF + ('/versions/%03d_migration.py' % (v + 1)) tmp_module = imp.new_module('old_model') old_model = api.create_model(Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_JEFF) exec(old_model, tmp_module.__dict__) script = api.make_update_script_for_model(Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_JEFF, tmp_module.meta, db.metadata) open(migration, "wt").write(script) api.upgrade(Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_JEFF) v = api.db_version(Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_JEFF) print('New migration saved as ' + migration) print('Current database version: ' + str(v))
#!flask/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 migration = SQLALCHEMY_MIGRATE_REPO + '/version/%03_migrate.py' % (api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + 1) tmp_mdel = imp.new_module('old_model') old_mdel = api.create_module(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) exec old_model in tmp_module.__dict__ script = api.make_update_script_for_model(SQLALCHEMY_MIGRATE_REPO) open(migration, "wt").write(script) a = api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) print "New migration saved as " + migration print "Current database version: " + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
######数据库回退到前一个版本 import sys sys.path.append('..') from migrate.versioning import api from app import db from conf import SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO v = api.db_version(url=SQLALCHEMY_DATABASE_URI, repository=SQLALCHEMY_MIGRATE_REPO) api.downgrade(url=SQLALCHEMY_DATABASE_URI, repository=SQLALCHEMY_MIGRATE_REPO, version=v - 1) v = api.db_version(url=SQLALCHEMY_DATABASE_URI, repository=SQLALCHEMY_MIGRATE_REPO) print('db downgrade done ,current version is ' + str(v))
def db_upgrade(): api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) print('Current database version: ' + str(v))
def runCouchPotato(options, base_path, args, data_dir = None, log_dir = None, Env = None, desktop = None): try: locale.setlocale(locale.LC_ALL, "") encoding = locale.getpreferredencoding() except (locale.Error, IOError): encoding = None # for OSes that are poorly configured I'll just force UTF-8 if not encoding or encoding in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'): encoding = 'UTF-8' # Do db stuff db_path = os.path.join(data_dir, 'couchpotato.db') # Backup before start and cleanup old databases new_backup = os.path.join(data_dir, 'db_backup', str(int(time.time()))) # Create path and copy if not os.path.isdir(new_backup): os.makedirs(new_backup) src_files = [options.config_file, db_path, db_path + '-shm', db_path + '-wal'] for src_file in src_files: if os.path.isfile(src_file): shutil.copy2(src_file, os.path.join(new_backup, os.path.basename(src_file))) # Remove older backups, keep backups 3 days or at least 3 backups = [] for directory in os.listdir(os.path.dirname(new_backup)): backup = os.path.join(os.path.dirname(new_backup), directory) if os.path.isdir(backup): backups.append(backup) total_backups = len(backups) for backup in backups: if total_backups > 3: if int(os.path.basename(backup)) < time.time() - 259200: for src_file in src_files: b_file = os.path.join(backup, os.path.basename(src_file)) if os.path.isfile(b_file): os.remove(b_file) os.rmdir(backup) total_backups -= 1 # Register environment settings Env.set('encoding', encoding) Env.set('app_dir', base_path) Env.set('data_dir', data_dir) Env.set('log_path', os.path.join(log_dir, 'CouchPotato.log')) Env.set('db_path', 'sqlite:///' + db_path) Env.set('cache_dir', os.path.join(data_dir, 'cache')) Env.set('cache', FileSystemCache(os.path.join(Env.get('cache_dir'), 'python'))) Env.set('console_log', options.console_log) Env.set('quiet', options.quiet) Env.set('desktop', desktop) Env.set('args', args) Env.set('options', options) # Determine debug debug = options.debug or Env.setting('debug', default = False, type = 'bool') Env.set('debug', debug) # Development development = Env.setting('development', default = False, type = 'bool') Env.set('dev', development) # Disable logging for some modules for logger_name in ['enzyme', 'guessit', 'subliminal', 'apscheduler']: logging.getLogger(logger_name).setLevel(logging.ERROR) for logger_name in ['gntp', 'migrate']: logging.getLogger(logger_name).setLevel(logging.WARNING) # Use reloader reloader = debug is True and development and not Env.get('desktop') and not options.daemon # Logger logger = logging.getLogger() formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%m-%d %H:%M:%S') level = logging.DEBUG if debug else logging.INFO logger.setLevel(level) # To screen if (debug or options.console_log) and not options.quiet and not options.daemon: hdlr = logging.StreamHandler(sys.stderr) hdlr.setFormatter(formatter) logger.addHandler(hdlr) # To file hdlr2 = handlers.RotatingFileHandler(Env.get('log_path'), 'a', 500000, 10) hdlr2.setFormatter(formatter) logger.addHandler(hdlr2) # Start logging & enable colors import color_logs from couchpotato.core.logger import CPLog log = CPLog(__name__) log.debug('Started with options %s', options) def customwarn(message, category, filename, lineno, file = None, line = None): log.warning('%s %s %s line:%s', (category, message, filename, lineno)) warnings.showwarning = customwarn # Load configs & plugins loader = Env.get('loader') loader.preload(root = base_path) loader.run() # Load migrations initialize = True db = Env.get('db_path') if os.path.isfile(db_path): initialize = False from migrate.versioning.api import version_control, db_version, version, upgrade repo = os.path.join(base_path, '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 couchpotato.core.settings.model import setup setup() if initialize: fireEvent('app.initialize', in_order = True) # Create app from couchpotato import app api_key = Env.setting('api_key') url_base = '/' + Env.setting('url_base').lstrip('/') if Env.setting('url_base') else '' # Basic config app.secret_key = api_key # app.debug = development config = { 'use_reloader': reloader, 'host': Env.setting('host', default = '0.0.0.0'), 'port': tryInt(Env.setting('port', default = 5000)) } # Static path app.static_folder = os.path.join(base_path, 'couchpotato', 'static') web.add_url_rule('api/%s/static/<path:filename>' % api_key, endpoint = 'static', view_func = app.send_static_file) # Register modules app.register_blueprint(web, url_prefix = '%s/' % url_base) app.register_blueprint(api, url_prefix = '%s/api/%s/' % (url_base, api_key)) # Some logging and fire load event try: log.info('Starting server on port %(port)s', config) except: pass fireEventAsync('app.load') # Go go go! web_container = WSGIContainer(app) web_container._log = _log loop = IOLoop.instance() application = Application([ (r'%s/api/%s/nonblock/(.*)/' % (url_base, api_key), NonBlockHandler), (r'.*', FallbackHandler, dict(fallback = web_container)), ], log_function = lambda x : None, debug = config['use_reloader'] ) try_restart = True restart_tries = 5 while try_restart: try: application.listen(config['port'], config['host'], no_keep_alive = True) loop.start() except Exception, e: try: nr, msg = e if nr == 48: log.info('Already in use, try %s more time after few seconds', restart_tries) time.sleep(1) restart_tries -= 1 if restart_tries > 0: continue else: return except: pass raise try_restart = False
def get_db_version(): return migrate_api.db_version(url=db_url, repository=db_repo)
#!C:\Users\stkba\Anaconda3\envs\flask\python from migrate.versioning import api from config import SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, SQLALCHEMY_TRACK_MODIFICATIONS api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) print('Current database version: ', str(v))
def _version_by_db(): v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) return v
import imp from migrate.versioning import api from application import db from application import application # from config import SQLALCHEMY_DATABASE_URI # from config import SQLALCHEMY_MIGRATE_REPO # SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository') # v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) v = api.db_version(application.config['SQLALCHEMY_DATABASE_URI'], application.config['SQLALCHEMY_MIGRATE_REPO']) migration = application.config['SQLALCHEMY_MIGRATE_REPO'] + ( '/versions/%03d_migration.py' % (v + 1)) tmp_module = imp.new_module('old_model') old_model = api.create_model(application.config['SQLALCHEMY_DATABASE_URI'], application.config['SQLALCHEMY_MIGRATE_REPO']) exec old_model in tmp_module.__dict__ script = api.make_update_script_for_model( application.config['SQLALCHEMY_DATABASE_URI'], application.config['SQLALCHEMY_MIGRATE_REPO'], tmp_module.meta, db.metadata) open(migration, "wt").write(script) api.upgrade(application.config['SQLALCHEMY_DATABASE_URI'], application.config['SQLALCHEMY_MIGRATE_REPO']) v = api.db_version(application.config['SQLALCHEMY_DATABASE_URI'], application.config['SQLALCHEMY_MIGRATE_REPO']) print('New migration saved as ' + migration) print('Current database version: ' + str(v))
def get_version(cls) -> int: return api.db_version(cls.__database_uri, cls.__migrate_repo_path)
def db_downgrade(): from migrate.versioning import api v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) api.downgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, v - 1) print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
def upgrade_db(): api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) print "Current database version is: {0}".format( str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)))
def _migrate_up(self, engine, version): """Migrate up to a new version of database.""" migration_api.upgrade(engine, self.REPOSITORY, version) assert_equal(version, migration_api.db_version(engine, self.REPOSITORY))
def statuses(folder: str = None): rows = [] folder_status = 'OK' folder_comment = '' folders = [ ROOT_FOLDER, DATA_FOLDER, MODEL_FOLDER, TASK_FOLDER, LOG_FOLDER, CONFIG_FOLDER, DB_FOLDER, REPORT_FOLDER, TMP_FOLDER ] for f in folders: if not exists(f): folder_status = 'ERROR' folder_comment = f'folder {f} does not exist' files = [join(CONFIG_FOLDER, '.env')] for f in files: if not exists(f): folder_status = 'ERROR' folder_comment = f'file {f} does not exist' rows.append({ 'name': 'Folders', 'status': folder_status, 'comment': folder_comment }) database_status = 'OK' database_comment = f'DB_TYPE = {DB_TYPE}' try: provider = DagProvider() provider.count() except Exception: database_status = 'ERROR' database_comment += ' ' + traceback.format_exc() rows.append({ 'name': 'Database', 'status': database_status, 'comment': database_comment }) redis_status = 'OK' redis_comment = f'' try: celery.backend.client.echo(1) except Exception: redis_status = 'ERROR' redis_comment += ' ' + traceback.format_exc() rows.append({ 'name': 'Redis', 'status': redis_status, 'comment': redis_comment }) if database_status == 'OK': migrate_status = 'OK' repository_folder = join(dirname(__file__), 'migration') repository_version = api.version(repository_folder) db_version = api.db_version(SA_CONNECTION_STRING, repository_folder) if db_version != repository_version: migrate_status = 'ERROR' migrate_comment = f'Repository version = {repository_version} ' \ f'Db version = {db_version}' else: migrate_comment = f'version: {db_version}' rows.append({ 'name': 'Migrate', 'status': migrate_status, 'comment': migrate_comment }) df = pd.DataFrame(rows) if folder is not None: print('Statuses:') print(df) df.to_csv(join(folder, 'statuses.csv'), index=False) return df
def _migrate_down(self, engine, version): """Migrate down to an old version of database.""" migration_api.downgrade(engine, self.REPOSITORY, version) assert_equal(version, migration_api.db_version(engine, self.REPOSITORY))
def runCouchPotato(options, base_path, args, data_dir = None, log_dir = None, Env = None, desktop = None): try: locale.setlocale(locale.LC_ALL, "") encoding = locale.getpreferredencoding() except (locale.Error, IOError): encoding = None # for OSes that are poorly configured I'll just force UTF-8 if not encoding or encoding in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'): encoding = 'UTF-8' Env.set('encoding', encoding) # Do db stuff db_path = toUnicode(os.path.join(data_dir, 'couchpotato.db')) # Backup before start and cleanup old databases new_backup = toUnicode(os.path.join(data_dir, 'db_backup', str(int(time.time())))) # Create path and copy if not os.path.isdir(new_backup): os.makedirs(new_backup) src_files = [options.config_file, db_path, db_path + '-shm', db_path + '-wal'] for src_file in src_files: if os.path.isfile(src_file): dst_file = toUnicode(os.path.join(new_backup, os.path.basename(src_file))) shutil.copyfile(src_file, dst_file) # Try and copy stats seperately try: shutil.copystat(src_file, dst_file) except: pass # Remove older backups, keep backups 3 days or at least 3 backups = [] for directory in os.listdir(os.path.dirname(new_backup)): backup = toUnicode(os.path.join(os.path.dirname(new_backup), directory)) if os.path.isdir(backup): backups.append(backup) total_backups = len(backups) for backup in backups: if total_backups > 3: if tryInt(os.path.basename(backup)) < time.time() - 259200: for the_file in os.listdir(backup): file_path = os.path.join(backup, the_file) try: if os.path.isfile(file_path): os.remove(file_path) except: raise os.rmdir(backup) total_backups -= 1 # Register environment settings Env.set('app_dir', toUnicode(base_path)) Env.set('data_dir', toUnicode(data_dir)) Env.set('log_path', toUnicode(os.path.join(log_dir, 'CouchPotato.log'))) Env.set('db_path', toUnicode('sqlite:///' + db_path)) Env.set('cache_dir', toUnicode(os.path.join(data_dir, 'cache'))) Env.set('cache', FileSystemCache(toUnicode(os.path.join(Env.get('cache_dir'), 'python')))) Env.set('console_log', options.console_log) Env.set('quiet', options.quiet) Env.set('desktop', desktop) Env.set('daemonized', options.daemon) Env.set('args', args) Env.set('options', options) # Determine debug debug = options.debug or Env.setting('debug', default = False, type = 'bool') Env.set('debug', debug) # Development development = Env.setting('development', default = False, type = 'bool') Env.set('dev', development) # Disable logging for some modules for logger_name in ['enzyme', 'guessit', 'subliminal', 'apscheduler']: logging.getLogger(logger_name).setLevel(logging.ERROR) for logger_name in ['gntp', 'migrate']: logging.getLogger(logger_name).setLevel(logging.WARNING) # Use reloader reloader = debug is True and development and not Env.get('desktop') and not options.daemon # Logger logger = logging.getLogger() formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%m-%d %H:%M:%S') level = logging.DEBUG if debug else logging.INFO logger.setLevel(level) logging.addLevelName(19, 'INFO') # To screen if (debug or options.console_log) and not options.quiet and not options.daemon: hdlr = logging.StreamHandler(sys.stderr) hdlr.setFormatter(formatter) logger.addHandler(hdlr) # To file hdlr2 = handlers.RotatingFileHandler(Env.get('log_path'), 'a', 500000, 10) hdlr2.setFormatter(formatter) logger.addHandler(hdlr2) # Start logging & enable colors import color_logs from couchpotato.core.logger import CPLog log = CPLog(__name__) log.debug('Started with options %s', options) def customwarn(message, category, filename, lineno, file = None, line = None): log.warning('%s %s %s line:%s', (category, message, filename, lineno)) warnings.showwarning = customwarn # Check if database exists db = Env.get('db_path') db_exists = os.path.isfile(toUnicode(db_path)) # Load migrations if db_exists: from migrate.versioning.api import version_control, db_version, version, upgrade repo = os.path.join(base_path, '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: if development: log.error('There is a database migration ready, but you are running development mode, so it won\'t be used. If you see this, you are stupid. Please disable development mode.') else: log.info('Doing database upgrade. From %d to %d', (current_db_version, latest_db_version)) upgrade(db, repo) # Configure Database from couchpotato.core.settings.model import setup setup() # Create app from couchpotato import WebHandler web_base = ('/' + Env.setting('url_base').lstrip('/') + '/') if Env.setting('url_base') else '/' Env.set('web_base', web_base) api_key = Env.setting('api_key') api_base = r'%sapi/%s/' % (web_base, api_key) Env.set('api_base', api_base) # Basic config host = Env.setting('host', default = '0.0.0.0') # app.debug = development config = { 'use_reloader': reloader, 'port': tryInt(Env.setting('port', default = 5050)), 'host': host if host and len(host) > 0 else '0.0.0.0', 'ssl_cert': Env.setting('ssl_cert', default = None), 'ssl_key': Env.setting('ssl_key', default = None), } # Load the app application = Application([], log_function = lambda x : None, debug = config['use_reloader'], gzip = True, ) Env.set('app', application) # Request handlers application.add_handlers(".*$", [ (r'%snonblock/(.*)(/?)' % api_base, NonBlockHandler), # API handlers (r'%s(.*)(/?)' % api_base, ApiHandler), # Main API handler (r'%sgetkey(/?)' % web_base, KeyHandler), # Get API key (r'%s' % api_base, RedirectHandler, {"url": web_base + 'docs/'}), # API docs # Catch all webhandlers (r'%s(.*)(/?)' % web_base, WebHandler), (r'(.*)', WebHandler), ]) # Static paths static_path = '%sstatic/' % api_base for dir_name in ['fonts', 'images', 'scripts', 'style']: application.add_handlers(".*$", [ ('%s%s/(.*)' % (static_path, dir_name), StaticFileHandler, {'path': toUnicode(os.path.join(base_path, 'couchpotato', 'static', dir_name))}) ]) Env.set('static_path', static_path) # Load configs & plugins loader = Env.get('loader') loader.preload(root = toUnicode(base_path)) loader.run() # Fill database with needed stuff if not db_exists: fireEvent('app.initialize', in_order = True) # Go go go! from tornado.ioloop import IOLoop loop = IOLoop.current() # Some logging and fire load event try: log.info('Starting server on port %(port)s', config) except: pass fireEventAsync('app.load') if config['ssl_cert'] and config['ssl_key']: server = HTTPServer(application, no_keep_alive = True, ssl_options = { "certfile": config['ssl_cert'], "keyfile": config['ssl_key'], }) else: server = HTTPServer(application, no_keep_alive = True) try_restart = True restart_tries = 5 while try_restart: try: server.listen(config['port'], config['host']) loop.start() except Exception, e: log.error('Failed starting: %s', traceback.format_exc()) try: nr, msg = e if nr == 48: log.info('Port (%s) needed for CouchPotato is already in use, try %s more time after few seconds', (config.get('port'), restart_tries)) time.sleep(1) restart_tries -= 1 if restart_tries > 0: continue else: return except: pass raise try_restart = False
def _db_version(cls): return versioning_api.db_version(CONF.db.sql_connection, cls._get_repo_path())