def init(): """ Prepare directory with alembic.ini, mako-files and directory for migrations. Part of functional was copied from original Alembic Init but changed some things with config """ if os.access(config.alembic_dir, os.F_OK): raise util.CommandError("Directory {} already exists".format( config.alembic_dir)) template_dir = os.path.join(config.template_path + config.template_name) if not os.access(template_dir, os.F_OK): raise util.CommandError("No such template {}".format(template_dir)) util.status( "Creating directory {}".format(os.path.abspath( config.alembic_dir)), os.makedirs, config.alembic_dir) versions = os.path.join(config.alembic_dir, 'versions') util.status("Creating directory %s" % os.path.abspath(versions), os.makedirs, versions) script = ScriptDirectory(config.alembic_dir) dirs = os.listdir(template_dir) dirs += [ 'versions/create_table_alembic_version_history.py', ] for file_ in dirs: file_path = os.path.join(template_dir, file_) if file_ == 'alembic.ini.mako': config_file = os.path.abspath('alembic.ini') if os.access(config_file, os.F_OK): util.msg( "File {} already exists, skipping".format(config_file)) else: script._generate_template( template_dir + '/alembic.ini.mako', os.path.join(config.alembic_dir, 'alembic.ini'), script_location=config.alembic_dir) elif os.path.isfile(file_path): output_file = os.path.join(config.alembic_dir, file_) script._copy_file(file_path, output_file) util.msg("Please edit configuration/connection/logging " "settings in {} before proceeding.".format( os.path.join(config.alembic_dir, 'alembic.ini')))
def handle(self, *args, **options): db_id = options.get("metadata", None) metadata_module_name = get_module_from_db_name(db_id) template = options.get("template") migrations_conf_dict = get_migrations_config(db_id) if migrations_conf_dict is None: raise CommandError("Migrations configuration not found.") directory = migrations_conf_dict.get("script_location", None) if directory is None: raise CommandError("Invalid Migration Scripts Location") if os.access(directory, os.F_OK): raise CommandError("Directory %s already exists" % directory) templates_directory = migrations_conf_dict.get("templates_directory", default_templates_directory) template_dir = os.path.join(templates_directory, template) if not os.access(template_dir, os.F_OK): raise CommandError("No such template %r" % template) self.stdout.write("Creating directory %s" % os.path.abspath(directory)) os.makedirs(directory) versions = os.path.join(directory, 'versions') self.stdout.write("Creating directory %s" % os.path.abspath(versions)) os.makedirs(versions) script = ScriptDirectory(directory) for file_ in os.listdir(template_dir): file_path = os.path.join(template_dir, file_) if os.path.isfile(file_path): output_file = os.path.join(directory, file_) script._copy_file( file_path, output_file )
def init(config, directory, template='generic'): """Initialize a new scripts directory.""" if os.access(directory, os.F_OK): raise util.CommandError("Directory %s already exists" % directory) template_dir = os.path.join(config.get_template_directory(), template) if not os.access(template_dir, os.F_OK): raise util.CommandError("No such template %r" % template) util.status("Creating directory %s" % os.path.abspath(directory), os.makedirs, directory) versions = os.path.join(directory, 'versions') util.status("Creating directory %s" % os.path.abspath(versions), os.makedirs, versions) script = ScriptDirectory(directory) for file_ in os.listdir(template_dir): file_path = os.path.join(template_dir, file_) if file_ == 'alembic.ini.mako': config_file = os.path.abspath(config.config_file_name) if os.access(config_file, os.F_OK): util.msg("File %s already exists, skipping" % config_file) else: script._generate_template( file_path, config_file, script_location=directory ) elif os.path.isfile(file_path): output_file = os.path.join(directory, file_) script._copy_file( file_path, output_file ) util.msg("Please edit configuration/connection/logging "\ "settings in %r before proceeding." % config_file)
def init(self, directory, template="mampoer_generic", package=False): """Initialize a new scripts directory. :param template: string name of the migration environment template to use. :param package: when True, write ``__init__.py`` files into the environment location as well as the versions/ location. .. versionadded:: 1.2 """ if os.access(directory, os.F_OK) and os.listdir(directory): raise util.CommandError( "Directory %s already exists and is not empty" % directory) template_dir = os.path.join(self.get_template_directory(), template) if not os.access(template_dir, os.F_OK): raise util.CommandError("No such template %r" % template) if not os.access(directory, os.F_OK): util.status( "Creating directory %s" % os.path.abspath(directory), os.makedirs, directory, ) versions = os.path.join(directory, "versions") util.status( "Creating directory %s" % os.path.abspath(versions), os.makedirs, versions, ) script = ScriptDirectory(directory) for file_ in os.listdir(template_dir): file_path = os.path.join(template_dir, file_) if file_ == "mampoer.ini.mako": config_file = os.path.abspath(self.config.config_file_name) if os.access(config_file, os.F_OK): util.msg("File %s already exists, skipping" % config_file) else: script._generate_template(file_path, config_file, script_location=directory) elif os.path.isfile(file_path): output_file = os.path.join(directory, file_) script._copy_file(file_path, output_file) if package: for path in [ os.path.join(os.path.abspath(directory), "__init__.py"), os.path.join(os.path.abspath(versions), "__init__.py"), ]: file_ = util.status("Adding %s" % path, open, path, "w") file_.close() util.msg("Please edit configuration/connection/logging " "settings in %r before proceeding." % config_file)