@budibase/backend-core 2.9.19 → 2.9.20

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.
Files changed (241) hide show
  1. package/dist/package.json +19 -4
  2. package/dist/src/security/permissions.d.ts +1 -1
  3. package/package.json +19 -4
  4. package/dist/tsconfig.build.tsbuildinfo +0 -1
  5. package/src/accounts/accounts.ts +0 -82
  6. package/src/accounts/api.ts +0 -59
  7. package/src/accounts/index.ts +0 -1
  8. package/src/auth/auth.ts +0 -208
  9. package/src/auth/index.ts +0 -1
  10. package/src/auth/tests/auth.spec.ts +0 -14
  11. package/src/blacklist/blacklist.ts +0 -54
  12. package/src/blacklist/index.ts +0 -1
  13. package/src/blacklist/tests/blacklist.spec.ts +0 -46
  14. package/src/cache/appMetadata.ts +0 -88
  15. package/src/cache/base/index.ts +0 -92
  16. package/src/cache/generic.ts +0 -30
  17. package/src/cache/index.ts +0 -5
  18. package/src/cache/tests/writethrough.spec.ts +0 -138
  19. package/src/cache/user.ts +0 -69
  20. package/src/cache/writethrough.ts +0 -133
  21. package/src/configs/configs.ts +0 -257
  22. package/src/configs/index.ts +0 -1
  23. package/src/configs/tests/configs.spec.ts +0 -184
  24. package/src/constants/db.ts +0 -63
  25. package/src/constants/index.ts +0 -2
  26. package/src/constants/misc.ts +0 -50
  27. package/src/context/Context.ts +0 -14
  28. package/src/context/identity.ts +0 -58
  29. package/src/context/index.ts +0 -3
  30. package/src/context/mainContext.ts +0 -310
  31. package/src/context/tests/index.spec.ts +0 -147
  32. package/src/context/types.ts +0 -11
  33. package/src/db/Replication.ts +0 -84
  34. package/src/db/constants.ts +0 -10
  35. package/src/db/couch/DatabaseImpl.ts +0 -238
  36. package/src/db/couch/connections.ts +0 -77
  37. package/src/db/couch/index.ts +0 -5
  38. package/src/db/couch/pouchDB.ts +0 -97
  39. package/src/db/couch/pouchDump.ts +0 -0
  40. package/src/db/couch/utils.ts +0 -50
  41. package/src/db/db.ts +0 -39
  42. package/src/db/errors.ts +0 -14
  43. package/src/db/index.ts +0 -12
  44. package/src/db/lucene.ts +0 -732
  45. package/src/db/searchIndexes/index.ts +0 -1
  46. package/src/db/searchIndexes/searchIndexes.ts +0 -62
  47. package/src/db/tests/index.spec.js +0 -25
  48. package/src/db/tests/lucene.spec.ts +0 -298
  49. package/src/db/tests/pouch.spec.js +0 -62
  50. package/src/db/tests/utils.spec.ts +0 -63
  51. package/src/db/utils.ts +0 -207
  52. package/src/db/views.ts +0 -241
  53. package/src/docIds/conversions.ts +0 -59
  54. package/src/docIds/ids.ts +0 -113
  55. package/src/docIds/index.ts +0 -2
  56. package/src/docIds/newid.ts +0 -5
  57. package/src/docIds/params.ts +0 -174
  58. package/src/docUpdates/index.ts +0 -29
  59. package/src/environment.ts +0 -201
  60. package/src/errors/errors.ts +0 -119
  61. package/src/errors/index.ts +0 -1
  62. package/src/events/analytics.ts +0 -6
  63. package/src/events/asyncEvents/index.ts +0 -2
  64. package/src/events/asyncEvents/publisher.ts +0 -12
  65. package/src/events/asyncEvents/queue.ts +0 -22
  66. package/src/events/backfill.ts +0 -183
  67. package/src/events/documentId.ts +0 -56
  68. package/src/events/events.ts +0 -40
  69. package/src/events/identification.ts +0 -310
  70. package/src/events/index.ts +0 -14
  71. package/src/events/processors/AnalyticsProcessor.ts +0 -64
  72. package/src/events/processors/AuditLogsProcessor.ts +0 -93
  73. package/src/events/processors/LoggingProcessor.ts +0 -37
  74. package/src/events/processors/Processors.ts +0 -52
  75. package/src/events/processors/async/DocumentUpdateProcessor.ts +0 -43
  76. package/src/events/processors/index.ts +0 -19
  77. package/src/events/processors/posthog/PosthogProcessor.ts +0 -118
  78. package/src/events/processors/posthog/index.ts +0 -2
  79. package/src/events/processors/posthog/rateLimiting.ts +0 -106
  80. package/src/events/processors/posthog/tests/PosthogProcessor.spec.ts +0 -168
  81. package/src/events/processors/types.ts +0 -1
  82. package/src/events/publishers/account.ts +0 -35
  83. package/src/events/publishers/app.ts +0 -155
  84. package/src/events/publishers/auditLog.ts +0 -26
  85. package/src/events/publishers/auth.ts +0 -73
  86. package/src/events/publishers/automation.ts +0 -110
  87. package/src/events/publishers/backfill.ts +0 -74
  88. package/src/events/publishers/backup.ts +0 -42
  89. package/src/events/publishers/datasource.ts +0 -48
  90. package/src/events/publishers/email.ts +0 -17
  91. package/src/events/publishers/environmentVariable.ts +0 -38
  92. package/src/events/publishers/group.ts +0 -99
  93. package/src/events/publishers/index.ts +0 -24
  94. package/src/events/publishers/installation.ts +0 -38
  95. package/src/events/publishers/layout.ts +0 -26
  96. package/src/events/publishers/license.ts +0 -84
  97. package/src/events/publishers/org.ts +0 -37
  98. package/src/events/publishers/plugin.ts +0 -47
  99. package/src/events/publishers/query.ts +0 -88
  100. package/src/events/publishers/role.ts +0 -62
  101. package/src/events/publishers/rows.ts +0 -29
  102. package/src/events/publishers/screen.ts +0 -36
  103. package/src/events/publishers/serve.ts +0 -43
  104. package/src/events/publishers/table.ts +0 -70
  105. package/src/events/publishers/user.ts +0 -202
  106. package/src/events/publishers/view.ts +0 -107
  107. package/src/featureFlags/index.ts +0 -77
  108. package/src/featureFlags/tests/featureFlags.spec.ts +0 -85
  109. package/src/helpers.ts +0 -9
  110. package/src/index.ts +0 -53
  111. package/src/installation.ts +0 -107
  112. package/src/logging/alerts.ts +0 -26
  113. package/src/logging/correlation/correlation.ts +0 -13
  114. package/src/logging/correlation/index.ts +0 -1
  115. package/src/logging/correlation/middleware.ts +0 -17
  116. package/src/logging/index.ts +0 -4
  117. package/src/logging/pino/logger.ts +0 -232
  118. package/src/logging/pino/middleware.ts +0 -45
  119. package/src/logging/system.ts +0 -81
  120. package/src/logging/tests/system.spec.ts +0 -61
  121. package/src/middleware/adminOnly.ts +0 -9
  122. package/src/middleware/auditLog.ts +0 -6
  123. package/src/middleware/authenticated.ts +0 -193
  124. package/src/middleware/builderOnly.ts +0 -20
  125. package/src/middleware/builderOrAdmin.ts +0 -20
  126. package/src/middleware/csrf.ts +0 -81
  127. package/src/middleware/errorHandling.ts +0 -29
  128. package/src/middleware/index.ts +0 -21
  129. package/src/middleware/internalApi.ts +0 -23
  130. package/src/middleware/joi-validator.ts +0 -45
  131. package/src/middleware/matchers.ts +0 -47
  132. package/src/middleware/passport/datasource/google.ts +0 -95
  133. package/src/middleware/passport/local.ts +0 -54
  134. package/src/middleware/passport/sso/google.ts +0 -77
  135. package/src/middleware/passport/sso/oidc.ts +0 -154
  136. package/src/middleware/passport/sso/sso.ts +0 -165
  137. package/src/middleware/passport/sso/tests/google.spec.ts +0 -67
  138. package/src/middleware/passport/sso/tests/oidc.spec.ts +0 -152
  139. package/src/middleware/passport/sso/tests/sso.spec.ts +0 -197
  140. package/src/middleware/passport/utils.ts +0 -38
  141. package/src/middleware/querystringToBody.ts +0 -28
  142. package/src/middleware/tenancy.ts +0 -36
  143. package/src/middleware/tests/builder.spec.ts +0 -180
  144. package/src/middleware/tests/matchers.spec.ts +0 -134
  145. package/src/migrations/definitions.ts +0 -40
  146. package/src/migrations/index.ts +0 -2
  147. package/src/migrations/migrations.ts +0 -191
  148. package/src/migrations/tests/__snapshots__/migrations.spec.ts.snap +0 -11
  149. package/src/migrations/tests/migrations.spec.ts +0 -64
  150. package/src/objectStore/buckets/app.ts +0 -40
  151. package/src/objectStore/buckets/global.ts +0 -29
  152. package/src/objectStore/buckets/index.ts +0 -3
  153. package/src/objectStore/buckets/plugins.ts +0 -71
  154. package/src/objectStore/buckets/tests/app.spec.ts +0 -171
  155. package/src/objectStore/buckets/tests/global.spec.ts +0 -74
  156. package/src/objectStore/buckets/tests/plugins.spec.ts +0 -111
  157. package/src/objectStore/cloudfront.ts +0 -41
  158. package/src/objectStore/index.ts +0 -3
  159. package/src/objectStore/objectStore.ts +0 -440
  160. package/src/objectStore/utils.ts +0 -27
  161. package/src/platform/index.ts +0 -3
  162. package/src/platform/platformDb.ts +0 -6
  163. package/src/platform/tenants.ts +0 -101
  164. package/src/platform/tests/tenants.spec.ts +0 -26
  165. package/src/platform/users.ts +0 -90
  166. package/src/plugin/index.ts +0 -1
  167. package/src/plugin/tests/validation.spec.ts +0 -83
  168. package/src/plugin/utils.ts +0 -156
  169. package/src/queue/constants.ts +0 -6
  170. package/src/queue/inMemoryQueue.ts +0 -141
  171. package/src/queue/index.ts +0 -2
  172. package/src/queue/listeners.ts +0 -195
  173. package/src/queue/queue.ts +0 -54
  174. package/src/redis/index.ts +0 -6
  175. package/src/redis/init.ts +0 -86
  176. package/src/redis/redis.ts +0 -308
  177. package/src/redis/redlockImpl.ts +0 -139
  178. package/src/redis/utils.ts +0 -117
  179. package/src/security/encryption.ts +0 -179
  180. package/src/security/permissions.ts +0 -159
  181. package/src/security/roles.ts +0 -420
  182. package/src/security/sessions.ts +0 -120
  183. package/src/security/tests/encryption.spec.ts +0 -31
  184. package/src/security/tests/permissions.spec.ts +0 -145
  185. package/src/security/tests/sessions.spec.ts +0 -12
  186. package/src/tenancy/db.ts +0 -6
  187. package/src/tenancy/index.ts +0 -2
  188. package/src/tenancy/tenancy.ts +0 -140
  189. package/src/tenancy/tests/tenancy.spec.ts +0 -184
  190. package/src/timers/index.ts +0 -1
  191. package/src/timers/timers.ts +0 -22
  192. package/src/users/db.ts +0 -460
  193. package/src/users/events.ts +0 -176
  194. package/src/users/index.ts +0 -4
  195. package/src/users/lookup.ts +0 -102
  196. package/src/users/users.ts +0 -276
  197. package/src/users/utils.ts +0 -55
  198. package/src/utils/hashing.ts +0 -14
  199. package/src/utils/index.ts +0 -3
  200. package/src/utils/stringUtils.ts +0 -8
  201. package/src/utils/tests/utils.spec.ts +0 -191
  202. package/src/utils/utils.ts +0 -239
  203. package/tests/core/logging.ts +0 -34
  204. package/tests/core/utilities/index.ts +0 -6
  205. package/tests/core/utilities/jestUtils.ts +0 -30
  206. package/tests/core/utilities/mocks/alerts.ts +0 -3
  207. package/tests/core/utilities/mocks/date.ts +0 -2
  208. package/tests/core/utilities/mocks/events.ts +0 -131
  209. package/tests/core/utilities/mocks/fetch.ts +0 -17
  210. package/tests/core/utilities/mocks/index.ts +0 -10
  211. package/tests/core/utilities/mocks/licenses.ts +0 -107
  212. package/tests/core/utilities/mocks/posthog.ts +0 -7
  213. package/tests/core/utilities/structures/Chance.ts +0 -20
  214. package/tests/core/utilities/structures/accounts.ts +0 -115
  215. package/tests/core/utilities/structures/apps.ts +0 -21
  216. package/tests/core/utilities/structures/common.ts +0 -7
  217. package/tests/core/utilities/structures/db.ts +0 -12
  218. package/tests/core/utilities/structures/documents/index.ts +0 -1
  219. package/tests/core/utilities/structures/documents/platform/index.ts +0 -1
  220. package/tests/core/utilities/structures/documents/platform/installation.ts +0 -12
  221. package/tests/core/utilities/structures/generator.ts +0 -2
  222. package/tests/core/utilities/structures/index.ts +0 -15
  223. package/tests/core/utilities/structures/koa.ts +0 -16
  224. package/tests/core/utilities/structures/licenses.ts +0 -167
  225. package/tests/core/utilities/structures/plugins.ts +0 -19
  226. package/tests/core/utilities/structures/quotas.ts +0 -67
  227. package/tests/core/utilities/structures/scim.ts +0 -80
  228. package/tests/core/utilities/structures/shared.ts +0 -19
  229. package/tests/core/utilities/structures/sso.ts +0 -119
  230. package/tests/core/utilities/structures/tenants.ts +0 -5
  231. package/tests/core/utilities/structures/userGroups.ts +0 -10
  232. package/tests/core/utilities/structures/users.ts +0 -73
  233. package/tests/core/utilities/testContainerUtils.ts +0 -98
  234. package/tests/core/utilities/utils/index.ts +0 -1
  235. package/tests/core/utilities/utils/time.ts +0 -3
  236. package/tests/extra/DBTestConfiguration.ts +0 -36
  237. package/tests/extra/index.ts +0 -2
  238. package/tests/extra/testEnv.ts +0 -95
  239. package/tests/index.ts +0 -1
  240. package/tests/jestEnv.ts +0 -6
  241. package/tests/jestSetup.ts +0 -28
