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

Fix: drop table statement #2036

Merged
merged 41 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
c62584c
chore: integration test fix
surbhigarg92 Jan 11, 2024
e7d385b
Merge branch 'googleapis:main' into main
surbhigarg92 Mar 4, 2024
e57b897
Merge branch 'googleapis:main' into main
surbhigarg92 Mar 4, 2024
c0c935e
Merge branch 'googleapis:main' into main
surbhigarg92 Mar 8, 2024
8875f2c
Merge branch 'googleapis:main' into main
surbhigarg92 Mar 26, 2024
9069624
fix: drop table
surbhigarg92 Apr 12, 2024
8b09c11
fix(deps): update dependency google-gax to v4.3.2 (#2026)
renovate-bot Mar 29, 2024
b8d63e4
feat(spanner): adding `EXPECTED_FULFILLMENT_PERIOD` to the indicate i…
gcf-owl-bot[bot] Apr 4, 2024
b6efb93
feat: optimisticLock option for getTransaction method (#2028)
alkatrivedi Apr 17, 2024
2587ab7
chore(main): release 7.7.0 (#2027)
release-please[bot] Apr 22, 2024
9993255
fix: drop table statement
alkatrivedi Apr 29, 2024
8697f09
Merge branch 'main' into drop-table-fix
alkatrivedi Apr 29, 2024
aa036f9
refactor: delete method for drop statement
alkatrivedi May 2, 2024
3f9e8eb
Modified delete method to use async await
surbhigarg92 May 3, 2024
d17e691
Merge branch 'googleapis:main' into drop-table-fix
alkatrivedi May 6, 2024
75e804c
fix: lint errors
alkatrivedi May 6, 2024
e09a6a0
test: unit test for delete method
alkatrivedi May 9, 2024
82206ee
fix: lint errors
alkatrivedi May 9, 2024
1ad935f
refactor: unit test for drop table
alkatrivedi May 9, 2024
41ac10e
refactor: remove unwanted commented lines
alkatrivedi May 9, 2024
c48bec4
fix: lint errors
alkatrivedi May 9, 2024
b88ca46
refactor: remove unwanted logs and refactor docs of the method
alkatrivedi May 9, 2024
35a97a8
refactor
alkatrivedi May 9, 2024
9fc73b3
remove sample file
alkatrivedi May 9, 2024
e7233e5
refactor: unit test for delete method
alkatrivedi May 9, 2024
b1b3fcd
fix: lint error
alkatrivedi May 10, 2024
deea952
Merge branch 'main' into drop-table-fix
alkatrivedi May 10, 2024
259b280
refactor: remove unused lines
alkatrivedi May 13, 2024
a276724
refactor: remove unused lines
alkatrivedi May 13, 2024
04c9af4
refactor: remove unused lines
alkatrivedi May 13, 2024
a6695ff
fix: lint errors
alkatrivedi May 13, 2024
e556994
refactor: remove sample file
alkatrivedi May 13, 2024
0f1b304
refactor
alkatrivedi May 13, 2024
5d9251c
fix: lint errors
alkatrivedi May 13, 2024
68d2c7b
fix: lint errors
alkatrivedi May 13, 2024
7b8d8f2
refactor
alkatrivedi May 14, 2024
84a74a1
fix: lint errors
alkatrivedi May 14, 2024
c260858
refactor
alkatrivedi May 15, 2024
87ae182
refactor
alkatrivedi May 15, 2024
4d5c455
refactor
alkatrivedi May 15, 2024
daf810b
Merge branch 'main' into drop-table-fix
alkatrivedi May 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ system-test/*key.json
.DS_Store
package-lock.json
__pycache__
.vscode
73 changes: 72 additions & 1 deletion src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@
): SessionPoolInterface;
}

export type GetDatabaseDialectCallback = NormalCallback<
EnumKey<typeof google.spanner.admin.database.v1.DatabaseDialect>
>;

export interface SetIamPolicyRequest {
policy: Policy | null;
updateMask?: FieldMask | null;
Expand Down Expand Up @@ -166,7 +170,15 @@
typeof databaseAdmin.spanner.admin.database.v1.Database.State
>,
'restoreInfo'
> & {restoreInfo?: IRestoreInfoTranslatedEnum | null};
> &
Omit<
TranslateEnumKeys<
databaseAdmin.spanner.admin.database.v1.IDatabase,
'databaseDialect',
typeof databaseAdmin.spanner.admin.database.v1.DatabaseDialect
>,
'restoreInfo'
> & {restoreInfo?: IRestoreInfoTranslatedEnum | null};

/**
* IRestoreInfo structure with restore source type enum translated to string form.
Expand Down Expand Up @@ -312,6 +324,9 @@
resourceHeader_: {[k: string]: string};
request: DatabaseRequest;
databaseRole?: string | null;
databaseDialect?: EnumKey<
typeof databaseAdmin.spanner.admin.database.v1.DatabaseDialect
> | null;
constructor(
instance: Instance,
name: string,
Expand Down Expand Up @@ -1476,6 +1491,61 @@
return metadata.state || undefined;
}

/**
* Retrieves the dialect of the database
*
* @see {@link #getMetadata}
*
* @method Database#getDatabaseDialect
*
* @param {object} [gaxOptions] Request configuration options,
* See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
* for more details.
* @param {GetDatabaseDialectCallback} [callback] Callback function.
* @returns {Promise<EnumKey<typeof, databaseAdmin.spanner.admin.database.v1.DatabaseDialect> | undefined>}
* When resolved, contains the database dialect of the database if the dialect is defined.
* @example
* const {Spanner} = require('@google-cloud/spanner');
* const spanner = new Spanner();
* const instance = spanner.instance('my-instance');
* const database = instance.database('my-database');
* const dialect = await database.getDatabaseDialect();
* const isGoogleSQL = (dialect === 'GOOGLE_STANDARD_SQL');
* const isPostgreSQL = (dialect === 'POSTGRESQL');
*/

alkatrivedi marked this conversation as resolved.
Show resolved Hide resolved
getDatabaseDialect(
options?: CallOptions
): Promise<
| EnumKey<typeof databaseAdmin.spanner.admin.database.v1.DatabaseDialect>
| undefined
>;
getDatabaseDialect(callback: GetDatabaseDialectCallback): void;
getDatabaseDialect(
options: CallOptions,
callback: GetDatabaseDialectCallback
): void;
async getDatabaseDialect(
optionsOrCallback?: CallOptions | GetDatabaseDialectCallback,
cb?: GetDatabaseDialectCallback

Check warning on line 1530 in src/database.ts

View workflow job for this annotation

GitHub Actions / lint

'cb' is defined but never used
): Promise<
| EnumKey<typeof databaseAdmin.spanner.admin.database.v1.DatabaseDialect>
| undefined
> {
const gaxOptions =
typeof optionsOrCallback === 'object' ? optionsOrCallback : {};

if (
alkatrivedi marked this conversation as resolved.
Show resolved Hide resolved
this.databaseDialect === 'DATABASE_DIALECT_UNSPECIFIED' ||
this.databaseDialect === null ||
this.databaseDialect === undefined
) {
const [metadata] = await this.getMetadata(gaxOptions);
this.databaseDialect = metadata.databaseDialect;
}
return this.databaseDialect || undefined;
}

/**
* @typedef {array} GetSchemaResponse
* @property {string[]} 0 An array of database DDL statements.
Expand Down Expand Up @@ -3429,6 +3499,7 @@
'batchTransaction',
'getRestoreInfo',
'getState',
'getDatabaseDialect',
'getOperations',
'runTransaction',
'runTransactionAsync',
Expand Down
38 changes: 33 additions & 5 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export type UpsertRowsCallback = CommitCallback;
export type UpsertRowsResponse = CommitResponse;
export type UpsertRowsOptions = MutateRowsOptions;

const GOOGLE_STANDARD_SQL = 'GOOGLE_STANDARD_SQL';
const POSTGRESQL = 'POSTGRESQL';
/**
* Create a Table object to interact with a table in a Cloud Spanner
* database.
Expand Down Expand Up @@ -342,11 +344,37 @@ class Table {
const callback =
typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb!;

return this.database.updateSchema(
'DROP TABLE `' + this.name + '`',
gaxOptions,
callback!
);
alkatrivedi marked this conversation as resolved.
Show resolved Hide resolved
const [schema, table] = this.name.includes('.')
? this.name.split('.')
: [null, this.name];

let dropStatement = 'DROP TABLE `' + table + '`';

const performDelete = async (): Promise<void | DropTableResponse> => {
const result = await this.database.getDatabaseDialect(gaxOptions);

if (result && result.includes(POSTGRESQL)) {
dropStatement = schema
? `DROP TABLE "${schema}"."${table}"`
: `DROP TABLE "${table}"`;
} else if (result && result.includes(GOOGLE_STANDARD_SQL)) {
dropStatement = schema
? 'DROP TABLE `' + schema + '`.`' + table + '`'
: dropStatement;
}

const updateSchemaResult = callback
? this.database.updateSchema(dropStatement, gaxOptions, callback)
: this.database.updateSchema(dropStatement, gaxOptions);

return updateSchemaResult as Promise<DropTableResponse | void>;
};

if (!callback) {
return performDelete() as Promise<DropTableResponse>;
} else {
performDelete();
}
}
/**
* @typedef {array} DeleteRowsResponse
Expand Down
30 changes: 30 additions & 0 deletions test/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const fakePfy = extend({}, pfy, {
'batchTransaction',
'getRestoreInfo',
'getState',
'getDatabaseDialect',
'getOperations',
'runTransaction',
'runTransactionAsync',
Expand Down Expand Up @@ -2795,6 +2796,35 @@ describe('Database', () => {
});
});

describe('getDatabaseDialect', () => {
it('should get database dialect from database metadata', async () => {
database.getMetadata = async () => [
{databaseDialect: 'GOOGLE_STANDARD_SQL'},
];
const result = await database.getDatabaseDialect();
assert.strictEqual(result, 'GOOGLE_STANDARD_SQL');
});

it('should accept and pass gaxOptions to getMetadata', async () => {
const options = {};
database.getMetadata = async gaxOptions => {
assert.strictEqual(gaxOptions, options);
return [{}];
};
await database.getDatabaseDialect(options);
});

it('should accept callback and return database dialect', done => {
const databaseDialect = 'GOOGLE_STANDARD_SQL';
database.getMetadata = async () => [{databaseDialect}];
database.getDatabaseDialect((err, result) => {
assert.ifError(err);
assert.strictEqual(result, databaseDialect);
done();
});
});
});

describe('getRestoreInfo', () => {
it('should get restore info from database metadata', async () => {
const restoreInfo = {sourceType: 'BACKUP'};
Expand Down
113 changes: 107 additions & 6 deletions test/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe('Table', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let TableCached: any;
let table;
let tableWithSchema;
let transaction: FakeTransaction;

const DATABASE = {
Expand All @@ -75,6 +76,7 @@ describe('Table', () => {
};

const NAME = 'table-name';
const NAMEWITHSCHEMA = 'schema.' + NAME;

before(() => {
Table = proxyquire('../src/table.js', {
Expand All @@ -86,6 +88,7 @@ describe('Table', () => {
beforeEach(() => {
extend(Table, TableCached);
table = new Table(DATABASE, NAME);
tableWithSchema = new Table(DATABASE, NAMEWITHSCHEMA);
transaction = new FakeTransaction();
});

Expand Down Expand Up @@ -209,26 +212,124 @@ describe('Table', () => {
});

describe('delete', () => {
it('should update the schema on the database', () => {
const updateSchemaReturnValue = {};
it('should update the schema on the database for GoogleSQL using await', async () => {
table.database = {
getDatabaseDialect: () => {
return 'GOOGLE_STANDARD_SQL';
},
updateSchema: schema => {
assert.strictEqual(schema, 'DROP TABLE `table-name`');
},
};

await table.delete();
});

it('should update the schema on the database for GoogleSQL using callbacks', () => {
function callback() {}
table.database = {
getDatabaseDialect: () => {
return 'GOOGLE_STANDARD_SQL';
},
updateSchema: (schema, gaxOptions, callback_) => {
assert.strictEqual(schema, 'DROP TABLE `table-name`');
assert.strictEqual(callback_, callback);
},
};
table.delete(callback);
});

it('should update the schema on the database for GoogleSQL with schema in the table name using await', async () => {
tableWithSchema.database = {
getDatabaseDialect: () => {
return 'GOOGLE_STANDARD_SQL';
},
updateSchema: schema => {
assert.strictEqual(schema, 'DROP TABLE `schema`.`table-name`');
},
};

await tableWithSchema.delete();
});

it('should update the schema on the database for GoogleSQL with schema in the table name using callbacks', () => {
function callback() {}
tableWithSchema.database = {
getDatabaseDialect: () => {
return 'GOOGLE_STANDARD_SQL';
},
updateSchema: (schema, gaxOptions, callback_) => {
assert.strictEqual(schema, 'DROP TABLE `schema`.`table-name`');
assert.strictEqual(callback_, callback);
},
};
tableWithSchema.delete(callback);
});

it('should update the schema on the database for PostgresSQL using await', async () => {
table.database = {
getDatabaseDialect: () => {
return 'POSTGRESQL';
},
updateSchema: schema => {
assert.strictEqual(schema, `DROP TABLE "${table.name}"`);
},
};

await table.delete();
});

it('should update the schema on the database for PostgresSQL using callbacks', () => {
function callback() {}

table.database = {
getDatabaseDialect: () => {
return 'POSTGRESQL';
},
updateSchema: (schema, gaxOptions, callback_) => {
assert.strictEqual(schema, 'DROP TABLE `' + table.name + '`');
assert.strictEqual(schema, 'DROP TABLE "table-name"');
assert.strictEqual(callback_, callback);
return updateSchemaReturnValue;
},
};

const returnValue = table.delete(callback);
assert.strictEqual(returnValue, updateSchemaReturnValue);
table.delete(callback);
});

it('should update the schema on the database for PostgresSQL with schema in the table name using await', async () => {
tableWithSchema.database = {
getDatabaseDialect: () => {
return 'POSTGRESQL';
},
updateSchema: schema => {
assert.strictEqual(schema, `DROP TABLE "schema"."${table.name}"`);
},
};

await tableWithSchema.delete();
});

it('should update the schema on the database for PostgresSQL with schema in the table name using callbacks', () => {
function callback() {}
tableWithSchema.database = {
getDatabaseDialect: () => {
return 'POSTGRESQL';
},
updateSchema: (schema, gaxOptions, callback_) => {
assert.strictEqual(schema, `DROP TABLE "schema"."${table.name}"`);
assert.strictEqual(callback_, callback);
},
};

tableWithSchema.delete(callback);
});

it('should accept and pass gaxOptions to updateSchema', done => {
const gaxOptions = {};
table.database = {
getDatabaseDialect: gaxOptionsFromTable => {
assert.strictEqual(gaxOptionsFromTable, gaxOptions);
return 'GOOGLE_STANDARD_SQL';
},
updateSchema: (schema, gaxOptionsFromTable) => {
assert.strictEqual(gaxOptionsFromTable, gaxOptions);
done();
Expand Down
Loading