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
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
示例#3
0
def populate_table_2_columns(database, table_name, row_count, val_size):
    all_ = KeySet(all_=True)
    columns = ('pkey', 'chunk_me', 'chunk_me_2')
    rows = list(
        database.execute_sql('SELECT COUNT(*) FROM {}'.format(table_name)))
    assert len(rows) == 1
    count = rows[0][0]
    if count != row_count:
        print_func("Repopulating table: {}".format(table_name))
        chunk_me = 'X' * val_size
        row_data = [(index, chunk_me, chunk_me) for index in range(row_count)]
        with database.batch() as batch:
            batch.delete(table_name, all_)
            batch.insert(table_name, columns, row_data)
    else:
        print_func("Leaving table: {}".format(table_name))
def populate_table(database, table_desc):
    all_ = KeySet(all_=True)
    columns = ('pkey', 'chunk_me')
    with database.snapshot() as snapshot:
        rows = list(snapshot.execute_sql(
            'SELECT COUNT(*) FROM {}'.format(table_desc.table)))
    assert len(rows) == 1
    count = rows[0][0]
    if count != table_desc.row_count:
        print_func("Repopulating table: {}".format(table_desc.table))
        chunk_me = table_desc.value()
        row_data = [(index, chunk_me) for index in range(table_desc.row_count)]
        with database.batch() as batch:
            batch.delete(table_desc.table, all_)
            batch.insert(table_desc.table, columns, row_data)
    else:
        print_func("Leaving table: {}".format(table_desc.table))
def populate_table(database, table_desc):
    all_ = KeySet(all_=True)
    columns = ('pkey', 'chunk_me')
    with database.snapshot() as snapshot:
        rows = list(snapshot.execute_sql(
            'SELECT COUNT(*) FROM {}'.format(table_desc.table)))
    assert len(rows) == 1
    count = rows[0][0]
    if count != table_desc.row_count:
        print_func("Repopulating table: {}".format(table_desc.table))
        chunk_me = table_desc.value()
        row_data = [(index, chunk_me) for index in range(table_desc.row_count)]
        with database.batch() as batch:
            batch.delete(table_desc.table, all_)
            batch.insert(table_desc.table, columns, row_data)
    else:
        print_func("Leaving table: {}".format(table_desc.table))
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()

    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()
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()

    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()