Exemple #1
0
 def test_sqlite_absolute(self):
     # Standard syntax
     self.assertEqual(('sqlite', {'path': '/var/db/trac.db'}),
                      parse_connection_uri('sqlite:///var/db/trac.db'))
     # Legacy syntax
     self.assertEqual(('sqlite', {'path': '/var/db/trac.db'}),
                      parse_connection_uri('sqlite:/var/db/trac.db'))
Exemple #2
0
 def test_postgres_with_quoted_password(self):
     self.assertEqual(('postgres', {
         'user': '******',
         'password': '******',
         'host': 'localhost',
         'path': '/trac'
     }), parse_connection_uri('postgres://*****:*****@localhost/trac'))
Exemple #3
0
    def _do_convert_db_in_place(self, dst_dburi):
        dbm = DatabaseManager(self.env)
        src_dburi = dbm.connection_uri
        if src_dburi == dst_dburi:
            printferr("Source database and destination database are same: %s",
                      dst_dburi)
            return 1

        env_path = mkdtemp(prefix='convert_db-',
                           dir=os.path.dirname(self.env.path))
        try:
            dst_env = self._create_env(env_path, dst_dburi)
            src_db = dbm.get_connection()
            dst_db = DatabaseManager(dst_env).get_connection()
            self._copy_tables(dst_env, src_db, dst_db, src_dburi, dst_dburi)
            del src_db
            del dst_db
            dst_env.shutdown()
            dst_env = None
            schema, params = parse_connection_uri(dst_dburi)
            if schema == 'sqlite':
                dbpath = os.path.join(self.env.path, params['path'])
                dbdir = os.path.dirname(dbpath)
                if not os.path.isdir(dbdir):
                    os.makedirs(dbdir)
                shutil.copy(os.path.join(env_path, params['path']), dbpath)
        finally:
            shutil.rmtree(env_path)

        backup_config_file(self.env, '.convert_db-%d' % int(time.time()))
        self.config.set('trac', 'database', dst_dburi)
        self.config.save()
Exemple #4
0
 def test_mysql_with_creds(self):
     self.assertEqual(('mysql', {
         'user': '******',
         'password': '******',
         'host': 'localhost',
         'port': 3306,
         'path': '/trac'
     }), parse_connection_uri('mysql://*****:*****@localhost:3306/trac'))
Exemple #5
0
 def test_sqlite_with_timeout_param(self):
     # In-memory database
     self.assertEqual(('sqlite', {
         'path': 'db/trac.db',
         'params': {
             'timeout': '10000'
         }
     }), parse_connection_uri('sqlite:db/trac.db?timeout=10000'))
Exemple #6
0
 def test_postgres_with_creds(self):
     self.assertEqual(('postgres', {
         'user': '******',
         'password': '******',
         'host': 'localhost',
         'port': 9431,
         'path': '/trac'
     }), parse_connection_uri(
         'postgres://*****:*****@localhost:9431/trac'))
Exemple #7
0
 def test_sqlite_windows_path(self):
     # In-memory database
     os_name = os.name
     try:
         os.name = 'nt'
         self.assertEqual(('sqlite', {'path': 'C:/project/db/trac.db'}),
                          parse_connection_uri('sqlite:C|/project/db/trac.db'))
     finally:
         os.name = os_name
Exemple #8
0
 def test_destroy_db(self):
     """Database doesn't exist after calling destroy_db."""
     self.env.db_query("SELECT name FROM system")
     self.assertIsNotNone(self.dbm._cnx_pool)
     self.dbm.destroy_db()
     self.assertIsNone(self.dbm._cnx_pool)  # No connection pool
     scheme, params = parse_connection_uri(get_dburi())
     if scheme != 'postgres' or params.get('schema', 'public') != 'public':
         self.assertFalse(self.dbm.db_exists())
     else:
         self.assertEqual([], self.dbm.get_table_names())
Exemple #9
0
def get_dburi():
    dburi = os.environ.get('TRAC_TEST_DB_URI')
    if dburi:
        scheme, db_prop = parse_connection_uri(dburi)
        # Assume the schema 'tractest' for PostgreSQL
        if scheme == 'postgres' and \
                not db_prop.get('params', {}).get('schema'):
            dburi += ('&' if '?' in dburi else '?') + 'schema=tractest'
        elif scheme == 'sqlite' and db_prop['path'] != ':memory:' and \
                not db_prop.get('params', {}).get('synchronous'):
            # Speed-up tests with SQLite database
            dburi += ('&' if '?' in dburi else '?') + 'synchronous=off'
        return dburi
    return 'sqlite::memory:'
