@egi/smart-db 2.3.2 → 2.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/drivers/smart-db-better-sqlite3.d.ts +36 -0
- package/drivers/smart-db-better-sqlite3.js +1 -0
- package/drivers/smart-db-mysql.d.ts +26 -0
- package/drivers/smart-db-mysql.js +1 -0
- package/drivers/smart-db-mysql2.d.ts +26 -0
- package/drivers/smart-db-mysql2.js +1 -0
- package/drivers/smart-db-sqlite3.d.ts +30 -0
- package/drivers/smart-db-sqlite3.js +1 -0
- package/helpers/extract-db-api.d.ts +1 -0
- package/helpers/extract-db-api.js +1 -0
- package/models/abstract-model.d.ts +22 -0
- package/models/abstract-model.js +1 -0
- package/models/smart-db-core-table-model.d.ts +35 -0
- package/models/smart-db-core-table-model.js +1 -0
- package/models/smart-db-dictionary.d.ts +13 -0
- package/models/smart-db-dictionary.js +1 -0
- package/models/smart-db-log-model.d.ts +65 -0
- package/models/smart-db-log-model.js +1 -0
- package/models/smart-db-version-model.d.ts +65 -0
- package/models/smart-db-version-model.js +1 -0
- package/models/smart-db-version-view-model.d.ts +71 -0
- package/models/smart-db-version-view-model.js +1 -0
- package/models/sqlite-master-model.d.ts +36 -0
- package/models/sqlite-master-model.js +1 -0
- package/models/sqlite-sequence-model.d.ts +24 -0
- package/models/sqlite-sequence-model.js +1 -0
- package/package.json +1 -1
- package/{src/smart-db-api.ts → smart-db-api.d.ts} +0 -3
- package/smart-db-api.js +1 -0
- package/smart-db-globals.d.ts +22 -0
- package/smart-db-globals.js +1 -0
- package/{src/smart-db-interfaces.ts → smart-db-interfaces.d.ts} +34 -82
- package/smart-db-interfaces.js +1 -0
- package/smart-db-log.d.ts +58 -0
- package/smart-db-log.js +1 -0
- package/smart-db-sql-build-data.d.ts +9 -0
- package/smart-db-sql-build-data.js +1 -0
- package/smart-db-upgrade-manager.d.ts +13 -0
- package/smart-db-upgrade-manager.js +1 -0
- package/smart-db.d.ts +67 -0
- package/smart-db.js +1 -0
- package/.eslintrc.json +0 -212
- package/README.md +0 -2
- package/bin/copy-assets +0 -6
- package/src/drivers/smart-db-better-sqlite3.ts +0 -284
- package/src/drivers/smart-db-mysql.ts +0 -159
- package/src/drivers/smart-db-mysql2.ts +0 -159
- package/src/drivers/smart-db-sqlite3.ts +0 -198
- package/src/helpers/extract-db-api.ts +0 -465
- package/src/helpers/terser-tree.ts +0 -39
- package/src/models/abstract-model.ts +0 -209
- package/src/models/smart-db-core-table-model.ts +0 -161
- package/src/models/smart-db-dictionary.ts +0 -28
- package/src/models/smart-db-log-model.ts +0 -316
- package/src/models/smart-db-version-model.ts +0 -316
- package/src/models/smart-db-version-view-model.ts +0 -347
- package/src/models/sqlite-master-model.ts +0 -140
- package/src/models/sqlite-sequence-model.ts +0 -91
- package/src/smart-db-globals.ts +0 -136
- package/src/smart-db-log.ts +0 -262
- package/src/smart-db-sql-build-data.ts +0 -28
- package/src/smart-db-upgrade-manager.ts +0 -171
- package/src/smart-db.ts +0 -854
- package/test/data/sql-engine-tests.json +0 -232
- package/test/db/mysql/database-init.sql +0 -11
- package/test/db/sqlite3/database-init.sql +0 -11
- package/test/exer.js +0 -28
- package/test/model/smart-db-dictionary.ts +0 -19
- package/test/model/test-table-model.ts +0 -214
- package/test/test.js +0 -273
- package/test/test2.js +0 -252
- package/tsconfig.json +0 -32
- package/tsconfig.pro.json +0 -32
- package/tsconfig.test-model.json +0 -23
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import * as fs from "fs";
|
|
2
|
-
import _ from "lodash";
|
|
3
|
-
import {SmartDbVersionViewModel} from "./models/smart-db-version-view-model";
|
|
4
|
-
import {SmartDb} from "./smart-db";
|
|
5
|
-
import {SmartDbOptions} from "./smart-db-interfaces";
|
|
6
|
-
import {SmartDbLog, SmartDbLogLocation} from "./smart-db-log";
|
|
7
|
-
|
|
8
|
-
export class SmartDbUpgradeManager {
|
|
9
|
-
private db: SmartDb;
|
|
10
|
-
private log: SmartDbLog;
|
|
11
|
-
|
|
12
|
-
constructor(db: SmartDb) {
|
|
13
|
-
this.db = db;
|
|
14
|
-
this.log = db.getLogger();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public hasDatabaseModule(module: string): Promise<boolean> {
|
|
18
|
-
return new Promise<boolean>((resolve, reject) => {
|
|
19
|
-
this.db.exists(SmartDbVersionViewModel).then((exists) => {
|
|
20
|
-
if (exists) {
|
|
21
|
-
this.db.getFirst(SmartDbVersionViewModel, {
|
|
22
|
-
module: module
|
|
23
|
-
}).then((row) => {
|
|
24
|
-
resolve(!!row);
|
|
25
|
-
}).catch((err) => {
|
|
26
|
-
reject(err);
|
|
27
|
-
});
|
|
28
|
-
} else {
|
|
29
|
-
resolve(false);
|
|
30
|
-
}
|
|
31
|
-
}).catch((err) => {
|
|
32
|
-
reject(err);
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public prepareDatabase(options: SmartDbOptions): Promise<SmartDbVersionViewModel> {
|
|
38
|
-
return new Promise<SmartDbVersionViewModel>((resolve, reject) => {
|
|
39
|
-
try {
|
|
40
|
-
const dirStat = fs.existsSync(options.sqlFilesDirectory) && fs.statSync(options.sqlFilesDirectory);
|
|
41
|
-
if (dirStat && dirStat.isDirectory()) {
|
|
42
|
-
const initFile = options.sqlFilesDirectory + `/${options.module}-init.sql`;
|
|
43
|
-
if (fs.existsSync(initFile)) {
|
|
44
|
-
this.hasDatabaseModule(options.module).then((hasDatabase) => {
|
|
45
|
-
if (hasDatabase) {
|
|
46
|
-
this.log.logInfo(SmartDbLogLocation.UpgradeManager, "prepareDatabase", `database module '${options.module}' exists`);
|
|
47
|
-
this.upgradeDatabase(options).then((finalVersionRow) => {
|
|
48
|
-
this.log.logInfo(SmartDbLogLocation.UpgradeManager, "prepareDatabase", `connected to module '${options.module}' version ${finalVersionRow.versionString}`);
|
|
49
|
-
resolve(finalVersionRow);
|
|
50
|
-
}).catch((err) => {
|
|
51
|
-
reject(err);
|
|
52
|
-
});
|
|
53
|
-
} else {
|
|
54
|
-
this.log.setDbLogging(false);
|
|
55
|
-
this.log.logInfo(SmartDbLogLocation.UpgradeManager, "prepareDatabase", `database '${options.module}' doesn't exists - begin creation`);
|
|
56
|
-
this.executeSqlScript(initFile).then(() => {
|
|
57
|
-
this.log.setDbLogging(true);
|
|
58
|
-
this.log.logInfo(SmartDbLogLocation.UpgradeManager, "prepareDatabase", `database module creation complete`);
|
|
59
|
-
this.hasDatabaseModule(options.module).then((hasDatabase) => {
|
|
60
|
-
if (hasDatabase) {
|
|
61
|
-
this.log.logInfo(SmartDbLogLocation.UpgradeManager, "prepareDatabase", `successfully initialized database for new module ${options.module}`);
|
|
62
|
-
this.upgradeDatabase(options).then((finalVersionRow) => {
|
|
63
|
-
this.log.logInfo(SmartDbLogLocation.UpgradeManager, "prepareDatabase", `connected to module '${options.module}' version ${finalVersionRow.versionString}`);
|
|
64
|
-
resolve(finalVersionRow);
|
|
65
|
-
}).catch((err) => {
|
|
66
|
-
reject(err);
|
|
67
|
-
});
|
|
68
|
-
} else {
|
|
69
|
-
reject("error running database initialization script");
|
|
70
|
-
}
|
|
71
|
-
}).catch((err) => {
|
|
72
|
-
reject(err);
|
|
73
|
-
});
|
|
74
|
-
}).catch((err) => {
|
|
75
|
-
reject(err);
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
}).catch((err) => {
|
|
79
|
-
reject(err);
|
|
80
|
-
});
|
|
81
|
-
} else {
|
|
82
|
-
reject(`missing mandatory '${options.module}-init.sql' file in ${initFile}`);
|
|
83
|
-
}
|
|
84
|
-
} else {
|
|
85
|
-
reject(`option 'sqlFilesDirectory' (${options.sqlFilesDirectory}) must point to an existing directory (absolute or relative to ${fs.realpathSync(".")})`);
|
|
86
|
-
}
|
|
87
|
-
} catch (err) {
|
|
88
|
-
reject(err);
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
private executeSqlScript(script: string): Promise<void> {
|
|
94
|
-
const sqlData = fs.readFileSync(script, "utf8");
|
|
95
|
-
return this.db.exec(sqlData);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
private upgradeDatabase(options: SmartDbOptions): Promise<SmartDbVersionViewModel> {
|
|
99
|
-
return new Promise<SmartDbVersionViewModel>((resolve, reject) => {
|
|
100
|
-
this.db.getFirst(SmartDbVersionViewModel, {
|
|
101
|
-
module: options.module
|
|
102
|
-
}).then((versionRow) => {
|
|
103
|
-
if (versionRow) {
|
|
104
|
-
fs.readdir(options.sqlFilesDirectory, (err, files) => {
|
|
105
|
-
if (err) {
|
|
106
|
-
reject(`unable to read database files from ${options.sqlFilesDirectory}`);
|
|
107
|
-
} else {
|
|
108
|
-
let upgradeScripts: string[] = [];
|
|
109
|
-
files.forEach((file) => {
|
|
110
|
-
const regExp = new RegExp(`^${options.module}-update.([0-9]{3})\\..*\\.sql$`);
|
|
111
|
-
const match = file.match(regExp);
|
|
112
|
-
if (match) {
|
|
113
|
-
const sequence = parseInt(match[1], 10);
|
|
114
|
-
if (sequence > versionRow.sequence) {
|
|
115
|
-
upgradeScripts.push(file);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
upgradeScripts = _.sortBy(upgradeScripts);
|
|
121
|
-
|
|
122
|
-
this.executeScriptSequentially(upgradeScripts, options).then(() => {
|
|
123
|
-
this.db.getFirst(SmartDbVersionViewModel, {module: options.module}, "*", "sequence DESC").then((finalVersionRow) => {
|
|
124
|
-
if (finalVersionRow) {
|
|
125
|
-
resolve(SmartDbVersionViewModel.from(finalVersionRow));
|
|
126
|
-
} else {
|
|
127
|
-
reject("unable to determine final smart db version");
|
|
128
|
-
}
|
|
129
|
-
}).catch((err) => {
|
|
130
|
-
reject(err);
|
|
131
|
-
});
|
|
132
|
-
}).catch((err) => {
|
|
133
|
-
reject(err);
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
} else {
|
|
138
|
-
reject(`missing version entry for module ${options.module} - add the mandatory insert into smart_db_version statement to your database creation script!`);
|
|
139
|
-
}
|
|
140
|
-
}).catch((err) => {
|
|
141
|
-
reject(err);
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
private executeScriptSequentially(scripts: string[], options: SmartDbOptions): Promise<boolean> {
|
|
147
|
-
return new Promise<boolean>(async (resolve, reject) => {
|
|
148
|
-
if (scripts && scripts.length > 0) {
|
|
149
|
-
this.log.logInfo(SmartDbLogLocation.UpgradeManager, "upgradeDatabase", `execute update script '${scripts[0]}' for module ${options.module}`);
|
|
150
|
-
await this.executeSqlScript(`${options.sqlFilesDirectory}/${scripts[0]}`).then(() => {
|
|
151
|
-
this.log.logInfo(SmartDbLogLocation.UpgradeManager, "upgradeDatabase", `successfully executed update script '${scripts[0]}' for module ${options.module}`);
|
|
152
|
-
if (scripts.length == 1) {
|
|
153
|
-
resolve(true);
|
|
154
|
-
}
|
|
155
|
-
}).catch((err) => {
|
|
156
|
-
reject(err);
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
if (scripts.length > 1) {
|
|
160
|
-
this.executeScriptSequentially(scripts.splice(1), options).then((status) => {
|
|
161
|
-
resolve(status);
|
|
162
|
-
}).catch((err) => {
|
|
163
|
-
reject(err);
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
} else {
|
|
167
|
-
resolve(true);
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
}
|