| 75 | } |
| 76 | |
| 77 | async function addColumn(instanceId, databaseId, projectId) { |
| 78 | // [START spanner_add_column] |
| 79 | |
| 80 | /** |
| 81 | * TODO(developer): Uncomment the following lines before running the sample. |
| 82 | */ |
| 83 | // const projectId = 'my-project-id'; |
| 84 | // const instanceId = 'my-instance'; |
| 85 | // const databaseId = 'my-database'; |
| 86 | |
| 87 | // Imports the Google Cloud client library |
| 88 | const {Spanner} = require('@google-cloud/spanner'); |
| 89 | |
| 90 | // creates a client |
| 91 | const spanner = new Spanner({ |
| 92 | projectId: projectId, |
| 93 | }); |
| 94 | |
| 95 | const databaseAdminClient = spanner.getDatabaseAdminClient(); |
| 96 | |
| 97 | // Creates a new index in the database |
| 98 | try { |
| 99 | const [operation] = await databaseAdminClient.updateDatabaseDdl({ |
| 100 | database: databaseAdminClient.databasePath( |
| 101 | projectId, |
| 102 | instanceId, |
| 103 | databaseId |
| 104 | ), |
| 105 | statements: ['ALTER TABLE Albums ADD COLUMN MarketingBudget INT64'], |
| 106 | }); |
| 107 | |
| 108 | console.log('Waiting for operation to complete...'); |
| 109 | await operation.promise(); |
| 110 | |
| 111 | console.log('Added the MarketingBudget column.'); |
| 112 | } catch (err) { |
| 113 | console.error('Failed to add column:', err.message || err); |
| 114 | } finally { |
| 115 | // Close the spanner client when finished. |
| 116 | // The databaseAdminClient does not require explicit closure. The closure of the Spanner client will automatically close the databaseAdminClient. |
| 117 | spanner.close(); |
| 118 | } |
| 119 | |
| 120 | // [END spanner_add_column] |
| 121 | } |
| 122 | |
| 123 | async function queryDataWithNewColumn(instanceId, databaseId, projectId) { |
| 124 | // [START spanner_query_data_with_new_column] |