示例#1
0
    def __init__(self, database_id, instance, ddl_statements=(), pool=None):
        self.database_id = database_id
        self._instance = instance
        self._ddl_statements = _check_ddl_statements(ddl_statements)

        if pool is None:
            pool = BurstyPool()

        self._pool = pool
        pool.bind(self)
示例#2
0
 def setUpClass(cls):
     pool = BurstyPool()
     cls._db = Config.INSTANCE.database(DATABASE_ID,
                                        ddl_statements=DDL_STATEMENTS,
                                        pool=pool)
     operation = cls._db.create()
     operation.result(30)  # raises on failure / timeout.
    def test_read_w_index(self):
        ROW_COUNT = 2000
        # Indexed reads cannot return non-indexed columns
        MY_COLUMNS = self.COLUMNS[0], self.COLUMNS[2]
        EXTRA_DDL = [
            'CREATE INDEX contacts_by_last_name ON contacts(last_name)',
        ]
        pool = BurstyPool()
        temp_db = Config.INSTANCE.database(
            'test_read_w_index', ddl_statements=DDL_STATEMENTS + EXTRA_DDL,
            pool=pool)
        operation = temp_db.create()
        self.to_delete.append(_DatabaseDropper(temp_db))

        # We want to make sure the operation completes.
        operation.result(30)  # raises on failure / timeout.

        session, committed = self._set_up_table(ROW_COUNT, db=temp_db)

        snapshot = session.snapshot(read_timestamp=committed)
        rows = list(snapshot.read(
            self.TABLE, MY_COLUMNS, self.ALL, index='contacts_by_last_name'))

        expected = list(reversed(
            [(row[0], row[2]) for row in self._row_data(ROW_COUNT)]))
        self._check_row_data(rows, expected)
def ensure_database(client):
    instance = client.instance(INSTANCE_NAME)

    if not instance.exists():
        configs = list(client.list_instance_configs())
        config_name = configs[0].name
        print_func("Creating instance: {}".format(INSTANCE_NAME))
        instance = client.instance(INSTANCE_NAME, config_name)
        operation = instance.create()
        operation.result(30)
    else:
        print_func("Instance exists: {}".format(INSTANCE_NAME))
        instance.reload()

    pool = BurstyPool()
    database = instance.database(
        DATABASE_NAME, ddl_statements=DDL_STATEMENTS, pool=pool)

    if not database.exists():
        print_func("Creating database: {}".format(DATABASE_NAME))
        operation = database.create()
        operation.result(30)
    else:
        print_func("Database exists: {}".format(DATABASE_NAME))
        database.reload()

    return database
示例#5
0
def _item_to_database(iterator, database_pb):
    """Convert a database protobuf to the native object.

    :type iterator: :class:`~google.cloud.iterator.Iterator`
    :param iterator: The iterator that is currently in use.

    :type database_pb: :class:`~google.spanner.admin.database.v1.Database`
    :param database_pb: A database returned from the API.

    :rtype: :class:`~google.cloud.spanner.database.Database`
    :returns: The next database in the page.
    """
    return Database.from_pb(database_pb, iterator.instance, pool=BurstyPool())
示例#6
0
    def test_create_database(self):
        pool = BurstyPool()
        temp_db_id = 'temp-db'  # test w/ hyphen
        temp_db = Config.INSTANCE.database(temp_db_id, pool=pool)
        operation = temp_db.create()
        self.to_delete.append(temp_db)

        # We want to make sure the operation completes.
        operation.result(30)  # raises on failure / timeout.

        name_attr = operator.attrgetter('name')
        expected = sorted([temp_db, self._db], key=name_attr)

        databases = list(Config.INSTANCE.list_databases())
        found = sorted(databases, key=name_attr)
        self.assertEqual(found, expected)
示例#7
0
    def test_update_database_ddl(self):
        pool = BurstyPool()
        temp_db_id = 'temp_db'
        temp_db = Config.INSTANCE.database(temp_db_id, pool=pool)
        create_op = temp_db.create()
        self.to_delete.append(temp_db)

        # We want to make sure the operation completes.
        create_op.result(90)  # raises on failure / timeout.

        operation = temp_db.update_ddl(DDL_STATEMENTS)

        # We want to make sure the operation completes.
        operation.result(90)  # raises on failure / timeout.

        temp_db.reload()

        self.assertEqual(len(temp_db.ddl_statements), len(DDL_STATEMENTS))
def remove_database(client):
    instance = client.instance(INSTANCE_NAME)

    if not instance.exists():
        print_func("Instance does not exist: {}".format(INSTANCE_NAME))
        return

    print_func("Instance exists: {}".format(INSTANCE_NAME))
    instance.reload()

    pool = BurstyPool()
    database = instance.database(DATABASE_NAME)

    if not database.exists():
        print_func("Database does not exist: {}".format(DATABASE_NAME))
        return
    print_func("Dropping database: {}".format(DATABASE_NAME))
    database.drop()
示例#9
0
 def setUpClass(cls):
     pool = BurstyPool()
     cls._db = Config.INSTANCE.database(DATABASE_ID,
                                        ddl_statements=DDL_STATEMENTS,
                                        pool=pool)
     cls._db.create()
示例#10
0
 def setUpClass(cls):
     pool = BurstyPool()
     cls._db = Config.INSTANCE.database(DATABASE_ID, pool=pool)
     cls._db.create()