@steedos/migrate 2.3.4 → 2.3.6

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/README.md CHANGED
@@ -1,3 +1,10 @@
1
+ <!--
2
+ * @Author: sunhaolin@hotoa.com
3
+ * @Date: 2021-05-24 12:32:57
4
+ * @LastEditors: sunhaolin@hotoa.com
5
+ * @LastEditTime: 2023-01-11 16:54:20
6
+ * @Description:
7
+ -->
1
8
  Steedos Migration Scripts
2
9
  ===
3
10
 
@@ -7,8 +14,9 @@ Migrate steedos database with [node-migrate](https://github.com/tj/node-migrate)
7
14
 
8
15
  ### DB Connection
9
16
 
10
- set database connection in steedos-config.yml
17
+ 项目根目录下执行 node 进入node控制台
11
18
 
19
+ 在node控制台执行 `require('dotenv-flow').config(); ` 目的是为了读取.env.local设置 `process.env.MONGO_URL`
12
20
  ### Migrate up
13
21
 
14
22
  open nodejs console.
@@ -27,15 +35,9 @@ var migrate = require("@steedos/migrate");
27
35
  migrate.down();
28
36
  ```
29
37
 
30
- ### Enable auto migrate for steedos
38
+ ### Default auto migrate for steedos
31
39
 
32
- steedos-config.yml
33
-
34
- ```yml
35
- datasources:
36
- default:
37
- auto_migrate: true
38
- ```
40
+ config STEEDOS_DB_AUTO_MIGRATE=false in .env.local to disable auto migrate
39
41
 
40
42
  ### Add migration script
41
43
 
package/client.js ADDED
@@ -0,0 +1,13 @@
1
+ /*
2
+ * @Author: sunhaolin@hotoa.com
3
+ * @Date: 2021-05-24 12:32:57
4
+ * @LastEditors: sunhaolin@hotoa.com
5
+ * @LastEditTime: 2023-01-11 11:51:05
6
+ * @Description:
7
+ */
8
+
9
+ const { MongoClient } = require("mongodb");
10
+
11
+ const client = new MongoClient(process.env.MONGO_URL, { useNewUrlParser: true, useUnifiedTopology: true });
12
+
13
+ module.exports = client;
package/index.js CHANGED
@@ -1,69 +1,67 @@
1
- var migrate = require('migrate')
2
- var path = require('path');
1
+ /*
2
+ * @Author: sunhaolin@hotoa.com
3
+ * @Date: 2021-05-24 12:32:57
4
+ * @LastEditors: sunhaolin@hotoa.com
5
+ * @LastEditTime: 2023-01-13 14:21:44
6
+ * @Description:
7
+ */
8
+ const migrate = require('migrate')
9
+ const path = require('path');
10
+ const validator = require('validator');
3
11
 
4
- var stateStore = path.join(process.cwd(), '.migrate');
5
- var migrationsDirectory = path.join(__dirname, 'migrations');
12
+ const stateStore = path.join(process.cwd(), '.steedos', '.migrate');
13
+ const migrationsDirectory = path.join(__dirname, 'migrations');
6
14
 
7
- // let Fiber = require('fibers');
8
-
9
- const up = async function() {
15
+ const up = async function () {
10
16
 
11
17
  migrate.load({
12
18
  stateStore: stateStore,
13
19
  migrationsDirectory: migrationsDirectory
14
20
  }, function (err, set) {
15
21
  if (err) {
16
- throw err
22
+ throw err
17
23
  }
18
24
  set.up(function (err) {
19
- if (err) {
20
- throw err
21
- }
22
- console.log('DB migrations up successfully.')
25
+ if (err) {
26
+ throw err
27
+ }
28
+
23
29
  })
24
30
  })
25
31
  }
26
32
 
27
- const init = async function() {
28
- var objectql = require("@steedos/objectql");
29
- if (objectql.getSteedosConfig().datasources.default.auto_migrate)
33
+ const init = async function () {
34
+ if (process.env.STEEDOS_DB_AUTO_MIGRATE !== false && process.env.STEEDOS_DB_AUTO_MIGRATE !== 'false') {
35
+ // 如果未设置为不自动执行,则默认自动执行
36
+ process.env.STEEDOS_DB_AUTO_MIGRATE = 'true'
37
+ }
38
+ const autoMigrate = validator.toBoolean(process.env.STEEDOS_DB_AUTO_MIGRATE || '', true);
39
+ // console.log('process.env.STEEDOS_DB_AUTO_MIGRATE:', process.env.STEEDOS_DB_AUTO_MIGRATE)
40
+ // console.log('autoMigrate:', autoMigrate)
41
+ if (autoMigrate) {
30
42
  up();
43
+ }
31
44
  }
32
45
 
33
- const down = async function() {
46
+ const down = async function () {
34
47
  migrate.load({
35
48
  stateStore: stateStore,
36
49
  migrationsDirectory: migrationsDirectory
37
50
  }, function (err, set) {
38
51
  if (err) {
39
- throw err
52
+ throw err
40
53
  }
41
54
  set.down(function (err) {
42
- if (err) {
43
- throw err
44
- }
45
- console.log('DB migrations down successfully.')
55
+ if (err) {
56
+ throw err
57
+ }
58
+ console.log('DB migrations down successfully.')
46
59
  })
47
60
  })
48
61
  }
49
62
 
50
- // const upSync = function(){
51
- // Fiber(function(){
52
- // let fiber = Fiber.current;
53
- // up.then(result => {
54
- // fiber.run();
55
- // }).catch(result => {
56
- // console.error(result)
57
- // fiber.run();
58
- // })
59
- // Fiber.yield();
60
- // }).run();
61
- // }
62
-
63
-
64
63
  module.exports = {
65
64
  init: init,
66
65
  up: up,
67
- // upSync: upSync,
68
66
  down: down
69
67
  }
@@ -0,0 +1,88 @@
1
+ /*
2
+ * @Author: sunhaolin@hotoa.com
3
+ * @Date: 2023-01-10 17:18:23
4
+ * @LastEditors: sunhaolin@hotoa.com
5
+ * @LastEditTime: 2023-02-11 10:49:47
6
+ * @Description:
7
+ */
8
+ 'use strict'
9
+
10
+ const client = require("../client");
11
+
12
+ module.exports.up = async function (next) {
13
+ await client.connect()
14
+ const db = client.db()
15
+ const instanceColl = db.collection('instances')
16
+ const tasksColl = db.collection('instance_tasks')
17
+ // 若已有instance_tasks则不执行
18
+ const tasksCount = await tasksColl.find({}).count()
19
+ if (tasksCount > 0) {
20
+ // console.log('[migration]', '库中已有instance_tasks,终止执行。')
21
+ return;
22
+ }
23
+ console.log('[migration] add-instance-tasks is running...')
24
+ await instanceColl.find({}).forEach(async function (insDoc) {
25
+ var docs = [];
26
+ var latestApproveIndexMap = {}
27
+ insDoc.traces.forEach(function (tDoc, tIdx) {
28
+ if (tIdx == 0 && (!insDoc.distribute_from_instance && !insDoc.forward_from_instance)) {
29
+ // 只有分发或转发的申请单,开始节点生成instance_tasks
30
+ return
31
+ }
32
+ if (tDoc.approves) {
33
+ tDoc.approves.forEach(function (aDoc) {
34
+ if (aDoc.type == 'distribute' || aDoc.judge == 'relocated' || aDoc.judge == 'terminated' || aDoc.judge == 'reassigned') {
35
+ return;
36
+ }
37
+ aDoc['space'] = insDoc.space;
38
+ aDoc['instance_name'] = insDoc.name;
39
+ aDoc['submitter'] = insDoc.submitter;
40
+ aDoc['submitter_name'] = insDoc.submitter_name;
41
+ aDoc['applicant'] = insDoc.applicant;
42
+ aDoc['applicant_name'] = insDoc.applicant_name;
43
+ aDoc['applicant_organization_name'] = insDoc.applicant_organization_name;
44
+ aDoc['submit_date'] = insDoc.submit_date;
45
+ aDoc['flow'] = insDoc.flow;
46
+ aDoc['flow_name'] = insDoc.flow_name;
47
+ aDoc['form'] = insDoc.form;
48
+ aDoc['step'] = tDoc.step;
49
+ aDoc['step_name'] = tDoc.name;
50
+ aDoc['category_name'] = insDoc.category_name;
51
+ aDoc['instance_state'] = insDoc.state;
52
+ aDoc['distribute_from_instance'] = insDoc.distribute_from_instance;
53
+ aDoc['forward_from_instance'] = insDoc.forward_from_instance;
54
+ aDoc['keywords'] = insDoc.keywords;
55
+ aDoc['is_archived'] = insDoc.is_archived;
56
+ aDoc['category'] = insDoc.category;
57
+ docs.push(aDoc)
58
+ if (aDoc.is_finished) { //记录下需要设置is_latest_approve的游标,如有重复审批则更新为最新的
59
+ latestApproveIndexMap[aDoc.handler] = docs.length - 1
60
+ }
61
+ })
62
+ }
63
+ })
64
+
65
+ for (const handler in latestApproveIndexMap) {
66
+ if (Object.hasOwnProperty.call(latestApproveIndexMap, handler)) {
67
+ docs[latestApproveIndexMap[handler]].is_latest_approve = true
68
+ }
69
+ }
70
+
71
+ if (docs.length > 0) {
72
+ try {
73
+ await tasksColl.insertMany(docs);
74
+ } catch (e) {
75
+ print(e);
76
+ printjson(docs.length)
77
+ }
78
+ }
79
+ })
80
+ const insCount = await instanceColl.find({}).count()
81
+ const insTasksCount = await tasksColl.find({}).count()
82
+ console.log('[migration] add-instance-tasks successfully ran.', 'instances:', insCount, ', instance_tasks:', insTasksCount)
83
+ }
84
+
85
+ module.exports.down = async function (next) {
86
+ const tasksColl = db.collection('instance_tasks')
87
+ await tasksColl.remove({})
88
+ }
package/package.json CHANGED
@@ -1,15 +1,16 @@
1
1
  {
2
2
  "name": "@steedos/migrate",
3
- "version": "2.3.4",
3
+ "version": "2.3.6",
4
4
  "description": "Migration scripts for steedos",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
7
7
  "private": false,
8
8
  "dependencies": {
9
- "migrate": "^1.6.2"
9
+ "migrate": "^1.6.2",
10
+ "mongodb": "^3.7.3"
10
11
  },
11
12
  "publishConfig": {
12
13
  "access": "public"
13
14
  },
14
- "gitHead": "adfbf2b14c15751d6702d8e127426ac1d9d813eb"
15
+ "gitHead": "3ef6bf3744c616d6a961b3264e5f204b9697ef6a"
15
16
  }
package/db.js DELETED
@@ -1,10 +0,0 @@
1
-
2
- var objectql = require("@steedos/objectql");
3
-
4
- var mongoUrl = objectql.getSteedosConfig().datasources.default.connection.url;
5
-
6
- var SteedosMongoDriver = objectql.SteedosMongoDriver;
7
-
8
- let driver = new SteedosMongoDriver({ url: process.env.MONGO_URL });
9
-
10
- module.exports = driver;