@hot-updater/server 1.0.0-rc.2 → 1.0.0-rc.21

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 (328) hide show
  1. package/LICENSE +12 -14
  2. package/dist/adapters/drizzle.cjs +32 -75
  3. package/dist/adapters/drizzle.d.cts +13 -5
  4. package/dist/adapters/drizzle.d.mts +13 -5
  5. package/dist/adapters/drizzle.mjs +32 -77
  6. package/dist/adapters/drizzleExecutor.cjs +114 -0
  7. package/dist/adapters/drizzleExecutor.d.cts +9 -0
  8. package/dist/adapters/drizzleExecutor.d.mts +9 -0
  9. package/dist/adapters/drizzleExecutor.mjs +112 -0
  10. package/dist/adapters/kysely.cjs +22 -31
  11. package/dist/adapters/kysely.d.cts +10 -6
  12. package/dist/adapters/kysely.d.mts +10 -6
  13. package/dist/adapters/kysely.mjs +23 -33
  14. package/dist/adapters/kyselyExecutor.cjs +32 -0
  15. package/dist/adapters/kyselyExecutor.d.cts +11 -0
  16. package/dist/adapters/kyselyExecutor.d.mts +11 -0
  17. package/dist/adapters/kyselyExecutor.mjs +32 -0
  18. package/dist/adapters/mongodb.cjs +18 -54
  19. package/dist/adapters/mongodb.d.cts +10 -5
  20. package/dist/adapters/mongodb.d.mts +10 -5
  21. package/dist/adapters/mongodb.mjs +19 -56
  22. package/dist/adapters/mongodbAdapter.cjs +192 -0
  23. package/dist/adapters/mongodbAdapter.d.cts +9 -0
  24. package/dist/adapters/mongodbAdapter.d.mts +9 -0
  25. package/dist/adapters/mongodbAdapter.mjs +191 -0
  26. package/dist/adapters/prisma.cjs +35 -267
  27. package/dist/adapters/prisma.d.cts +12 -7
  28. package/dist/adapters/prisma.d.mts +12 -7
  29. package/dist/adapters/prisma.mjs +36 -268
  30. package/dist/adapters/prismaExecutor.cjs +63 -0
  31. package/dist/adapters/prismaExecutor.mjs +63 -0
  32. package/dist/adapters/sqlProviders.cjs +14 -0
  33. package/dist/adapters/sqlProviders.mjs +14 -0
  34. package/dist/assembly/assemblePlugins.cjs +144 -0
  35. package/dist/assembly/assemblePlugins.d.cts +5 -0
  36. package/dist/assembly/assemblePlugins.d.mts +5 -0
  37. package/dist/assembly/assemblePlugins.mjs +139 -0
  38. package/dist/assembly/clientPlugins.cjs +88 -0
  39. package/dist/assembly/clientPlugins.mjs +88 -0
  40. package/dist/assembly/configError.d.cts +2 -0
  41. package/dist/assembly/configError.d.mts +2 -0
  42. package/dist/assembly/configError.mjs +2 -0
  43. package/dist/core/api.cjs +51 -0
  44. package/dist/core/api.d.cts +54 -0
  45. package/dist/core/api.d.mts +54 -0
  46. package/dist/core/api.mjs +51 -0
  47. package/dist/core/baseCandidates.cjs +28 -0
  48. package/dist/core/baseCandidates.mjs +28 -0
  49. package/dist/core/operations.cjs +351 -0
  50. package/dist/core/operations.mjs +351 -0
  51. package/dist/core/reads.cjs +273 -0
  52. package/dist/core/reads.d.cts +9 -0
  53. package/dist/core/reads.d.mts +9 -0
  54. package/dist/core/reads.mjs +267 -0
  55. package/dist/core/releases.cjs +193 -0
  56. package/dist/core/releases.mjs +190 -0
  57. package/dist/core/writes.cjs +96 -0
  58. package/dist/core/writes.mjs +91 -0
  59. package/dist/createHotUpdaterCore.cjs +72 -91
  60. package/dist/createHotUpdaterCore.d.cts +106 -38
  61. package/dist/createHotUpdaterCore.d.mts +106 -38
  62. package/dist/createHotUpdaterCore.mjs +74 -89
  63. package/dist/db/engineDrizzleSchema.cjs +53 -0
  64. package/dist/db/engineDrizzleSchema.mjs +53 -0
  65. package/dist/db/enginePrismaSchema.cjs +84 -0
  66. package/dist/db/enginePrismaSchema.d.cts +5 -0
  67. package/dist/db/enginePrismaSchema.d.mts +5 -0
  68. package/dist/db/enginePrismaSchema.mjs +83 -0
  69. package/dist/db/releaseCatalog.cjs +24 -47
  70. package/dist/db/releaseCatalog.mjs +20 -44
  71. package/dist/db/updateArtifacts.cjs +43 -72
  72. package/dist/db/updateArtifacts.mjs +40 -69
  73. package/dist/handler.cjs +59 -43
  74. package/dist/handler.d.cts +3 -5
  75. package/dist/handler.d.mts +3 -5
  76. package/dist/handler.mjs +61 -43
  77. package/dist/handlerAdminRoutes.cjs +374 -0
  78. package/dist/handlerAdminRoutes.mjs +372 -0
  79. package/dist/handlerParameters.cjs +6 -11
  80. package/dist/handlerParameters.mjs +6 -10
  81. package/dist/handlerReleaseCatalogRoutes.cjs +32 -15
  82. package/dist/handlerReleaseCatalogRoutes.mjs +32 -15
  83. package/dist/handlerTypes.d.cts +2 -55
  84. package/dist/handlerTypes.d.mts +2 -55
  85. package/dist/handlerVersionRoutes.cjs +20 -4
  86. package/dist/handlerVersionRoutes.mjs +20 -4
  87. package/dist/index.cjs +9 -9
  88. package/dist/index.d.cts +8 -9
  89. package/dist/index.d.mts +8 -9
  90. package/dist/index.mjs +3 -4
  91. package/dist/node.cjs +3 -6
  92. package/dist/node.d.cts +2 -2
  93. package/dist/node.d.mts +2 -2
  94. package/dist/node.mjs +4 -4
  95. package/dist/package.cjs +1 -1
  96. package/dist/package.mjs +1 -1
  97. package/dist/plugins/api-keys/index.cjs +9 -0
  98. package/dist/plugins/api-keys/index.d.cts +1 -0
  99. package/dist/plugins/api-keys/index.d.mts +1 -0
  100. package/dist/plugins/api-keys/index.mjs +2 -0
  101. package/dist/plugins/definePlugin.d.cts +2 -0
  102. package/dist/plugins/definePlugin.d.mts +2 -0
  103. package/dist/plugins/insights/index.cjs +9 -0
  104. package/dist/plugins/insights/index.d.cts +1 -0
  105. package/dist/plugins/insights/index.d.mts +1 -0
  106. package/dist/plugins/insights/index.mjs +2 -0
  107. package/dist/plugins/official.cjs +30 -0
  108. package/dist/plugins/official.mjs +29 -0
  109. package/dist/storageAccess.cjs +13 -10
  110. package/dist/storageAccess.mjs +13 -10
  111. package/dist/types/index.d.cts +2 -28
  112. package/dist/types/index.d.mts +2 -28
  113. package/package.json +26 -14
  114. package/dist/_virtual/_rolldown/runtime.cjs +0 -23
  115. package/dist/adapters/databaseConstraintErrors.cjs +0 -22
  116. package/dist/adapters/databaseConstraintErrors.mjs +0 -21
  117. package/dist/adapters/databasePluginUtils.cjs +0 -116
  118. package/dist/adapters/databasePluginUtils.mjs +0 -106
  119. package/dist/adapters/drizzleCrud.cjs +0 -311
  120. package/dist/adapters/drizzleCrud.mjs +0 -308
  121. package/dist/adapters/drizzleLazyDB.cjs +0 -106
  122. package/dist/adapters/drizzleLazyDB.mjs +0 -106
  123. package/dist/adapters/drizzleQuery.cjs +0 -69
  124. package/dist/adapters/drizzleQuery.mjs +0 -69
  125. package/dist/adapters/insightsTransaction.cjs +0 -22
  126. package/dist/adapters/insightsTransaction.mjs +0 -22
  127. package/dist/adapters/kyselyCrud.cjs +0 -305
  128. package/dist/adapters/kyselyCrud.mjs +0 -304
  129. package/dist/adapters/kyselyQuery.cjs +0 -56
  130. package/dist/adapters/kyselyQuery.mjs +0 -56
  131. package/dist/adapters/mongodbCollections.cjs +0 -38
  132. package/dist/adapters/mongodbCollections.mjs +0 -32
  133. package/dist/adapters/mongodbQuery.cjs +0 -94
  134. package/dist/adapters/mongodbQuery.mjs +0 -86
  135. package/dist/adapters/mongodbReads.cjs +0 -210
  136. package/dist/adapters/mongodbReads.mjs +0 -210
  137. package/dist/adapters/mongodbWrites.cjs +0 -148
  138. package/dist/adapters/mongodbWrites.mjs +0 -148
  139. package/dist/adapters/prismaQuery.cjs +0 -57
  140. package/dist/adapters/prismaQuery.mjs +0 -56
  141. package/dist/adapters/prismaRows.cjs +0 -219
  142. package/dist/adapters/prismaRows.mjs +0 -209
  143. package/dist/apiKeys.cjs +0 -143
  144. package/dist/apiKeys.d.cts +0 -49
  145. package/dist/apiKeys.d.mts +0 -49
  146. package/dist/apiKeys.mjs +0 -133
  147. package/dist/db/createBundleDiff.cjs +0 -144
  148. package/dist/db/createBundleDiff.d.cts +0 -16
  149. package/dist/db/createBundleDiff.d.mts +0 -16
  150. package/dist/db/createBundleDiff.mjs +0 -141
  151. package/dist/db/databasePluginCore.cjs +0 -137
  152. package/dist/db/databasePluginCore.mjs +0 -137
  153. package/dist/db/fixedMigrator.cjs +0 -79
  154. package/dist/db/fixedMigrator.mjs +0 -78
  155. package/dist/db/fixedMigratorMongo.cjs +0 -321
  156. package/dist/db/fixedMigratorMongo.mjs +0 -321
  157. package/dist/db/fixedMigratorShared.cjs +0 -23
  158. package/dist/db/fixedMigratorShared.mjs +0 -18
  159. package/dist/db/index.cjs +0 -27
  160. package/dist/db/index.d.cts +0 -12
  161. package/dist/db/index.d.mts +0 -12
  162. package/dist/db/index.mjs +0 -22
  163. package/dist/db/mongoMigrationExecution.cjs +0 -11
  164. package/dist/db/mongoMigrationExecution.mjs +0 -11
  165. package/dist/db/releaseCatalog.d.cts +0 -16
  166. package/dist/db/releaseCatalog.d.mts +0 -16
  167. package/dist/db/schema/insightsCollation.cjs +0 -14
  168. package/dist/db/schema/insightsCollation.mjs +0 -14
  169. package/dist/db/schema/mongodb.cjs +0 -25
  170. package/dist/db/schema/mongodb.mjs +0 -25
  171. package/dist/db/schema/registry.cjs +0 -21
  172. package/dist/db/schema/registry.mjs +0 -17
  173. package/dist/db/schema/sql.cjs +0 -105
  174. package/dist/db/schema/sql.mjs +0 -97
  175. package/dist/db/schema/sqlOperations.cjs +0 -27
  176. package/dist/db/schema/sqlOperations.mjs +0 -26
  177. package/dist/db/schemaGenerators.cjs +0 -275
  178. package/dist/db/schemaGenerators.mjs +0 -273
  179. package/dist/db/schemaReadiness.cjs +0 -26
  180. package/dist/db/schemaReadiness.d.cts +0 -9
  181. package/dist/db/schemaReadiness.d.mts +0 -9
  182. package/dist/db/schemaReadiness.mjs +0 -25
  183. package/dist/db/sqlMigrationExecution.cjs +0 -67
  184. package/dist/db/sqlMigrationExecution.mjs +0 -67
  185. package/dist/db/types.cjs +0 -22
  186. package/dist/db/types.d.cts +0 -118
  187. package/dist/db/types.d.mts +0 -118
  188. package/dist/db/types.mjs +0 -19
  189. package/dist/handlerBundleRoutes.cjs +0 -155
  190. package/dist/handlerBundleRoutes.mjs +0 -155
  191. package/dist/handlerReleaseManagementRoutes.cjs +0 -124
  192. package/dist/handlerReleaseManagementRoutes.mjs +0 -124
  193. package/dist/insights/domain.d.cts +0 -89
  194. package/dist/insights/domain.d.mts +0 -89
  195. package/dist/insights/errors.cjs +0 -15
  196. package/dist/insights/errors.mjs +0 -14
  197. package/dist/insights/eventInput.cjs +0 -155
  198. package/dist/insights/eventInput.mjs +0 -153
  199. package/dist/insights/provider.cjs +0 -314
  200. package/dist/insights/provider.d.cts +0 -6
  201. package/dist/insights/provider.d.mts +0 -6
  202. package/dist/insights/provider.mjs +0 -314
  203. package/dist/insights/queryInput.cjs +0 -93
  204. package/dist/insights/queryInput.mjs +0 -91
  205. package/dist/insights/routes.cjs +0 -58
  206. package/dist/insights/routes.mjs +0 -56
  207. package/dist/insights/types.d.cts +0 -32
  208. package/dist/insights/types.d.mts +0 -32
  209. package/dist/schema/dsl.cjs +0 -103
  210. package/dist/schema/dsl.mjs +0 -87
  211. package/dist/schema/index.cjs +0 -20
  212. package/dist/schema/index.mjs +0 -7
  213. package/dist/schema/settings.cjs +0 -9
  214. package/dist/schema/settings.mjs +0 -9
  215. package/dist/schema/types.cjs +0 -8
  216. package/dist/schema/types.mjs +0 -6
  217. package/dist/schema/v1_0_0.cjs +0 -406
  218. package/dist/schema/v1_0_0.mjs +0 -398
  219. package/src/adapters/databaseConstraintErrors.ts +0 -25
  220. package/src/adapters/databasePluginTestDatabase.ts +0 -120
  221. package/src/adapters/databasePluginUtils.spec.ts +0 -29
  222. package/src/adapters/databasePluginUtils.ts +0 -214
  223. package/src/adapters/drizzle.spec.ts +0 -336
  224. package/src/adapters/drizzle.ts +0 -162
  225. package/src/adapters/drizzleCrud.ts +0 -571
  226. package/src/adapters/drizzleLazyDB.ts +0 -271
  227. package/src/adapters/drizzleQuery.ts +0 -164
  228. package/src/adapters/insightsTransaction.ts +0 -32
  229. package/src/adapters/kysely.spec.ts +0 -174
  230. package/src/adapters/kysely.ts +0 -110
  231. package/src/adapters/kyselyCrud.ts +0 -709
  232. package/src/adapters/kyselyQuery.ts +0 -128
  233. package/src/adapters/mongodb.integration.spec.ts +0 -210
  234. package/src/adapters/mongodb.spec.ts +0 -209
  235. package/src/adapters/mongodb.ts +0 -138
  236. package/src/adapters/mongodbCollections.ts +0 -67
  237. package/src/adapters/mongodbQuery.ts +0 -255
  238. package/src/adapters/mongodbReads.ts +0 -380
  239. package/src/adapters/mongodbRows.spec.ts +0 -64
  240. package/src/adapters/mongodbRows.ts +0 -118
  241. package/src/adapters/mongodbTestClient.ts +0 -377
  242. package/src/adapters/mongodbTestFilter.ts +0 -169
  243. package/src/adapters/mongodbWrites.ts +0 -287
  244. package/src/adapters/prisma.spec.ts +0 -293
  245. package/src/adapters/prisma.ts +0 -565
  246. package/src/adapters/prismaQuery.ts +0 -129
  247. package/src/adapters/prismaRows.spec.ts +0 -98
  248. package/src/adapters/prismaRows.ts +0 -337
  249. package/src/adapters/prismaTestClient.ts +0 -413
  250. package/src/apiKeys.integration.spec.ts +0 -192
  251. package/src/apiKeys.spec.ts +0 -162
  252. package/src/apiKeys.ts +0 -220
  253. package/src/calculatePagination.ts +0 -34
  254. package/src/createHotUpdaterCore.ts +0 -246
  255. package/src/db/bundleRows.spec.ts +0 -164
  256. package/src/db/bundleRows.ts +0 -10
  257. package/src/db/createBundleDiff.spec.ts +0 -450
  258. package/src/db/createBundleDiff.ts +0 -364
  259. package/src/db/databasePluginCore.spec.ts +0 -62
  260. package/src/db/databasePluginCore.testFixtures.ts +0 -76
  261. package/src/db/databasePluginCore.ts +0 -185
  262. package/src/db/databasePluginUpdateCheck.bench.ts +0 -110
  263. package/src/db/fixedMigrator.spec.ts +0 -296
  264. package/src/db/fixedMigrator.ts +0 -147
  265. package/src/db/fixedMigratorMongo.spec.ts +0 -239
  266. package/src/db/fixedMigratorMongo.ts +0 -425
  267. package/src/db/fixedMigratorShared.ts +0 -36
  268. package/src/db/hotUpdaterSchema.ts +0 -5
  269. package/src/db/index.spec.ts +0 -1269
  270. package/src/db/index.ts +0 -47
  271. package/src/db/mongoMigrationExecution.ts +0 -23
  272. package/src/db/node-sqlite.d.ts +0 -25
  273. package/src/db/releaseCatalog.ts +0 -148
  274. package/src/db/schema/definitions.ts +0 -1
  275. package/src/db/schema/insightsCollation.ts +0 -21
  276. package/src/db/schema/mongodb.ts +0 -40
  277. package/src/db/schema/registry.ts +0 -55
  278. package/src/db/schema/sql.ts +0 -244
  279. package/src/db/schema/sqlOperations.ts +0 -63
  280. package/src/db/schema/types.ts +0 -1
  281. package/src/db/schemaGenerators.ts +0 -494
  282. package/src/db/schemaReadiness.ts +0 -34
  283. package/src/db/sqlMigrationExecution.spec.ts +0 -65
  284. package/src/db/sqlMigrationExecution.ts +0 -108
  285. package/src/db/types.spec.ts +0 -63
  286. package/src/db/types.ts +0 -228
  287. package/src/db/updateArtifacts.spec.ts +0 -359
  288. package/src/db/updateArtifacts.ts +0 -541
  289. package/src/handler-standalone.integration.spec.ts +0 -528
  290. package/src/handler.spec.ts +0 -190
  291. package/src/handler.testFixtures.ts +0 -30
  292. package/src/handler.ts +0 -245
  293. package/src/handlerAuth.integration.spec.ts +0 -33
  294. package/src/handlerBundleRoutes.ts +0 -259
  295. package/src/handlerErrors.ts +0 -7
  296. package/src/handlerManagement.spec.ts +0 -262
  297. package/src/handlerParameters.ts +0 -105
  298. package/src/handlerReleaseCatalogRoutes.ts +0 -154
  299. package/src/handlerReleaseManagementRoutes.ts +0 -213
  300. package/src/handlerTypes.ts +0 -96
  301. package/src/handlerVersionRoutes.ts +0 -12
  302. package/src/index.ts +0 -30
  303. package/src/insights/domain.ts +0 -103
  304. package/src/insights/errors.ts +0 -11
  305. package/src/insights/eventInput.ts +0 -225
  306. package/src/insights/insights.integration.spec.ts +0 -381
  307. package/src/insights/provider.spec.ts +0 -385
  308. package/src/insights/provider.ts +0 -591
  309. package/src/insights/queryInput.ts +0 -141
  310. package/src/insights/routes.ts +0 -104
  311. package/src/insights/types.ts +0 -55
  312. package/src/internalRouter.ts +0 -104
  313. package/src/node.spec.ts +0 -119
  314. package/src/node.ts +0 -111
  315. package/src/releaseCatalogRoutes.integration.spec.ts +0 -238
  316. package/src/runtime.spec.ts +0 -194
  317. package/src/runtime.testFixtures.ts +0 -71
  318. package/src/schema/dsl-all-versions.spec.ts +0 -37
  319. package/src/schema/dsl.ts +0 -187
  320. package/src/schema/index.ts +0 -10
  321. package/src/schema/settings.ts +0 -15
  322. package/src/schema/types.ts +0 -79
  323. package/src/schema/v1_0_0.spec.ts +0 -220
  324. package/src/schema/v1_0_0.ts +0 -529
  325. package/src/storageAccess.spec.ts +0 -152
  326. package/src/storageAccess.ts +0 -141
  327. package/src/types/index.ts +0 -34
  328. package/src/version.ts +0 -3