Exemple #10
0
def get_dburi():
    dburi = os.environ.get('TRAC_TEST_DB_URI')
    if dburi:
        scheme, db_prop = parse_connection_uri(dburi)
        # Assume the schema 'tractest' for PostgreSQL
        if scheme == 'postgres' and \
                not db_prop.get('params', {}).get('schema'):
            dburi += ('&' if '?' in dburi else '?') + 'schema=tractest'
        elif scheme == 'sqlite' and db_prop['path'] != ':memory:' and \
                not db_prop.get('params', {}).get('synchronous'):
            # Speed-up tests with SQLite database
            dburi += ('&' if '?' in dburi else '?') + 'synchronous=off'
        return dburi
    return 'sqlite::memory:'
Exemple #11
0
    def backup(self, dest_file):
        from subprocess import Popen, PIPE
        db_url = self.env.config.get('trac', 'database')
        scheme, db_prop = parse_connection_uri(db_url)
        db_params = db_prop.setdefault('params', {})
        db_name = os.path.basename(db_prop['path'])

        args = [self.mysqldump_path, '--no-defaults']
        if 'host' in db_prop:
            args.extend(['-h', db_prop['host']])
        if 'port' in db_prop:
            args.extend(['-P', str(db_prop['port'])])
        if 'user' in db_prop:
            args.extend(['-u', db_prop['user']])
        for name, value in db_params.iteritems():
            if name == 'compress' and as_int(value, 0):
                args.append('--compress')
            elif name == 'named_pipe' and as_int(value, 0):
                args.append('--protocol=pipe')
            elif name == 'read_default_file':  # Must be first
                args.insert(1, '--defaults-file=' + value)
            elif name == 'unix_socket':
                args.extend(['--protocol=socket', '--socket=' + value])
            elif name not in ('init_command', 'read_default_group'):
                self.log.warning("Invalid connection string parameter '%s'",
                                 name)
        args.extend(['-r', dest_file, db_name])

        environ = os.environ.copy()
        if 'password' in db_prop:
            environ['MYSQL_PWD'] = str(db_prop['password'])
        try:
            p = Popen(args, env=environ, stderr=PIPE, close_fds=close_fds)
        except OSError as e:
            raise TracError(
                _("Unable to run %(path)s: %(msg)s",
                  path=self.mysqldump_path,
                  msg=exception_to_unicode(e)))
        errmsg = p.communicate()[1]
        if p.returncode != 0:
            raise TracError(
                _("mysqldump failed: %(msg)s", msg=to_unicode(errmsg.strip())))
        if not os.path.exists(dest_file):
            raise TracError(_("No destination file created"))
        return dest_file
Exemple #12
0
    def backup(self, dest_file):
        from subprocess import Popen, PIPE
        db_url = self.env.config.get('trac', 'database')
        scheme, db_prop = parse_connection_uri(db_url)
        db_params = db_prop.setdefault('params', {})
        db_name = os.path.basename(db_prop['path'])

        args = [self.mysqldump_path]
        if 'host' in db_prop:
            args.extend(['-h', db_prop['host']])
        if 'port' in db_prop:
            args.extend(['-P', str(db_prop['port'])])
        if 'user' in db_prop:
            args.extend(['-u', db_prop['user']])
        for name, value in db_params.iteritems():
            if name == 'compress' and as_int(value, 0):
                args.append('--compress')
            elif name == 'named_pipe' and as_int(value, 0):
                args.append('--protocol=pipe')
            elif name == 'read_default_file':  # Must be first
                args.insert(1, '--defaults-file=' + value)
            elif name == 'unix_socket':
                args.extend(['--protocol=socket', '--socket=' + value])
            elif name not in ('init_command', 'read_default_group'):
                self.log.warning("Invalid connection string parameter '%s'",
                                 name)
        args.extend(['-r', dest_file, db_name])

        environ = os.environ.copy()
        if 'password' in db_prop:
            environ['MYSQL_PWD'] = str(db_prop['password'])
        try:
            p = Popen(args, env=environ, stderr=PIPE, close_fds=close_fds)
        except OSError as e:
            raise TracError(_("Unable to run %(path)s: %(msg)s",
                              path=self.mysqldump_path,
                              msg=exception_to_unicode(e)))
        errmsg = p.communicate()[1]
        if p.returncode != 0:
            raise TracError(_("mysqldump failed: %(msg)s",
                              msg=to_unicode(errmsg.strip())))
        if not os.path.exists(dest_file):
            raise TracError(_("No destination file created"))
        return dest_file
