Пример #1
0
 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')
Пример #2
0
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
Пример #3
0
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')
Пример #4
0
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
Пример #5
0
def init(**opts):
    '''Initialize database migration management
    '''
    try:
        opts['repo'].init_db()
    except DBError, e:
        abort(e)
Пример #6
0
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')
Пример #7
0
def init(**opts):
    '''Initialize database migration management
    '''
    try:
        opts['repo'].init_db()
    except DBError as e:
        abort(e)
    print('Versioning table initialized successfully')
Пример #8
0
def init(**opts):
    '''Initialize database migration management
    '''
    try:
        opts['repo'].init_db()
    except DBError as e:
        abort(e)
    print('Versioning table initialized successfully')
Пример #9
0
    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)
Пример #10
0
    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)
Пример #11
0
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)
Пример #12
0
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
Пример #13
0
    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)
Пример #14
0
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))
Пример #15
0
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))