anote-server-libs 0.9.3 → 0.9.5
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/models/ApiCall.ts +40 -40
- package/models/Migration.ts +43 -43
- package/models/repository/BaseModelRepository.js +23 -23
- package/models/repository/BaseModelRepository.ts +168 -168
- package/models/repository/MemoryCache.ts +87 -87
- package/models/repository/ModelDao.ts +268 -268
- package/package.json +35 -35
- package/services/WithBody.ts +65 -65
- package/services/WithTransaction.js +3 -1
- package/services/WithTransaction.ts +161 -159
- package/services/utils.js +16 -16
- package/services/utils.ts +180 -180
- package/tsconfig.json +29 -29
- package/.vscode/settings.json +0 -3
package/models/ApiCall.ts
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
import { ConnectionPool, DateTimeOffset, Request } from 'mssql';
|
|
2
|
-
import {Pool} from 'pg';
|
|
3
|
-
import {Logger} from 'winston';
|
|
4
|
-
import {Model, ModelDao} from './repository/ModelDao';
|
|
5
|
-
|
|
6
|
-
export interface ApiCall extends Model<string> {
|
|
7
|
-
responseCode: number;
|
|
8
|
-
responseJson: string;
|
|
9
|
-
expiresAt: Date;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export class ApiCallRepository extends ModelDao<string, ApiCall> {
|
|
13
|
-
constructor(protected pool: Pool, protected poolMssql: ConnectionPool, protected logger: Logger) {
|
|
14
|
-
super(pool, poolMssql, logger, 'api_call', 5, pool ? 'id=$1,"updatedOn"=$2,"responseCode"=$3,"responseJson"=$4,"expiresAt"=$5' :
|
|
15
|
-
'id=@1,"updatedOn"=@2,"responseCode"=@3,"responseJson"=@4,"expiresAt"=@5');
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
buildObject(q: any): ApiCall {
|
|
19
|
-
if(!q) return undefined;
|
|
20
|
-
q.id = q.id.trim();
|
|
21
|
-
q.updatedOn = q.updatedOn && new Date(q.updatedOn);
|
|
22
|
-
q.responseCode = parseInt(q.responseCode, 10);
|
|
23
|
-
q.expiresAt = q.expiresAt && new Date(q.expiresAt);
|
|
24
|
-
return q;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
serialize(instance: ApiCall, request?: Request) {
|
|
28
|
-
if(request) {
|
|
29
|
-
request.input('1', instance.id);
|
|
30
|
-
request.input('2', DateTimeOffset, instance.updatedOn);
|
|
31
|
-
request.input('3', instance.responseCode);
|
|
32
|
-
request.input('4', instance.responseJson);
|
|
33
|
-
request.input('5', DateTimeOffset, instance.expiresAt);
|
|
34
|
-
return undefined;
|
|
35
|
-
} else {
|
|
36
|
-
return [instance.id, instance.updatedOn, instance.responseCode, instance.responseJson, instance.expiresAt];
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
}
|
|
1
|
+
import { ConnectionPool, DateTimeOffset, Request } from 'mssql';
|
|
2
|
+
import {Pool} from 'pg';
|
|
3
|
+
import {Logger} from 'winston';
|
|
4
|
+
import {Model, ModelDao} from './repository/ModelDao';
|
|
5
|
+
|
|
6
|
+
export interface ApiCall extends Model<string> {
|
|
7
|
+
responseCode: number;
|
|
8
|
+
responseJson: string;
|
|
9
|
+
expiresAt: Date;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class ApiCallRepository extends ModelDao<string, ApiCall> {
|
|
13
|
+
constructor(protected pool: Pool, protected poolMssql: ConnectionPool, protected logger: Logger) {
|
|
14
|
+
super(pool, poolMssql, logger, 'api_call', 5, pool ? 'id=$1,"updatedOn"=$2,"responseCode"=$3,"responseJson"=$4,"expiresAt"=$5' :
|
|
15
|
+
'id=@1,"updatedOn"=@2,"responseCode"=@3,"responseJson"=@4,"expiresAt"=@5');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
buildObject(q: any): ApiCall {
|
|
19
|
+
if(!q) return undefined;
|
|
20
|
+
q.id = q.id.trim();
|
|
21
|
+
q.updatedOn = q.updatedOn && new Date(q.updatedOn);
|
|
22
|
+
q.responseCode = parseInt(q.responseCode, 10);
|
|
23
|
+
q.expiresAt = q.expiresAt && new Date(q.expiresAt);
|
|
24
|
+
return q;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
serialize(instance: ApiCall, request?: Request) {
|
|
28
|
+
if(request) {
|
|
29
|
+
request.input('1', instance.id);
|
|
30
|
+
request.input('2', DateTimeOffset, instance.updatedOn);
|
|
31
|
+
request.input('3', instance.responseCode);
|
|
32
|
+
request.input('4', instance.responseJson);
|
|
33
|
+
request.input('5', DateTimeOffset, instance.expiresAt);
|
|
34
|
+
return undefined;
|
|
35
|
+
} else {
|
|
36
|
+
return [instance.id, instance.updatedOn, instance.responseCode, instance.responseJson, instance.expiresAt];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
}
|
package/models/Migration.ts
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
import { ConnectionPool, Request } from 'mssql';
|
|
2
|
-
import {Pool} from 'pg';
|
|
3
|
-
import {Logger} from 'winston';
|
|
4
|
-
import {ModelDao} from './repository/ModelDao';
|
|
5
|
-
|
|
6
|
-
export interface Migration {
|
|
7
|
-
id: number;
|
|
8
|
-
hash: string;
|
|
9
|
-
sqlUp: string;
|
|
10
|
-
sqlDown: string;
|
|
11
|
-
state: number;
|
|
12
|
-
skip: boolean;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export class MigrationRepository extends ModelDao<number, Migration> {
|
|
16
|
-
constructor(protected pool: Pool, protected poolMssql: ConnectionPool, protected logger: Logger) {
|
|
17
|
-
super(pool, poolMssql, logger, 'migration', 6, pool ? 'id=$1,hash=$2,"sqlUp"=$3,"sqlDown"=$4,state=$5,skip=$6' :
|
|
18
|
-
'id=@1,hash=@2,"sqlUp"=@3,"sqlDown"=@4,state=@5,skip=@6');
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
buildObject(q: any): Migration {
|
|
22
|
-
if(!q) return undefined;
|
|
23
|
-
q.id = parseInt(q.id, 10);
|
|
24
|
-
q.hash = q.hash.trim();
|
|
25
|
-
q.state = parseInt(q.state, 10);
|
|
26
|
-
q.skip = q.skip ? true : false;
|
|
27
|
-
return q;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
serialize(instance: Migration, request?: Request) {
|
|
31
|
-
if(request) {
|
|
32
|
-
request.input('1', instance.id);
|
|
33
|
-
request.input('2', instance.hash);
|
|
34
|
-
request.input('3', instance.sqlUp);
|
|
35
|
-
request.input('4', instance.sqlDown);
|
|
36
|
-
request.input('5', instance.state);
|
|
37
|
-
request.input('6', instance.skip ? 1 : 0);
|
|
38
|
-
return undefined;
|
|
39
|
-
} else {
|
|
40
|
-
return [instance.id, instance.hash, instance.sqlUp, instance.sqlDown, instance.state, instance.skip];
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
1
|
+
import { ConnectionPool, Request } from 'mssql';
|
|
2
|
+
import {Pool} from 'pg';
|
|
3
|
+
import {Logger} from 'winston';
|
|
4
|
+
import {ModelDao} from './repository/ModelDao';
|
|
5
|
+
|
|
6
|
+
export interface Migration {
|
|
7
|
+
id: number;
|
|
8
|
+
hash: string;
|
|
9
|
+
sqlUp: string;
|
|
10
|
+
sqlDown: string;
|
|
11
|
+
state: number;
|
|
12
|
+
skip: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class MigrationRepository extends ModelDao<number, Migration> {
|
|
16
|
+
constructor(protected pool: Pool, protected poolMssql: ConnectionPool, protected logger: Logger) {
|
|
17
|
+
super(pool, poolMssql, logger, 'migration', 6, pool ? 'id=$1,hash=$2,"sqlUp"=$3,"sqlDown"=$4,state=$5,skip=$6' :
|
|
18
|
+
'id=@1,hash=@2,"sqlUp"=@3,"sqlDown"=@4,state=@5,skip=@6');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
buildObject(q: any): Migration {
|
|
22
|
+
if(!q) return undefined;
|
|
23
|
+
q.id = parseInt(q.id, 10);
|
|
24
|
+
q.hash = q.hash.trim();
|
|
25
|
+
q.state = parseInt(q.state, 10);
|
|
26
|
+
q.skip = q.skip ? true : false;
|
|
27
|
+
return q;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
serialize(instance: Migration, request?: Request) {
|
|
31
|
+
if(request) {
|
|
32
|
+
request.input('1', instance.id);
|
|
33
|
+
request.input('2', instance.hash);
|
|
34
|
+
request.input('3', instance.sqlUp);
|
|
35
|
+
request.input('4', instance.sqlDown);
|
|
36
|
+
request.input('5', instance.state);
|
|
37
|
+
request.input('6', instance.skip ? 1 : 0);
|
|
38
|
+
return undefined;
|
|
39
|
+
} else {
|
|
40
|
+
return [instance.id, instance.hash, instance.sqlUp, instance.sqlDown, instance.state, instance.skip];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -25,32 +25,32 @@ class BaseModelRepository {
|
|
|
25
25
|
}
|
|
26
26
|
migrate(migrationsPath, callback, onlyAboveOrEquals = 0) {
|
|
27
27
|
(this.db ? Promise.all([
|
|
28
|
-
this.db.query(`CREATE TABLE IF NOT EXISTS migration (
|
|
29
|
-
id integer PRIMARY KEY,
|
|
30
|
-
hash text NOT NULL,
|
|
31
|
-
"sqlUp" text NOT NULL,
|
|
32
|
-
"sqlDown" text NOT NULL,
|
|
33
|
-
state integer NOT NULL,
|
|
34
|
-
skip boolean NOT NULL
|
|
28
|
+
this.db.query(`CREATE TABLE IF NOT EXISTS migration (
|
|
29
|
+
id integer PRIMARY KEY,
|
|
30
|
+
hash text NOT NULL,
|
|
31
|
+
"sqlUp" text NOT NULL,
|
|
32
|
+
"sqlDown" text NOT NULL,
|
|
33
|
+
state integer NOT NULL,
|
|
34
|
+
skip boolean NOT NULL
|
|
35
35
|
)`),
|
|
36
36
|
this.db.query(`ALTER TABLE migration ADD COLUMN IF NOT EXISTS skip BOOLEAN NOT NULL DEFAULT FALSE`)
|
|
37
37
|
]) : Promise.all([
|
|
38
|
-
this.dbMssql.query(`IF NOT EXISTS (SELECT * FROM sysobjects WHERE name=\'migration\' AND xtype=\'U\') CREATE TABLE migration (
|
|
39
|
-
id int PRIMARY KEY,
|
|
40
|
-
hash text NOT NULL,
|
|
41
|
-
"sqlUp" text NOT NULL,
|
|
42
|
-
"sqlDown" text NOT NULL,
|
|
43
|
-
state int NOT NULL,
|
|
44
|
-
skip bit NOT NULL
|
|
38
|
+
this.dbMssql.query(`IF NOT EXISTS (SELECT * FROM sysobjects WHERE name=\'migration\' AND xtype=\'U\') CREATE TABLE migration (
|
|
39
|
+
id int PRIMARY KEY,
|
|
40
|
+
hash text NOT NULL,
|
|
41
|
+
"sqlUp" text NOT NULL,
|
|
42
|
+
"sqlDown" text NOT NULL,
|
|
43
|
+
state int NOT NULL,
|
|
44
|
+
skip bit NOT NULL
|
|
45
45
|
)`),
|
|
46
|
-
this.dbMssql.query(`IF NOT EXISTS (
|
|
47
|
-
SELECT *
|
|
48
|
-
FROM INFORMATION_SCHEMA.COLUMNS
|
|
49
|
-
WHERE TABLE_NAME = 'migration'
|
|
50
|
-
AND COLUMN_NAME = 'skip'
|
|
51
|
-
)
|
|
52
|
-
BEGIN
|
|
53
|
-
ALTER TABLE migration ADD skip BIT NOT NULL DEFAULT 0;
|
|
46
|
+
this.dbMssql.query(`IF NOT EXISTS (
|
|
47
|
+
SELECT *
|
|
48
|
+
FROM INFORMATION_SCHEMA.COLUMNS
|
|
49
|
+
WHERE TABLE_NAME = 'migration'
|
|
50
|
+
AND COLUMN_NAME = 'skip'
|
|
51
|
+
)
|
|
52
|
+
BEGIN
|
|
53
|
+
ALTER TABLE migration ADD skip BIT NOT NULL DEFAULT 0;
|
|
54
54
|
END`)
|
|
55
55
|
])).then(() => {
|
|
56
56
|
this.Migration.getAllBy('id').then((migrations) => {
|
|
@@ -97,7 +97,7 @@ END`)
|
|
|
97
97
|
}
|
|
98
98
|
applyUp(migration) {
|
|
99
99
|
return new Promise((resolve, reject) => {
|
|
100
|
-
const sqlParts = migration.content.split(
|
|
100
|
+
const sqlParts = migration.content.split(/-{4,}/);
|
|
101
101
|
this.Migration.create({
|
|
102
102
|
id: migration.id,
|
|
103
103
|
hash: migration.hash,
|
|
@@ -1,168 +1,168 @@
|
|
|
1
|
-
import * as crypto from 'crypto';
|
|
2
|
-
import * as fs from 'fs';
|
|
3
|
-
import * as Memcached from 'memcached';
|
|
4
|
-
import {ConnectionPool, Transaction} from 'mssql';
|
|
5
|
-
import {ClientBase, Pool} from 'pg';
|
|
6
|
-
import {Logger} from 'winston';
|
|
7
|
-
import { ApiCallRepository } from '../ApiCall';
|
|
8
|
-
import { Migration, MigrationRepository } from '../Migration';
|
|
9
|
-
import {ModelRepr} from './ModelDao';
|
|
10
|
-
|
|
11
|
-
export class BaseModelRepository {
|
|
12
|
-
Migration: MigrationRepository;
|
|
13
|
-
ApiCall: ApiCallRepository;
|
|
14
|
-
|
|
15
|
-
constructor(public db: Pool, public dbSpare: Pool, public dbMssql: ConnectionPool, public logger: Logger, public cache: Memcached) {
|
|
16
|
-
if(db && dbSpare) {
|
|
17
|
-
const dbQuery = db.query.bind(db);
|
|
18
|
-
db.query = (function(text: any, values: any, cb: any) {
|
|
19
|
-
if((this.idleCount + this.waitingCount) >= this.totalCount && this.totalCount === this.options.max)
|
|
20
|
-
return dbSpare.query(text, values, cb);
|
|
21
|
-
return dbQuery(text, values, cb);
|
|
22
|
-
}).bind(db);
|
|
23
|
-
}
|
|
24
|
-
this.Migration = new MigrationRepository(db, dbMssql, logger);
|
|
25
|
-
this.ApiCall = new ApiCallRepository(db, dbMssql, logger);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// TODO: alter table migration if exists without ok column
|
|
29
|
-
migrate(migrationsPath: string, callback: (() => void), onlyAboveOrEquals = 0) {
|
|
30
|
-
(this.db ? Promise.all([
|
|
31
|
-
this.db.query(`CREATE TABLE IF NOT EXISTS migration (
|
|
32
|
-
id integer PRIMARY KEY,
|
|
33
|
-
hash text NOT NULL,
|
|
34
|
-
"sqlUp" text NOT NULL,
|
|
35
|
-
"sqlDown" text NOT NULL,
|
|
36
|
-
state integer NOT NULL,
|
|
37
|
-
skip boolean NOT NULL
|
|
38
|
-
)`),
|
|
39
|
-
this.db.query(`ALTER TABLE migration ADD COLUMN IF NOT EXISTS skip BOOLEAN NOT NULL DEFAULT FALSE`)]) : Promise.all([
|
|
40
|
-
this.dbMssql.query(`IF NOT EXISTS (SELECT * FROM sysobjects WHERE name=\'migration\' AND xtype=\'U\') CREATE TABLE migration (
|
|
41
|
-
id int PRIMARY KEY,
|
|
42
|
-
hash text NOT NULL,
|
|
43
|
-
"sqlUp" text NOT NULL,
|
|
44
|
-
"sqlDown" text NOT NULL,
|
|
45
|
-
state int NOT NULL,
|
|
46
|
-
skip bit NOT NULL
|
|
47
|
-
)`),
|
|
48
|
-
this.dbMssql.query(`IF NOT EXISTS (
|
|
49
|
-
SELECT *
|
|
50
|
-
FROM INFORMATION_SCHEMA.COLUMNS
|
|
51
|
-
WHERE TABLE_NAME = 'migration'
|
|
52
|
-
AND COLUMN_NAME = 'skip'
|
|
53
|
-
)
|
|
54
|
-
BEGIN
|
|
55
|
-
ALTER TABLE migration ADD skip BIT NOT NULL DEFAULT 0;
|
|
56
|
-
END`)])).then(() => {
|
|
57
|
-
this.Migration.getAllBy('id').then((migrations: Migration[]) => {
|
|
58
|
-
if(migrations.find(migration => migration.state !== 0)) process.exit(4); // Have to fix manually
|
|
59
|
-
// Read the new ones
|
|
60
|
-
fs.readdir(migrationsPath, (_, files) => {
|
|
61
|
-
const migrationsAvailable = files
|
|
62
|
-
.filter(file => /[0-9]+\.sql/.test(file))
|
|
63
|
-
.map(file => parseInt(file.split('.sql')[0], 10))
|
|
64
|
-
.filter(file => file > 0)
|
|
65
|
-
.sort((a, b) => a - b)
|
|
66
|
-
.map(file => {
|
|
67
|
-
const content = fs.readFileSync(migrationsPath + file + '.sql', 'utf-8');
|
|
68
|
-
return {
|
|
69
|
-
id: file,
|
|
70
|
-
content: content,
|
|
71
|
-
hash: crypto.createHash('sha256').update(content).digest('hex')
|
|
72
|
-
};
|
|
73
|
-
});
|
|
74
|
-
if(migrationsAvailable.length === 0
|
|
75
|
-
|| migrationsAvailable.length
|
|
76
|
-
!== migrationsAvailable[migrationsAvailable.length - 1].id) process.exit(5); // Did not use OK files
|
|
77
|
-
let highestCommon = onlyAboveOrEquals;
|
|
78
|
-
while(highestCommon < migrations.length && highestCommon < migrationsAvailable.length
|
|
79
|
-
&& migrations[highestCommon].hash === migrationsAvailable[highestCommon].hash)
|
|
80
|
-
highestCommon++;
|
|
81
|
-
this.applyDownUntil(migrations, migrations.length, highestCommon).then(highestCommon => {
|
|
82
|
-
this.applyUpUntil(migrationsAvailable, Math.min(highestCommon, migrations.length), migrationsAvailable.length).then(callback, process.exit);
|
|
83
|
-
}, process.exit);
|
|
84
|
-
});
|
|
85
|
-
}, () => process.exit(3));
|
|
86
|
-
}, () => process.exit(2));
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
lockTables(tables: ModelRepr[], client: ClientBase | Transaction): Promise<any> {
|
|
90
|
-
if(this.db)
|
|
91
|
-
return Promise.all(tables.map(t => (<ClientBase>client).query('LOCK TABLE ' + t.table + ' IN EXCLUSIVE MODE')));
|
|
92
|
-
return Promise.all(tables.map(t => (<Transaction>client).request().query('SELECT id FROM ' + t.table + ' WITH (UPDLOCK)')));
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
private applyUpUntil(migrations: {id: number, content: string, hash: string}[], current: number, until: number): Promise<void> {
|
|
96
|
-
if(current < until)
|
|
97
|
-
return this.applyUp(migrations[current]).then(() => this.applyUpUntil(migrations, current + 1, until));
|
|
98
|
-
return Promise.resolve();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
private applyUp(migration: {id: number, content: string, hash: string}): Promise<void> {
|
|
102
|
-
return new Promise((resolve, reject) => {
|
|
103
|
-
const sqlParts = migration.content.split(
|
|
104
|
-
this.Migration.create({
|
|
105
|
-
id: migration.id,
|
|
106
|
-
hash: migration.hash,
|
|
107
|
-
sqlUp: sqlParts[0],
|
|
108
|
-
sqlDown: sqlParts[1],
|
|
109
|
-
state: 2,
|
|
110
|
-
skip: false
|
|
111
|
-
}).then(() => {
|
|
112
|
-
(<any>(this.db|| this.dbMssql)).query(sqlParts[0], (err: any) => {
|
|
113
|
-
if(err) {
|
|
114
|
-
console.error(err);
|
|
115
|
-
reject(10);
|
|
116
|
-
} else {
|
|
117
|
-
(<any>(this.db|| this.dbMssql)).query('UPDATE "migration" SET "state"=0 WHERE "id"=' + migration.id, (err2: any) => {
|
|
118
|
-
if(err2) reject(11); // No cleanup
|
|
119
|
-
else resolve();
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
}, () => process.exit(9));
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
private applyDownUntil(migrations: Migration[], current: number, until: number): Promise<number> {
|
|
128
|
-
if(current > until && !migrations[current - 1]?.skip) {
|
|
129
|
-
current--;
|
|
130
|
-
return this.applyDown(migrations[current]).then(() => this.applyDownUntil(migrations, current, until));
|
|
131
|
-
}
|
|
132
|
-
return Promise.resolve(current);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
private applyDown(migration: Migration): Promise<void> {
|
|
136
|
-
return new Promise((resolve, reject) => {
|
|
137
|
-
(<any>(this.db|| this.dbMssql)).query('UPDATE "migration" SET "state"=1 WHERE "id"=' + migration.id, (err: any) => {
|
|
138
|
-
if(err) reject(6); // No required change
|
|
139
|
-
else (<any>(this.db|| this.dbMssql)).query(migration.sqlDown, (err2: any) => {
|
|
140
|
-
if(err2) {
|
|
141
|
-
console.error(err2);
|
|
142
|
-
reject(7);
|
|
143
|
-
} // No apply down
|
|
144
|
-
else (<any>(this.db|| this.dbMssql)).query('DELETE FROM "migration" WHERE "id"=' + migration.id, (err3: any) => {
|
|
145
|
-
if(err3 && migration.id !== 1) reject(8); // No cleanup for not base migration
|
|
146
|
-
else {
|
|
147
|
-
if(migration.id === 1) {
|
|
148
|
-
(this.db ? this.db.query('CREATE TABLE IF NOT EXISTS migration (' +
|
|
149
|
-
'id integer PRIMARY KEY,' +
|
|
150
|
-
'hash text NOT NULL,' +
|
|
151
|
-
'"sqlUp" text NOT NULL,' +
|
|
152
|
-
'"sqlDown" text NOT NULL,' +
|
|
153
|
-
'state integer NOT NULL' +
|
|
154
|
-
')') : this.dbMssql.query('if not exists (select * from sysobjects where name=\'migration\' and xtype=\'U\') CREATE TABLE migration (' +
|
|
155
|
-
'id int PRIMARY KEY,' +
|
|
156
|
-
'hash text NOT NULL,' +
|
|
157
|
-
'"sqlUp" text NOT NULL,' +
|
|
158
|
-
'"sqlDown" text NOT NULL,' +
|
|
159
|
-
'state int NOT NULL' +
|
|
160
|
-
')')).then(() => resolve(), reject);
|
|
161
|
-
} else resolve();
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
}
|
|
1
|
+
import * as crypto from 'crypto';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as Memcached from 'memcached';
|
|
4
|
+
import {ConnectionPool, Transaction} from 'mssql';
|
|
5
|
+
import {ClientBase, Pool} from 'pg';
|
|
6
|
+
import {Logger} from 'winston';
|
|
7
|
+
import { ApiCallRepository } from '../ApiCall';
|
|
8
|
+
import { Migration, MigrationRepository } from '../Migration';
|
|
9
|
+
import {ModelRepr} from './ModelDao';
|
|
10
|
+
|
|
11
|
+
export class BaseModelRepository {
|
|
12
|
+
Migration: MigrationRepository;
|
|
13
|
+
ApiCall: ApiCallRepository;
|
|
14
|
+
|
|
15
|
+
constructor(public db: Pool, public dbSpare: Pool, public dbMssql: ConnectionPool, public logger: Logger, public cache: Memcached) {
|
|
16
|
+
if(db && dbSpare) {
|
|
17
|
+
const dbQuery = db.query.bind(db);
|
|
18
|
+
db.query = (function(text: any, values: any, cb: any) {
|
|
19
|
+
if((this.idleCount + this.waitingCount) >= this.totalCount && this.totalCount === this.options.max)
|
|
20
|
+
return dbSpare.query(text, values, cb);
|
|
21
|
+
return dbQuery(text, values, cb);
|
|
22
|
+
}).bind(db);
|
|
23
|
+
}
|
|
24
|
+
this.Migration = new MigrationRepository(db, dbMssql, logger);
|
|
25
|
+
this.ApiCall = new ApiCallRepository(db, dbMssql, logger);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// TODO: alter table migration if exists without ok column
|
|
29
|
+
migrate(migrationsPath: string, callback: (() => void), onlyAboveOrEquals = 0) {
|
|
30
|
+
(this.db ? Promise.all([
|
|
31
|
+
this.db.query(`CREATE TABLE IF NOT EXISTS migration (
|
|
32
|
+
id integer PRIMARY KEY,
|
|
33
|
+
hash text NOT NULL,
|
|
34
|
+
"sqlUp" text NOT NULL,
|
|
35
|
+
"sqlDown" text NOT NULL,
|
|
36
|
+
state integer NOT NULL,
|
|
37
|
+
skip boolean NOT NULL
|
|
38
|
+
)`),
|
|
39
|
+
this.db.query(`ALTER TABLE migration ADD COLUMN IF NOT EXISTS skip BOOLEAN NOT NULL DEFAULT FALSE`)]) : Promise.all([
|
|
40
|
+
this.dbMssql.query(`IF NOT EXISTS (SELECT * FROM sysobjects WHERE name=\'migration\' AND xtype=\'U\') CREATE TABLE migration (
|
|
41
|
+
id int PRIMARY KEY,
|
|
42
|
+
hash text NOT NULL,
|
|
43
|
+
"sqlUp" text NOT NULL,
|
|
44
|
+
"sqlDown" text NOT NULL,
|
|
45
|
+
state int NOT NULL,
|
|
46
|
+
skip bit NOT NULL
|
|
47
|
+
)`),
|
|
48
|
+
this.dbMssql.query(`IF NOT EXISTS (
|
|
49
|
+
SELECT *
|
|
50
|
+
FROM INFORMATION_SCHEMA.COLUMNS
|
|
51
|
+
WHERE TABLE_NAME = 'migration'
|
|
52
|
+
AND COLUMN_NAME = 'skip'
|
|
53
|
+
)
|
|
54
|
+
BEGIN
|
|
55
|
+
ALTER TABLE migration ADD skip BIT NOT NULL DEFAULT 0;
|
|
56
|
+
END`)])).then(() => {
|
|
57
|
+
this.Migration.getAllBy('id').then((migrations: Migration[]) => {
|
|
58
|
+
if(migrations.find(migration => migration.state !== 0)) process.exit(4); // Have to fix manually
|
|
59
|
+
// Read the new ones
|
|
60
|
+
fs.readdir(migrationsPath, (_, files) => {
|
|
61
|
+
const migrationsAvailable = files
|
|
62
|
+
.filter(file => /[0-9]+\.sql/.test(file))
|
|
63
|
+
.map(file => parseInt(file.split('.sql')[0], 10))
|
|
64
|
+
.filter(file => file > 0)
|
|
65
|
+
.sort((a, b) => a - b)
|
|
66
|
+
.map(file => {
|
|
67
|
+
const content = fs.readFileSync(migrationsPath + file + '.sql', 'utf-8');
|
|
68
|
+
return {
|
|
69
|
+
id: file,
|
|
70
|
+
content: content,
|
|
71
|
+
hash: crypto.createHash('sha256').update(content).digest('hex')
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
if(migrationsAvailable.length === 0
|
|
75
|
+
|| migrationsAvailable.length
|
|
76
|
+
!== migrationsAvailable[migrationsAvailable.length - 1].id) process.exit(5); // Did not use OK files
|
|
77
|
+
let highestCommon = onlyAboveOrEquals;
|
|
78
|
+
while(highestCommon < migrations.length && highestCommon < migrationsAvailable.length
|
|
79
|
+
&& migrations[highestCommon].hash === migrationsAvailable[highestCommon].hash)
|
|
80
|
+
highestCommon++;
|
|
81
|
+
this.applyDownUntil(migrations, migrations.length, highestCommon).then(highestCommon => {
|
|
82
|
+
this.applyUpUntil(migrationsAvailable, Math.min(highestCommon, migrations.length), migrationsAvailable.length).then(callback, process.exit);
|
|
83
|
+
}, process.exit);
|
|
84
|
+
});
|
|
85
|
+
}, () => process.exit(3));
|
|
86
|
+
}, () => process.exit(2));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
lockTables(tables: ModelRepr[], client: ClientBase | Transaction): Promise<any> {
|
|
90
|
+
if(this.db)
|
|
91
|
+
return Promise.all(tables.map(t => (<ClientBase>client).query('LOCK TABLE ' + t.table + ' IN EXCLUSIVE MODE')));
|
|
92
|
+
return Promise.all(tables.map(t => (<Transaction>client).request().query('SELECT id FROM ' + t.table + ' WITH (UPDLOCK)')));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private applyUpUntil(migrations: {id: number, content: string, hash: string}[], current: number, until: number): Promise<void> {
|
|
96
|
+
if(current < until)
|
|
97
|
+
return this.applyUp(migrations[current]).then(() => this.applyUpUntil(migrations, current + 1, until));
|
|
98
|
+
return Promise.resolve();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private applyUp(migration: {id: number, content: string, hash: string}): Promise<void> {
|
|
102
|
+
return new Promise((resolve, reject) => {
|
|
103
|
+
const sqlParts = migration.content.split(/-{4,}/);
|
|
104
|
+
this.Migration.create({
|
|
105
|
+
id: migration.id,
|
|
106
|
+
hash: migration.hash,
|
|
107
|
+
sqlUp: sqlParts[0],
|
|
108
|
+
sqlDown: sqlParts[1],
|
|
109
|
+
state: 2,
|
|
110
|
+
skip: false
|
|
111
|
+
}).then(() => {
|
|
112
|
+
(<any>(this.db|| this.dbMssql)).query(sqlParts[0], (err: any) => {
|
|
113
|
+
if(err) {
|
|
114
|
+
console.error(err);
|
|
115
|
+
reject(10);
|
|
116
|
+
} else {
|
|
117
|
+
(<any>(this.db|| this.dbMssql)).query('UPDATE "migration" SET "state"=0 WHERE "id"=' + migration.id, (err2: any) => {
|
|
118
|
+
if(err2) reject(11); // No cleanup
|
|
119
|
+
else resolve();
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}, () => process.exit(9));
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private applyDownUntil(migrations: Migration[], current: number, until: number): Promise<number> {
|
|
128
|
+
if(current > until && !migrations[current - 1]?.skip) {
|
|
129
|
+
current--;
|
|
130
|
+
return this.applyDown(migrations[current]).then(() => this.applyDownUntil(migrations, current, until));
|
|
131
|
+
}
|
|
132
|
+
return Promise.resolve(current);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private applyDown(migration: Migration): Promise<void> {
|
|
136
|
+
return new Promise((resolve, reject) => {
|
|
137
|
+
(<any>(this.db|| this.dbMssql)).query('UPDATE "migration" SET "state"=1 WHERE "id"=' + migration.id, (err: any) => {
|
|
138
|
+
if(err) reject(6); // No required change
|
|
139
|
+
else (<any>(this.db|| this.dbMssql)).query(migration.sqlDown, (err2: any) => {
|
|
140
|
+
if(err2) {
|
|
141
|
+
console.error(err2);
|
|
142
|
+
reject(7);
|
|
143
|
+
} // No apply down
|
|
144
|
+
else (<any>(this.db|| this.dbMssql)).query('DELETE FROM "migration" WHERE "id"=' + migration.id, (err3: any) => {
|
|
145
|
+
if(err3 && migration.id !== 1) reject(8); // No cleanup for not base migration
|
|
146
|
+
else {
|
|
147
|
+
if(migration.id === 1) {
|
|
148
|
+
(this.db ? this.db.query('CREATE TABLE IF NOT EXISTS migration (' +
|
|
149
|
+
'id integer PRIMARY KEY,' +
|
|
150
|
+
'hash text NOT NULL,' +
|
|
151
|
+
'"sqlUp" text NOT NULL,' +
|
|
152
|
+
'"sqlDown" text NOT NULL,' +
|
|
153
|
+
'state integer NOT NULL' +
|
|
154
|
+
')') : this.dbMssql.query('if not exists (select * from sysobjects where name=\'migration\' and xtype=\'U\') CREATE TABLE migration (' +
|
|
155
|
+
'id int PRIMARY KEY,' +
|
|
156
|
+
'hash text NOT NULL,' +
|
|
157
|
+
'"sqlUp" text NOT NULL,' +
|
|
158
|
+
'"sqlDown" text NOT NULL,' +
|
|
159
|
+
'state int NOT NULL' +
|
|
160
|
+
')')).then(() => resolve(), reject);
|
|
161
|
+
} else resolve();
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}
|