def staging_env(create=True, template="generic", sourceless=False): from alembic import command, script cfg = _testing_config() if create: path = os.path.join(_get_staging_directory(), "scripts") if os.path.exists(path): shutil.rmtree(path) command.init(cfg, path, template=template) if sourceless: try: # do an import so that a .pyc/.pyo is generated. util.load_python_file(path, "env.py") except AttributeError: # we don't have the migration context set up yet # so running the .env py throws this exception. # theoretically we could be using py_compiler here to # generate .pyc/.pyo without importing but not really # worth it. pass assert sourceless in ( "pep3147_envonly", "simple", "pep3147_everything", ), sourceless make_sourceless( os.path.join(path, "env.py"), "pep3147" if "pep3147" in sourceless else "simple", ) sc = script.ScriptDirectory.from_config(cfg) return sc
def test_init_file_doesnt_exist(self): def access_(path, mode): if "generic" in path: return True else: return False template_dir = self.cfg.get_template_directory() with mock.patch( "alembic.command.os.access", side_effect=access_ ) as access, mock.patch( "alembic.command.os.makedirs" ) as makedirs, mock.patch( "alembic.command.ScriptDirectory" ): command.init(self.cfg, directory="foobar") eq_( access.mock_calls, [ mock.call("foobar", os.F_OK), mock.call(os.path.join(template_dir, "generic"), os.F_OK), mock.call("foobar", os.F_OK), mock.call( os.path.abspath("./scratch/test_alembic.ini"), os.F_OK ), ], ) eq_( makedirs.mock_calls, [mock.call("foobar"), mock.call("foobar/versions")], )
def init(directory): "Generates a new migration" config = Config() config.set_main_option('script_location', directory) config.config_file_name = 'alembic.ini' command.init(config, directory, 'flask') os.rename('alembic.ini', os.path.join(directory, 'alembic.ini'))
def staging_env(create=True, template="generic", sourceless=False): from alembic import command, script cfg = _testing_config() if create: path = os.path.join(_get_staging_directory(), "scripts") assert not os.path.exists(path), ( "staging directory %s already exists; poor cleanup?" % path) command.init(cfg, path, template=template) if sourceless: try: # do an import so that a .pyc/.pyo is generated. util.load_python_file(path, "env.py") except AttributeError: # we don't have the migration context set up yet # so running the .env py throws this exception. # theoretically we could be using py_compiler here to # generate .pyc/.pyo without importing but not really # worth it. pass assert sourceless in ( "pep3147_envonly", "simple", "pep3147_everything", ), sourceless make_sourceless( os.path.join(path, "env.py"), "pep3147" if "pep3147" in sourceless else "simple", ) sc = script.ScriptDirectory.from_config(cfg) return sc
def initializeDatabase(cls, databaseUrl): migrationRootPath = PyGlassModelUtils.getMigrationPathFromDatabaseUrl(databaseUrl, root=True) migrationPath = PyGlassModelUtils.getMigrationPathFromDatabaseUrl(databaseUrl) configPath = os.path.join(migrationPath, 'alembic.ini') workingPath = os.curdir if not os.path.exists(migrationRootPath): os.makedirs(migrationRootPath) if os.path.exists(migrationPath + 'alembic.ini'): return False os.chdir(migrationRootPath) config = alembicConfig.Config() config.config_file_name = configPath alembicCmd.init( config=config, directory=migrationPath ) # Refresh config with proper settings cp = ConfigParser.ConfigParser() cp.read(configPath) cp.set('alembic', 'sqlalchemy.url', PyGlassModelUtils.getEngineUrl(databaseUrl)) f = open(configPath, 'w+') cp.write(f) f.close() os.chdir(workingPath) return True
def init(self): """Initializes Alembic migrations for the project. """ config = self.alembic_config(with_ini=False) config.config_file_name = self.alembic_config_file alembic_command.init(config, config.get_main_option('script_location'), 'watson') return True
def test_init_file_exists_and_is_empty(self): def access_(path, mode): if "generic" in path or path == "foobar": return True else: return False def listdir_(path): if path == "foobar": return [] else: return ["file1", "file2", "alembic.ini.mako"] with mock.patch( "alembic.command.os.access", side_effect=access_ ), mock.patch("alembic.command.os.makedirs") as makedirs, mock.patch( "alembic.command.os.listdir", side_effect=listdir_ ), mock.patch( "alembic.command.ScriptDirectory" ): command.init(self.cfg, directory="foobar") eq_( makedirs.mock_calls, [mock.call(os.path.normpath("foobar/versions"))], )
def init(directory=None): "Generates a new migration" if directory is None: directory = current_app.extensions["migrate"].directory config = Config() config.set_main_option("script_location", directory) config.config_file_name = os.path.join(directory, "alembic.ini") command.init(config, directory, "flask")
def init(directory, ignore, db_url): """ Init the db tools """ alembic_cfg = Config('migrations/alembic.ini') script_location = os.path.join(current_work_dir, 'migrations') command.init(alembic_cfg, script_location) _update_alembic_config_file(directory, ignore, db_url)
def init(directory=None): """Generates a new migration""" if directory is None: directory = current_app.extensions['migrate'].directory config = Config() config.set_main_option('script_location', directory) config.config_file_name = os.path.join(directory, 'alembic.ini') command.init(config, directory, 'flask')
def init(app, directory: 'location of scripts directory' = None): """Initialize a new scripts directory.""" directory = directory or app.settings['DATABASE_MIGRATIONS_DIRECTORY'] config = Config() config.set_main_option('script_location', directory) config.config_file_name = os.path.join(directory, 'alembic.ini') alembic.init(config, directory=directory, template='henson')
def init(context, directory='migrations', multidb=False): """Generates a new migration""" config = Config() config.set_main_option('script_location', directory) config.config_file_name = os.path.join(directory, 'alembic.ini') # pylint: disable=W0201 if multidb: command.init(config, directory, 'flask-multidb') else: command.init(config, directory, 'flask')
def init(context, directory='migrations', multidb=False): """Generates a new migration""" config = Config() config.set_main_option('script_location', directory) config.config_file_name = os.path.join(directory, 'alembic.ini') if multidb: command.init(config, directory, 'flask-multidb') else: command.init(config, directory, 'flask')
def init(directory, multidb): """Generates a new migration""" config = Config() config.set_main_option('script_location', directory) config.config_file_name = os.path.join(directory, 'alembic.ini') if multidb: command.init(config, directory, 'flask-multidb') else: command.init(config, directory, 'flask')
def init(context, directory="migrations", multidb=False): """Generates a new migration""" config = Config() config.set_main_option("script_location", directory) config.config_file_name = os.path.join(directory, "alembic.ini") if multidb: command.init(config, directory, "flask-multidb") else: command.init(config, directory, "flask")
def init(directory=None): """Creates a new migration repository""" app = current_app() if directory is None: directory = app.extra['migrate'].directory config = Config() config.set_main_option('script_location', directory) config.config_file_name = os.path.join(directory, 'alembic.ini') config = app.extra['migrate'].migrate.call_configure_callbacks(config) command.init(config, directory, 'fastapi')
def staging_env(create=True, template="generic"): from alembic import command, script cfg = _testing_config() if create: path = os.path.join(staging_directory, 'scripts') if os.path.exists(path): shutil.rmtree(path) command.init(cfg, path) sc = script.ScriptDirectory.from_config(cfg) return sc
def init(directory=None, multidb=False): """Creates a new migration repository""" if directory is None: directory = current_app.extensions['migrate'].directory config = Config() config.set_main_option('script_location', directory) config.config_file_name = os.path.join(directory, 'alembic.ini') config = current_app.extensions['migrate']. \ migrate.call_configure_callbacks(config) command.init(config, directory, 'tornado')
def init_db(config): """Creates a new migration repository.""" directory = os.path.join('yui', 'models', 'migrations') c = Config() c.set_main_option('script_location', directory) c.set_main_option('sqlalchemy.url', config.DATABASE_URL) c.config_file_name = os.path.join(directory, 'alembic.ini') command.init(c, directory, 'chatterbox')
def init(context, directory='migrations', multidb=False): """Generates a new migration""" from app import create_app with create_app().app_context(): config = Config() config.set_main_option('script_location', directory) config.config_file_name = os.path.join(directory, 'alembic.ini') if multidb: command.init(config, directory, 'flask-multidb') else: command.init(config, directory, 'flask')
def init(directory=None, multidb=False): """Creates a new migration repository""" if directory is None: directory = current_app.extensions["migrate"].directory config = Config() config.set_main_option("script_location", directory) config.config_file_name = os.path.join(directory, "alembic.ini") config = current_app.extensions["migrate"].migrate.call_configure_callbacks(config) if multidb: command.init(config, directory, "flask-multidb") else: command.init(config, directory, "flask")
def init(directory=None, multidb=False): """Generates a new migration""" if directory is None: directory = current_app.extensions['migrate'].directory config = Config() config.set_main_option('script_location', directory) config.config_file_name = os.path.join(directory, 'alembic.ini') config = current_app.extensions['migrate'].\ migrate.call_configure_callbacks(config) if multidb: command.init(config, directory, 'flask-multidb') else: command.init(config, directory, 'flask')
def init(directory=None, template=None): """Creates a new migration repository""" if directory is None: directory = migrate_manager.migrate_config.directory config = Config() config.set_main_option('script_location', directory) config.config_file_name = os.path.join(directory, 'alembic.ini') config = migrate_manager.migrate_config.migrate.call_configure_callbacks( config) if not os.path.exists( os.path.join(config.get_template_directory(), template)): raise ValueError("Template does not exist") command.init(config, directory, template)
def init(context, directory="migrations", multidb=False): """初始化迁移脚本""" if directory is None: directory = current_app.extensions["migrate"].directory config = Config() config.set_main_option("script_location", directory) config.config_file_name = os.path.join(directory, "alembic.ini") print(config.get_template_directory()) config = current_app.extensions[ "migrate"].migrate.call_configure_callbacks(config) if multidb: command.init(config, directory, "flask-multidb") else: command.init(config, directory, "flask")
def handle(self, **options): options['secret_key'] = token_hex(60) options['template'] = 'project_template' base_dir, name = super(Command, self).handle(**options) # Setup Alembic Migrations alembic_dir = os.path.join(base_dir, name, 'migrations') cfg = Config(os.path.join(alembic_dir, 'alembic.ini')) command.init(config=cfg, directory=alembic_dir) with open(os.path.join(alembic_dir, 'env.py'), 'r') as f: env_py = f.read() env_py = env_py.replace('target_metadata = None', METADATA_CODE) env_py = env_py.replace('config = context.config', SQL_CONNECT_OVERRIDE) with open(os.path.join(alembic_dir, 'env.py'), 'w') as f: f.write(env_py)
def init(directory): click.echo(f"Init database Migrate File at {directory}") path = os.path.abspath(directory) if os.path.exists(path): # if os.path.getsize(path) > 0: # click.echo(f"Path {path} already exists and not empty") # return if not os.path.isdir(path): click.echo(f"Path {path} is not a folder") return os.makedirs(path, exist_ok=True) conf = config.Config() conf.set_main_option('script_location', path) conf.config_file_name = os.path.join(directory, 'alembic.ini') command.init(conf, directory)
def initialize(directory=None, multidb=False): from alembic import command from alembic.config import Config as AlembicConfig class Config(AlembicConfig): def get_template_directory(self): package_dir = os.path.abspath(os.path.dirname(__file__)) return os.path.join(package_dir, "templates") """Creates a new migration repository""" config = Config() config.set_main_option("script_location", directory) config.config_file_name = os.path.join(directory, "alembic.ini") command.init(config, directory, "singledb")
def init(self, multidb: bool = False) -> t.Any: config: AlembicConfig = self.configuration.to_alembic_config() return command.init(config, directory=self.configuration.script_location, template='multidb' if multidb else 'default', package=False)
def initialize(self, project_name): """ ____This function is serve as initialize installer, and run through interface command line____ @param project_name: string, name for your project either name or just . for current directory """ if project_name != '.': createDirectory(os.path.join(os.getcwd(), project_name)) self.project = os.path.join(os.getcwd(), project_name) init(ini_configuration(self.project), self.project) self._buildDirectory() self._fileToManage() configReset(self.project)
def run_alembic_cmd(self, opt): ''' Logic for running different Alembic commands. ''' from alembic import command as alembic_cmd config = self.get_config() # command consume any number of parameters but first is command name cmd = opt.command.pop(0) # init command needs to point to lux template, not alembic default if cmd == 'init': dirname = config.get_main_option('script_location') # line 63 will be executed in: # https://github.com/zzzeek/alembic/blob/master/alembic/command.py # since we do not use any *.ini file, we simply silence error # about referenced before assignment as it have no negative impact. try: alembic_cmd.init(config, dirname, template='lux') except UnboundLocalError: # pragma nocover pass # merge required two revision name elif cmd == 'merge': if len(opt.command) != 2: raise CommandError('Command %s required revisions id.' % cmd) alembic_cmd.merge(config, opt.command, message=opt.msg, branch_label=opt.branch) elif cmd == 'revision': alembic_cmd.revision(config, message=opt.msg, branch_label=opt.branch) # auto command is a shortcut for `revision --autogenerate` elif cmd == 'auto': alembic_cmd.revision(config, autogenerate=True, message=opt.msg, branch_label=opt.branch) # this commands required revision name, but do not take any message or # branch labels elif cmd in ('show', 'stamp', 'upgrade', 'downgrade'): if len(opt.command) != 1: raise CommandError('Command %s required revision id' % cmd) getattr(alembic_cmd, cmd)(config, *opt.command) else: # execute commands without any additional params getattr(alembic_cmd, cmd)(config)
def init(directory=None, multidb=False, template=None, package=False): """Creates a new migration repository""" if directory is None: directory = current_app.extensions['migrate'].directory template_directory = None if template is not None and ('/' in template or '\\' in template): template_directory, template = os.path.split(template) config = Config(template_directory=template_directory) config.set_main_option('script_location', directory) config.config_file_name = os.path.join(directory, 'alembic.ini') config = current_app.extensions['migrate'].\ migrate.call_configure_callbacks(config) if multidb and template is None: template = 'flask-multidb' elif template is None: template = 'flask' command.init(config, directory, template=template, package=package)
def test_init_file_doesnt_exist(self): def access_(path, mode): if "generic" in path: return True else: return False with mock.patch( "alembic.command.os.access", side_effect=access_), mock.patch( "alembic.command.os.makedirs") as makedirs, mock.patch( "alembic.command.ScriptDirectory"): command.init(self.cfg, directory="foobar") eq_( makedirs.mock_calls, [mock.call("foobar"), mock.call("foobar/versions")], )
def init(self, script_location=None): try: import geoalchemy2 except ImportError: template = 'generic' else: template = 'geoalchemy' # PS: This config file is just a workaround, it is not needed. We # just use it to initialize the config. Changes done to this file # will not be reflected. script_location = self.config.get_main_option('script_location') config_file = os.path.join(script_location, 'alembic.ini') self.config.config_file_name = config_file directory = script_location or self.config.get_main_option('script_location') command.init(self.config, directory=directory, template=template)
def init(self, script_location=None): try: import geoalchemy2 except ImportError: template = 'generic' else: template = 'geoalchemy' # PS: This config file is just a workaround, it is not needed. We # just use it to initialize the config. Changes done to this file # will not be reflected. script_location = self.config.get_main_option('script_location') config_file = os.path.join(script_location, 'alembic.ini') self.config.config_file_name = config_file directory = script_location or self.config.get_main_option( 'script_location') command.init(self.config, directory=directory, template=template)
def get_alembic_conf(): alembic_cfg = Config() alembic_cfg.set_main_option("script_location", "migrations") alembic_cfg.set_main_option("sqlalchemy.url", DATABASE_URL) alembic_cfg.config_file_name = os.path.join("migrations", 'alembic.ini') if os.path.isdir('migrations') is False: click.echo(click.style("Initiating alembic...", fg='bright_blue')) alembic.init(alembic_cfg, 'migrations') with open('migrations/env.py', 'r+') as f: content = f.read() content = content.replace( 'target_metadata = None', f'from {MAIN_MODULE_NAME} import db\ntarget_metadata = db.metadata' ) f.seek(0) f.write(content) return alembic_cfg
def test_init_file_exists_and_is_empty(self): def access_(path, mode): if "generic" in path or path == "foobar": return True else: return False def listdir_(path): if path == "foobar": return [] else: return ["file1", "file2", "alembic.ini.mako"] template_dir = self.cfg.get_template_directory() with mock.patch( "alembic.command.os.access", side_effect=access_ ) as access, mock.patch( "alembic.command.os.makedirs" ) as makedirs, mock.patch( "alembic.command.os.listdir", side_effect=listdir_ ) as listdir, mock.patch( "alembic.command.ScriptDirectory" ): command.init(self.cfg, directory="foobar") eq_( access.mock_calls, [ mock.call("foobar", os.F_OK), mock.call(os.path.join(template_dir, "generic"), os.F_OK), mock.call("foobar", os.F_OK), mock.call( os.path.abspath("./scratch/test_alembic.ini"), os.F_OK ), ], ) eq_( listdir.mock_calls, [ mock.call("foobar"), mock.call(os.path.join(template_dir, "generic")), ], ) eq_(makedirs.mock_calls, [mock.call("foobar/versions")])
def run_alembic_cmd(self, opt): ''' Logic for running different Alembic commands. ''' from alembic import command as alembic_cmd config = self.get_config() # command consume any number of parameters but first is command name cmd = opt.command.pop(0) # init command needs to point to lux template, not alembic default if cmd == 'init': dirname = config.get_main_option('script_location') # line 63 will be executed in: # https://github.com/zzzeek/alembic/blob/master/alembic/command.py # since we do not use any *.ini file, we simply silence error # about referenced before assignment as it have no negative impact. try: alembic_cmd.init(config, dirname, template='lux') except UnboundLocalError: # pragma nocover pass # merge required two revision name elif cmd == 'merge': if len(opt.command) != 2: raise CommandError('Command: %s required revisions id.' % cmd) alembic_cmd.merge(config, *opt.command, message=opt.msg, branch_label=opt.branch) elif cmd == 'revision': alembic_cmd.revision(config, message=opt.msg, branch_label=opt.branch) # auto command is a shortcut for `revision --autogenerate` elif cmd == 'auto': alembic_cmd.revision(config, autogenerate=True, message=opt.msg, branch_label=opt.branch) # this commands required revision name, but do not take any message or # branch labels elif cmd in ('show', 'stamp', 'upgrade', 'downgrade'): if len(opt.command) != 1: raise CommandError('Command: %s required revision id' % cmd) getattr(alembic_cmd, cmd)(config, *opt.command) else: # execute commands without any additional params getattr(alembic_cmd, cmd)(config)
def init_db(): if path.exists(_migration_path): print(Fore.YELLOW + _migration_path + " has existed!") return else: from alembic import command from alembic.config import Config alembic_cfg = Config(file_="alembic.ini", ) command.init(alembic_cfg, _set_unix_path(_migration_path), template="generic") # 修改 env.py 文件 patch_env() print("\n" + Fore.YELLOW + "请修改 alembic.ini 中 sqlalchemy.url 的值. \n" "请修改 .alembic/search_models.py 中 search_rules 的路径. \n")
def run(self, args): # let's hijack init and list_templates # we want to provide our own config object # in order to provide a custom get_template_directory function if len(args) and args[0] in ['list_templates', 'init']: config = FlaskAlembicConfig("alembic.ini") if args[0] == 'list_templates': return list_templates(config) else: try: return init(config, 'alembic', template='flask') except util.CommandError, e: util.err(str(e))
def initializeDatabase(cls, databaseUrl, resourcesPath =None, localResourcesPath =None): migrationRootPath = PyGlassModelUtils.getMigrationPathFromDatabaseUrl( databaseUrl=databaseUrl, root=True, resourcesPath=resourcesPath) migrationPath = PyGlassModelUtils.getMigrationPathFromDatabaseUrl( databaseUrl=databaseUrl, resourcesPath=resourcesPath) configPath = FileUtils.makeFilePath(migrationPath, 'alembic.ini') if not os.path.exists(migrationRootPath): os.makedirs(migrationRootPath) if os.path.exists(migrationPath + 'alembic.ini'): return False os.chdir(migrationRootPath) config = alembicConfig.Config() config.config_file_name = configPath alembicCmd.init(config=config, directory=migrationPath) # Refresh config with proper settings cp = configparser.ConfigParser() cp.read(configPath) cp.set('alembic', 'sqlalchemy.url', PyGlassModelUtils.getEngineUrl( databaseUrl=databaseUrl, localResourcesPath=localResourcesPath)) f = open(configPath, 'w+') cp.write(f) f.close() return True
def init(self): dirname = self.cfg.get_main_option('script_location') alembic_cmd.init(self.cfg, dirname, template='lux')
def init(extension_module_name, **kwargs): pkg_env = ExtensionPackageEnv(extension_module_name) command.init(pkg_env.config, pkg_env.script_dir, **kwargs)
def init(): """ Initialize a new database migration. """ config = _get_config() return command.init(config, 'migrations', 'alembic')
def init(self, directory, **kwargs): """Generates a new migration""" config = Config() config.set_main_option('script_location', directory) config.config_file_name = os.path.join(directory, 'alembic.ini') command.init(config, directory, 'relengapi')
def init(): "Generates a new migration" config = _get_config() command.init(config, 'migrations', 'alembic')