| 178 | } |
| 179 | |
| 180 | async function addTimestampColumn(instanceId, databaseId, projectId) { |
| 181 | // [START spanner_add_timestamp_column] |
| 182 | // Imports the Google Cloud client library |
| 183 | const {Spanner} = require('@google-cloud/spanner'); |
| 184 | |
| 185 | /** |
| 186 | * TODO(developer): Uncomment the following lines before running the sample. |
| 187 | */ |
| 188 | // const projectId = 'my-project-id'; |
| 189 | // const instanceId = 'my-instance'; |
| 190 | // const databaseId = 'my-database'; |
| 191 | |
| 192 | // Creates a client |
| 193 | const spanner = new Spanner({ |
| 194 | projectId: projectId, |
| 195 | }); |
| 196 | |
| 197 | // Gets a reference to a Cloud Spanner Database Admin Client object |
| 198 | const databaseAdminClient = spanner.getDatabaseAdminClient(); |
| 199 | |
| 200 | const request = [ |
| 201 | `ALTER TABLE Albums ADD COLUMN LastUpdateTime TIMESTAMP OPTIONS |
| 202 | (allow_commit_timestamp=true)`, |
| 203 | ]; |
| 204 | |
| 205 | // Adds a new commit timestamp column to the Albums table |
| 206 | try { |
| 207 | const [operation] = await databaseAdminClient.updateDatabaseDdl({ |
| 208 | database: databaseAdminClient.databasePath( |
| 209 | projectId, |
| 210 | instanceId, |
| 211 | databaseId |
| 212 | ), |
| 213 | statements: request, |
| 214 | }); |
| 215 | |
| 216 | console.log('Waiting for operation to complete...'); |
| 217 | |
| 218 | await operation.promise(); |
| 219 | |
| 220 | console.log( |
| 221 | 'Added LastUpdateTime as a commit timestamp column in Albums table.' |
| 222 | ); |
| 223 | } catch (err) { |
| 224 | console.error('ERROR:', err); |
| 225 | } finally { |
| 226 | // Close the spanner client when finished. |
| 227 | // The databaseAdminClient does not require explicit closure. The closure of the Spanner client will automatically close the databaseAdminClient. |
| 228 | spanner.close(); |
| 229 | } |
| 230 | // [END spanner_add_timestamp_column] |
| 231 | } |
| 232 | |
| 233 | async function updateWithTimestamp(instanceId, databaseId, projectId) { |
| 234 | // [START spanner_update_data_with_timestamp_column] |