def check_database(self): if not self.engine.has_table(SchemaVersion.__tablename__): log.info("Creating database schema table") SchemaVersion.__table__.create(bind=self.engine) repository = Repository(upgrades.__path__[0]) session = self.session() if not session.query(SchemaVersion).first(): session.add(SchemaVersion( "Network Audio Monitor Schema Version Control", path.abspath(path.expanduser(repository.path)), 0) ) session.commit() schema_version = session.query(SchemaVersion).first() if schema_version.version >= repository.latest: log.info("No database upgrade required") return component.get("EventManager").emit(DatabaseUpgradeRequired()) log.warn("Upgrading database (from -> to...)") component.get("EventManager").emit(DatabaseUpgradeStart()) upgrade(self.engine, repository) log.warn("Upgrade complete.") component.get("EventManager").emit(DatabaseUpgradeComplete())
def initialize_startup(): """ Force DB tables create, in case no data is already found.""" is_db_empty = False session = SA_SESSIONMAKER() inspector = reflection.Inspector.from_engine(session.connection()) if len(inspector.get_table_names()) < 1: LOGGER.debug("Database access exception, maybe DB is empty") is_db_empty = True session.close() if is_db_empty: LOGGER.info("Initializing Database") if os.path.exists(cfg.DB_VERSIONING_REPO): shutil.rmtree(cfg.DB_VERSIONING_REPO) migratesqlapi.create(cfg.DB_VERSIONING_REPO, os.path.split(cfg.DB_VERSIONING_REPO)[1]) _update_sql_scripts() migratesqlapi.version_control(cfg.DB_URL, cfg.DB_VERSIONING_REPO, version=cfg.DB_CURRENT_VERSION) session = SA_SESSIONMAKER() model.Base.metadata.create_all(bind=session.connection()) session.commit() session.close() LOGGER.info("Database Default Tables created successfully!") else: _update_sql_scripts() migratesqlapi.upgrade(cfg.DB_URL, cfg.DB_VERSIONING_REPO, version=cfg.DB_CURRENT_VERSION) LOGGER.info("Database already has some data, will not be re-created!") return is_db_empty
def upgrade_db(*opts): ''' Upgrades the Database to current. ''' api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
def db_sync(): repo_path = os.path.abspath(os.path.dirname(migrate_repo.__file__)) try: versioning_api.upgrade(CONF.database.connection, repo_path) except versioning_exceptions.DatabaseNotControlledError: versioning_api.version_control(CONF.database.connection, repo_path) versioning_api.upgrade(CONF.database.connection, repo_path)
def setup_db(settings): """ We need to create the test sqlite database to run our tests against If the db exists, remove it We're using the SA-Migrations API to create the db and catch it up to the latest migration level for testing In theory, we could use this API to do version specific testing as well if we needed to. If we want to run any tests with a fresh db we can call this function """ from migrate.versioning import api as mig sa_url = settings['sqlalchemy.url'] migrate_repository = 'migrations' # we're hackish here since we're going to assume the test db is whatever is # after the last slash of the SA url sqlite:///somedb.db db_name = sa_url[sa_url.rindex('/') + 1:] try: os.remove(db_name) except: pass open(db_name, 'w').close() mig.version_control(sa_url, migrate_repository) mig.upgrade(sa_url, migrate_repository)
def _generateMigrationScript(dryrun): newVersion = findMaxAvailableVersion() + 1 migration = SQLALCHEMY_MIGRATE_REPO + \ '/versions/%03d_migration.py' % (newVersion) 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) if not dryrun: 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)) else: print 'Dryrun:' print '\nNew migration will be as ' + migration print '\nNew migration script will be:\n"\n%s"' % str(script) print '\nNew database version will be: ' + str(newVersion)
def db_sync(engine): db_version(engine) # This is needed to create a version stamp in empty DB repository = _find_migrate_repo() versioning_api.upgrade(engine, repository) config = _alembic_config() config._engine = engine alembic.command.upgrade(config, "head")
def execute(self, parsed_args): url = cfg.CONF['backend:powerdns'].database_connection if not os.path.exists(REPOSITORY): raise Exception('Migration Repository 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('PowerDNS database not yet initialized') LOG.info("Attempting to synchronize PowerDNS database from version " "'%s' to '%s'", current_version, target_version if target_version is not None else "latest") 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('PowerDNS database synchronized successfully')
def _setup(config): # disable delayed execution # config['adhocracy.amqp.host'] = None # FIXME: still do this with rq instead of rabbitmq # NOTE: this is called from tests so it may have side effects # Create the tables if they don't already exist url = config.get('sqlalchemy.url') migrate_repo = os.path.join(os.path.dirname(__file__), 'migration') repo_version = migrateapi.version(migrate_repo) if config.get('adhocracy.setup.drop', "OH_NOES") == "KILL_EM_ALL": meta.data.drop_all(bind=meta.engine) meta.engine.execute("DROP TABLE IF EXISTS migrate_version") try: db_version = migrateapi.db_version(url, migrate_repo) if db_version < repo_version: migrateapi.upgrade(url, migrate_repo) initial_setup = False except DatabaseNotControlledError: meta.data.create_all(bind=meta.engine) migrateapi.version_control(url, migrate_repo, version=repo_version) initial_setup = True install.setup_entities(config, initial_setup)
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 sync(self, version=None): url = cfg.CONF['storage:sqlalchemy'].database_connection if not os.path.exists(REPOSITORY): raise Exception('Migration Repository Not Found') try: target_version = int(version) if 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 is not None else "latest") if target_version and target_version < current_version: versioning_api.downgrade(url=url, repository=REPOSITORY, version=version) else: versioning_api.upgrade(url=url, repository=REPOSITORY, version=version) LOG.info('Database synchronized successfully')
def __init__(self, file, repository, echoresults): #for backward compatibelity if re.match('^\w+://', file) == None: file = 'sqlite:///'+file self.version = 2 self.dbfile = file self.repository = repository #migrate code try: dbversion = api.db_version(file, self.repository) #print dbversion except : dbversion = 0 api.version_control(file, self.repository, dbversion) if dbversion < self.version: api.upgrade(file, self.repository, self.version) elif dbversion > self.version: api.downgrade(file, self.repository, self.version) engine = create_engine(file , echo=False)#edit by hassan : echoresults to True metadata = Base.metadata metadata.create_all(engine) Session = sessionmaker(engine) self.session = Session()
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 _upgrade(self, version=None): if version: api.upgrade(self.sqlalchemy_database_uri, self.sqlalchemy_migration_path, version) else: api.upgrade(self.sqlalchemy_database_uri, self.sqlalchemy_migration_path)
def _memorydb_migrate_db(**kwargs): """ This is crazy crackheaded, and abusive to sqlalchemy. We'll take out dispose so the migrate stuff doesn't kill it, and push through the migrate. This makes a bunch of assumptions that are likely stupid, but since this is done on a memory-backed db for testing, it's probably okay. Just don't run this on a real db. """ def dispose_patch(*args, **kwargs): pass global engine Base.metadata.create_all(bind=engine) for table in reversed(Base.metadata.sorted_tables): session.execute(table.delete()) session.commit() old_dispose = engine.dispose engine.dispose = dispose_patch repo_path = repo.Repository( os.path.abspath(os.path.dirname(opencenter_repo.__file__))) migrate_api.version_control(engine, repo_path) migrate_api.upgrade(engine, repo_path) engine.dispose = old_dispose
def sync(conf): register_conf_opts(conf) repo_path = os.path.abspath(os.path.dirname(migrate_repo.__file__)) try: versioning_api.upgrade(conf.sql.connection, repo_path) except versioning_exceptions.DatabaseNotControlledError: versioning_api.version_control(conf.sql.connection, repo_path) versioning_api.upgrade(conf.sql.connection, repo_path)
def test_passing_engine(self): repo = self.tmp_repos() api.create(repo, 'temp') api.script('First Version', repo) engine = construct_engine('sqlite:///:memory:') api.version_control(engine, repo) api.upgrade(engine, repo)
def setup_db(settings): """ We need to create the test sqlite database to run our tests against If the db exists, remove it We're using the SA-Migrations API to create the db and catch it up to the latest migration level for testing In theory, we could use this API to do version specific testing as well if we needed to. If we want to run any tests with a fresh db we can call this function """ from migrate.versioning import api as mig sa_url = settings['sqlalchemy.url'] migrate_repository = 'migrations' if 'mysql' in sa_url: # MYSQL CONFIG from sqlalchemy import create_engine engine = create_engine(sa_url) # # drop any existing data all_tables = engine.execute('SHOW TABLES'); if all_tables.rowcount: qry = "`, `".join([res[0] for res in all_tables]) engine.execute("DROP TABLES `" + qry + '`;') elif 'postgres' in sa_url: # MYSQL CONFIG from sqlalchemy import create_engine engine = create_engine(sa_url) # # drop any existing data all_tables = engine.execute("""SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' """); if all_tables.rowcount: qry = ", ".join([res[0] for res in all_tables]) engine.execute("DROP TABLE " + qry + ';') else: # we're hackish here since we're going to assume the test db is whatever is # after the last slash of the SA url sqlite:///somedb.db db_name = sa_url[sa_url.rindex('/') + 1:] try: # if this is a sqlite db then try to take care of the db file os.remove(db_name) open(db_name, 'w').close() except: pass mig.version_control(sa_url, migrate_repository) mig.upgrade(sa_url, migrate_repository)
def __init__(self, repo_path): super(DatabaseFixture, self).__init__() self.golden_db = self._mktemp() engine = sqlalchemy.create_engine('sqlite:///%s' % self.golden_db) repo = repository.Repository(repo_path) versioning_api.version_control(engine, repository=repo) versioning_api.upgrade(engine, repository=repo) self.working_copy = self._mktemp() self.url = 'sqlite:///%s' % self.working_copy
def main(parser, options, args): engine_url = config["sqlalchemy.url"] latest_version = version(migrate_repository) try: version_control(engine_url, migrate_repository, version=initial_version, echo=DEBUG) except DatabaseAlreadyControlledError: pass upgrade(engine_url, migrate_repository, version=latest_version, echo=DEBUG) sys.exit(0)
def db_init(name, version=None): """Init a database and put it under ``migrate`` control.""" db_url = config.db_url_tmpl % name migrate_repo = config.db_migrate_repo try: migrate_api.version_control(db_url, migrate_repo) except DatabaseAlreadyControlledError: pass migrate_api.upgrade(db_url, migrate_repo, version)
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 create_db(): """ Creates the test DB """ try: db_version(URI, REPO) except DatabaseNotControlledError: version_control(URI, REPO) upgrade(URI, REPO)
def db_upgrade(version=None): """Upgrade the database""" v1 = get_db_version() migrate_api.upgrade(url=db_url, repository=db_repo, version=version) v2 = get_db_version() if v1 == v2: print 'Database already up-to-date.' else: print 'Upgraded: %s ... %s' % (v1, v2)
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 sync(self): """ Migrate database schemas. """ repo_path = os.path.abspath(os.path.dirname(migrate_repo.__file__)) try: versioning_api.upgrade(self.connection_url, repo_path) except versioning_exceptions.DatabaseNotControlledError: versioning_api.version_control(self.connection_url, repo_path) versioning_api.upgrade(self.connection_url, repo_path)
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 take_action(self, parsed_args): # TODO: Support specifying version url = cfg.CONF['storage:sqlalchemy'].database_connection if not os.path.exists(REPOSITORY): raise Exception('Migration Respository Not Found') LOG.info('Attempting to synchronize database') versioning_api.upgrade(url=url, repository=REPOSITORY, version=None) LOG.info('Database synchronized sucessfully')
def setup(): Database.logger.debug("Setting up database.") if not os.path.exists(DATABASE_FILE): base.metadata.create_all(engine) api.version_control(DATABASE_URI, MIGRATE_REPO, version=api.version(MIGRATE_REPO)) else: try: api.version_control(DATABASE_URI, MIGRATE_REPO, version=1) except migrate.DatabaseAlreadyControlledError: pass api.upgrade(DATABASE_URI, MIGRATE_REPO)
def migratedb(): """Creates a migration file and version.""" migration = app.config.get('SQLALCHEMY_MIGRATE_REPO') + '/versions/%03d_migration.py' % (api.db_version(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO')) + 1) tmp_module = imp.new_module('old_model') old_model = api.create_model(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO')) exec old_model in tmp_module.__dict__ script = api.make_update_script_for_model(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO'), tmp_module.meta, db.metadata) open(migration, "wt").write(script) api.upgrade(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO')) print 'New migration saved as ' + migration print 'Current database version: ' + str(api.db_version(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO')))
def upgrade(self): """Run all available upgrade scripts for the repository.""" upgrade(self._db_url, self._repository_path)
import imp from migrate.versioning import api from app import database from config import Config v = api.db_version(Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_REPO) migration = Config.SQLALCHEMY_MIGRATE_REPO + ('/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_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, database.metadata) open(migration, "wt").write(script) api.upgrade(Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_REPO) v = api.db_version(Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_REPO) print('New migration saved as ' + migration) print('Current database version: ' + str(v))
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))
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 db_upgrade(): from migrate.versioning import api api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
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 tryInt(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) 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(db_path) # Load configs & plugins loader = Env.get('loader') loader.preload(root=base_path) loader.run() # 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 and not development: 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() # Fill database with needed stuff if not db_exists: 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, 'port': tryInt(Env.setting('port', default=5000)), 'host': Env.setting('host', default="0.0.0.0"), 'ssl_cert': Env.setting('ssl_cert', default=None), 'ssl_key': Env.setting('ssl_key', default=None), } # 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! from tornado.ioloop import IOLoop 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']) 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: 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 upgrade(cls): api.upgrade(cls.__database_uri, cls.__migrate_repo_path)
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))
#!/usr/bin/env python # coding: utf-8 #********************************** # author: h3idan # datetime: 2013-07-19 17:09 #********************************** ''' 数据库回滚之后,执行该文件。 更新回最新版本的数据库 ''' import sys sys.path.append('../') from migrate.versioning import api from app.models 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']))
# this file allows you to migrate your database to the latest changes you have made import imp from migrate.versioning import api from ParentalControl 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))
"This migration will perform initial setup of the annotation \n" "store, and create the required admin accounts. Proceed? [Y/n] ") if r and r[0] in ['n', 'N']: sys.exit(1) print("\nCreating SQLite database and ElasticSearch indices... ", end="") app = annotateit.create_app() annotateit.create_indices(app) migrate_args = dict(url=app.config['SQLALCHEMY_DATABASE_URI'], debug='False', repository='migration') migrate.version_control(**migrate_args) migrate.upgrade(**migrate_args) print("done.\n") username = raw_input("Admin username [admin]: ").strip() if not username: username = '******' email = '' while not email: email = raw_input("Admin email: ").strip() password = '' while not password: password = getpass("Admin password: ")
#!/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))