Exemple #13
0
    def backup(self, dest_file):
        from subprocess import Popen, PIPE
        db_url = self.env.config.get('trac', 'database')
        scheme, db_prop = parse_connection_uri(db_url)
        db_params = db_prop.setdefault('params', {})
        db_name = os.path.basename(db_prop['path'])

        args = [self.pg_dump_path, '-C', '--inserts', '-x', '-Z', '8']
        if 'user' in db_prop:
            args.extend(['-U', db_prop['user']])
        host = db_params.get('host', db_prop.get('host'))
        if host:
            args.extend(['-h', host])
            if '/' not in host:
                args.extend(['-p', str(db_prop.get('port', '5432'))])

        if 'schema' in db_params:
            # Need quote for -n (--schema) option in PostgreSQL 8.2+
            if re.search(r' 8\.[01]\.', self._version()):
                args.extend(['-n', db_params['schema']])
            else:
                args.extend(['-n', '"%s"' % db_params['schema']])

        dest_file += ".gz"
        args.extend(['-f', dest_file, db_name])

        environ = os.environ.copy()
        if 'password' in db_prop:
            environ['PGPASSWORD'] = str(db_prop['password'])
        try:
            p = Popen(args, env=environ, stderr=PIPE, close_fds=close_fds)
        except OSError as e:
            raise TracError(
                _("Unable to run %(path)s: %(msg)s",
                  path=self.pg_dump_path,
                  msg=exception_to_unicode(e)))
        errmsg = p.communicate()[1]
        if p.returncode != 0:
            raise TracError(
                _("pg_dump failed: %(msg)s", msg=to_unicode(errmsg.strip())))
        if not os.path.exists(dest_file):
            raise TracError(_("No destination file created"))
        return dest_file
Exemple #14
0
    def backup(self, dest_file):
        from subprocess import Popen, PIPE
        db_url = self.env.config.get('trac', 'database')
        scheme, db_prop = parse_connection_uri(db_url)
        db_params = db_prop.setdefault('params', {})
        db_name = os.path.basename(db_prop['path'])

        args = [self.pg_dump_path, '-C', '--inserts', '-x', '-Z', '8']
        if 'user' in db_prop:
            args.extend(['-U', db_prop['user']])
        host = db_params.get('host', db_prop.get('host'))
        if host:
            args.extend(['-h', host])
            if '/' not in host:
                args.extend(['-p', str(db_prop.get('port', '5432'))])

        if 'schema' in db_params:
            # Need quote for -n (--schema) option in PostgreSQL 8.2+
            if re.search(r' 8\.[01]\.', self._version()):
                args.extend(['-n', db_params['schema']])
            else:
                args.extend(['-n', '"%s"' % db_params['schema']])

        dest_file += ".gz"
        args.extend(['-f', dest_file, db_name])

        environ = os.environ.copy()
        if 'password' in db_prop:
            environ['PGPASSWORD'] = str(db_prop['password'])
        try:
            p = Popen(args, env=environ, stderr=PIPE, close_fds=close_fds)
        except OSError as e:
            raise TracError(_("Unable to run %(path)s: %(msg)s",
                              path=self.pg_dump_path,
                              msg=exception_to_unicode(e)))
        errmsg = p.communicate()[1]
        if p.returncode != 0:
            raise TracError(_("pg_dump failed: %(msg)s",
                              msg=to_unicode(errmsg.strip())))
        if not os.path.exists(dest_file):
            raise TracError(_("No destination file created"))
        return dest_file
Exemple #15
0
 def test_sqlite_relative(self):
     # Default syntax for specifying DB path relative to the environment
     # directory
     self.assertEqual(('sqlite', {'path': 'db/trac.db'}),
                      parse_connection_uri('sqlite:db/trac.db'))
Exemple #16
0
 def test_mysql_simple(self):
     self.assertEqual(('mysql', {'host': 'localhost', 'path': '/trac'}),
                  parse_connection_uri('mysql://localhost/trac'))
Exemple #17
0
 def test_mysql_with_creds(self):
     self.assertEqual(('mysql', {'user': '******', 'password': '******',
                                 'host': 'localhost', 'port': 3306,
                                 'path': '/trac'}),
                  parse_connection_uri('mysql://*****:*****@localhost:3306/trac'))
Exemple #18
0
 def test_postgres_with_quoted_password(self):
     self.assertEqual(('postgres', {'user': '******', 'password': '******',
                                    'host': 'localhost', 'path': '/trac'}),
                  parse_connection_uri('postgres://*****:*****@localhost/trac'))
Exemple #19
0
 def test_postgres_with_creds(self):
     self.assertEqual(('postgres', {'user': '******', 'password': '******',
                                    'host': 'localhost', 'port': 9431,
                                    'path': '/trac'}),
              parse_connection_uri('postgres://*****:*****@localhost:9431/trac'))
Exemple #20
0
 def test_postgres_with_port(self):
     self.assertEqual(('postgres', {'host': 'localhost', 'port': 9431,
                                    'path': '/trac'}),
                      parse_connection_uri('postgres://localhost:9431/trac'))
Exemple #21
0
 def test_postgres_simple(self):
     self.assertEqual(('postgres', {'host': 'localhost', 'path': '/trac'}),
                      parse_connection_uri('postgres://localhost/trac'))
Exemple #22
0
 def test_sqlite_with_timeout_param(self):
     # In-memory database
     self.assertEqual(('sqlite', {'path': 'db/trac.db',
                                  'params': {'timeout': '10000'}}),
                      parse_connection_uri('sqlite:db/trac.db?timeout=10000'))