背景

https://github.com/macacajs/macaca-datahub/issues/178

image.png

重点变更

数据库表变更

新增 groups 表,为 interfaces 表新增 groupUniqId 字段。

  1. 'use strict';
  2. module.exports = {
  3. up: async (db, Sequelize) => {
  4. const { STRING, UUID, UUIDV4, DATE } = Sequelize;
  5. await db.createTable('groups', {
  6. uniqId: {
  7. type: UUID,
  8. defaultValue: UUIDV4,
  9. primaryKey: true,
  10. allowNull: false,
  11. },
  12. groupName: {
  13. type: STRING,
  14. allowNull: false,
  15. },
  16. groupType: {
  17. type: STRING,
  18. allowNull: false,
  19. },
  20. belongedUniqId: {
  21. type: STRING,
  22. allowNull: false,
  23. },
  24. createdAt: {
  25. type: DATE,
  26. allowNull: false,
  27. },
  28. updatedAt: {
  29. type: DATE,
  30. allowNull: false,
  31. },
  32. }, {
  33. indexes: [
  34. {
  35. fields: [
  36. 'uniqId',
  37. ],
  38. unique: true,
  39. },
  40. ],
  41. });
  42. await db.addColumn('interfaces', 'groupUniqId', {
  43. type: Sequelize.STRING,
  44. allowNull: true,
  45. });
  46. },
  47. down: async db => {
  48. await db.dropTable('groups');
  49. await db.removeColumn('interfaces', 'groupUniqId');
  50. },
  51. };

Api 变更

  1. 新增 分组 相关接口及对应的 controller -> service -> model

    1. router.get('/api/group', controller.api.group.showAll);
    2. router.post('/api/group', controller.api.group.create);
    3. router.put('/api/group/:uniqId', controller.api.group.update);
    4. router.delete('/api/group/:uniqId', controller.api.group.delete);
  2. 为了前端接口场景分组功能的实现,现变更 interface 接口返回的数据结构。 ```javascript router.get(‘/api/interface’, controller.api.interface.showAll);

原数据结构: [ { “protocol”: “http”, “pathname”: “test1”, “method”: “ALL”, “projectUniqId”: “626eec31-8113-4510-ba50-2af8ba4c4ace”, “description”: “interface test”, “currentScene”: “scene1”, “proxyConfig”: {}, “uniqId”: “7db907e1-e63c-40a5-b2f8-0f5fc1c0322c”, “createdAt”: “2022-01-22T09:45:20.682Z”, “updatedAt”: “2022-01-22T09:57:24.774Z” } ]

现数据结构: { “interfaceGroupList”: [ { “groupName”: “默认分组”, “groupUniqId”: “025fa207-0174-42b7-a0b5-26d8c65f73ad”, “interfaceList”: [ { “protocol”: “http”, “pathname”: “test1”, “method”: “ALL”, “projectUniqId”: “626eec31-8113-4510-ba50-2af8ba4c4ace”, “description”: “interface test”, “currentScene”: “scene1”, “proxyConfig”: {}, “uniqId”: “7db907e1-e63c-40a5-b2f8-0f5fc1c0322c”, “groupUniqId”: “025fa207-0174-42b7-a0b5-26d8c65f73ad”, “createdAt”: “2022-01-22T09:45:20.682Z”, “updatedAt”: “2022-01-22T09:57:24.774Z” } ] }, { “groupName”: “group1”, “groupUniqId”: “7a19359a-b2f4-488a-a24e-96a3a53b4f2c”, “interfaceList”: [] } ], “interfaceList”: [ { “protocol”: “http”, “pathname”: “test1”, “method”: “ALL”, “projectUniqId”: “626eec31-8113-4510-ba50-2af8ba4c4ace”, “description”: “interface test”, “currentScene”: “scene1”, “proxyConfig”: {}, “uniqId”: “7db907e1-e63c-40a5-b2f8-0f5fc1c0322c”, “groupUniqId”: “025fa207-0174-42b7-a0b5-26d8c65f73ad”, “createdAt”: “2022-01-22T09:45:20.682Z”, “updatedAt”: “2022-01-22T09:57:24.774Z” } ] }

  1. <a name="Ntiei"></a>
  2. ### 导入导出数据结构变更
  3. 由于新增了分组功能,datahub 内部数据的组织结构发生了变化,故导入导出的 json 数据结构也发生了相应的变化,但考虑存在存量 json 数据,导入数据的接口 [依旧兼容](https://github.com/macacajs/macaca-datahub/pull/193/files#diff-40685415d847ce26a00a7da3ba3dc01fa472310e1aae133de9a545d99727f72bR78) 旧的 json 数据,同时也兼容 swagger 数据的导入,但导出的 json 数据均为新结构的数据。
  4. ```javascript
  5. 原导出的 project json 数据:
  6. [
  7. {
  8. "pathname": "test1",
  9. "method": "ALL",
  10. "description": "interface test",
  11. "uniqId": "7db907e1-e63c-40a5-b2f8-0f5fc1c0322c",
  12. "currentScene": "scene1",
  13. "proxyConfig": {},
  14. "scenes": [
  15. {
  16. "sceneName": "scene1",
  17. "data": {
  18. "success": true,
  19. "data": "default data"
  20. },
  21. "interfaceUniqId": "7db907e1-e63c-40a5-b2f8-0f5fc1c0322c",
  22. "contextConfig": {
  23. "responseDelay": "1",
  24. "responseStatus": 200,
  25. "responseHeaders": {
  26. "set-cookie": "name=sample;domain=.smaple.com"
  27. }
  28. },
  29. "uniqId": "56e0fbec-6419-4b36-a44c-588940649553",
  30. "format": "json",
  31. "createdAt": "2022-01-22T09:57:17.109Z",
  32. "updatedAt": "2022-01-22T09:57:24.785Z"
  33. }
  34. ],
  35. "schemas": []
  36. }
  37. ]
  38. 现导出的 project json 数据:
  39. [
  40. {
  41. "groupName": "默认分组",
  42. "groupUniqId": "b031ecdd-0855-4247-a7d9-24d452cb9e94",
  43. "interfaceList": [
  44. {
  45. "pathname": "test1",
  46. "method": "ALL",
  47. "description": "interface test",
  48. "uniqId": "d915c23f-5be0-4dd9-a88c-d1b576878e0e",
  49. "groupUniqId": "b031ecdd-0855-4247-a7d9-24d452cb9e94",
  50. "currentScene": "scene1",
  51. "proxyConfig": {},
  52. "scenes": [
  53. {
  54. "sceneName": "scene1",
  55. "data": {
  56. "success": true,
  57. "data": "default data"
  58. },
  59. "interfaceUniqId": "d915c23f-5be0-4dd9-a88c-d1b576878e0e",
  60. "contextConfig": {
  61. "responseDelay": "1",
  62. "responseStatus": 200,
  63. "responseHeaders": {
  64. "set-cookie": "name=sample;domain=.smaple.com"
  65. }
  66. },
  67. "uniqId": "6c532901-f11a-41b6-9434-a4a0bc8d4728",
  68. "format": "json",
  69. "createdAt": "2022-02-28T09:13:54.240Z",
  70. "updatedAt": "2022-02-28T09:13:54.240Z"
  71. }
  72. ],
  73. "schemas": []
  74. }
  75. ]
  76. }
  77. ]

单元测试变更

由于新增了场景接口分组功能,导致内部大量单测需要做相应的变化,具体变更见 https://github.com/macacajs/macaca-datahub/pull/193