anote-server-libs 0.6.11 → 0.7.0

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.
@@ -4,8 +4,8 @@ exports.MigrationRepository = void 0;
4
4
  const ModelDao_1 = require("./repository/ModelDao");
5
5
  class MigrationRepository extends ModelDao_1.ModelDao {
6
6
  constructor(pool, poolMssql, logger) {
7
- super(pool, poolMssql, logger, 'migration', 5, pool ? 'id=$1,hash=$2,"sqlUp"=$3,"sqlDown"=$4,state=$5' :
8
- 'id=@1,hash=@2,"sqlUp"=@3,"sqlDown"=@4,state=@5');
7
+ super(pool, poolMssql, logger, 'migration', 6, pool ? 'id=$1,hash=$2,"sqlUp"=$3,"sqlDown"=$4,state=$5,skip=$6' :
8
+ 'id=@1,hash=@2,"sqlUp"=@3,"sqlDown"=@4,state=@5,skip=@6');
9
9
  this.pool = pool;
10
10
  this.poolMssql = poolMssql;
11
11
  this.logger = logger;
@@ -16,6 +16,7 @@ class MigrationRepository extends ModelDao_1.ModelDao {
16
16
  q.id = parseInt(q.id, 10);
17
17
  q.hash = q.hash.trim();
18
18
  q.state = parseInt(q.state, 10);
19
+ q.skip = q.skip ? true : false;
19
20
  return q;
20
21
  }
21
22
  serialize(instance, request) {
@@ -25,10 +26,11 @@ class MigrationRepository extends ModelDao_1.ModelDao {
25
26
  request.input('3', instance.sqlUp);
26
27
  request.input('4', instance.sqlDown);
27
28
  request.input('5', instance.state);
29
+ request.input('6', instance.skip ? 1 : 0);
28
30
  return undefined;
29
31
  }
30
32
  else {
31
- return [instance.id, instance.hash, instance.sqlUp, instance.sqlDown, instance.state];
33
+ return [instance.id, instance.hash, instance.sqlUp, instance.sqlDown, instance.state, instance.skip];
32
34
  }
33
35
  }
34
36
  }
@@ -9,12 +9,13 @@ export interface Migration {
9
9
  sqlUp: string;
10
10
  sqlDown: string;
11
11
  state: number;
12
+ skip: boolean;
12
13
  }
13
14
 
14
15
  export class MigrationRepository extends ModelDao<number, Migration> {
15
16
  constructor(protected pool: Pool, protected poolMssql: ConnectionPool, protected logger: Logger) {
16
- super(pool, poolMssql, logger, 'migration', 5, pool ? 'id=$1,hash=$2,"sqlUp"=$3,"sqlDown"=$4,state=$5' :
17
- 'id=@1,hash=@2,"sqlUp"=@3,"sqlDown"=@4,state=@5');
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');
18
19
  }
19
20
 
20
21
  buildObject(q: any): Migration {
@@ -22,6 +23,7 @@ export class MigrationRepository extends ModelDao<number, Migration> {
22
23
  q.id = parseInt(q.id, 10);
23
24
  q.hash = q.hash.trim();
24
25
  q.state = parseInt(q.state, 10);
26
+ q.skip = q.skip ? true : false;
25
27
  return q;
26
28
  }
27
29
 
@@ -32,9 +34,10 @@ export class MigrationRepository extends ModelDao<number, Migration> {
32
34
  request.input('3', instance.sqlUp);
33
35
  request.input('4', instance.sqlDown);
34
36
  request.input('5', instance.state);
37
+ request.input('6', instance.skip ? 1 : 0);
35
38
  return undefined;
36
39
  } else {
37
- return [instance.id, instance.hash, instance.sqlUp, instance.sqlDown, instance.state];
40
+ return [instance.id, instance.hash, instance.sqlUp, instance.sqlDown, instance.state, instance.skip];
38
41
  }
39
42
  }
40
43
  }
@@ -24,19 +24,35 @@ class BaseModelRepository {
24
24
  this.ApiCall = new ApiCall_1.ApiCallRepository(db, dbMssql, logger);
25
25
  }
26
26
  migrate(migrationsPath, callback, onlyAboveOrEquals = 0) {
27
- (this.db ? this.db.query('CREATE TABLE IF NOT EXISTS migration (' +
28
- 'id integer PRIMARY KEY,' +
29
- 'hash text NOT NULL,' +
30
- '"sqlUp" text NOT NULL,' +
31
- '"sqlDown" text NOT NULL,' +
32
- 'state integer NOT NULL' +
33
- ')') : this.dbMssql.query('if not exists (select * from sysobjects where name=\'migration\' and xtype=\'U\') CREATE TABLE migration (' +
34
- 'id int PRIMARY KEY,' +
35
- 'hash text NOT NULL,' +
36
- '"sqlUp" text NOT NULL,' +
37
- '"sqlDown" text NOT NULL,' +
38
- 'state int NOT NULL' +
39
- ')')).then(() => {
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
35
+ )`),
36
+ this.db.query(`ALTER TABLE migration ADD COLUMN IF NOT EXISTS skip BOOLEAN NOT NULL DEFAULT FALSE`)
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
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;
54
+ END`)
55
+ ])).then(() => {
40
56
  this.Migration.getAllBy('id').then((migrations) => {
41
57
  if (migrations.find(migration => migration.state !== 0))
42
58
  process.exit(4);
@@ -87,7 +103,8 @@ class BaseModelRepository {
87
103
  hash: migration.hash,
88
104
  sqlUp: sqlParts[0],
89
105
  sqlDown: sqlParts[1],
90
- state: 2
106
+ state: 2,
107
+ skip: false
91
108
  }).then(() => {
92
109
  (this.db || this.dbMssql).query(sqlParts[0], (err) => {
93
110
  if (err) {
@@ -107,7 +124,7 @@ class BaseModelRepository {
107
124
  });
108
125
  }
109
126
  applyDownUntil(migrations, current, until) {
110
- if (current > until) {
127
+ if (current > until && !migrations[current].skip) {
111
128
  current--;
112
129
  return this.applyDown(migrations[current]).then(() => this.applyDownUntil(migrations, current, until));
113
130
  }
@@ -25,20 +25,35 @@ export class BaseModelRepository {
25
25
  this.ApiCall = new ApiCallRepository(db, dbMssql, logger);
26
26
  }
27
27
 
28
+ // TODO: alter table migration if exists without ok column
28
29
  migrate(migrationsPath: string, callback: (() => void), onlyAboveOrEquals = 0) {
29
- (this.db ? this.db.query('CREATE TABLE IF NOT EXISTS migration (' +
30
- 'id integer PRIMARY KEY,' +
31
- 'hash text NOT NULL,' +
32
- '"sqlUp" text NOT NULL,' +
33
- '"sqlDown" text NOT NULL,' +
34
- 'state integer NOT NULL' +
35
- ')') : this.dbMssql.query('if not exists (select * from sysobjects where name=\'migration\' and xtype=\'U\') CREATE TABLE migration (' +
36
- 'id int PRIMARY KEY,' +
37
- 'hash text NOT NULL,' +
38
- '"sqlUp" text NOT NULL,' +
39
- '"sqlDown" text NOT NULL,' +
40
- 'state int NOT NULL' +
41
- ')')).then(() => {
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(() => {
42
57
  this.Migration.getAllBy('id').then((migrations: Migration[]) => {
43
58
  if(migrations.find(migration => migration.state !== 0)) process.exit(4); // Have to fix manually
44
59
  // Read the new ones
@@ -91,7 +106,8 @@ export class BaseModelRepository {
91
106
  hash: migration.hash,
92
107
  sqlUp: sqlParts[0],
93
108
  sqlDown: sqlParts[1],
94
- state: 2
109
+ state: 2,
110
+ skip: false
95
111
  }).then(() => {
96
112
  (<any>(this.db|| this.dbMssql)).query(sqlParts[0], (err: any) => {
97
113
  if(err) {
@@ -109,7 +125,7 @@ export class BaseModelRepository {
109
125
  }
110
126
 
111
127
  private applyDownUntil(migrations: Migration[], current: number, until: number): Promise<void> {
112
- if(current > until) {
128
+ if(current > until && !migrations[current].skip) {
113
129
  current--;
114
130
  return this.applyDown(migrations[current]).then(() => this.applyDownUntil(migrations, current, until));
115
131
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anote-server-libs",
3
- "version": "0.6.11",
3
+ "version": "0.7.0",
4
4
  "description": "Helpers for express-TS servers",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",