@@ -1,239 +0,0 @@
1
- import { MongoClient } from "mongodb";
2
- import { describe, expect, it, vi } from "vitest";
3
-
4
- import { createInMemoryDatabasePlugin } from "../../../test-utils/test/inMemoryDatabasePlugin";
5
- import { HOT_UPDATER_SCHEMA_VERSION } from "../schema/types";
6
- import { createDatabasePluginCore } from "./databasePluginCore";
7
- import { createMongoMigrator } from "./fixedMigrator";
8
- import { createSchemaReadinessChecker } from "./schemaReadiness";
9
-
10
- function createSettingsMongoClient(
11
- values: Readonly<Record<string, unknown>>,
12
- ): MongoClient {
13
- return {
14
- db: () => ({
15
- collection: () => ({
16
- find: ({ key }: { readonly key: string }) => ({
17
- limit: () => ({
18
- toArray: async () => {
19
- const value = values[key];
20
- return value === undefined ? [] : [{ key, value }];
21
- },
22
- }),
23
- }),
24
- listIndexes: () => ({
25
- toArray: async () => [{ key: { key: 1 }, unique: true }],
26
- }),
27
- }),
28
- }),
29
- } as unknown as MongoClient;
30
- }
31
-
32
- describe("MongoDB migration", () => {
33
- it("creates the current schema from an empty database", async () => {
34
- const settings = new Map<string, unknown>();
35
- const settingsCollection = {
36
- find: ({ key }: { readonly key: string }) => ({
37
- limit: () => ({
38
- toArray: async () => {
39
- const value = settings.get(key);
40
- return value === undefined ? [] : [{ key, value }];
41
- },
42
- }),
43
- }),
44
- createIndex: vi.fn(async (): Promise<string> => "key_1"),
45
- listIndexes: () => ({ toArray: async () => [] }),
46
- updateOne: async (
47
- { key }: { readonly key: string },
48
- update: { readonly $set: { readonly value: unknown } },
49
- ) => void settings.set(key, update.$set.value),
50
- };
51
- const modelCollection = {
52
- find: () => ({ toArray: async () => [] }),
53
- listIndexes: () => ({ toArray: async () => [] }),
54
- createIndex: vi.fn(async () => "created"),
55
- };
56
- const command = vi.fn(
57
- async (_input: Record<string, unknown>): Promise<void> => undefined,
58
- );
59
- const database = {
60
- command,
61
- createCollection: async () => undefined,
62
- collection: (name: string) =>
63
- name === "private_hot_updater_settings"
64
- ? settingsCollection
65
- : modelCollection,
66
- };
67
- const client = { db: () => database } as unknown as MongoClient;
68
-
69
- await (
70
- await createMongoMigrator(client).migrateToLatest({
71
- mode: "from-schema",
72
- updateSettings: true,
73
- })
74
- ).execute();
75
-
76
- expect(settingsCollection.createIndex).toHaveBeenCalledWith(
77
- { key: 1 },
78
- { unique: true },
79
- );
80
- expect(settings.get("schema.core")).toBe(HOT_UPDATER_SCHEMA_VERSION);
81
- expect(modelCollection.createIndex).toHaveBeenCalledWith(
82
- { install_id: 1 },
83
- {
84
- name: "bundle_installations_install_id_idx",
85
- unique: true,
86
- collation: { locale: "simple" },
87
- },
88
- );
89
- expect(modelCollection.createIndex).toHaveBeenCalledWith(
90
- {
91
- type: 1,
92
- platform: 1,
93
- channel: 1,
94
- from_bundle_id: 1,
95
- received_at_ms: 1,
96
- id: 1,
97
- },
98
- {
99
- name: "bundle_events_from_bundle_idx",
100
- collation: { locale: "simple" },
101
- },
102
- );
103
- expect(modelCollection.createIndex).toHaveBeenCalledWith(
104
- {
105
- type: 1,
106
- platform: 1,
107
- channel: 1,
108
- to_bundle_id: 1,
109
- received_at_ms: 1,
110
- id: 1,
111
- },
112
- { name: "bundle_events_to_bundle_idx", collation: { locale: "simple" } },
113
- );
114
- expect(modelCollection.createIndex).toHaveBeenCalledWith(
115
- { platform: 1, channel: 1, to_bundle_id: 1, received_at_ms: 1 },
116
- {
117
- name: "bundle_installations_bundle_idx",
118
- collation: { locale: "simple" },
119
- },
120
- );
121
- const commands = command.mock.calls.map(([input]) => input);
122
- expect(commands.find(({ collMod }) => collMod === "bundles")).toMatchObject(
123
- {
124
- validationAction: "error",
125
- validationLevel: "strict",
126
- validator: {
127
- $and: [
128
- {
129
- $jsonSchema: {
130
- properties: {
131
- archive_byte_size: {
132
- bsonType: ["double", "int", "long"],
133
- maximum: Number.MAX_SAFE_INTEGER,
134
- minimum: 0,
135
- },
136
- },
137
- required: expect.arrayContaining(["archive_byte_size"]),
138
- },
139
- },
140
- {
141
- $expr: {
142
- $eq: [{ $trunc: "$archive_byte_size" }, "$archive_byte_size"],
143
- },
144
- },
145
- ],
146
- },
147
- },
148
- );
149
- expect(
150
- commands.find(({ collMod }) => collMod === "bundle_patches"),
151
- ).toMatchObject({
152
- validationAction: "error",
153
- validationLevel: "strict",
154
- validator: {
155
- $and: [
156
- {
157
- $jsonSchema: {
158
- properties: {
159
- byte_size: {
160
- bsonType: ["double", "int", "long"],
161
- maximum: Number.MAX_SAFE_INTEGER,
162
- minimum: 0,
163
- },
164
- },
165
- required: expect.arrayContaining(["byte_size"]),
166
- },
167
- },
168
- {
169
- $expr: {
170
- $and: expect.arrayContaining([
171
- {
172
- $eq: [{ $trunc: "$byte_size" }, "$byte_size"],
173
- },
174
- ]),
175
- },
176
- },
177
- ],
178
- },
179
- });
180
- });
181
-
182
- it("is a no-op when schema.core is current", async () => {
183
- const client = createSettingsMongoClient({
184
- "schema.core": HOT_UPDATER_SCHEMA_VERSION,
185
- });
186
- const migrator = createMongoMigrator(client);
187
-
188
- await expect(migrator.getVersion()).resolves.toBe(
189
- HOT_UPDATER_SCHEMA_VERSION,
190
- );
191
- await expect(
192
- migrator.migrateToLatest({ mode: "from-schema" }),
193
- ).resolves.toMatchObject({ operations: [] });
194
- });
195
-
196
- it("ignores a leftover version marker when schema.core is current", async () => {
197
- const client = createSettingsMongoClient({
198
- "schema.core": HOT_UPDATER_SCHEMA_VERSION,
199
- version: "0.36.0",
200
- });
201
- const migrator = createMongoMigrator(client);
202
-
203
- await expect(migrator.getVersion()).resolves.toBe(
204
- HOT_UPDATER_SCHEMA_VERSION,
205
- );
206
- await expect(
207
- migrator.migrateToLatest({ mode: "from-schema" }),
208
- ).resolves.toMatchObject({ operations: [] });
209
- });
210
-
211
- it("rejects in-place upgrade from a v0 schema marker", async () => {
212
- const client = createSettingsMongoClient({ "schema.core": "0.38.0" });
213
- const migrator = createMongoMigrator(client);
214
-
215
- await expect(
216
- migrator.migrateToLatest({ mode: "from-schema" }),
217
- ).rejects.toThrow("Hot Updater v1 cannot migrate schema 0.38.0");
218
- });
219
-
220
- it("blocks v0 schema readiness before reading bundle data", async () => {
221
- const client = createSettingsMongoClient({ version: "0.36.0" });
222
- const migrator = createMongoMigrator(client);
223
- const plugin = createInMemoryDatabasePlugin();
224
- const count = vi.spyOn(plugin.models.bundles, "count");
225
- const findMany = vi.spyOn(plugin.models.bundles, "findMany");
226
- const core = createDatabasePluginCore(plugin, async () => null, {
227
- beforeOperation: createSchemaReadinessChecker(
228
- "v0-mongodb",
229
- () => migrator,
230
- ),
231
- });
232
-
233
- await expect(core.api.getBundles({ limit: 1 })).rejects.toThrow(
234
- "Hot Updater v1 cannot migrate schema 0.36.0",
235
- );
236
- expect(count).not.toHaveBeenCalled();
237
- expect(findMany).not.toHaveBeenCalled();
238
- });
239
- });
@@ -1,425 +0,0 @@
1
- import type { MongoClient } from "mongodb";
2
-
3
- import {
4
- HOT_UPDATER_CORE_SCHEMA_KEY,
5
- HOT_UPDATER_SCHEMA_VERSION,
6
- HOT_UPDATER_SETTINGS_TABLE,
7
- } from "../schema/types";
8
- import {
9
- assertCurrentOrEmptySchemaVersion,
10
- assertSupportedMigrationMode,
11
- getEmptyMigrationResult,
12
- inferLegacyCoreSchemaVersion,
13
- isCurrentSchemaVersion,
14
- } from "./fixedMigratorShared";
15
- import { executeMongoMigration } from "./mongoMigrationExecution";
16
- import { createMongoMigrationOperations } from "./schema/mongodb";
17
- import {
18
- hotUpdaterSchema,
19
- schemaIndexAppliesToProvider,
20
- } from "./schema/registry";
21
- import type {
22
- MigrateOptions,
23
- MigrationOperation,
24
- MigrationResult,
25
- Migrator,
26
- } from "./types";
27
-
28
- const isMongoNamespaceExistsError = (error: unknown): boolean => {
29
- if (!(error instanceof Error)) return false;
30
- const mongoError = error as { code?: unknown; codeName?: unknown };
31
- return mongoError.code === 48 || mongoError.codeName === "NamespaceExists";
32
- };
33
-
34
- const isMongoNamespaceNotFoundError = (error: unknown): boolean => {
35
- if (!(error instanceof Error)) return false;
36
- const mongoError = error as { code?: unknown; codeName?: unknown };
37
- return mongoError.code === 26 || mongoError.codeName === "NamespaceNotFound";
38
- };
39
-
40
- const ignoreExistingCollection = (error: unknown): undefined => {
41
- if (isMongoNamespaceExistsError(error)) {
42
- return undefined;
43
- }
44
- throw error;
45
- };
46
-
47
- const mongoNullableString = { bsonType: ["string", "null"] } as const;
48
- const mongoByteSize = {
49
- bsonType: ["double", "int", "long"],
50
- maximum: Number.MAX_SAFE_INTEGER,
51
- minimum: 0,
52
- } as const;
53
-
54
- const mongoBundleValidator = {
55
- $and: [
56
- {
57
- $jsonSchema: {
58
- bsonType: "object",
59
- properties: {
60
- archive_byte_size: mongoByteSize,
61
- asset_base_storage_uri: mongoNullableString,
62
- file_hash: { bsonType: "string" },
63
- git_commit_hash: mongoNullableString,
64
- id: { bsonType: "string" },
65
- manifest_file_hash: mongoNullableString,
66
- manifest_storage_uri: mongoNullableString,
67
- metadata: { bsonType: "object" },
68
- platform: { enum: ["ios", "android"] },
69
- storage_uri: { bsonType: "string" },
70
- },
71
- required: [
72
- "id",
73
- "platform",
74
- "file_hash",
75
- "git_commit_hash",
76
- "storage_uri",
77
- "archive_byte_size",
78
- "metadata",
79
- "manifest_storage_uri",
80
- "manifest_file_hash",
81
- "asset_base_storage_uri",
82
- ],
83
- },
84
- },
85
- {
86
- $expr: {
87
- $eq: [{ $trunc: "$archive_byte_size" }, "$archive_byte_size"],
88
- },
89
- },
90
- ],
91
- } as const;
92
-
93
- const mongoPatchValidator = {
94
- $and: [
95
- {
96
- $jsonSchema: {
97
- bsonType: "object",
98
- properties: {
99
- base_bundle_id: { bsonType: "string" },
100
- base_file_hash: { bsonType: "string" },
101
- bundle_id: { bsonType: "string" },
102
- id: { bsonType: "string" },
103
- order_index: mongoByteSize,
104
- byte_size: mongoByteSize,
105
- patch_file_hash: { bsonType: "string" },
106
- patch_storage_uri: { bsonType: "string" },
107
- },
108
- required: [
109
- "id",
110
- "bundle_id",
111
- "base_bundle_id",
112
- "base_file_hash",
113
- "patch_file_hash",
114
- "patch_storage_uri",
115
- "byte_size",
116
- "order_index",
117
- ],
118
- },
119
- },
120
- {
121
- $expr: {
122
- $and: [
123
- { $eq: [{ $trunc: "$order_index" }, "$order_index"] },
124
- { $eq: [{ $trunc: "$byte_size" }, "$byte_size"] },
125
- ],
126
- },
127
- },
128
- ],
129
- } as const;
130
-
131
- const mongoReleaseValidator = {
132
- $jsonSchema: {
133
- bsonType: "object",
134
- properties: {
135
- bundle_id: mongoNullableString,
136
- channel_id: { bsonType: "string" },
137
- created_at_ms: { bsonType: ["double", "int", "long"] },
138
- enabled: { bsonType: "bool" },
139
- fingerprint_hash: mongoNullableString,
140
- id: { bsonType: "string" },
141
- kind: { enum: ["BUNDLE", "EMBEDDED"] },
142
- message: mongoNullableString,
143
- operation: { enum: ["DEPLOY", "PROMOTE", "ROLLBACK"] },
144
- platform: { enum: ["ios", "android"] },
145
- revision: { bsonType: ["int", "long"] },
146
- rollout_cohort_count: { bsonType: ["int", "long"] },
147
- scope_key: { bsonType: "string" },
148
- should_force_update: { bsonType: "bool" },
149
- source_release_id: mongoNullableString,
150
- strategy: { enum: ["APP_VERSION", "FINGERPRINT"] },
151
- target_app_version: mongoNullableString,
152
- target_cohorts: { bsonType: "array", items: { bsonType: "string" } },
153
- updated_at_ms: { bsonType: ["double", "int", "long"] },
154
- },
155
- required: [
156
- "id",
157
- "revision",
158
- "scope_key",
159
- "channel_id",
160
- "platform",
161
- "kind",
162
- "bundle_id",
163
- "strategy",
164
- "target_app_version",
165
- "fingerprint_hash",
166
- "enabled",
167
- "should_force_update",
168
- "message",
169
- "rollout_cohort_count",
170
- "target_cohorts",
171
- "operation",
172
- "source_release_id",
173
- "created_at_ms",
174
- "updated_at_ms",
175
- ],
176
- },
177
- } as const;
178
-
179
- const mongoReleaseCatalogValidator = {
180
- $jsonSchema: {
181
- bsonType: "object",
182
- properties: {
183
- catalog_id: { bsonType: "string" },
184
- byte_size: { bsonType: ["int", "long"] },
185
- catalog_hash: { bsonType: "string" },
186
- channel_id: { bsonType: "string" },
187
- channel_key: { bsonType: "string" },
188
- fingerprint_hash: mongoNullableString,
189
- generation: { bsonType: ["double", "int", "long"] },
190
- is_tombstone: { bsonType: "bool" },
191
- payload: { bsonType: "string" },
192
- platform: { enum: ["ios", "android"] },
193
- scope_key: { bsonType: "string" },
194
- strategy: { enum: ["APP_VERSION", "FINGERPRINT"] },
195
- updated_at_ms: { bsonType: ["double", "int", "long"] },
196
- },
197
- required: [
198
- "scope_key",
199
- "catalog_id",
200
- "strategy",
201
- "channel_id",
202
- "channel_key",
203
- "platform",
204
- "fingerprint_hash",
205
- "generation",
206
- "payload",
207
- "catalog_hash",
208
- "byte_size",
209
- "is_tombstone",
210
- "updated_at_ms",
211
- ],
212
- },
213
- } as const;
214
-
215
- const createSettingsKeyIndexOperation = (): MigrationOperation => ({
216
- description: `Ensure unique MongoDB index: ${HOT_UPDATER_SETTINGS_TABLE}(key)`,
217
- type: "custom",
218
- });
219
-
220
- export const createMongoMigrator = (client: MongoClient): Migrator => {
221
- const settings = client
222
- .db()
223
- .collection<{ key: string; value: unknown }>(HOT_UPDATER_SETTINGS_TABLE);
224
- const getSetting = async (key: string): Promise<string | undefined> => {
225
- const rows = await settings.find({ key }).limit(2).toArray();
226
- const [row, duplicate] = rows;
227
- if (!row) return undefined;
228
- if (duplicate) {
229
- throw new Error(`Duplicate Hot Updater schema setting: ${key}`);
230
- }
231
- if (typeof row.value !== "string") {
232
- throw new Error(`Invalid Hot Updater schema setting: ${key}`);
233
- }
234
- return row.value;
235
- };
236
- const getCoreVersion = (): Promise<string | undefined> =>
237
- getSetting(HOT_UPDATER_CORE_SCHEMA_KEY);
238
- const getSchemaVersions = async (): Promise<{
239
- readonly coreVersion: string | undefined;
240
- readonly legacyCoreVersion: string | undefined;
241
- }> => {
242
- const coreVersion = await getCoreVersion();
243
- const legacyCoreVersion = inferLegacyCoreSchemaVersion(
244
- await getSetting("version"),
245
- );
246
- return { coreVersion, legacyCoreVersion };
247
- };
248
- const getVersion = async (): Promise<string | undefined> => {
249
- const { coreVersion, legacyCoreVersion } = await getSchemaVersions();
250
- return coreVersion ?? legacyCoreVersion;
251
- };
252
- const getSettingsKeyIndexState = async (): Promise<
253
- "missing" | "non-unique" | "unique"
254
- > => {
255
- const indexes = await settings
256
- .listIndexes()
257
- .toArray()
258
- .catch((error: unknown) => {
259
- if (isMongoNamespaceNotFoundError(error)) return [];
260
- throw error;
261
- });
262
- const index = indexes.find(({ key }) => {
263
- const fields = Object.entries(key);
264
- return (
265
- fields.length === 1 && fields[0]?.[0] === "key" && fields[0][1] === 1
266
- );
267
- });
268
- if (!index) return "missing";
269
- return index.unique === true &&
270
- index.sparse !== true &&
271
- index.partialFilterExpression === undefined
272
- ? "unique"
273
- : "non-unique";
274
- };
275
- const rejectNonUniqueSettingsKeyIndex = (): never => {
276
- throw new Error(
277
- "Hot Updater settings key index must enforce uniqueness for every key.",
278
- );
279
- };
280
- const ensureSettingsKeyIndex = async (): Promise<void> => {
281
- const state = await getSettingsKeyIndexState();
282
- if (state === "unique") return;
283
- if (state === "non-unique") rejectNonUniqueSettingsKeyIndex();
284
- await settings.createIndex({ key: 1 }, { unique: true });
285
- };
286
- const makeResult = async (
287
- options: MigrateOptions = {},
288
- ): Promise<MigrationResult> => {
289
- assertSupportedMigrationMode(options);
290
-
291
- const { coreVersion, legacyCoreVersion } = await getSchemaVersions();
292
- const settingsKeyIndexState = await getSettingsKeyIndexState();
293
- if (settingsKeyIndexState === "non-unique") {
294
- rejectNonUniqueSettingsKeyIndex();
295
- }
296
- if (isCurrentSchemaVersion(coreVersion)) {
297
- if (settingsKeyIndexState === "unique") {
298
- return getEmptyMigrationResult();
299
- }
300
- return {
301
- ...getEmptyMigrationResult(),
302
- execute: ensureSettingsKeyIndex,
303
- operations: [createSettingsKeyIndexOperation()],
304
- };
305
- }
306
- assertCurrentOrEmptySchemaVersion(coreVersion ?? legacyCoreVersion);
307
- const settingsOperation =
308
- options.updateSettings === false
309
- ? undefined
310
- : ({
311
- type: "custom",
312
- key: HOT_UPDATER_CORE_SCHEMA_KEY,
313
- value: HOT_UPDATER_SCHEMA_VERSION,
314
- } satisfies MigrationOperation);
315
- return {
316
- operations: [
317
- createSettingsKeyIndexOperation(),
318
- ...createMongoMigrationOperations(settingsOperation),
319
- ],
320
- execute: async () => {
321
- const db = client.db();
322
- await executeMongoMigration({
323
- updateSettings: options.updateSettings !== false,
324
- backend: {
325
- ensureCollections: async () => {
326
- for (const table of hotUpdaterSchema.tables) {
327
- if (table.internal) continue;
328
- await db
329
- .createCollection(table.ormName)
330
- .catch(ignoreExistingCollection);
331
- }
332
- },
333
- ensureIndexes: async () => {
334
- await ensureSettingsKeyIndex();
335
- for (const table of hotUpdaterSchema.tables) {
336
- if (table.internal) continue;
337
- const isInsights =
338
- table.ormName === "bundle_events" ||
339
- table.ormName === "bundle_installations";
340
- const collection = db.collection(table.ormName);
341
- const collation = isInsights ? { locale: "simple" } : undefined;
342
- const primaryKey = table.columns.find(
343
- (column) => column.primaryKey,
344
- );
345
- if (!primaryKey) {
346
- throw new Error(
347
- `MongoDB table ${table.ormName} does not define a primary key.`,
348
- );
349
- }
350
- const idIndexName = `${table.ormName}_${primaryKey.ormName}_idx`;
351
- await collection.createIndex(
352
- { [primaryKey.ormName]: 1 },
353
- {
354
- name: idIndexName,
355
- unique: true,
356
- ...(collation ? { collation } : {}),
357
- },
358
- );
359
- for (const index of (table.indexes ?? []).filter((item) =>
360
- schemaIndexAppliesToProvider(item, "mongodb"),
361
- )) {
362
- await collection.createIndex(
363
- Object.fromEntries(
364
- index.columns.map((column) => [column, 1]),
365
- ),
366
- {
367
- name: index.name,
368
- ...(index.unique ? { unique: true } : {}),
369
- ...(collation ? { collation } : {}),
370
- },
371
- );
372
- }
373
- }
374
- },
375
- enforceSchema: async () => {
376
- for (const [collection, validator] of [
377
- ["bundles", mongoBundleValidator],
378
- ["bundle_patches", mongoPatchValidator],
379
- ["releases", mongoReleaseValidator],
380
- ["release_catalogs", mongoReleaseCatalogValidator],
381
- ] as const) {
382
- await db.command({
383
- collMod: collection,
384
- validationAction: "error",
385
- validationLevel: "strict",
386
- validator,
387
- });
388
- }
389
- },
390
- updateVersion: async () => {
391
- await settings.updateOne(
392
- { key: HOT_UPDATER_CORE_SCHEMA_KEY },
393
- { $set: { value: HOT_UPDATER_SCHEMA_VERSION } },
394
- { upsert: true },
395
- );
396
- },
397
- },
398
- });
399
- },
400
- };
401
- };
402
-
403
- return {
404
- getVersion,
405
- getNameVariants: async () => undefined,
406
- next: async () => {
407
- const { coreVersion } = await getSchemaVersions();
408
- return isCurrentSchemaVersion(coreVersion)
409
- ? undefined
410
- : { version: HOT_UPDATER_SCHEMA_VERSION };
411
- },
412
- previous: async () => undefined,
413
- up: makeResult,
414
- down: async () => {
415
- throw new Error("No previous schema to migrate to.");
416
- },
417
- migrateTo: async (version, options) => {
418
- if (version !== HOT_UPDATER_SCHEMA_VERSION) {
419
- throw new Error(`Invalid version ${version}`);
420
- }
421
- return makeResult(options);
422
- },
423
- migrateToLatest: makeResult,
424
- };
425
- };
@@ -1,36 +0,0 @@
1
- import { HOT_UPDATER_SCHEMA_VERSION } from "../schema/types";
2
- import type { MigrateOptions, MigrationResult } from "./types";
3
-
4
- export const getEmptyMigrationResult = (): MigrationResult => ({
5
- operations: [],
6
- execute: async () => {},
7
- getSQL: () => "",
8
- });
9
-
10
- export const assertSupportedMigrationMode = (options: MigrateOptions): void => {
11
- if (options.mode === "from-database") {
12
- throw new Error("Hot Updater migrations support only mode: 'from-schema'.");
13
- }
14
- };
15
-
16
- export const unsupportedSchemaUpgradeMessage = (version: string): string =>
17
- `Hot Updater v1 cannot migrate schema ${version} in place. Create a new empty database and run migrate or generate against schema ${HOT_UPDATER_SCHEMA_VERSION}.`;
18
-
19
- export const assertCurrentOrEmptySchemaVersion = (
20
- currentVersion: string | undefined,
21
- ): void => {
22
- if (
23
- currentVersion !== undefined &&
24
- currentVersion !== HOT_UPDATER_SCHEMA_VERSION
25
- ) {
26
- throw new Error(unsupportedSchemaUpgradeMessage(currentVersion));
27
- }
28
- };
29
-
30
- export const inferLegacyCoreSchemaVersion = (
31
- legacyVersion: string | undefined,
32
- ): string | undefined => legacyVersion;
33
-
34
- export const isCurrentSchemaVersion = (
35
- currentVersion: string | undefined,
36
- ): boolean => currentVersion === HOT_UPDATER_SCHEMA_VERSION;
@@ -1,5 +0,0 @@
1
- export * from "../schema";
2
- export * from "./schema/mongodb";
3
- export * from "./schema/registry";
4
- export * from "./schema/sql";
5
- export * from "./schema/sqlOperations";