Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add samples for PITR #222

Merged
merged 12 commits into from Feb 25, 2021
17 changes: 11 additions & 6 deletions samples/samples/backup_sample.py
Expand Up @@ -34,7 +34,8 @@ def create_backup(instance_id, database_id, backup_id):

# Create a backup
expire_time = datetime.utcnow() + timedelta(days=14)
backup = instance.backup(backup_id, database=database, expire_time=expire_time)
version_time = datetime.now()
backup = instance.backup(backup_id, database=database, expire_time=expire_time, version_time=version_time)
operation = backup.create()

# Wait for backup operation to complete.
Expand All @@ -47,8 +48,8 @@ def create_backup(instance_id, database_id, backup_id):
# Get the name, create time and backup size.
backup.reload()
print(
"Backup {} of size {} bytes was created at {}".format(
backup.name, backup.size_bytes, backup.create_time
"Backup {} of size {} bytes was created at {} for version of database at {}".format(
backup.name, backup.size_bytes, backup.create_time, backup.version_time
)
)

Expand All @@ -57,14 +58,17 @@ def create_backup(instance_id, database_id, backup_id):


# [START spanner_restore_backup]
def restore_database(instance_id, new_database_id, backup_id):
def restore_database(instance_id, database_id, new_database_id, backup_id):
zoercai marked this conversation as resolved.
Show resolved Hide resolved
"""Restores a database from a backup."""
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
# Create a backup on database_id.

# Start restoring backup to a new database.
backup = instance.backup(backup_id)
expire_time = datetime.utcnow() + timedelta(days=14)
version_time = datetime.now()
backup = instance.backup(backup_id, database=database, expire_time=expire_time, version_time=version_time)
zoercai marked this conversation as resolved.
Show resolved Hide resolved
new_database = instance.database(new_database_id)
operation = new_database.restore(backup)

Expand All @@ -75,10 +79,11 @@ def restore_database(instance_id, new_database_id, backup_id):
new_database.reload()
restore_info = new_database.restore_info
print(
"Database {} restored to {} from backup {}.".format(
"Database {} restored to {} from backup {} with version time {}.".format(
restore_info.backup_info.source_database,
new_database_id,
restore_info.backup_info.backup,
restore_info.backup_info.version_time
)
)

Expand Down
2 changes: 1 addition & 1 deletion samples/samples/backup_sample_test.py
Expand Up @@ -72,7 +72,7 @@ def test_create_backup(capsys, database):

@RetryErrors(exception=DeadlineExceeded, max_tries=2)
def test_restore_database(capsys):
backup_sample.restore_database(INSTANCE_ID, RESTORE_DB_ID, BACKUP_ID)
backup_sample.restore_database(INSTANCE_ID, DATABASE_ID, RESTORE_DB_ID, BACKUP_ID)
out, _ = capsys.readouterr()
assert (DATABASE_ID + " restored to ") in out
assert (RESTORE_DB_ID + " from backup ") in out
Expand Down
63 changes: 63 additions & 0 deletions samples/samples/version_retention_period.py
@@ -0,0 +1,63 @@
# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This application demonstrates how to create and restore from backups
using Cloud Spanner.

For more information, see the README.rst under /spanner.
"""

import argparse
from datetime import datetime, timedelta
import time

from google.cloud import spanner

# [START spanner_create_database_with_version_retention_period]
def create_database_with_version_retention_period(instance_id, database_id, retention_period):
"""Creates a database with a version retention period."""
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
ddl_statements = [
"CREATE TABLE Singers ("
+ " SingerId INT64 NOT NULL,"
+ " FirstName STRING(1024),"
+ " LastName STRING(1024),"
+ " SingerInfo BYTES(MAX)"
+ ") PRIMARY KEY (SingerId)",
"CREATE TABLE Albums ("
+ " SingerId INT64 NOT NULL,"
+ " AlbumId INT64 NOT NULL,"
+ " AlbumTitle STRING(MAX)"
+ ") PRIMARY KEY (SingerId, AlbumId),"
+ " INTERLEAVE IN PARENT Singers ON DELETE CASCADE",
"ALTER DATABASE {}"
" SET OPTIONS (version_retention_period = '{}')".format(
database_id, retention_period
)
]
db = instance.database(database_id, ddl_statements)
operation = db.create()

operation.result(30)

db.reload()

print("Database {} created with version retention period {} and earliest version time {}".format(
db.database_id, db.version_retention_period(), db.earliest_version_time()
))

db.drop()

# [END spanner_create_database_with_version_retention_period]
54 changes: 54 additions & 0 deletions samples/samples/version_retention_period_test.py
@@ -0,0 +1,54 @@
# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import uuid

from google.api_core.exceptions import DeadlineExceeded
from google.cloud import spanner
import pytest
from test_utils.retry import RetryErrors

import version_retention_period

def unique_instance_id():
""" Creates a unique id for the database. """
return f"test_instance_{uuid.uuid4().hex[:10]}"


def unique_database_id():
""" Creates a unique id for the database. """
return f"test_db_{uuid.uuid4().hex[:10]}"


INSTANCE_ID = unique_instance_id()
DATABASE_ID = unique_database_id()

@pytest.fixture(scope="module")
def spanner_instance():
spanner_client = spanner.Client()
instance_config = "{}/instanceConfigs/{}".format(
spanner_client.project_name, "regional-us-central1"
)
instance = spanner_client.instance(INSTANCE_ID, instance_config)
op = instance.create()
op.result(120) # block until completion
yield instance
instance.delete()

@RetryErrors(exception=DeadlineExceeded, max_tries=2)
def test_restore_database(capsys):
retention_period = "7d"
version_retention_period.create_database_with_version_retention_period(INSTANCE_ID, DATABASE_ID, retention_period)
out, _ = capsys.readouterr()
assert (DATABASE_ID + " created with ") in out
assert ("retention period " + version_retention_period) in out