Skip to content

Commit

Permalink
samples: rewrote the samples to match spec
Browse files Browse the repository at this point in the history
  • Loading branch information
skuruppu committed Feb 2, 2021
1 parent eb8af9c commit 81b4585
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 266 deletions.
9 changes: 7 additions & 2 deletions samples/backups-create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2020 Google LLC
* Copyright 2021 Google LLC
* 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
Expand Down Expand Up @@ -46,9 +46,12 @@ async function createBackup(instanceId, databaseId, backupId, projectId) {
const databasePath = database.formattedName_;
// Expire backup 14 days in the future
const expireTime = Date.now() + 1000 * 60 * 60 * 24 * 14;
// Create a backup of the state of the database now.
const versionTime = Date.now();
const [, operation] = await backup.create({
databasePath: databasePath,
expireTime: expireTime,
versionTime: versionTime,
});

console.log(`Waiting for backup ${backup.formattedName_} to complete...`);
Expand All @@ -60,7 +63,9 @@ async function createBackup(instanceId, databaseId, backupId, projectId) {
console.log(
`Backup ${backupInfo.name} of size ` +
`${backupInfo.sizeBytes} bytes was created at ` +
`${new PreciseDate(backupInfo.createTime).toISOString()}`
`${new PreciseDate(backupInfo.createTime).toISOString()}` +
'for version of database at ' +
`${new PreciseDate(backupInfo.versionTime).toISOString()}`
);
} else {
console.error('ERROR: Backup is not ready.');
Expand Down
9 changes: 6 additions & 3 deletions samples/backups-restore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2020 Google LLC
* Copyright 2021 Google LLC
* 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
Expand All @@ -17,8 +17,9 @@

async function restoreBackup(instanceId, databaseId, backupId, projectId) {
// [START spanner_restore_backup]
// Imports the Google Cloud client library
// Imports the Google Cloud client library and precise date library
const {Spanner} = require('@google-cloud/spanner');
const {PreciseDate} = require('@google-cloud/precise-date');

/**
* TODO(developer): Uncomment the following lines before running the sample.
Expand Down Expand Up @@ -53,7 +54,9 @@ async function restoreBackup(instanceId, databaseId, backupId, projectId) {
const restoreInfo = await database.getRestoreInfo();
console.log(
`Database ${restoreInfo.backupInfo.sourceDatabase} was restored ` +
`to ${databaseId} from backup ${restoreInfo.backupInfo.backup}.`
`to ${databaseId} from backup ${restoreInfo.backupInfo.backup} ` +
'with version time ' +
`${new PreciseDate(restoreInfo.backupInfo.versionTime).toISOString()}.`
);
// [END spanner_restore_backup]
}
Expand Down
15 changes: 12 additions & 3 deletions samples/database-create-with-version-retention-period.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2020 Google LLC
* Copyright 2021 Google LLC
* 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
Expand Down Expand Up @@ -38,8 +38,9 @@ async function createDatabaseWithVersionRetentionPeriod(
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);

// Creates a new database.
try {
// Create a new database with an extra statement which will alter the
// database after creation to set the version retention period.
console.log(`Creating database ${instance.formattedName_}.`);
const versionRetentionStatement = `
ALTER DATABASE \`${databaseId}\`
Expand All @@ -50,9 +51,17 @@ async function createDatabaseWithVersionRetentionPeriod(

console.log(`Waiting for operation on ${database.id} to complete...`);
await operation.promise();

console.log(`
Created database ${databaseId} with version retention period.`);

const [data] = await database.get();
console.log(
`Version retention period: ${data.metadata.versionRetentionPeriod}`
);
const earliestVersionTime = Spanner.timestamp(
data.metadata.earliestVersionTime
);
console.log(`Earliest version time: ${earliestVersionTime}`);
} catch (err) {
console.error('ERROR:', err);
}
Expand Down
59 changes: 0 additions & 59 deletions samples/database-get-schema-with-version-retention-period.js

This file was deleted.

58 changes: 0 additions & 58 deletions samples/database-get-with-version-retention-period.js

This file was deleted.

64 changes: 0 additions & 64 deletions samples/database-update-with-version-retention-period.js

This file was deleted.

51 changes: 0 additions & 51 deletions samples/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,6 @@ async function queryDataWithNewColumn(instanceId, databaseId, projectId) {
const {
createDatabaseWithVersionRetentionPeriod,
} = require('./database-create-with-version-retention-period');
const {
updateDatabaseWithVersionRetentionPeriod,
} = require('./database-update-with-version-retention-period');
const {
getDatabaseSchemaWithVersionRetentionPeriod,
} = require('./database-get-schema-with-version-retention-period');
const {
getDatabaseWithVersionRetentionPeriod,
} = require('./database-get-with-version-retention-period');

require('yargs')
.demand(1)
Expand Down Expand Up @@ -209,54 +200,12 @@ require('yargs')
opts.projectId
)
)
.command(
'updateDatabaseWithVersionRetentionPeriod <instanceName> <databaseId> <projectId>',
'Updates the version retention period of the database.',
{},
opts =>
updateDatabaseWithVersionRetentionPeriod(
opts.instanceName,
opts.databaseId,
opts.projectId
)
)
.command(
'getDatabaseSchemaWithVersionRetentionPeriod <instanceName> <databaseId> <projectId>',
'Gets database schema that includes the version retention period.',
{},
opts =>
getDatabaseSchemaWithVersionRetentionPeriod(
opts.instanceName,
opts.databaseId,
opts.projectId
)
)
.command(
'getDatabaseWithVersionRetentionPeriod <instanceName> <databaseId> <projectId>',
'Gets database that includes a version retention period.',
{},
opts =>
getDatabaseWithVersionRetentionPeriod(
opts.instanceName,
opts.databaseId,
opts.projectId
)
)
.example('node $0 createDatabase "my-instance" "my-database" "my-project-id"')
.example('node $0 addColumn "my-instance" "my-database" "my-project-id"')
.example('node $0 queryNewColumn "my-instance" "my-database" "my-project-id"')
.example(
'node $0 createDatabaseWithVersionRetentionPeriod "my-instance" "my-database-id" "my-project-id"'
)
.example(
'node $0 updateDatabaseWithVersionRetentionPeriod "my-instance" "my-database-id" "my-project-id"'
)
.example(
'node $0 getDatabaseSchemaWithVersionRetentionPeriod "my-instance" "my-database-id" "my-project-id"'
)
.example(
'node $0 getDatabaseWithVersionRetentionPeriod "my-instance" "my-database-id" "my-project-id"'
)
.wrap(120)
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/spanner/docs')
Expand Down
28 changes: 2 additions & 26 deletions samples/system-test/spanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ describe('Spanner', () => {
output,
new RegExp(
`Database (.+) was restored to ${RESTORE_DATABASE_ID} from backup ` +
`(.+)${BACKUP_ID}`
`(.+)${BACKUP_ID} with version time (.+)`
)
);
});
Expand Down Expand Up @@ -951,31 +951,7 @@ describe('Spanner', () => {
`Created database ${VERSION_RETENTION_DATABASE_ID} with version retention period.`
)
);
});

// get_database_ddl
it('should get a database schema with a retention period set', async () => {
const output = execSync(
`${schemaCmd} getDatabaseSchemaWithVersionRetentionPeriod "${INSTANCE_ID}" "${VERSION_RETENTION_DATABASE_ID}" ${PROJECT_ID}`
);
assert.include(output, "version_retention_period = '1d'");
});

// update_version_retention_period
it('should update version retention period on database', async () => {
const output = execSync(
`${schemaCmd} updateDatabaseWithVersionRetentionPeriod "${INSTANCE_ID}" "${VERSION_RETENTION_DATABASE_ID}" ${PROJECT_ID}`
);
assert.include(output, 'Waiting for operation to complete...');
assert.include(output, 'Updated the version retention period.');
});

// get_database
it('should get a database with a retention period set', async () => {
const output = execSync(
`${schemaCmd} getDatabaseWithVersionRetentionPeriod "${INSTANCE_ID}" "${VERSION_RETENTION_DATABASE_ID}" ${PROJECT_ID}`
);
assert.include(output, 'Version retention period: 2d');
assert.include(output, 'Version retention period: 1d');
assert.include(output, 'Earliest version time:');
});
});

0 comments on commit 81b4585

Please sign in to comment.