<commit_before>'''Perform database migrations using Alembic.

This is a click wrapper around the alembic library.
'''
import logging
import os

from flask import current_app
from flask.cli import FlaskGroup
import click
import alembic
import alembic.config

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

DEFAULT_DIR = 'migrations'
EXT_NAME = 'migrate'
HELP = {
	'autogenerate': (
		'Populate revision script with candidate migration '
		'operations, based on comparison of database to model.'
		),
	'branch-label': 'Specify a branch label to apply to the new revision',
	'head': 'Specify head revision or <branchname>@head to base new revision on.',
	'message': "Message string to use with 'revision'",
	'resolve-dependencies': 'Treat dependency versions as down revisions',
	'rev-id': 'Specify a hardcoded revision id instead of generating one',
	'rev-range': 'Specify a revision range; format is [start]:[end]',
	'splice': 'Allow a non-head revision as the "head" to splice onto',
	'sql': "Don't emit SQL to database - dump to standard output/file instead",
	'tag': "Arbitrary 'tag' name - can be used by custom env.py scripts.",
	'verbose': 'Use more verbose output',
	'version-path': 'Specify specific path from config for version file',
	'x-arg': "Additional arguments consumed by custom env.py scripts",
	}


class _MigrateConfig(object):
	def __init__(self, directory, **kwargs):
		self.directory = directory
		self.configure_args = kwargs


class Migrate(object):
	"""Flask extension for alembic migrations."""
	def __init__(self, app=None, directory=DEFAULT_DIR, **kwargs):
		if app is not None:
			self.init_app(app, directory, **kwargs)

	def init_app(self, app, directory=DEFAULT_DIR, **kwargs):
		if not hasattr(app, 'extensions'):
			app.extensions = {}
		app.extensions[EXT_NAME] = _MigrateConfig(directory, **kwargs)


class Config(alembic.config.Config):
	def get_template_directory(self):
		package_dir = os.path.abspath(os.path.dirname(__file__))
		return os.path.join(package_dir, 'templates')


def _get_config(x_arg=None):
	directory = current_app.extensions[EXT_NAME].directory
	config = Config(os.path.join(directory, 'alembic.ini'))
	config.set_main_option('script_location', directory)
	if x_arg is not None:
		if config.cmd_opts is None:
			config.cmd_opts = lambda: None
		if not getattr(config.cmd_opts, 'x', None):
			config.cmd_opts.x = []
		config.cmd_opts.x.append(x_arg)
	return config


cli = FlaskGroup(help=__doc__, name='migrate')


@cli.command()
def init():
	"""Generates a new migration"""
	directory = current_app.extensions[EXT_NAME].directory
	config = Config()
	config.set_main_option('script_location', directory)
	config.config_file_name = os.path.join(directory, 'alembic.ini')<commit_msg>Configure with db.<commit_after>'''Perform database migrations using Alembic.

This is a click wrapper around the alembic library.
'''
import logging
import os

from flask import current_app
from flask.cli import FlaskGroup
import click
import alembic
import alembic.config

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

DEFAULT_DIR = 'migrations'
EXT_NAME = 'migrate'
HELP = {
	'autogenerate': (
		'Populate revision script with candidate migration '
		'operations, based on comparison of database to model.'
		),
	'branch-label': 'Specify a branch label to apply to the new revision',
	'head': 'Specify head revision or <branchname>@head to base new revision on.',
	'message': "Message string to use with 'revision'",
	'resolve-dependencies': 'Treat dependency versions as down revisions',
	'rev-id': 'Specify a hardcoded revision id instead of generating one',
	'rev-range': 'Specify a revision range; format is [start]:[end]',
	'splice': 'Allow a non-head revision as the "head" to splice onto',
	'sql': "Don't emit SQL to database - dump to standard output/file instead",
	'tag': "Arbitrary 'tag' name - can be used by custom env.py scripts.",
	'verbose': 'Use more verbose output',
	'version-path': 'Specify specific path from config for version file',
	'x-arg': "Additional arguments consumed by custom env.py scripts",
	}


class _MigrateConfig(object):
	def __init__(self, directory, db, **kwargs):
		self.directory = directory
		self.db = db
		self.configure_args = kwargs


class Migrate(object):
	"""Flask extension for alembic migrations."""
	def __init__(self, app=None, db=None, directory=DEFAULT_DIR, **kwargs):
		if app is not None:
			self.init_app(app, db, directory, **kwargs)

	def init_app(self, app, db=None, directory=DEFAULT_DIR, **kwargs):
		if not hasattr(app, 'extensions'):
			app.extensions = {}
		app.extensions[EXT_NAME] = _MigrateConfig(directory, db, **kwargs)


class Config(alembic.config.Config):
	def get_template_directory(self):
		package_dir = os.path.abspath(os.path.dirname(__file__))
		return os.path.join(package_dir, 'templates')


def _get_config(x_arg=None):
	directory = current_app.extensions[EXT_NAME].directory
	config = Config(os.path.join(directory, 'alembic.ini'))
	config.set_main_option('script_location', directory)
	if x_arg is not None:
		if config.cmd_opts is None:
			config.cmd_opts = lambda: None
		if not getattr(config.cmd_opts, 'x', None):
			config.cmd_opts.x = []
		config.cmd_opts.x.append(x_arg)
	return config


cli = FlaskGroup(help=__doc__, name='migrate')


@cli.command()
def init():
	"""Generates a new migration"""
	directory = current_app.extensions[EXT_NAME].directory
	config = Config()
	config.set_main_option('script_location', directory)
	config.config_file_name = os.path.join(directory, 'alembic.ini')
{'fraction_non_alphanumeric': 0.12136445242369838, 'fraction_numerical': 0.0, 'mean_word_length': 5.323496027241771, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6709143', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1768, 'n_words': 538}