ti2 1.0.15 → 1.0.16
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/.github/workflows/tests.yml +1 -3
- package/cli.js +36 -0
- package/controllers/admin.js +46 -1
- package/controllers/app.js +52 -1
- package/models/db.js +0 -1
- package/package.json +4 -1
- package/tutorials/plugin-development.md +6 -3
- package/tutorials/setup-your-instance.md +1 -1
package/cli.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { Command, Argument } = require('commander');
|
|
4
|
+
|
|
5
|
+
const { migrateApp } = require('./controllers/app');
|
|
6
|
+
const { migrate } = require('./controllers/admin');
|
|
7
|
+
|
|
8
|
+
const program = new Command();
|
|
9
|
+
|
|
10
|
+
program
|
|
11
|
+
.name('ti2-cli')
|
|
12
|
+
.description('TI2 Command line interface utilities')
|
|
13
|
+
.version((require('./package.json')).version);
|
|
14
|
+
|
|
15
|
+
program.command('dbapp')
|
|
16
|
+
.description('Handle database migrations for apps')
|
|
17
|
+
.argument('<string>', 'integration to operate on')
|
|
18
|
+
.addArgument(
|
|
19
|
+
new Argument('<action>', 'action to take', 'migrate')
|
|
20
|
+
.choices(['migrate', 'revert']),
|
|
21
|
+
)
|
|
22
|
+
.action(async (integration, action) => {
|
|
23
|
+
await migrateApp({ integrationId: integration, action });
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
program.command('db')
|
|
27
|
+
.description('Handle database migrations for integrations')
|
|
28
|
+
.addArgument(
|
|
29
|
+
new Argument('<action>', 'action to take', 'migrate')
|
|
30
|
+
.choices(['migrate', 'revert']),
|
|
31
|
+
)
|
|
32
|
+
.action(async action => {
|
|
33
|
+
await migrate({ action });
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
program.parse();
|
package/controllers/admin.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
const jwt = require('jsonwebtoken');
|
|
2
2
|
const assert = require('assert');
|
|
3
3
|
const { omit } = require('ramda');
|
|
4
|
+
const { Umzug, SequelizeStorage } = require('umzug');
|
|
5
|
+
const Sequelize = require('sequelize');
|
|
6
|
+
const fs = require('fs').promises;
|
|
7
|
+
const path = require('path');
|
|
8
|
+
|
|
4
9
|
const sqldb = require('../models');
|
|
5
10
|
|
|
6
11
|
const { env: { jwtSecret } } = process;
|
|
@@ -91,10 +96,50 @@ const resetIntegrationToken = async (req, res, next) => {
|
|
|
91
96
|
}
|
|
92
97
|
};
|
|
93
98
|
|
|
99
|
+
const migrate = async ({ action }) => {
|
|
100
|
+
const { sequelize } = sqldb;
|
|
101
|
+
const migrationsPath = path.join(
|
|
102
|
+
__dirname,
|
|
103
|
+
'../',
|
|
104
|
+
'migrations',
|
|
105
|
+
);
|
|
106
|
+
try {
|
|
107
|
+
await fs.access(migrationsPath);
|
|
108
|
+
} catch (err) {
|
|
109
|
+
throw Error(`Could not find any migrations on ${migrationsPath}`);
|
|
110
|
+
}
|
|
111
|
+
const umzug = new Umzug({
|
|
112
|
+
migrations: {
|
|
113
|
+
glob: `${migrationsPath}/*.js`,
|
|
114
|
+
resolve: ({ name, path: migPath, context }) => {
|
|
115
|
+
const migration = require(migPath);
|
|
116
|
+
return {
|
|
117
|
+
name,
|
|
118
|
+
up: async () => migration.up(context, Sequelize),
|
|
119
|
+
down: async () => migration.down(context, Sequelize),
|
|
120
|
+
};
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
context: sequelize.getQueryInterface(),
|
|
124
|
+
storage: new SequelizeStorage({
|
|
125
|
+
sequelize,
|
|
126
|
+
}),
|
|
127
|
+
logger: console,
|
|
128
|
+
});
|
|
129
|
+
if (action === 'migrate') {
|
|
130
|
+
return umzug.up();
|
|
131
|
+
}
|
|
132
|
+
if (action === 'revert') {
|
|
133
|
+
return umzug.down({ to: 0 });
|
|
134
|
+
}
|
|
135
|
+
throw Error('No recognized action');
|
|
136
|
+
};
|
|
137
|
+
|
|
94
138
|
module.exports = {
|
|
95
139
|
createApp,
|
|
96
|
-
deleteApp,
|
|
97
140
|
createUserToken,
|
|
141
|
+
deleteApp,
|
|
98
142
|
listApps,
|
|
143
|
+
migrate,
|
|
99
144
|
resetIntegrationToken,
|
|
100
145
|
};
|
package/controllers/app.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
const jwt = require('jwt-promise');
|
|
2
2
|
const { omit } = require('ramda');
|
|
3
|
+
const assert = require('assert');
|
|
4
|
+
const { Umzug, SequelizeStorage } = require('umzug');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const Sequelize = require('sequelize');
|
|
7
|
+
const fs = require('fs').promises;
|
|
8
|
+
|
|
3
9
|
const sqldb = require('../models');
|
|
4
10
|
|
|
5
11
|
const { env: { jwtSecret } } = process;
|
|
@@ -83,9 +89,54 @@ const listAppTokens = async (req, res, next) => {
|
|
|
83
89
|
}
|
|
84
90
|
};
|
|
85
91
|
|
|
92
|
+
const migrateApp = async ({ integrationId, action }) => {
|
|
93
|
+
const { sequelize } = sqldb;
|
|
94
|
+
assert(integrationId);
|
|
95
|
+
assert(action);
|
|
96
|
+
const migrationsPath = path.join(
|
|
97
|
+
__dirname,
|
|
98
|
+
'../',
|
|
99
|
+
'../',
|
|
100
|
+
`ti2-${integrationId}`,
|
|
101
|
+
'migrations',
|
|
102
|
+
);
|
|
103
|
+
try {
|
|
104
|
+
await fs.access(migrationsPath);
|
|
105
|
+
} catch (err) {
|
|
106
|
+
throw Error(`Could not find any migrations for ${integrationId} on ${migrationsPath}`);
|
|
107
|
+
}
|
|
108
|
+
const umzug = new Umzug({
|
|
109
|
+
migrations: {
|
|
110
|
+
glob: `${migrationsPath}/*.js`,
|
|
111
|
+
resolve: ({ name, path: migPath, context }) => {
|
|
112
|
+
const migration = require(migPath);
|
|
113
|
+
return {
|
|
114
|
+
name,
|
|
115
|
+
up: async () => migration.up(context, Sequelize),
|
|
116
|
+
down: async () => migration.down(context, Sequelize),
|
|
117
|
+
};
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
context: sequelize.getQueryInterface(),
|
|
121
|
+
storage: new SequelizeStorage({
|
|
122
|
+
sequelize,
|
|
123
|
+
tableName: `SequelizeMeta-${integrationId}`,
|
|
124
|
+
}),
|
|
125
|
+
logger: console,
|
|
126
|
+
});
|
|
127
|
+
if (action === 'migrate') {
|
|
128
|
+
return umzug.up();
|
|
129
|
+
}
|
|
130
|
+
if (action === 'revert') {
|
|
131
|
+
return umzug.down({ to: 0 });
|
|
132
|
+
}
|
|
133
|
+
throw Error('No recognized action');
|
|
134
|
+
};
|
|
135
|
+
|
|
86
136
|
module.exports = {
|
|
87
|
-
jwtEncode,
|
|
88
137
|
createAppToken,
|
|
89
138
|
deleteAppToken,
|
|
139
|
+
jwtEncode,
|
|
90
140
|
listAppTokens,
|
|
141
|
+
migrateApp,
|
|
91
142
|
};
|
package/models/db.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ti2",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.16",
|
|
4
4
|
"description": "Tourist Industry Exchange (TI2)",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"type": "git",
|
|
13
13
|
"url": "git+https://github.com/ti2travel/ti2.git"
|
|
14
14
|
},
|
|
15
|
+
"bin": "./cli.js",
|
|
15
16
|
"author": "",
|
|
16
17
|
"license": "GPL-3",
|
|
17
18
|
"bugs": {
|
|
@@ -20,6 +21,7 @@
|
|
|
20
21
|
"homepage": "https://github.com/ti2travel/ti2#readme",
|
|
21
22
|
"dependencies": {
|
|
22
23
|
"bluebird": "^3.7.2",
|
|
24
|
+
"commander": "^9.2.0",
|
|
23
25
|
"express": "^4.17.2",
|
|
24
26
|
"js-yaml": "^4.1.0",
|
|
25
27
|
"jsonwebtoken": "^8.5.1",
|
|
@@ -32,6 +34,7 @@
|
|
|
32
34
|
"swagger-express-middleware": "3.0.0-alpha.5",
|
|
33
35
|
"swagger-routes-express": "3.3.1",
|
|
34
36
|
"swagger-ui-express": "4.3.0",
|
|
37
|
+
"umzug": "^3.1.1",
|
|
35
38
|
"uninstall": "^0.0.0"
|
|
36
39
|
},
|
|
37
40
|
"devDependencies": {
|
|
@@ -216,14 +216,17 @@ Ti2 uses [Sequelize v6.13](https://sequelize.org/v6) which is the database ORM w
|
|
|
216
216
|
|
|
217
217
|
Migrations should be run after the fact, from the root of ti2 instance like so:
|
|
218
218
|
|
|
219
|
-
|
|
220
219
|
```
|
|
221
|
-
$
|
|
220
|
+
$ ti2 dbapp [plugin name] migrate
|
|
222
221
|
```
|
|
223
222
|
|
|
224
223
|
We strongly encourage all the plugin's table names are prefixed with the plugin name (i.e. ti2-pluginName-cacheTable).
|
|
225
224
|
|
|
226
|
-
This should be executed after
|
|
225
|
+
This should be executed after all ti2 host migrations had been executed.
|
|
226
|
+
|
|
227
|
+
```
|
|
228
|
+
$ ti2 db migrate
|
|
229
|
+
```
|
|
227
230
|
|
|
228
231
|
## Scheduled Task (to be released on v2)
|
|
229
232
|
|