@@ -1,180 +0,0 @@
1
- import adminOnly from "../adminOnly"
2
- import builderOnly from "../builderOnly"
3
- import builderOrAdmin from "../builderOrAdmin"
4
- import { structures } from "../../../tests"
5
- import { ContextUser, ServiceType } from "@budibase/types"
6
- import { doInAppContext } from "../../context"
7
- import env from "../../environment"
8
- env._set("SERVICE_TYPE", ServiceType.APPS)
9
-
10
- const appId = "app_aaa"
11
- const basicUser = structures.users.user()
12
- const adminUser = structures.users.adminUser()
13
- const adminOnlyUser = structures.users.adminOnlyUser()
14
- const builderUser = structures.users.builderUser()
15
- const appBuilderUser = structures.users.appBuilderUser(appId)
16
-
17
- function buildUserCtx(user: ContextUser) {
18
- return {
19
- internal: false,
20
- user,
21
- throw: jest.fn(),
22
- } as any
23
- }
24
-
25
- function passed(throwFn: jest.Func, nextFn: jest.Func) {
26
- expect(throwFn).not.toBeCalled()
27
- expect(nextFn).toBeCalled()
28
- }
29
-
30
- function threw(throwFn: jest.Func) {
31
- // cant check next, the throw function doesn't actually throw - so it still continues
32
- expect(throwFn).toBeCalled()
33
- }
34
-
35
- describe("adminOnly middleware", () => {
36
- it("should allow admin user", () => {
37
- const ctx = buildUserCtx(adminUser),
38
- next = jest.fn()
39
- adminOnly(ctx, next)
40
- passed(ctx.throw, next)
41
- })
42
-
43
- it("should not allow basic user", () => {
44
- const ctx = buildUserCtx(basicUser),
45
- next = jest.fn()
46
- adminOnly(ctx, next)
47
- threw(ctx.throw)
48
- })
49
-
50
- it("should not allow builder user", () => {
51
- const ctx = buildUserCtx(builderUser),
52
- next = jest.fn()
53
- adminOnly(ctx, next)
54
- threw(ctx.throw)
55
- })
56
- })
57
-
58
- describe("builderOnly middleware", () => {
59
- it("should allow builder user", () => {
60
- const ctx = buildUserCtx(builderUser),
61
- next = jest.fn()
62
- builderOnly(ctx, next)
63
- passed(ctx.throw, next)
64
- })
65
-
66
- it("should allow app builder user", () => {
67
- const ctx = buildUserCtx(appBuilderUser),
68
- next = jest.fn()
69
- doInAppContext(appId, () => {
70
- builderOnly(ctx, next)
71
- })
72
- passed(ctx.throw, next)
73
- })
74
-
75
- it("should allow admin and builder user", () => {
76
- const ctx = buildUserCtx(adminUser),
77
- next = jest.fn()
78
- builderOnly(ctx, next)
79
- passed(ctx.throw, next)
80
- })
81
-
82
- it("should not allow admin user", () => {
83
- const ctx = buildUserCtx(adminOnlyUser),
84
- next = jest.fn()
85
- builderOnly(ctx, next)
86
- threw(ctx.throw)
87
- })
88
-
89
- it("should not allow app builder user to different app", () => {
90
- const ctx = buildUserCtx(appBuilderUser),
91
- next = jest.fn()
92
- doInAppContext("app_bbb", () => {
93
- builderOnly(ctx, next)
94
- })
95
- threw(ctx.throw)
96
- })
97
-
98
- it("should not allow basic user", () => {
99
- const ctx = buildUserCtx(basicUser),
100
- next = jest.fn()
101
- builderOnly(ctx, next)
102
- threw(ctx.throw)
103
- })
104
- })
105
-
106
- describe("builderOrAdmin middleware", () => {
107
- it("should allow builder user", () => {
108
- const ctx = buildUserCtx(builderUser),
109
- next = jest.fn()
110
- builderOrAdmin(ctx, next)
111
- passed(ctx.throw, next)
112
- })
113
-
114
- it("should allow builder and admin user", () => {
115
- const ctx = buildUserCtx(adminUser),
116
- next = jest.fn()
117
- builderOrAdmin(ctx, next)
118
- passed(ctx.throw, next)
119
- })
120
-
121
- it("should allow admin user", () => {
122
- const ctx = buildUserCtx(adminOnlyUser),
123
- next = jest.fn()
124
- builderOrAdmin(ctx, next)
125
- passed(ctx.throw, next)
126
- })
127
-
128
- it("should allow app builder user", () => {
129
- const ctx = buildUserCtx(appBuilderUser),
130
- next = jest.fn()
131
- doInAppContext(appId, () => {
132
- builderOrAdmin(ctx, next)
133
- })
134
- passed(ctx.throw, next)
135
- })
136
-
137
- it("should not allow basic user", () => {
138
- const ctx = buildUserCtx(basicUser),
139
- next = jest.fn()
140
- builderOrAdmin(ctx, next)
141
- threw(ctx.throw)
142
- })
143
- })
144
-
145
- describe("check service difference", () => {
146
- it("should not allow without app ID in apps", () => {
147
- env._set("SERVICE_TYPE", ServiceType.APPS)
148
- const appId = "app_a"
149
- const ctx = buildUserCtx({
150
- ...basicUser,
151
- builder: {
152
- apps: [appId],
153
- },
154
- })
155
- const next = jest.fn()
156
- doInAppContext(appId, () => {
157
- builderOnly(ctx, next)
158
- })
159
- passed(ctx.throw, next)
160
- doInAppContext("app_b", () => {
161
- builderOnly(ctx, next)
162
- })
163
- threw(ctx.throw)
164
- })
165
-
166
- it("should allow without app ID in worker", () => {
167
- env._set("SERVICE_TYPE", ServiceType.WORKER)
168
- const ctx = buildUserCtx({
169
- ...basicUser,
170
- builder: {
171
- apps: ["app_a"],
172
- },
173
- })
174
- const next = jest.fn()
175
- doInAppContext("app_b", () => {
176
- builderOnly(ctx, next)
177
- })
178
- passed(ctx.throw, next)
179
- })
180
- })
@@ -1,134 +0,0 @@
1
- import * as matchers from "../matchers"
2
- import { structures } from "../../../tests"
3
-
4
- describe("matchers", () => {
5
- it("matches by path and method", () => {
6
- const pattern = [
7
- {
8
- route: "/api/tests",
9
- method: "POST",
10
- },
11
- ]
12
- const ctx = structures.koa.newContext()
13
- ctx.request.url = "/api/tests"
14
- ctx.request.method = "POST"
15
-
16
- const built = matchers.buildMatcherRegex(pattern)
17
-
18
- expect(!!matchers.matches(ctx, built)).toBe(true)
19
- })
20
-
21
- it("wildcards path", () => {
22
- const pattern = [
23
- {
24
- route: "/api/tests",
25
- method: "POST",
26
- },
27
- ]
28
- const ctx = structures.koa.newContext()
29
- ctx.request.url = "/api/tests/id/something/else"
30
- ctx.request.method = "POST"
31
-
32
- const built = matchers.buildMatcherRegex(pattern)
33
-
34
- expect(!!matchers.matches(ctx, built)).toBe(true)
35
- })
36
-
37
- it("doesn't wildcard path with strict", () => {
38
- const pattern = [
39
- {
40
- route: "/api/tests",
41
- method: "POST",
42
- strict: true,
43
- },
44
- ]
45
- const ctx = structures.koa.newContext()
46
- ctx.request.url = "/api/tests/id/something/else"
47
- ctx.request.method = "POST"
48
-
49
- const built = matchers.buildMatcherRegex(pattern)
50
-
51
- expect(!!matchers.matches(ctx, built)).toBe(false)
52
- })
53
-
54
- it("matches with param", () => {
55
- const pattern = [
56
- {
57
- route: "/api/tests/:testId",
58
- method: "GET",
59
- },
60
- ]
61
- const ctx = structures.koa.newContext()
62
- ctx.request.url = "/api/tests/id"
63
- ctx.request.method = "GET"
64
-
65
- const built = matchers.buildMatcherRegex(pattern)
66
-
67
- expect(!!matchers.matches(ctx, built)).toBe(true)
68
- })
69
-
70
- // TODO: Support the below behaviour
71
- // Strict does not work when a param is present
72
- // it("matches with param with strict", () => {
73
- // const pattern = [{
74
- // route: "/api/tests/:testId",
75
- // method: "GET",
76
- // strict: true
77
- // }]
78
- // const ctx = structures.koa.newContext()
79
- // ctx.request.url = "/api/tests/id"
80
- // ctx.request.method = "GET"
81
- //
82
- // const built = matchers.buildMatcherRegex(pattern)
83
- //
84
- // expect(!!matchers.matches(ctx, built)).toBe(true)
85
- // })
86
-
87
- it("doesn't match by path", () => {
88
- const pattern = [
89
- {
90
- route: "/api/tests",
91
- method: "POST",
92
- },
93
- ]
94
- const ctx = structures.koa.newContext()
95
- ctx.request.url = "/api/unknown"
96
- ctx.request.method = "POST"
97
-
98
- const built = matchers.buildMatcherRegex(pattern)
99
-
100
- expect(!!matchers.matches(ctx, built)).toBe(false)
101
- })
102
-
103
- it("doesn't match by method", () => {
104
- const pattern = [
105
- {
106
- route: "/api/tests",
107
- method: "POST",
108
- },
109
- ]
110
- const ctx = structures.koa.newContext()
111
- ctx.request.url = "/api/tests"
112
- ctx.request.method = "GET"
113
-
114
- const built = matchers.buildMatcherRegex(pattern)
115
-
116
- expect(!!matchers.matches(ctx, built)).toBe(false)
117
- })
118
-
119
- it("matches by path and wildcard method", () => {
120
- const pattern = [
121
- {
122
- route: "/api/tests",
123
- method: "ALL",
124
- },
125
- ]
126
- const ctx = structures.koa.newContext()
127
- ctx.request.url = "/api/tests"
128
- ctx.request.method = "GET"
129
-
130
- const built = matchers.buildMatcherRegex(pattern)
131
-
132
- expect(!!matchers.matches(ctx, built)).toBe(true)
133
- })
134
- })
@@ -1,40 +0,0 @@
1
- import {
2
- MigrationType,
3
- MigrationName,
4
- MigrationDefinition,
5
- } from "@budibase/types"
6
-
7
- export const DEFINITIONS: MigrationDefinition[] = [
8
- {
9
- type: MigrationType.GLOBAL,
10
- name: MigrationName.USER_EMAIL_VIEW_CASING,
11
- },
12
- {
13
- type: MigrationType.GLOBAL,
14
- name: MigrationName.SYNC_QUOTAS,
15
- },
16
- {
17
- type: MigrationType.APP,
18
- name: MigrationName.APP_URLS,
19
- },
20
- {
21
- type: MigrationType.APP,
22
- name: MigrationName.EVENT_APP_BACKFILL,
23
- },
24
- {
25
- type: MigrationType.APP,
26
- name: MigrationName.TABLE_SETTINGS_LINKS_TO_ACTIONS,
27
- },
28
- {
29
- type: MigrationType.GLOBAL,
30
- name: MigrationName.EVENT_GLOBAL_BACKFILL,
31
- },
32
- {
33
- type: MigrationType.INSTALLATION,
34
- name: MigrationName.EVENT_INSTALLATION_BACKFILL,
35
- },
36
- {
37
- type: MigrationType.GLOBAL,
38
- name: MigrationName.GLOBAL_INFO_SYNC_USERS,
39
- },
40
- ]
@@ -1,2 +0,0 @@
1
- export * from "./migrations"
2
- export * from "./definitions"
@@ -1,191 +0,0 @@
1
- import { DEFAULT_TENANT_ID } from "../constants"
2
- import {
3
- DocumentType,
4
- StaticDatabases,
5
- getAllApps,
6
- getGlobalDBName,
7
- getDB,
8
- } from "../db"
9
- import environment from "../environment"
10
- import * as platform from "../platform"
11
- import * as context from "../context"
12
- import { DEFINITIONS } from "."
13
- import {
14
- Migration,
15
- MigrationOptions,
16
- MigrationType,
17
- MigrationNoOpOptions,
18
- App,
19
- } from "@budibase/types"
20
-
21
- export const getMigrationsDoc = async (db: any) => {
22
- // get the migrations doc
23
- try {
24
- return await db.get(DocumentType.MIGRATIONS)
25
- } catch (err: any) {
26
- if (err.status && err.status === 404) {
27
- return { _id: DocumentType.MIGRATIONS }
28
- } else {
29
- console.error(err)
30
- throw err
31
- }
32
- }
33
- }
34
-
35
- export const backPopulateMigrations = async (opts: MigrationNoOpOptions) => {
36
- // filter migrations to the type and populate a no-op migration
37
- const migrations: Migration[] = DEFINITIONS.filter(
38
- def => def.type === opts.type
39
- ).map(d => ({ ...d, fn: () => {} }))
40
- await runMigrations(migrations, { noOp: opts })
41
- }
42
-
43
- export const runMigration = async (
44
- migration: Migration,
45
- options: MigrationOptions = {}
46
- ) => {
47
- const migrationType = migration.type
48
- let tenantId: string | undefined
49
- if (migrationType !== MigrationType.INSTALLATION) {
50
- tenantId = context.getTenantId()
51
- }
52
- const migrationName = migration.name
53
- const silent = migration.silent
54
-
55
- const log = (message: string) => {
56
- if (!silent) {
57
- console.log(message)
58
- }
59
- }
60
-
61
- // get the db to store the migration in
62
- let dbNames: string[]
63
- if (migrationType === MigrationType.GLOBAL) {
64
- dbNames = [getGlobalDBName()]
65
- } else if (migrationType === MigrationType.APP) {
66
- if (options.noOp) {
67
- if (!options.noOp.appId) {
68
- throw new Error("appId is required for noOp app migration")
69
- }
70
- dbNames = [options.noOp.appId]
71
- } else {
72
- const apps = (await getAllApps(migration.appOpts)) as App[]
73
- dbNames = apps.map(app => app.appId)
74
- }
75
- } else if (migrationType === MigrationType.INSTALLATION) {
76
- dbNames = [StaticDatabases.PLATFORM_INFO.name]
77
- } else {
78
- throw new Error(`Unrecognised migration type [${migrationType}]`)
79
- }
80
-
81
- const length = dbNames.length
82
- let count = 0
83
-
84
- // run the migration against each db
85
- for (const dbName of dbNames) {
86
- count++
87
- const lengthStatement = length > 1 ? `[${count}/${length}]` : ""
88
-
89
- const db = getDB(dbName)
90
-
91
- try {
92
- const doc = await getMigrationsDoc(db)
93
-
94
- // the migration has already been run
95
- if (doc[migrationName]) {
96
- // check for force
97
- if (
98
- options.force &&
99
- options.force[migrationType] &&
100
- options.force[migrationType].includes(migrationName)
101
- ) {
102
- log(`[Migration: ${migrationName}] [DB: ${dbName}] Forcing`)
103
- } else {
104
- // no force, exit
105
- return
106
- }
107
- }
108
-
109
- // check if the migration is not a no-op
110
- if (!options.noOp) {
111
- log(
112
- `[Migration: ${migrationName}] [DB: ${dbName}] Running ${lengthStatement}`
113
- )
114
-
115
- if (migration.preventRetry) {
116
- // eagerly set the completion date
117
- // so that we never run this migration twice even upon failure
118
- doc[migrationName] = Date.now()
119
- const response = await db.put(doc)
120
- doc._rev = response.rev
121
- }
122
-
123
- // run the migration
124
- if (migrationType === MigrationType.APP) {
125
- await context.doInAppContext(db.name, async () => {
126
- await migration.fn(db)
127
- })
128
- } else {
129
- await migration.fn(db)
130
- }
131
-
132
- log(`[Migration: ${migrationName}] [DB: ${dbName}] Complete`)
133
- }
134
-
135
- // mark as complete
136
- doc[migrationName] = Date.now()
137
- await db.put(doc)
138
- } catch (err) {
139
- console.error(
140
- `[Migration: ${migrationName}] [DB: ${dbName}] Error: `,
141
- err
142
- )
143
- throw err
144
- }
145
- }
146
- }
147
-
148
- export const runMigrations = async (
149
- migrations: Migration[],
150
- options: MigrationOptions = {}
151
- ) => {
152
- let tenantIds
153
-
154
- if (environment.MULTI_TENANCY) {
155
- if (options.noOp) {
156
- tenantIds = [options.noOp.tenantId]
157
- } else if (!options.tenantIds || !options.tenantIds.length) {
158
- // run for all tenants
159
- tenantIds = await platform.tenants.getTenantIds()
160
- } else {
161
- tenantIds = options.tenantIds
162
- }
163
- } else {
164
- // single tenancy
165
- tenantIds = [DEFAULT_TENANT_ID]
166
- }
167
-
168
- if (tenantIds.length > 1) {
169
- console.log(`Checking migrations for ${tenantIds.length} tenants`)
170
- } else {
171
- console.log("Checking migrations")
172
- }
173
-
174
- let count = 0
175
- // for all tenants
176
- for (const tenantId of tenantIds) {
177
- count++
178
- if (tenantIds.length > 1) {
179
- console.log(`Progress [${count}/${tenantIds.length}]`)
180
- }
181
- // for all migrations
182
- for (const migration of migrations) {
183
- // run the migration
184
- await context.doInTenant(
185
- tenantId,
186
- async () => await runMigration(migration, options)
187
- )
188
- }
189
- }
190
- console.log("Migrations complete")
191
- }
@@ -1,11 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`migrations should match snapshot 1`] = `
4
- {
5
- "_id": "migrations",
6
- "_rev": "1-2f64479842a0513aa8b97f356b0b9127",
7
- "createdAt": "2020-01-01T00:00:00.000Z",
8
- "test": 1577836800000,
9
- "updatedAt": "2020-01-01T00:00:00.000Z",
10
- }
11
- `;
@@ -1,64 +0,0 @@
1
- import { testEnv, DBTestConfiguration } from "../../../tests/extra"
2
- import * as migrations from "../index"
3
- import * as context from "../../context"
4
- import { MigrationType } from "@budibase/types"
5
-
6
- testEnv.multiTenant()
7
-
8
- describe("migrations", () => {
9
- const config = new DBTestConfiguration()
10
-
11
- const migrationFunction = jest.fn()
12
-
13
- const MIGRATIONS = [
14
- {
15
- type: MigrationType.GLOBAL,
16
- name: "test" as any,
17
- fn: migrationFunction,
18
- },
19
- ]
20
-
21
- beforeEach(() => {
22
- config.newTenant()
23
- })
24
-
25
- afterEach(async () => {
26
- jest.clearAllMocks()
27
- })
28
-
29
- const migrate = () => {
30
- return migrations.runMigrations(MIGRATIONS, {
31
- tenantIds: [config.tenantId],
32
- })
33
- }
34
-
35
- it("should run a new migration", async () => {
36
- await config.doInTenant(async () => {
37
- await migrate()
38
- expect(migrationFunction).toHaveBeenCalled()
39
- const db = context.getGlobalDB()
40
- const doc = await migrations.getMigrationsDoc(db)
41
- expect(doc.test).toBeDefined()
42
- })
43
- })
44
-
45
- it("should match snapshot", async () => {
46
- await config.doInTenant(async () => {
47
- await migrate()
48
- const doc = await migrations.getMigrationsDoc(context.getGlobalDB())
49
- expect(doc).toMatchSnapshot()
50
- })
51
- })
52
-
53
- it("should skip a previously run migration", async () => {
54
- await config.doInTenant(async () => {
55
- const db = context.getGlobalDB()
56
- await migrate()
57
- const previousDoc = await migrations.getMigrationsDoc(db)
58
- await migrate()
59
- const currentDoc = await migrations.getMigrationsDoc(db)
60
- expect(migrationFunction).toHaveBeenCalledTimes(1)
61
- expect(currentDoc.test).toBe(previousDoc.test)
62
- })
63
- })
64
- })
@@ -1,40 +0,0 @@
1
- import env from "../../environment"
2
- import * as objectStore from "../objectStore"
3
- import * as cloudfront from "../cloudfront"
4
-
5
- /**
6
- * In production the client library is stored in the object store, however in development
7
- * we use the symlinked version produced by lerna, located in node modules. We link to this
8
- * via a specific endpoint (under /api/assets/client).
9
- * @param {string} appId In production we need the appId to look up the correct bucket, as the
10
- * version of the client lib may differ between apps.
11
- * @param {string} version The version to retrieve.
12
- * @return {string} The URL to be inserted into appPackage response or server rendered
13
- * app index file.
14
- */
15
- export const clientLibraryUrl = (appId: string, version: string) => {
16
- if (env.isProd()) {
17
- let file = `${objectStore.sanitizeKey(appId)}/budibase-client.js`
18
- if (env.CLOUDFRONT_CDN) {
19
- // append app version to bust the cache
20
- if (version) {
21
- file += `?v=${version}`
22
- }
23
- // don't need to use presigned for client with cloudfront
24
- // file is public
25
- return cloudfront.getUrl(file)
26
- } else {
27
- return objectStore.getPresignedUrl(env.APPS_BUCKET_NAME, file)
28
- }
29
- } else {
30
- return `/api/assets/client`
31
- }
32
- }
33
-
34
- export const getAppFileUrl = (s3Key: string) => {
35
- if (env.CLOUDFRONT_CDN) {
36
- return cloudfront.getPresignedUrl(s3Key)
37
- } else {
38
- return objectStore.getPresignedUrl(env.APPS_BUCKET_NAME, s3Key)
39
- }
40
- }
@@ -1,29 +0,0 @@
1
- import env from "../../environment"
2
- import * as context from "../../context"
3
- import * as objectStore from "../objectStore"
4
- import * as cloudfront from "../cloudfront"
5
-
6
- // URLs
7
-
8
- export const getGlobalFileUrl = (type: string, name: string, etag?: string) => {
9
- let file = getGlobalFileS3Key(type, name)
10
- if (env.CLOUDFRONT_CDN) {
11
- if (etag) {
12
- file = `${file}?etag=${etag}`
13
- }
14
- return cloudfront.getPresignedUrl(file)
15
- } else {
16
- return objectStore.getPresignedUrl(env.GLOBAL_BUCKET_NAME, file)
17
- }
18
- }
19
-
20
- // KEYS
21
-
22
- export const getGlobalFileS3Key = (type: string, name: string) => {
23
- let file = `${type}/${name}`
24
- if (env.MULTI_TENANCY) {
25
- const tenantId = context.getTenantId()
26
- file = `${tenantId}/${file}`
27
- }
28
- return file
29
- }
@@ -1,3 +0,0 @@
1
- export * from "./app"
2
- export * from "./global"
3
- export * from "./plugins"