def createdb(): """ Creates a database with all of the tables defined in the SQLAlchemy models. Creates and initializes an SQLAlchemy-migrate repository if none exists. """ # Create New DB Reflecting SQLAlchemy Data Models db.create_all(app=app) admin = User('admin', '/home/admin', 'password') db.session.add(admin) db.session.commit() # Create SQLAlchemy-migrate Versioning Repository If Absent if not os.path.exists(app.config['SQLALCHEMY_MIGRATE_REPO']): api.create(app.config['SQLALCHEMY_MIGRATE_REPO'], 'database repository') api.version_control(app.config['SQLALCHEMY_DATABASE_URI'], app.config['SQLALCHEMY_MIGRATE_REPO']) print "SQLAlchemy-migrate Versioning Repository Created in: " +\ app.config['SQLALCHEMY_MIGRATE_REPO'] else: api.version_control(app.config['SQLALCHEMY_DATABASE_URI'], app.config['SQLALCHEMY_MIGRATE_REPO'], api.version( app.config['SQLALCHEMY_MIGRATE_REPO'])) print "Database created in: " + app.config['SQLALCHEMY_DATABASE_URI']
def run(self): db.create_all() 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))
def create_database(): db.create_all() if not os.path.exists(c.SQLALCHEMY_MIGRATE_REPO): api.create(c.SQLALCHEMY_MIGRATE_REPO, 'database repository') api.version_control(c.SQLALCHEMY_DATABASE_URI, c.SQLALCHEMY_MIGRATE_REPO) else: api.version_control(c.SQLALCHEMY_DATABASE_URI, c.SQLALCHEMY_MIGRATE_REPO, api.version(c.SQLALCHEMY_MIGRATE_REPO))
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 create_db(): Base.metadata.create_all(engine) if not 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)) M1A = Module(name=u'M1A', slot=1, gpio=8, io_type='input', rpull=False, status=False, device_id='') M1B = Module(name=u'M1B', slot=1, gpio=7, io_type='input', rpull=False, status=False, device_id='') M1C = Module(name=u'M1C', slot=1, gpio=11, io_type='input', rpull=False, status=False, device_id='') M2A = Module(name=u'M2A', slot=2, gpio=9, io_type='input', rpull=False, status=False, device_id='') M2B = Module(name=u'M2B', slot=2, gpio=10, io_type='input', rpull=False, status=False, device_id='') M2C = Module(name=u'M2C', slot=2, gpio=5, io_type='input', rpull=False, status=False, device_id='') M3A = Module(name=u'M3A', slot=3, gpio=6, io_type='input', rpull=False, status=False, device_id='') M3B = Module(name=u'M3B', slot=3, gpio=12, io_type='input', rpull=False, status=False, device_id='') M3C = Module(name=u'M3C', slot=3, gpio=13, io_type='input', rpull=False, status=False, device_id='') M4A = Module(name=u'M4A', slot=4, gpio=0, io_type='input', rpull=False, status=False, device_id='') M4B = Module(name=u'M4B', slot=4, gpio=1, io_type='input', rpull=False, status=False, device_id='') M4C = Module(name=u'M4C', slot=4, gpio=16, io_type='input', rpull=False, status=False, device_id='') M5A = Module(name=u'M5A', slot=5, gpio=17, io_type='input', rpull=False, status=False, device_id='') M5B = Module(name=u'M5B', slot=5, gpio=18, io_type='input', rpull=False, status=False, device_id='') M5C = Module(name=u'M5C', slot=5, gpio=19, io_type='input', rpull=False, status=False, device_id='') M6A = Module(name=u'M6A', slot=6, gpio=20, io_type='input', rpull=False, status=False, device_id='') M6B = Module(name=u'M6B', slot=6, gpio=21, io_type='input', rpull=False, status=False, device_id='') M6C = Module(name=u'M6C', slot=6, gpio=22, io_type='input', rpull=False, status=False, device_id='') M7A = Module(name=u'M7A', slot=7, gpio=23, io_type='input', rpull=False, status=False, device_id='') M7B = Module(name=u'M7B', slot=7, gpio=24, io_type='input', rpull=False, status=False, device_id='') M7C = Module(name=u'M7C', slot=7, gpio=25, io_type='input', rpull=False, status=False, device_id='') modules = [M1A, M1B, M1C, M2A, M2B, M2C, M3A, M3B, M3C, M4A, M4B, M4C, M5A, M5B, M5C, M6A, M6B, M6C, M7A, M7B, M7C] for m in modules: session.add(m) session.commit()
def __init__(self): database.create_all() if not os.path.exists(SQLALCHEMY_MIGRATE_CONT): api.create(SQLALCHEMY_MIGRATE_CONT, "database container") api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_CONT) else: api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_CONT, api.version(SQLALCHEMY_MIGRATE_CONT))
def initdb(): """Creates all database tables.""" db.create_all() if not os.path.exists(app.config.get('SQLALCHEMY_MIGRATE_REPO')): api.create(app.config.get('SQLALCHEMY_MIGRATE_REPO'), 'database repository') api.version_control(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO')) else: api.version_control(app.config.get('SQLALCHEMY_DATABASE_URI'), app.config.get('SQLALCHEMY_MIGRATE_REPO'), api.version(app.config.get('SQLALCHEMY_MIGRATE_REPO')))
def create_db_and_update(db_uri, migrate_repo): if not os.path.exists(migrate_repo): api.create(migrate_repo, 'database repository') api.version_control(db_uri, migrate_repo) else: api.version_control(db_uri, migrate_repo, api.version(migrate_repo))
def init_db(): import models Base.metadata.create_all(bind=engine) 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))
def create(): print '{} is being instantiated now!'.format(SQLALCHEMY_DATABASE_URI) orm_db.create_all() 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))
def db_create(): pgdb.create_all() if not os.path.exists(Config.SQLALCHEMY_MIGRATE_REPO): api.create(Config.SQLALCHEMY_MIGRATE_REPO, 'database repository') api.version_control(Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_REPO) else: api.version_control(Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_MIGRATE_REPO, api.version(Config.SQLALCHEMY_MIGRATE_REPO))
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 create(): create_engine(SQLALCHEMY_DATABASE_URI, echo=True) 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))
def create_db(): from migrate.versioning import api db.create_all() 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))
def create_mg(): ''' 初始化数据迁移仓库 ''' if not os.path.exists(MG_REPO): api.create(MG_REPO, 'database respository') api.version_control(DB_URI, MG_REPO) else: api.version_control(DB_URI, MG_REPO, api.version(MG_REPO))
def create(): db.create_all() if is_sqlite(): if not os.path.exists(app.config['SQLALCHEMY_MIGRATE_REPO']): api.create(app.config['SQLALCHEMY_MIGRATE_REPO'], 'database repository') api.version_control(app.config['SQLALCHEMY_DATABASE_URI'], app.config['SQLALCHEMY_MIGRATE_REPO']) else: api.version_control(app.config['SQLALCHEMY_DATABASE_URI'], app.config['SQLALCHEMY_MIGRATE_REPO'], api.version(app.config['SQLALCHEMY_MIGRATE_REPO']))
def create_db(): db.session.rollback() db.drop_all() db.create_all() 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))
def CreateDB(): print ("Creating DB") db.create_all() print ("Putting DB under version control") 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))
def db_create(): # This creates the new database. db.create_all() # If no repo existed, the creation will prepare for the first migration. if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository') api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) print '\nDatabase creation completed\n' else: api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
def createdb(): sqlite3.connect(os.path.join(basedir, 'app.db')) db.create_all() if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository') api.version_control(DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) else: api.version_control(DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))
def db_create(): """Create database.""" from migrate.versioning import api from wpcgi.db import db db.create_all() 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))
def create_db(): try: db.create_all() 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)) except DatabaseAlreadyControlledError: print 'sqldb alread exists'
def init_db(): """Initializes the database and, if needed, the version control for it""" db.create_all() if 'SQLALCHEMY_MIGRATE_URI' in app.config: if not exists(app.config['SQLALCHEMY_MIGRATE_PATH']): api.create(app.config['SQLALCHEMY_MIGRATE_PATH'], 'database repository') api.version_control(app.config['SQLALCHEMY_MIGRATE_URI'], app.config['SQLALCHEMY_MIGRATE_PATH']) else: api.version_control(app.config['SQLALCHEMY_MIGRATE_URI'], app.config['SQLALCHEMY_MIGRATE_PATH'], api.version(app.config['SQLALCHEMY_MIGRATE_PATH']))
def main(): database.create_all() # If repo already exists if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository') api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) # If create from scratch else: api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))
def setup_module(): if os.path.exists(TEST_DB): os.remove(TEST_DB) global db_persistence DB_URL = "sqlite:///"+TEST_DB if os.path.exists(DB_URL): shutil.rmtree(DB_URL) create(DB_URL, "./mors_repo") version_control(DB_URL, "./mors_repo") upgrade(DB_URL,"./mors_repo") db_persistence = DbPersistence(DB_URL) return db_persistence
def createall(): """Creates database tables """ db.create_all() SQLALCHEMY_DATABASE_URI = current_app.config['SQLALCHEMY_DATABASE_URI'] SQLALCHEMY_MIGRATE_REPO = current_app.config['SQLALCHEMY_MIGRATE_REPO'] 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))
def createDB(drop_first=False): ''' Create the database ''' if drop_first: db.drop_all() db.create_all() repo = app.config['SQLALCHEMY_MIGRATE_REPO'] uri = app.config['SQLALCHEMY_DATABASE_URI'] if not os.path.exists(repo): api.create(repo, 'database repository') api.version_control(uri, repo) else: api.version_control(uri, repo, api.version(repo))
def db_sync(self): print "DataBase Sync" from migrate.versioning import api from fpost.config import SQLALCHEMY_DATABASE_URI from fpost.config import SQLALCHEMY_MIGRATE_REPO from fpost.app import db import os.path db.create_all() 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))
def init_db(): # import all modules here that might define models so that # they will be registered properly on the metadata. Otherwise # you will have to import them first before calling init_db() Base.metadata.create_all(bind=engine) 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)) newdb()
def db_create(): ''' Create the DB from scratch on specified server ''' from migrate.versioning import api from app import db import os.path db.create_all() if not os.path.exists(app.config['SQLALCHEMY_MIGRATE_REPO']): api.create(app.config['SQLALCHEMY_MIGRATE_REPO'], 'database repository') api.version_control(app.config['SQLALCHEMY_DATABASE_URI'], app.config['SQLALCHEMY_MIGRATE_REPO']) else: api.version_control(app.config['SQLALCHEMY_DATABASE_URI'], app.config['SQLALCHEMY_MIGRATE_REPO'], api.version(app.config['SQLALCHEMY_MIGRATE_REPO']))
def test_script(self): repo = self.tmp_repos() api.create(repo, 'temp') api.script('first version', repo)
from migrate.versioning import api from config import Config from app import db import os.path db.create_all() if not os.path.exists(Config.SQLALCHEMY_DATABASE_URI): api.create(Config.SQLALCHEMY_DATABASE_URI, 'database repository') api.version_control(Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_DATABASE_URI) else: api.version_control(Config.SQLALCHEMY_DATABASE_URI, Config.SQLALCHEMY_DATABASE_URI, api.version(Config.SQLALCHEMY_DATABASE_URI))
def test_script_sql(self): repo = self.tmp_repos() api.create(repo, 'temp') api.script_sql('postgres', 'desc', repo)
#!flask/bin/python from migrate.versioning import api from config import SQLALCHEMY_DATABASE_URI from config import SQLALCHEMY_BINDS from config import SQLALCHEMY_MIGRATE_REPO from config import SQLALCHEMY_BINDS_MIGRATE_REPO from app import db import os.path db.create_all() 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)) for name, uri in SQLALCHEMY_BINDS.items(): if not os.path.exists(SQLALCHEMY_BINDS_MIGRATE_REPO[name]): api.create(SQLALCHEMY_BINDS_MIGRATE_REPO[name], 'database repository') api.version_control(uri, SQLALCHEMY_BINDS_MIGRATE_REPO[name]) else: api.version_control(uri, SQLALCHEMY_BINDS_MIGRATE_REPO[name], api.version(SQLALCHEMY_BINDS_MIGRATE_REPO[name]))
#!venv/bin/python3 from migrate.versioning import api from config import SQLALCHEMY_DATABASE_URI from config import SQLALCHEMY_MIGRATE_REPO from FlaskMedia import db import os.path db.create_all() if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): api.create(SQLALCHEMY_MIGRATE_REPO, 'db repo') api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) else: api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))
def test_version(self): repo = self.tmp_repos() api.create(repo, 'temp') api.version(repo)
#!flask/bin/python from migrate.versioning import api import sys import pathlib p =pathlib.Path(__file__).parent.parent.absolute() sys.path.append(str(p)) from app import db import os.path SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(str(p), 'api.db') SQLALCHEMY_MIGRATE_REPO = os.path.join(str(p), 'db_repository') db.create_all() 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))
def commit_version_control(db_commit): if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository') api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) elif db_commit is not None: api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))
#!/usr/bin/env python # -*- coding: utf-8 -*- 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() if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): api.create(SQLALCHEMY_MIGRATE_REPO, "db") api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) else: api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))
#!haoliVPNEnv/bin/python from migrate.versioning import api from application import db,create_app import os import subprocess import sys from firstApp.models import Role app=create_app() app.app_context().push() db.create_all() Role.insert_role() if not os.path.exists(app.config['SQLALCHEMY_MIGRATE_REPO']): api.create(app.config['SQLALCHEMY_MIGRATE_REPO'], 'database repository') api.version_control(app.config['SQLALCHEMY_DATABASE_URI'], app.config['SQLALCHEMY_MIGRATE_REPO']) else: api.version_control(app.config['SQLALCHEMY_DATABASE_URI'], app.config['SQLALCHEMY_MIGRATE_REPO'], api.version(app.config['SQLALCHEMY_MIGRATE_REPO'])) # print DATABASE_URL # # try: # output = subprocess.check_output('chown nobody:nobody ' + DATABASE_URL, stderr=subprocess.STDOUT) # returncode = 0 # except subprocess.CalledProcessError as e: # stderr = "command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output) # returncode = -1 # except Exception, e: # stderr = str(e) # returncode = -1
import imp from migrate.versioning import api from app import db from config import SQLALCHEMY_MIGRATE_REPO from config import SQLALCHEMY_DATABASE_URI v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) migration = SQLALCHEMY_MIGRATE_REPO + (r'\versions\%03d_migration.py' % (v + 1)) tmp_module = imp.new_module('old_model') old_model = api.create(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('Currnt database version: ' + str(v))
#!flask/bin/python import os.path import sys sys.path.append('../') from app import app, db from migrate.versioning import api db.init_app(app) with app.test_request_context(): db.create_all() uri = app.config['SQLALCHEMY_DATABASE_URI'] repo = app.config['SQLALCHEMY_MIGRATE_REPO'] if not os.path.exists(repo): api.create(repo, 'database repository') api.version_control(uri, repo) else: api.version_control(uri, repo, api.version.Collection.version(repo))
def _setup(self, url): super(TestSchemaAPI, self)._setup(url) self.repo = self.tmp_repos() api.create(self.repo, 'temp') self.schema = api.version_control(url, self.repo)
def test_version_control(self): repo = self.tmp_repos() api.create(repo, 'temp') api.version_control('sqlite:///', repo) api.version_control('sqlite:///', unicode(repo))
from migrate.versioning import api from settings import SQLALCHEMY_DB_URI, SQLALCHEMY_MIGRATE_REPO from stats import db, Base import os.path print SQLALCHEMY_DB_URI Base.metadata.create_all(db) if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): api.create(SQLALCHEMY_MIGRATE_REPO, 'migration repository') api.version_control(SQLALCHEMY_DB_URI, SQLALCHEMY_MIGRATE_REPO) else: api.version_control(SQLALCHEMY_DB_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))
import os from config import config from flaskprj.models import db from migrate.versioning import api from flaskprj import create_app from flaskprj.models import Role from sqlite3 import OperationalError app = create_app() app.app_context().push() db.create_all() try: app = create_app() app.app_context().push() Role.insert_roles() except OperationalError: pass sqluri = config[os.getenv('FLASK_CONFIG') or 'default'].SQLALCHEMY_DATABASE_URI sqlmr = config[os.getenv('FLASK_CONFIG') or 'default'].SQLALCHEMY_MIGRATE_REPO if not os.path.exists(sqlmr): api.create(sqlmr, 'database_repository') api.version_control(sqluri, sqlmr) else: api.version_control(sqluri, sqlmr, api.version(sqlmr))
def test_create(self): tmprepo = self.tmp_repos() api.create(tmprepo, 'temp') # repository already exists self.assertRaises(KnownError, api.create, tmprepo, 'temp')