def inner(*args, **kwargs): try: repo = Repository(kwargs['config'], kwargs['define']) except NomadIniNotFound, e: sys.stderr.write("Create '%s' to use nomad, example:\n%s\n" % (e, EXAMPLE_INI)) abort('config file not found')
def get_engine(conf): try: enginepath = conf['nomad']['engine'] if '.' not in enginepath: enginepath = 'nomad.engine.' + enginepath except KeyError: raise NomadError('nomad.engine is not defined in config file') try: enginemod = __import__(enginepath, {}, {}, ['']) except ImportError as e: raise NomadError('cannot use engine %s: %s' % (enginepath, e)) try: url = geturl(conf['nomad']['url']) except KeyError: abort('database url was not found in the nomad Configuration') engine = getattr(enginemod, 'engine')(url) try: engine.connection except DBError as e: abort(e) return engine, url
def create(name, dependencies=('d', [], 'migration dependencies'), prefix_date=('p', False, 'prefix migration name with date'), **opts): '''Create new migration ''' repo = opts['repo'] deps = map(repo.get, dependencies) if prefix_date: name = date.today().strftime('%Y%m%d-') + name path = op.join(repo.path, name) try: os.mkdir(path) except OSError as e: if e.errno == 17: abort('directory %s already exists' % path) raise with open(op.join(path, 'migration.ini'), 'w') as f: f.write('[nomad]\n') f.write('dependencies = %s\n' % ', '.join(d.name for d in deps)) with open(op.join(path, 'up.sql'), 'w') as f: f.write('-- SQL ALTER statements for database migration\n')
def init(**opts): '''Initialize database migration management ''' try: opts['repo'].init_db() except DBError, e: abort(e)
def init(**opts): '''Initialize database migration management ''' try: opts['repo'].init_db() except DBError as e: abort(e) print('Versioning table initialized successfully')
def inner(*args, **kwargs): try: repo = Repository(kwargs['config'], kwargs['define']) except NomadIniNotFound as e: sys.stderr.write("Create '%s' to use nomad, example:\n%s\n" % (e, EXAMPLE_INI)) abort('config file not found') except (IOError, NomadError) as e: abort(e) return func(repo=repo, *args, **kwargs)
def getconfig(func): if func.__name__.startswith('help') or func.__name__ in ('version',): return func def inner(*args, **kwargs): try: repo = Repository(kwargs['config'], kwargs['define']) except NomadIniNotFound, e: sys.stderr.write("Create '%s' to use nomad, example:\n%s\n" % (e, EXAMPLE_INI)) abort('config file not found') except (IOError, NomadError), e: abort(e)
def create(name, dependencies=('d', [], 'migration dependencies'), prefix_date=('p', False, 'prefix migration name with date'), **opts): '''Create new migration ''' repo = opts['repo'] deps = map(repo.get, dependencies) if prefix_date: name = date.today().strftime('%Y%m%d-') + name path = op.join(repo.path, name) try: os.mkdir(path) except OSError, e: if e.errno == 17: abort('directory %s already exists' % path) raise
def __init__(self, confpath, overrides=None): self.conf = ConfigParser( interpolation=ExtendedInterpolation(), defaults={ 'confpath': op.abspath(confpath), 'confdir': op.dirname(op.abspath(confpath)), }) self.conf.read_dict(self.DEFAULTS) if not self.conf.read([confpath]): raise NomadIniNotFound(confpath) for k, v in (overrides or {}).iteritems(): section, key = k.split('.') self.conf.set(section, key, v) self.confpath = confpath self.path = self.conf.get('nomad', 'path', fallback=op.dirname(confpath) or '.') try: enginepath = self.conf['nomad']['engine'] if not '.' in enginepath: enginepath = 'nomad.engine.' + enginepath except KeyError: raise NomadError('nomad.engine is not defined in config file') try: enginemod = __import__(enginepath, {}, {}, ['']) except ImportError as e: raise NomadError('cannot use engine %s: %s' % (enginepath, e)) try: self.url = geturl(self.conf['nomad']['url']) except KeyError: abort('database url in %s is not found' % self) self.engine = getattr(enginemod, 'engine')(self.url) try: self.engine.connection except DBError as e: abort(e)
def apply(all=('a', False, 'apply all available migrations'), init=('', False, 'init if not initialized yet'), env=('e', [], 'list of additional environment variables'), fake=('', False, 'record migration as applied, but do not do anything'), *names, **opts): '''Apply migration and all of it dependencies You can pass additional environment variables to your migrations scripts using option `-e` (in addition to `NOMAD_<confvar>`s, which are passed by default): nomad apply -e ONE=one -e TWO=two -a ''' repo = opts['repo'] if init: try: repo.init_db() except DBError: pass if names: migrations = [repo.get(x) for x in names] elif all: migrations = [x for x in repo.available if x not in repo.applied] else: abort('Supply names of migrations to apply') if env: env = dict(x.split('=', 1) for x in env) for m in migrations: if m.applied: abort('migration %s is already applied' % m) for m in migrations: try: m.apply(env=env, fake=fake) except DBError as e: abort('cannot apply migration %s: %s' % (m, e))