Skip to content

Commit

Permalink
fix(createTableAndIndexes): do not try to create table and indexes wh…
Browse files Browse the repository at this point in the history
…en already exists
  • Loading branch information
mipopon committed May 9, 2022
1 parent d31daed commit 7facb8d
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/repository.ts
Expand Up @@ -158,6 +158,21 @@ export class Repository<T> {
*/

private async createTable(): Promise<number> {
const selectTable = await this.queryService.querySingle<{
name: string;
}>(
[
`SELECT i.name`,
`FROM information_schema.user_tables AS i`,
`WHERE i.name = ? AND i.status = ?`,
].join(' '),
this.config.tableName,
'ACTIVE',
);
if (selectTable?.name === this.config.tableName) {
return 1;
}

const result = await this.queryService.execute(
`CREATE TABLE ${this.config.tableName}`,
);
Expand All @@ -172,7 +187,25 @@ export class Repository<T> {

private async createIndexes(indexFields: string[]) {
const results: Result[] = [];
for (const field of indexFields) {

const selectIndex = await this.queryService.query<{
expr: string;
indexId: string;
status: string;
}>(
[
`SELECT VALUE indexes`,
`FROM information_schema.user_tables AS i, i.indexes AS indexes`,
`WHERE i.name = ?`,
].join(' '),
);

const currentIndex = selectIndex.map(val => val.expr);
const newIndexFields = indexFields.filter(
field => currentIndex.indexOf(field) === -1,
);

for (const field of newIndexFields) {
try {
const result = await this.queryService.execute(
`CREATE INDEX ON ${this.config.tableName} (${field})`,
Expand Down

0 comments on commit 7facb8d

Please sign in to comment.