def _migrate(url, repository, version, upgrade, err, **opts): engine = construct_engine(url, **opts) schema = ControlledSchema(engine, repository) version = _migrate_version(schema, version, upgrade, err) changeset = schema.changeset(version) for ver, change in changeset: nextver = ver + changeset.step print '%s -> %s... ' % (ver, nextver) if opts.get('preview_sql'): if isinstance(change, PythonScript): print change.preview_sql(url, changeset.step, **opts) elif isinstance(change, SqlScript): print change.source() elif opts.get('preview_py'): source_ver = max(ver, nextver) module = schema.repository.version(source_ver).script().module funcname = upgrade and "upgrade" or "downgrade" func = getattr(module, funcname) if isinstance(change, PythonScript): print inspect.getsource(func) else: raise UsageError("Python source can be only displayed" " for python migration files") else: schema.runchange(ver, change, changeset.step) print 'done'
def __init__(self, url, schema=None, reflectMetadata=True, row_type=dict): kw = {} if url.startswith('postgres'): kw['poolclass'] = NullPool self.lock = threading.RLock() self.local = threading.local() if '?' in url: url, query = url.split('?', 1) query = parse_qs(query) if schema is None: # le pop schema_qs = query.pop('schema', query.pop('searchpath', [])) if len(schema_qs): schema = schema_qs.pop() if len(query): url = url + '?' + urlencode(query, doseq=True) self.schema = schema engine = create_engine(url, **kw) self.url = url self.engine = construct_engine(engine) self.metadata = MetaData(schema=schema) self.metadata.bind = self.engine if reflectMetadata: self.metadata.reflect(self.engine) self.row_type = row_type self._tables = {}
def load_environment(global_conf, app_conf): """\ Configure the Pylons environment via the ``pylons.config`` object """ # Pylons paths root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) paths = dict(root=root, controllers=os.path.join(root, 'controllers'), static_files=os.path.join(root, 'public'), templates=[os.path.join(root, 'templates')]) # Initialize config with the basic options config.init_app(global_conf, app_conf, package='openspending.ui', paths=paths) config['routes.map'] = routing.make_map() config['pylons.app_globals'] = app_globals.Globals() config['pylons.h'] = helpers # set log level in markdown markdown.logger.setLevel(logging.WARN) # SQLAlchemy engine = engine_from_config(config, 'openspending.db.') engine = construct_engine(engine) init_model(engine) # Configure Solr import openspending.lib.solr_util as solr solr.configure(config)
def load_environment(global_conf, app_conf): """\ Configure the Pylons environment via the ``pylons.config`` object """ # Pylons paths root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) paths = dict(root=root, controllers=os.path.join(root, 'controllers'), static_files=os.path.join(root, 'public'), templates=[os.path.join(root, 'templates')]) # Initialize config with the basic options config.init_app( global_conf, app_conf, package='openspending.ui', paths=paths) config['routes.map'] = routing.make_map() config['pylons.app_globals'] = app_globals.Globals() config['pylons.h'] = helpers # set log level in markdown markdown.logger.setLevel(logging.WARN) # SQLAlchemy engine = engine_from_config(config, 'openspending.db.') engine = construct_engine(engine) init_model(engine) # Configure Solr import openspending.lib.solr_util as solr solr.configure(config)
def drop_version_control(url, repository, **opts): """%prog drop_version_control URL REPOSITORY_PATH Removes version control from a database. """ engine = construct_engine(url, **opts) schema = ControlledSchema(engine, repository) schema.drop()
def compare_model_to_db(url, model, repository, **opts): """%prog compare_model_to_db URL MODEL REPOSITORY_PATH Compare the current model (assumed to be a module level variable of type sqlalchemy.MetaData) against the current database. NOTE: This is EXPERIMENTAL. """ # TODO: get rid of EXPERIMENTAL label engine = construct_engine(url, **opts) print ControlledSchema.compare_model_to_db(engine, model, repository)
def create_model(url, repository, **opts): """%prog create_model URL REPOSITORY_PATH Dump the current database as a Python model to stdout. NOTE: This is EXPERIMENTAL. """ # TODO: get rid of EXPERIMENTAL label engine = construct_engine(url, **opts) declarative = opts.get('declarative', False) print ControlledSchema.create_model(engine, repository, declarative)
def patched_with_engine(f, *a, **kw): url = a[0] engine = migrate_util.construct_engine(url, **kw) try: kw['engine'] = engine return f(*a, **kw) finally: if isinstance(engine, migrate_util.Engine) and engine is not url: migrate_util.log.debug('Disposing SQLAlchemy engine %s', engine) engine.dispose()
def connect(url): """ Create an engine for the given database URL. """ kw = {} if url.startswith('postgres'): kw['pool_size'] = 1 engine = create_engine(url, **kw) engine = construct_engine(engine) meta = MetaData() meta.bind = engine engine._metadata = meta return engine
def make_update_script_for_model(url, oldmodel, model, repository, **opts): """%prog make_update_script_for_model URL OLDMODEL MODEL REPOSITORY_PATH Create a script changing the old Python model to the new (current) Python model, sending to stdout. NOTE: This is EXPERIMENTAL. """ # TODO: get rid of EXPERIMENTAL label engine = construct_engine(url, **opts) print PythonScript.make_update_script_for_model(engine, oldmodel, model, repository, **opts)
def preview_sql(self, url, step, **args): """Mock engine to store all executable calls in a string \ and execute the step""" buf = StringIO() args['engine_arg_strategy'] = 'mock' args['engine_arg_executor'] = lambda s, p='': buf.write(s + p) engine = construct_engine(url, **args) self.run(engine, step) return buf.getvalue()
def make_update_script_for_model(url, oldmodel, model, repository, **opts): """%prog make_update_script_for_model URL OLDMODEL MODEL REPOSITORY_PATH Create a script changing the old Python model to the new (current) Python model, sending to stdout. NOTE: This is EXPERIMENTAL. """ # TODO: get rid of EXPERIMENTAL label engine = construct_engine(url, **opts) print PythonScript.make_update_script_for_model( engine, oldmodel, model, repository, **opts)
def update_db_from_model(url, model, repository, **opts): """%prog update_db_from_model URL MODEL REPOSITORY_PATH Modify the database to match the structure of the current Python model. This also sets the db_version number to the latest in the repository. NOTE: This is EXPERIMENTAL. """ # TODO: get rid of EXPERIMENTAL label engine = construct_engine(url, **opts) schema = ControlledSchema(engine, repository) schema.update_db_from_model(model)
def __init__(self, url): kw = {} if url.startswith('postgres'): kw['poolclass'] = NullPool engine = create_engine(url, **kw) self.lock = RLock() self.url = url self.engine = construct_engine(engine) self.metadata = MetaData() self.metadata.bind = self.engine self.metadata.reflect(self.engine) self._tables = {}
def db_version(url, repository, **opts): """%prog db_version URL REPOSITORY_PATH Show the current version of the repository with the given connection string, under version control of the specified repository. The url should be any valid SQLAlchemy connection string. """ engine = construct_engine(url, **opts) schema = ControlledSchema(engine, repository) return schema.version
def load_environment(global_conf, app_conf): """\ Configure the Pylons environment via the ``pylons.config`` object """ # Pylons paths root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) paths = dict(root=root, controllers=os.path.join(root, 'controllers'), static_files=os.path.join(root, 'public'), templates=[os.path.join(root, 'templates')]) # Initialize config with the basic options config.init_app(global_conf, app_conf, package='openspending.ui', paths=paths) config['routes.map'] = routing.make_map() config['pylons.app_globals'] = app_globals.Globals() config['pylons.h'] = helpers # set log level in markdown markdown.logger.setLevel(logging.WARN) # Establish celery loader: from openspending.command import celery # Translator (i18n) config['openspending.ui.translations'] = MultiDomainTranslator([config.get('lang', 'en')]) translator = Translator(config['openspending.ui.translations']) def template_loaded(template): translator.setup(template) template_paths = [paths['templates'][0]] extra_template_paths = config.get('extra_template_paths', '') if extra_template_paths: # must be first for them to override defaults template_paths = extra_template_paths.split(',') + template_paths # Create the Genshi TemplateLoader config['pylons.app_globals'].genshi_loader = TemplateLoader( search_path=template_paths, auto_reload=True, callback=template_loaded ) # SQLAlchemy engine = engine_from_config(config, 'openspending.db.') engine = construct_engine(engine) init_model(engine) # Configure Solr import openspending.lib.solr_util as solr solr.configure(config)
def setup_database(self): """ Configure the database based on the provided configuration file, but be sure to overwrite the url so that it will use sqlite in memory, irrespective of what the user has set in test.ini. Construct the sqlalchemy engine with versioning and initialise everything. """ config['openspending.db.url'] = 'sqlite:///:memory:' engine = engine_from_config(config, 'openspending.db.') engine = construct_engine(engine) init_model(engine)
def connect(url): """ Create an engine for the given database URL. """ kw = {} if url.startswith('postgres'): #kw['pool_size'] = 5 from sqlalchemy.pool import NullPool kw['poolclass'] = NullPool engine = create_engine(url, **kw) engine = construct_engine(engine) meta = MetaData() meta.bind = engine engine._metadata = meta return engine
def connect(url): """ Create an engine for the given database URL. """ kw = {} if url.startswith('postgres'): #kw['pool_size'] = 5 from sqlalchemy.pool import NullPool kw['poolclass'] = NullPool engine = create_engine(url, **kw) engine = construct_engine(engine) meta = MetaData() meta.bind = engine engine._metadata = meta engine._tables = dict() engine._indexes = dict() return engine
def setup_package(): ''' Create a new, not scoped global sqlalchemy session and rebind it to a new root transaction to which we can roll back. Otherwise :func:`adhocracy.model.init_model` will create as scoped session and invalidates the connection we need to begin a new root transaction. Return: The new root `connection` ''' from sqlalchemy import engine_from_config from migrate.versioning.util import construct_engine config['openspending.db.url'] = 'sqlite:///:memory:' engine = engine_from_config(config, 'openspending.db.') engine = construct_engine(engine) init_model(engine)
def load_environment(global_conf, app_conf): """Configure the Pylons environment via the ``pylons.config`` object """ # Pylons paths root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) paths = dict(root=root, controllers=os.path.join(root, 'controllers'), static_files=os.path.join(root, 'public'), templates=[os.path.join(root, 'templates')]) # Initialize config with the basic options config.init_app(global_conf, app_conf, package='openspending.etl.ui', paths=paths) config['routes.map'] = make_map() config['pylons.app_globals'] = app_globals.Globals() config['pylons.h'] = helpers # set log level in markdown markdown.logger.setLevel(logging.WARN) ## redo template setup to use genshi.search_path (so remove std template setup) template_paths = [paths['templates'][0]] # Translator (i18n) config['openspending.etl.ui.translations'] = MultiDomainTranslator([config.get('lang', 'en')]) translator = Translator(config['openspending.etl.ui.translations']) def template_loaded(template): translator.setup(template) config['pylons.app_globals'].genshi_loader = TemplateLoader( template_paths, auto_reload=True, callback=template_loaded) # SQLAlchemy engine = engine_from_config(config, 'openspending.db.') engine = construct_engine(engine) init_model(engine) # Configure ckan import openspending.etl.importer.ckan as ckan ckan.configure(config) # Configure Solr import openspending.lib.solr_util as solr solr.configure(config)
def __init__(self, url, search_path): kw = {} if url.startswith('postgres'): kw['poolclass'] = NullPool engine = create_engine(url, **kw) self.lock = RLock() self.url = url self.search_path = search_path self.engine = construct_engine(engine) self.metadata = MetaData() self.metadata.bind = self.engine if self.search_path and url.startswith('postgres'): for schema in self.search_path: self.metadata.reflect(self.engine, schema=schema) else: self.metadata.reflect(self.engine) self._tables = {}
def _migrate(self, repository, version, upgrade, **opts): engine = construct_engine(self.url, **opts) schema = api.ControlledSchema(engine, repository) version = self._migrate_version(schema, version, upgrade) yield "<h2>Migrating %s</h2>\n" % repository.id changeset = schema.changeset(version) if not changeset: yield "<p>This repository is already up to date.</p>\n" return for ver, change in changeset: nextver = ver + changeset.step doc = schema.repository.version(max(ver, nextver)).script().module.__doc__ yield "<h3>Version %s -> %s - %s</h3>\n" % (ver, nextver, doc) for message in schema.runchange(ver, change, changeset.step): yield message yield "\n<p>Done!</p>\n"
def _migrate(self, repository, version, upgrade, **opts): engine = construct_engine(self.url, **opts) schema = api.ControlledSchema(engine, repository) version = self._migrate_version(schema, version, upgrade) yield '<h2>Migrating %s</h2>\n' % repository.id changeset = schema.changeset(version) if not changeset: yield '<p>This repository is already up to date.</p>\n' return for ver, change in changeset: nextver = ver + changeset.step doc = schema.repository.version(max(ver, nextver)). \ script().module.__doc__ yield '<h3>Version %s -> %s - %s</h3>\n' % (ver, nextver, doc) for message in schema.runchange(ver, change, changeset.step): yield message yield '\n<p>Done!</p>\n'
def version_control(url, repository, version=None, **opts): """%prog version_control URL REPOSITORY_PATH [VERSION] Mark a database as under this repository's version control. Once a database is under version control, schema changes should only be done via change scripts in this repository. This creates the table version_table in the database. The url should be any valid SQLAlchemy connection string. By default, the database begins at version 0 and is assumed to be empty. If the database is not empty, you may specify a version at which to begin instead. No attempt is made to verify this version's correctness - the database schema is expected to be identical to what it would be if the database were created from scratch. """ engine = construct_engine(url, **opts) ControlledSchema.create(engine, repository, version)
def test(repository, url=None, **opts): """%prog test REPOSITORY_PATH URL [VERSION] Performs the upgrade and downgrade option on the given database. This is not a real test and may leave the database in a bad state. You should therefore better run the test on a copy of your database. """ engine = construct_engine(url, **opts) repos = Repository(repository) script = repos.version(None).script() # Upgrade print "Upgrading...", script.run(engine, 1) print "done" print "Downgrading...", script.run(engine, -1) print "done" print "Success"
def load_environment(global_conf, app_conf): """\ Configure the Pylons environment via the ``pylons.config`` object """ # Pylons paths root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) paths = dict(root=root, controllers=os.path.join(root, 'controllers'), static_files=os.path.join(root, 'public'), templates=[os.path.join(root, 'templates')]) # Initialize config with the basic options config.init_app( global_conf, app_conf, package='openspending.ui', paths=paths) config['routes.map'] = routing.make_map() config['pylons.app_globals'] = app_globals.Globals() config['pylons.h'] = helpers # set log level in markdown markdown.logger.setLevel(logging.WARN) # Establish celery loader # This must be done (even if it results in a pyflakes error) because # celery loading is done from openspending.command.celery.__init__.py # Fixing that loading is how you would fix the flakes error from openspending.command import celery # SQLAlchemy engine = engine_from_config(config, 'openspending.db.') engine = construct_engine(engine) init_model(engine) # Configure Solr import openspending.lib.solr_util as solr solr.configure(config)
def load_environment(global_conf, app_conf): """\ Configure the Pylons environment via the ``pylons.config`` object """ # Pylons paths root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) paths = dict(root=root, controllers=os.path.join(root, 'controllers'), static_files=os.path.join(root, 'public'), templates=[os.path.join(root, 'templates')]) # Initialize config with the basic options config.init_app(global_conf, app_conf, package='openspending.ui', paths=paths) config['routes.map'] = routing.make_map() config['pylons.app_globals'] = app_globals.Globals() config['pylons.h'] = helpers # set log level in markdown markdown.logger.setLevel(logging.WARN) # Establish celery loader # This must be done (even if it results in a pyflakes error) because # celery loading is done from openspending.command.celery.__init__.py # Fixing that loading is how you would fix the flakes error from openspending.command import celery # SQLAlchemy engine = engine_from_config(config, 'openspending.db.') engine = construct_engine(engine) init_model(engine) # Configure Solr import openspending.lib.solr_util as solr solr.configure(config)
def __init__(self, engine): self.engine = construct_engine(engine) self.meta = MetaData() self.meta.bind = self.engine
def update_pylons_db_from_model(url, model_str, commit=False): """update_pylons_db_from_model URL MODEL_STR COMMIT Modify the database to match the structure of the current Pylons model. NOTE: This is EXPERIMENTAL. """ # TODO: get rid of EXPERIMENTAL label global _debug_messages # engine = opts.pop('engine') # relies on @with_engine, and url being first argument engine = construct_engine(url) # direct approach if _debug_messages: print "engine= ", engine if _debug_messages: print "sqlalchemy-migrate-pylons : engine created" model_metadata = load_model(model_str) if _debug_messages: print "sqlalchemy-migrate-pylons : model metadata loaded from model_str" #schema.MetaData(engine).bind=model_metadata #print " Meta Internal: ", model.metadata, "\n" #print " Meta Migrate : ", sqlalchemy.MetaData(engine), "\n" #model.metadata=sqlalchemy.MetaData(engine) #model_metadata = declarative_base(metadata=sqlalchemy.MetaData(engine)) #model_metadata.DBsession.configure(bind=engine, autocommit=False) #model_metadata.engine = engine #model_metadata.reflect(bind=engine) #declarative_base(metadata=model_metadata) #sqlalchemy.MetaData(engine).bind = engine diff = schemadiff.getDiffOfModelAgainstDatabase(model_metadata, engine) print "\n====== sqlalchemy-migrate-pylons : Model vs. Database ======\n", diff, "\n" if commit: # genmodel.ModelGenerator(diff).applyModel() # print "\n Engine in Diffs : ", diff.conn.engine, "\n" #print " Meta Internal: ", model.session, "\n" #print " Meta Migrate : ", sqlalchemy.MetaData(diff.conn.engine), "\n" #meta = model.metadata #sqlalchemy.MetaData(diff.conn.engine).bind = engine #meta.DBsession.configure(bind=engine, autocommit=False) if hasattr(diff, 'tablesMissingInDatabase'): print "MISSING : ", diff.tablesMissingInDatabase print "MISSING : ", sqlalchemy.sql.util.sort_tables(diff.tablesMissingInDatabase) diff.tablesMissingInDatabase = sqlalchemy.sql.util.sort_tables(diff.tablesMissingInDatabase) print "====== sqlalchemy-migrate-pylons : Table Creation order : ======\n", diff, "\n" #sqlalchemy.MetaData(engine).bind = engine if hasattr(diff, 'tablesMissingInDatabase'): ## Oldrelease... newmodel = genmodel.ModelGenerator(diff, declarative=True) #newmodel.reflect(bind=engine) else: # This is a change for new release... newmodel = genmodel.ModelGenerator(diff, engine, declarative=True) if hasattr(diff, 'tablesMissingInDatabase'): ## Oldrelease... print "====== sqlalchemy-migrate-pylons : Different Model created ======\n" print " New Model (in Python): \n\n", newmodel.toUpgradeDowngradePython()[0], "\n" # Show the Declarations print "# Upgrade Code :\n", newmodel.toUpgradeDowngradePython()[1], "\n" # Show the Upgrade code newmodel.applyModel() print "====== sqlalchemy-migrate-pylons : Database Migrated ======\n" diff = schemadiff.getDiffOfModelAgainstDatabase(model_metadata, engine) print "\n====== sqlalchemy-migrate-pylons : Model vs. Database (after migration) ======\n", diff, "\n" if isinstance(engine, Engine): engine.dispose()
def mig_create_engine(*a, **kw): engine = fsqla.create_engine(*a, **kw) return construct_engine(engine)