Beispiel #1
0
    def query(self, statement, *args):
        statement = self.prepare(statement)
        c = self.connection.cursor()
        try:
            c.execute(statement, args)
        except self.exc as e:
            raise DBError(e)

        data = self.fetch(c)
        c.close()
        return data
Beispiel #2
0
    def _apply(self, env=None):
        '''The real work for applying a migration
        '''
        filenames = sorted(os.listdir(self.path), key=humankey)

        for fn in filenames:
            if fn == 'migration.ini':
                continue
            path = op.join(self.path, fn)

            if fn.endswith('.sql'):
                with open(path) as f:
                    self.repo.engine.query(clean_sql(f.read()), escape=True)
                print('  sql migration applied: %s' % fn)

            elif fn.endswith('.j2'):
                import jinja2
                j2_env = jinja2.Environment(
                    loader=jinja2.FileSystemLoader(os.path.dirname(path)))
                j2_sql = j2_env.get_template(fn).render(self.get_config_dict())
                self.repo.engine.query(clean_sql(j2_sql), escape=True)
                print('  sql template migration applied: %s' % fn)

            elif os.access(path, os.X_OK) and not op.isdir(path):
                callenv = dict(
                    os.environ,
                    # for backward compatibility
                    NOMAD_DBURL=self.repo.url,
                    **self.get_env())
                if env:
                    callenv.update(env)
                retcode = call(path, env=callenv)
                if retcode:
                    raise DBError('script failed: %s' % fn)
                print('  script migration applied: %s' % fn)

            else:
                print('  skipping file: %s' % fn)
Beispiel #3
0
 def connect(self):
     p = urlparse.urlparse(self.url)
     if p.scheme not in CONNECTORS:
         raise DBError('scheme "%s" not supported' % p.scheme)
     return CONNECTORS[p.scheme](p)
Beispiel #4
0
 def connect(self):
     try:
         return self.module.connect(**self.parameters)
     except self.module.OperationalError as e:
         raise DBError('psycopg2: %s' % e)
Beispiel #5
0
 def query(self, statement, *args, **kwargs):
     statement = self.prepare(statement, kwargs.pop('escape', False))
     try:
         return self.connection.execute(statement, *args, **kwargs)
     except exc.SQLAlchemyError as e:
         raise DBError(str(e))