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

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 (191) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +1 -1
  2. package/dist/adapters/databaseConstraintErrors.cjs +0 -1
  3. package/dist/adapters/databasePluginUtils.cjs +0 -1
  4. package/dist/adapters/drizzle.cjs +10 -3
  5. package/dist/adapters/drizzle.d.cts +0 -1
  6. package/dist/adapters/drizzle.d.mts +0 -1
  7. package/dist/adapters/drizzle.mjs +11 -3
  8. package/dist/adapters/drizzleCrud.cjs +62 -1
  9. package/dist/adapters/drizzleCrud.mjs +61 -2
  10. package/dist/adapters/drizzleLazyDB.cjs +6 -1
  11. package/dist/adapters/drizzleLazyDB.mjs +6 -1
  12. package/dist/adapters/drizzleQuery.cjs +3 -2
  13. package/dist/adapters/drizzleQuery.mjs +3 -1
  14. package/dist/adapters/insightsTransaction.cjs +22 -0
  15. package/dist/adapters/insightsTransaction.mjs +22 -0
  16. package/dist/adapters/kysely.cjs +2 -1
  17. package/dist/adapters/kysely.d.cts +0 -1
  18. package/dist/adapters/kysely.d.mts +0 -1
  19. package/dist/adapters/kysely.mjs +3 -1
  20. package/dist/adapters/kyselyCrud.cjs +54 -3
  21. package/dist/adapters/kyselyCrud.mjs +54 -3
  22. package/dist/adapters/kyselyQuery.cjs +2 -2
  23. package/dist/adapters/kyselyQuery.mjs +2 -1
  24. package/dist/adapters/mongodb.cjs +27 -1
  25. package/dist/adapters/mongodb.d.cts +1 -1
  26. package/dist/adapters/mongodb.d.mts +1 -1
  27. package/dist/adapters/mongodb.mjs +28 -1
  28. package/dist/adapters/mongodbCollections.cjs +2 -0
  29. package/dist/adapters/mongodbCollections.mjs +2 -0
  30. package/dist/adapters/mongodbQuery.cjs +21 -4
  31. package/dist/adapters/mongodbQuery.mjs +21 -5
  32. package/dist/adapters/mongodbReads.cjs +91 -29
  33. package/dist/adapters/mongodbReads.mjs +92 -29
  34. package/dist/adapters/mongodbWrites.cjs +16 -2
  35. package/dist/adapters/mongodbWrites.mjs +18 -3
  36. package/dist/adapters/prisma.cjs +55 -7
  37. package/dist/adapters/prisma.d.cts +0 -1
  38. package/dist/adapters/prisma.d.mts +0 -1
  39. package/dist/adapters/prisma.mjs +56 -7
  40. package/dist/adapters/prismaQuery.cjs +0 -1
  41. package/dist/adapters/prismaRows.cjs +22 -1
  42. package/dist/adapters/prismaRows.mjs +22 -1
  43. package/dist/apiKeys.cjs +6 -2
  44. package/dist/apiKeys.d.cts +0 -1
  45. package/dist/apiKeys.d.mts +0 -1
  46. package/dist/apiKeys.mjs +3 -3
  47. package/dist/createHotUpdaterCore.cjs +24 -9
  48. package/dist/createHotUpdaterCore.d.cts +1 -1
  49. package/dist/createHotUpdaterCore.d.mts +1 -1
  50. package/dist/createHotUpdaterCore.mjs +23 -9
  51. package/dist/db/createBundleDiff.cjs +2 -2
  52. package/dist/db/createBundleDiff.d.cts +1 -5
  53. package/dist/db/createBundleDiff.d.mts +1 -5
  54. package/dist/db/databasePluginCore.cjs +0 -1
  55. package/dist/db/fixedMigrator.cjs +2 -2
  56. package/dist/db/fixedMigrator.mjs +2 -2
  57. package/dist/db/fixedMigratorMongo.cjs +6 -4
  58. package/dist/db/fixedMigratorMongo.mjs +6 -4
  59. package/dist/db/index.d.cts +0 -1
  60. package/dist/db/index.d.mts +0 -1
  61. package/dist/db/releaseCatalog.cjs +0 -1
  62. package/dist/db/releaseCatalog.d.cts +0 -1
  63. package/dist/db/releaseCatalog.d.mts +0 -1
  64. package/dist/db/schema/insightsCollation.cjs +14 -0
  65. package/dist/db/schema/insightsCollation.mjs +14 -0
  66. package/dist/db/schema/sql.cjs +11 -7
  67. package/dist/db/schema/sql.mjs +8 -8
  68. package/dist/db/schemaGenerators.cjs +28 -9
  69. package/dist/db/schemaGenerators.mjs +28 -9
  70. package/dist/db/schemaReadiness.cjs +2 -0
  71. package/dist/db/schemaReadiness.d.cts +1 -0
  72. package/dist/db/schemaReadiness.d.mts +1 -0
  73. package/dist/db/schemaReadiness.mjs +2 -0
  74. package/dist/db/sqlMigrationExecution.cjs +1 -1
  75. package/dist/db/sqlMigrationExecution.mjs +1 -0
  76. package/dist/db/types.cjs +3 -1
  77. package/dist/db/types.d.cts +3 -6
  78. package/dist/db/types.d.mts +3 -6
  79. package/dist/db/types.mjs +2 -2
  80. package/dist/db/updateArtifacts.cjs +0 -1
  81. package/dist/handler.d.cts +1 -2
  82. package/dist/handler.d.mts +1 -2
  83. package/dist/handlerReleaseCatalogRoutes.cjs +0 -1
  84. package/dist/handlerReleaseManagementRoutes.cjs +0 -1
  85. package/dist/handlerTypes.d.cts +0 -1
  86. package/dist/handlerTypes.d.mts +0 -1
  87. package/dist/handlerVersionRoutes.d.cts +1 -0
  88. package/dist/handlerVersionRoutes.d.mts +1 -0
  89. package/dist/index.cjs +1 -1
  90. package/dist/index.d.cts +4 -4
  91. package/dist/index.d.mts +4 -4
  92. package/dist/index.mjs +1 -1
  93. package/dist/insights/domain.d.cts +37 -69
  94. package/dist/insights/domain.d.mts +37 -69
  95. package/dist/insights/errors.cjs +1 -8
  96. package/dist/insights/errors.mjs +2 -8
  97. package/dist/insights/eventInput.cjs +49 -5
  98. package/dist/insights/eventInput.mjs +48 -6
  99. package/dist/insights/provider.cjs +314 -0
  100. package/dist/insights/provider.d.cts +6 -0
  101. package/dist/insights/provider.d.mts +6 -0
  102. package/dist/insights/provider.mjs +314 -0
  103. package/dist/insights/queryInput.cjs +78 -43
  104. package/dist/insights/queryInput.mjs +76 -40
  105. package/dist/insights/routes.cjs +20 -25
  106. package/dist/insights/routes.mjs +22 -27
  107. package/dist/insights/types.d.cts +27 -12
  108. package/dist/insights/types.d.mts +27 -12
  109. package/dist/node.d.cts +0 -1
  110. package/dist/node.d.mts +0 -1
  111. package/dist/package.cjs +1 -1
  112. package/dist/package.mjs +1 -1
  113. package/dist/schema/index.cjs +17 -3
  114. package/dist/schema/index.mjs +4 -4
  115. package/dist/schema/v1_0_0.cjs +72 -30
  116. package/dist/schema/v1_0_0.mjs +72 -31
  117. package/dist/storageAccess.cjs +0 -1
  118. package/dist/types/index.d.cts +0 -1
  119. package/dist/types/index.d.mts +0 -1
  120. package/package.json +7 -7
  121. package/src/adapters/databasePluginTestDatabase.ts +14 -0
  122. package/src/adapters/drizzle.spec.ts +99 -0
  123. package/src/adapters/drizzle.ts +21 -3
  124. package/src/adapters/drizzleCrud.ts +154 -2
  125. package/src/adapters/drizzleLazyDB.ts +17 -3
  126. package/src/adapters/insightsTransaction.ts +32 -0
  127. package/src/adapters/kysely.spec.ts +29 -0
  128. package/src/adapters/kysely.ts +10 -1
  129. package/src/adapters/kyselyCrud.ts +121 -0
  130. package/src/adapters/mongodb.integration.spec.ts +210 -0
  131. package/src/adapters/mongodb.spec.ts +69 -1
  132. package/src/adapters/mongodb.ts +66 -2
  133. package/src/adapters/mongodbCollections.ts +5 -0
  134. package/src/adapters/mongodbQuery.ts +32 -3
  135. package/src/adapters/mongodbReads.ts +69 -0
  136. package/src/adapters/mongodbTestClient.ts +41 -6
  137. package/src/adapters/mongodbTestFilter.ts +4 -1
  138. package/src/adapters/mongodbWrites.ts +27 -0
  139. package/src/adapters/prisma.spec.ts +97 -0
  140. package/src/adapters/prisma.ts +103 -3
  141. package/src/adapters/prismaRows.ts +37 -1
  142. package/src/adapters/prismaTestClient.ts +22 -5
  143. package/src/apiKeys.integration.spec.ts +1 -1
  144. package/src/createHotUpdaterCore.ts +21 -7
  145. package/src/db/databasePluginUpdateCheck.bench.ts +3 -0
  146. package/src/db/fixedMigrator.spec.ts +65 -0
  147. package/src/db/fixedMigratorMongo.spec.ts +56 -9
  148. package/src/db/fixedMigratorMongo.ts +10 -10
  149. package/src/db/index.spec.ts +8 -1
  150. package/src/db/schema/insightsCollation.ts +21 -0
  151. package/src/db/schema/sql.ts +8 -5
  152. package/src/db/schemaGenerators.ts +41 -13
  153. package/src/db/types.spec.ts +8 -0
  154. package/src/db/types.ts +10 -4
  155. package/src/handler-standalone.integration.spec.ts +5 -7
  156. package/src/index.ts +2 -2
  157. package/src/insights/domain.ts +39 -75
  158. package/src/insights/errors.ts +0 -7
  159. package/src/insights/eventInput.ts +60 -3
  160. package/src/insights/insights.integration.spec.ts +254 -17
  161. package/src/insights/provider.spec.ts +385 -0
  162. package/src/insights/provider.ts +591 -0
  163. package/src/insights/queryInput.ts +112 -73
  164. package/src/insights/routes.ts +37 -56
  165. package/src/insights/types.ts +47 -36
  166. package/src/schema/dsl-all-versions.spec.ts +3 -1
  167. package/src/schema/v1_0_0.spec.ts +77 -3
  168. package/src/schema/v1_0_0.ts +94 -30
  169. package/dist/insights/bounded/activeOverview.cjs +0 -87
  170. package/dist/insights/bounded/activeOverview.mjs +0 -85
  171. package/dist/insights/bounded/installationSearch.cjs +0 -42
  172. package/dist/insights/bounded/installationSearch.mjs +0 -42
  173. package/dist/insights/bounded/persistence.cjs +0 -39
  174. package/dist/insights/bounded/persistence.mjs +0 -38
  175. package/dist/insights/bounded/provider.cjs +0 -170
  176. package/dist/insights/bounded/provider.d.cts +0 -7
  177. package/dist/insights/bounded/provider.d.mts +0 -7
  178. package/dist/insights/bounded/provider.mjs +0 -170
  179. package/dist/insights/bounded/scan.cjs +0 -144
  180. package/dist/insights/bounded/scan.mjs +0 -138
  181. package/dist/insights/persistence.d.cts +0 -6
  182. package/dist/insights/persistence.d.mts +0 -6
  183. package/src/insights/bounded/activeOverview.spec.ts +0 -189
  184. package/src/insights/bounded/activeOverview.ts +0 -148
  185. package/src/insights/bounded/installationSearch.spec.ts +0 -130
  186. package/src/insights/bounded/installationSearch.ts +0 -69
  187. package/src/insights/bounded/persistence.ts +0 -43
  188. package/src/insights/bounded/provider.spec.ts +0 -219
  189. package/src/insights/bounded/provider.ts +0 -229
  190. package/src/insights/bounded/scan.ts +0 -235
  191. package/src/insights/persistence.ts +0 -10
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  assertStorageOperations,
3
+ type InsightsModel,
3
4
  type StoragePlugin,
4
5
  } from "@hot-updater/plugin-core";
5
6
 
@@ -18,7 +19,7 @@ import {
18
19
  isDatabasePlugin,
19
20
  } from "./db/types";
20
21
  import { createHotUpdaterHandlers, type HotUpdaterHandlers } from "./handler";
21
- import { createInsightsProvider } from "./insights/bounded/provider";
22
+ import { createInsightsProvider } from "./insights/provider";
22
23
  import type { InsightsProvider } from "./insights/types";
23
24
  import { createStorageAccess } from "./storageAccess";
24
25
 
@@ -168,16 +169,29 @@ export function createHotUpdaterCore(
168
169
  readStorageText,
169
170
  });
170
171
  const clientAccess = normalizeClientAccess(options.clientAccess);
171
- const insights = createInsightsProvider({
172
- async append(row) {
172
+ const insightsModel: InsightsModel = {
173
+ async record(input) {
173
174
  await assertSchemaReady();
174
- return plugin.models.insights.append(row);
175
+ return plugin.models.insights.record(input);
175
176
  },
176
- async scan(input) {
177
+ async listEvents(input) {
177
178
  await assertSchemaReady();
178
- return plugin.models.insights.scan(input);
179
+ return plugin.models.insights.listEvents(input);
179
180
  },
180
- });
181
+ async findInstallations(input) {
182
+ await assertSchemaReady();
183
+ return plugin.models.insights.findInstallations(input);
184
+ },
185
+ async countInstallations(input) {
186
+ await assertSchemaReady();
187
+ return plugin.models.insights.countInstallations(input);
188
+ },
189
+ async countEvents(input) {
190
+ await assertSchemaReady();
191
+ return plugin.models.insights.countEvents(input);
192
+ },
193
+ };
194
+ const insights = createInsightsProvider(insightsModel);
181
195
  const apiKeys = createApiKeyManagement({
182
196
  apiKeys: plugin.models.apiKeys,
183
197
  beforeOperation: assertSchemaReady,
@@ -54,6 +54,9 @@ class BenchmarkMutationError extends Error {
54
54
 
55
55
  const name = "release-catalog-benchmark";
56
56
  const adapter = createDatabasePluginAdapter(name, {
57
+ async recordInsights() {
58
+ throw new BenchmarkMutationError();
59
+ },
57
60
  async count() {
58
61
  return 0;
59
62
  },
@@ -1,11 +1,14 @@
1
1
  import { DatabaseSync, type SqliteValue } from "node:sqlite";
2
2
 
3
3
  import { PGlite } from "@electric-sql/pglite";
4
+ import { toInsightsInstallationRow } from "@hot-updater/plugin-core";
4
5
  import { Kysely, SqliteDialect } from "kysely";
5
6
  import { PGliteDialect } from "kysely-pglite-dialect";
6
7
  import { afterEach, describe, expect, it, vi } from "vitest";
7
8
 
9
+ import { createBundleEventRowFixture } from "../../../test-utils/src/databaseTestFixtures";
8
10
  import { createInMemoryDatabasePlugin } from "../../../test-utils/test/inMemoryDatabasePlugin";
11
+ import { kyselyAdapter } from "../adapters/kysely";
9
12
  import { createDatabasePluginCore } from "./databasePluginCore";
10
13
  import { createKyselyMigrator } from "./fixedMigrator";
11
14
  import { createSchemaReadinessChecker } from "./schemaReadiness";
@@ -54,6 +57,68 @@ describe("Kysely migrator", () => {
54
57
  await Promise.all(databases.splice(0).map((db) => db.close()));
55
58
  });
56
59
 
60
+ it("creates the final Insights access paths in the initial schema", async () => {
61
+ const database = new PGlite();
62
+ databases.push(database);
63
+ const kysely = new Kysely<SettingsDatabase>({
64
+ dialect: new PGliteDialect(database),
65
+ });
66
+ kyselyInstances.push(kysely);
67
+ const migrator = createKyselyMigrator({
68
+ db: kysely,
69
+ provider: "postgresql",
70
+ });
71
+ await expect(migrator.next()).resolves.toEqual({ version: "1.0.0" });
72
+ await expect(migrator.previous()).resolves.toBeUndefined();
73
+ const migration = await migrator.migrateToLatest();
74
+ await migration.execute();
75
+ await expect(migrator.getVersion()).resolves.toBe("1.0.0");
76
+ const plugin = kyselyAdapter({ db: kysely, provider: "postgresql" });
77
+ const event = createBundleEventRowFixture("706", 100);
78
+ const input = { event, installation: toInsightsInstallationRow(event) };
79
+ await plugin.models.insights.record(input);
80
+
81
+ const repeated = await migrator.migrateToLatest();
82
+ expect(repeated.operations).toEqual([]);
83
+ expect(repeated.getSQL?.()).toBe("");
84
+ await repeated.execute();
85
+ await expect(migrator.next()).resolves.toBeUndefined();
86
+ await expect(
87
+ plugin.models.insights.findInstallations({ installId: event.install_id }),
88
+ ).resolves.toEqual([input.installation]);
89
+ await expect(
90
+ plugin.models.insights.countEvents({
91
+ filter: {
92
+ platform: "ios",
93
+ channel: "production",
94
+ type: "UPDATE_APPLIED",
95
+ toBundleId: event.to_bundle_id,
96
+ },
97
+ sinceMs: 0,
98
+ beforeReceivedAtMs: 101,
99
+ }),
100
+ ).resolves.toBe(1);
101
+ const indexes = await database.query<{ indexname: string }>(
102
+ "select indexname from pg_indexes where tablename in ('bundle_events', 'bundle_installations')",
103
+ );
104
+ expect(indexes.rows.map(({ indexname }) => indexname)).toEqual(
105
+ expect.arrayContaining([
106
+ "bundle_events_from_bundle_idx",
107
+ "bundle_events_to_bundle_idx",
108
+ "bundle_installations_scope_idx",
109
+ "bundle_installations_bundle_idx",
110
+ ]),
111
+ );
112
+ await database.exec("set enable_seqscan = off");
113
+ const plan = await database.query<{ "QUERY PLAN": string }>(
114
+ "explain select * from bundle_events where type = 'UPDATE_APPLIED' and platform = 'ios' and channel = 'production' and to_bundle_id = $1 and received_at_ms < 101 order by received_at_ms desc, id desc limit 10",
115
+ [event.to_bundle_id],
116
+ );
117
+ expect(plan.rows.map((row) => row["QUERY PLAN"]).join("\n")).toContain(
118
+ "bundle_events_to_bundle_idx",
119
+ );
120
+ });
121
+
57
122
  it("creates schema 1.0.0 from an empty database", async () => {
58
123
  const database = new PGlite();
59
124
  databases.push(database);
@@ -2,6 +2,7 @@ import { MongoClient } from "mongodb";
2
2
  import { describe, expect, it, vi } from "vitest";
3
3
 
4
4
  import { createInMemoryDatabasePlugin } from "../../../test-utils/test/inMemoryDatabasePlugin";
5
+ import { HOT_UPDATER_SCHEMA_VERSION } from "../schema/types";
5
6
  import { createDatabasePluginCore } from "./databasePluginCore";
6
7
  import { createMongoMigrator } from "./fixedMigrator";
7
8
  import { createSchemaReadinessChecker } from "./schemaReadiness";
@@ -29,7 +30,7 @@ function createSettingsMongoClient(
29
30
  }
30
31
 
31
32
  describe("MongoDB migration", () => {
32
- it("creates schema 1.0.0 from an empty database", async () => {
33
+ it("creates the current schema from an empty database", async () => {
33
34
  const settings = new Map<string, unknown>();
34
35
  const settingsCollection = {
35
36
  find: ({ key }: { readonly key: string }) => ({
@@ -50,7 +51,7 @@ describe("MongoDB migration", () => {
50
51
  const modelCollection = {
51
52
  find: () => ({ toArray: async () => [] }),
52
53
  listIndexes: () => ({ toArray: async () => [] }),
53
- createIndex: async () => "created",
54
+ createIndex: vi.fn(async () => "created"),
54
55
  };
55
56
  const command = vi.fn(
56
57
  async (_input: Record<string, unknown>): Promise<void> => undefined,
@@ -76,7 +77,47 @@ describe("MongoDB migration", () => {
76
77
  { key: 1 },
77
78
  { unique: true },
78
79
  );
79
- expect(settings.get("schema.core")).toBe("1.0.0");
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
+ );
80
121
  const commands = command.mock.calls.map(([input]) => input);
81
122
  expect(commands.find(({ collMod }) => collMod === "bundles")).toMatchObject(
82
123
  {
@@ -138,24 +179,30 @@ describe("MongoDB migration", () => {
138
179
  });
139
180
  });
140
181
 
141
- it("is a no-op when schema.core is already 1.0.0", async () => {
142
- const client = createSettingsMongoClient({ "schema.core": "1.0.0" });
182
+ it("is a no-op when schema.core is current", async () => {
183
+ const client = createSettingsMongoClient({
184
+ "schema.core": HOT_UPDATER_SCHEMA_VERSION,
185
+ });
143
186
  const migrator = createMongoMigrator(client);
144
187
 
145
- await expect(migrator.getVersion()).resolves.toBe("1.0.0");
188
+ await expect(migrator.getVersion()).resolves.toBe(
189
+ HOT_UPDATER_SCHEMA_VERSION,
190
+ );
146
191
  await expect(
147
192
  migrator.migrateToLatest({ mode: "from-schema" }),
148
193
  ).resolves.toMatchObject({ operations: [] });
149
194
  });
150
195
 
151
- it("ignores a leftover version marker when schema.core is 1.0.0", async () => {
196
+ it("ignores a leftover version marker when schema.core is current", async () => {
152
197
  const client = createSettingsMongoClient({
153
- "schema.core": "1.0.0",
198
+ "schema.core": HOT_UPDATER_SCHEMA_VERSION,
154
199
  version: "0.36.0",
155
200
  });
156
201
  const migrator = createMongoMigrator(client);
157
202
 
158
- await expect(migrator.getVersion()).resolves.toBe("1.0.0");
203
+ await expect(migrator.getVersion()).resolves.toBe(
204
+ HOT_UPDATER_SCHEMA_VERSION,
205
+ );
159
206
  await expect(
160
207
  migrator.migrateToLatest({ mode: "from-schema" }),
161
208
  ).resolves.toMatchObject({ operations: [] });
@@ -334,7 +334,11 @@ export const createMongoMigrator = (client: MongoClient): Migrator => {
334
334
  await ensureSettingsKeyIndex();
335
335
  for (const table of hotUpdaterSchema.tables) {
336
336
  if (table.internal) continue;
337
+ const isInsights =
338
+ table.ormName === "bundle_events" ||
339
+ table.ormName === "bundle_installations";
337
340
  const collection = db.collection(table.ormName);
341
+ const collation = isInsights ? { locale: "simple" } : undefined;
338
342
  const primaryKey = table.columns.find(
339
343
  (column) => column.primaryKey,
340
344
  );
@@ -344,18 +348,13 @@ export const createMongoMigrator = (client: MongoClient): Migrator => {
344
348
  );
345
349
  }
346
350
  const idIndexName = `${table.ormName}_${primaryKey.ormName}_idx`;
347
- const existingIndexes = await collection
348
- .listIndexes()
349
- .toArray();
350
- const existingIdIndex = existingIndexes.find(
351
- ({ name }) => name === idIndexName,
352
- );
353
- if (existingIdIndex && existingIdIndex.unique !== true) {
354
- await collection.dropIndex(idIndexName);
355
- }
356
351
  await collection.createIndex(
357
352
  { [primaryKey.ormName]: 1 },
358
- { name: idIndexName, unique: true },
353
+ {
354
+ name: idIndexName,
355
+ unique: true,
356
+ ...(collation ? { collation } : {}),
357
+ },
359
358
  );
360
359
  for (const index of (table.indexes ?? []).filter((item) =>
361
360
  schemaIndexAppliesToProvider(item, "mongodb"),
@@ -367,6 +366,7 @@ export const createMongoMigrator = (client: MongoClient): Migrator => {
367
366
  {
368
367
  name: index.name,
369
368
  ...(index.unique ? { unique: true } : {}),
369
+ ...(collation ? { collation } : {}),
370
370
  },
371
371
  );
372
372
  }
@@ -415,7 +415,7 @@ describe("server/db hotUpdater (PGlite + Kysely)", async () => {
415
415
  });
416
416
 
417
417
  describe("migrator enhancements", () => {
418
- it("registers only schema 1.0.0", () => {
418
+ it("registers only the initial schema", () => {
419
419
  expect(hotUpdaterSchemaVersions.map((schema) => schema.version)).toEqual([
420
420
  "1.0.0",
421
421
  ]);
@@ -837,6 +837,9 @@ describe("server/db hotUpdater (PGlite + Kysely)", async () => {
837
837
  bundle_events: {
838
838
  id: "event_id",
839
839
  },
840
+ bundle_installations: {
841
+ install_id: "install_id",
842
+ },
840
843
  bundle_patches: {
841
844
  bundle_id: "bundle_id",
842
845
  id: "patch_id",
@@ -878,6 +881,10 @@ describe("server/db hotUpdater (PGlite + Kysely)", async () => {
878
881
  findFirst: vi.fn(),
879
882
  findMany: vi.fn(),
880
883
  },
884
+ bundle_installations: {
885
+ findFirst: vi.fn(),
886
+ findMany: vi.fn(),
887
+ },
881
888
  bundle_patches: {
882
889
  findFirst: vi.fn(),
883
890
  findMany: vi.fn(),
@@ -0,0 +1,21 @@
1
+ import { bundleEventsV100, bundleInstallationsV100 } from "../../schema/v1_0_0";
2
+ import type { ORMSQLProvider } from "../types";
3
+ import { getSqlType, sqlColumnDefinition } from "./sql";
4
+
5
+ export const getInsightsCollationSql = (
6
+ provider: ORMSQLProvider,
7
+ ): readonly string[] =>
8
+ [bundleEventsV100, bundleInstallationsV100].flatMap((table) =>
9
+ table.columns.flatMap((column) => {
10
+ const collation = column.providerCollations?.[provider];
11
+ if (collation === undefined) return [];
12
+ if (provider === "mysql") {
13
+ return [
14
+ `alter table ${table.ormName} modify column ${sqlColumnDefinition(table, { ...column, primaryKey: undefined }, provider)}`,
15
+ ];
16
+ }
17
+ return [
18
+ `alter table ${table.ormName} alter column ${column.ormName} type ${getSqlType(column.type, provider)} collate ${collation}`,
19
+ ];
20
+ }),
21
+ );
@@ -5,6 +5,7 @@ import {
5
5
  type HotUpdaterColumnType,
6
6
  type HotUpdaterForeignKeySchema,
7
7
  type HotUpdaterTableSchema,
8
+ type HotUpdaterVersionedSchema,
8
9
  } from "../../schema/types";
9
10
  import type {
10
11
  MigrationOperation,
@@ -209,9 +210,10 @@ export const createTableStatement = (
209
210
  export const createForeignKeySqlStatements = (
210
211
  provider: ORMSQLProvider,
211
212
  relationMode: RelationMode = "foreign-keys",
213
+ schema: HotUpdaterVersionedSchema = hotUpdaterSchema,
212
214
  ): readonly string[] => {
213
215
  if (relationMode !== "foreign-keys" || provider === "sqlite") return [];
214
- return hotUpdaterSchema.tables.flatMap((table) =>
216
+ return schema.tables.flatMap((table) =>
215
217
  (table.foreignKeys ?? []).map((foreignKey) =>
216
218
  createForeignKeySql(table, foreignKey, provider),
217
219
  ),
@@ -221,21 +223,22 @@ export const createForeignKeySqlStatements = (
221
223
  export const createTableSql = (
222
224
  provider: ORMSQLProvider,
223
225
  relationMode: RelationMode = "foreign-keys",
226
+ schema: HotUpdaterVersionedSchema = hotUpdaterSchema,
224
227
  ): readonly string[] => [
225
- ...hotUpdaterSchema.tables.map((table) =>
228
+ ...schema.tables.map((table) =>
226
229
  createTableStatement(table, provider, relationMode),
227
230
  ),
228
- ...hotUpdaterSchema.tables.flatMap((table) =>
231
+ ...schema.tables.flatMap((table) =>
229
232
  (table.indexes ?? [])
230
233
  .filter((index) => schemaIndexAppliesToProvider(index, provider))
231
234
  .map((index) => createIndexSql(table, index, provider)),
232
235
  ),
233
236
  ...(provider === "sqlite"
234
237
  ? []
235
- : hotUpdaterSchema.tables.flatMap((table) =>
238
+ : schema.tables.flatMap((table) =>
236
239
  (table.checks ?? []).map((check) =>
237
240
  createCheckSql(table, check, provider),
238
241
  ),
239
242
  )),
240
- ...createForeignKeySqlStatements(provider, relationMode),
243
+ ...createForeignKeySqlStatements(provider, relationMode, schema),
241
244
  ];
@@ -7,11 +7,13 @@ import {
7
7
  type HotUpdaterTableSchema,
8
8
  type HotUpdaterVersionedSchema,
9
9
  } from "../schema/types";
10
+ import { getInsightsCollationSql } from "./schema/insightsCollation";
10
11
  import {
11
12
  getHotUpdaterSchemaVersion,
12
13
  hotUpdaterSchema,
13
14
  schemaIndexAppliesToProvider,
14
15
  } from "./schema/registry";
16
+ import { getSqlType } from "./schema/sql";
15
17
  import type { ORMProvider, ORMSQLProvider, SchemaGenerator } from "./types";
16
18
  import { getSQLProvider } from "./types";
17
19
 
@@ -154,19 +156,33 @@ const prismaIndexes = (
154
156
  export const generatePrismaSchema = (
155
157
  provider: ORMProvider,
156
158
  schema: HotUpdaterVersionedSchema = hotUpdaterSchema,
157
- ) =>
158
- schema.tables
159
- .map((table) => {
160
- const lines = [
161
- ...table.columns.map((column) => prismaField(column, provider)),
162
- ...prismaRelationFields(table, schema),
163
- ...prismaIndexes(table, provider),
164
- ];
165
- return `model ${table.ormName} {\n${lines
166
- .map((line) => ` ${line}`)
167
- .join("\n")}\n}`;
168
- })
169
- .join("\n\n");
159
+ ) => {
160
+ const sqlProvider = getSQLProvider(provider);
161
+ const collationSql = sqlProvider ? getInsightsCollationSql(sqlProvider) : [];
162
+ const header =
163
+ collationSql.length === 0
164
+ ? ""
165
+ : [
166
+ "// Apply these statements in the initial Prisma SQL migration to preserve exact Insights identities and cursors:",
167
+ ...collationSql.map((statement) => `// ${statement};`),
168
+ "",
169
+ ].join("\n");
170
+ return (
171
+ header +
172
+ schema.tables
173
+ .map((table) => {
174
+ const lines = [
175
+ ...table.columns.map((column) => prismaField(column, provider)),
176
+ ...prismaRelationFields(table, schema),
177
+ ...prismaIndexes(table, provider),
178
+ ];
179
+ return `model ${table.ormName} {\n${lines
180
+ .map((line) => ` ${line}`)
181
+ .join("\n")}\n}`;
182
+ })
183
+ .join("\n\n")
184
+ );
185
+ };
170
186
 
171
187
  const drizzleImportSource = (provider: ORMSQLProvider) =>
172
188
  provider === "sqlite"
@@ -186,6 +202,18 @@ const drizzleColumnFn = (
186
202
  column: HotUpdaterColumnSchema,
187
203
  provider: ORMSQLProvider,
188
204
  ): { code: string; imports: readonly string[] } => {
205
+ const collation = column.providerCollations?.[provider];
206
+ if (collation !== undefined) {
207
+ const charset =
208
+ provider === "mysql"
209
+ ? ` character set ${collation === "ascii_bin" ? "ascii" : "utf8mb4"}`
210
+ : "";
211
+ const dataType = `${getSqlType(column.type, provider)}${charset} collate ${collation}`;
212
+ return {
213
+ code: `customType<{ data: string }>({ dataType: () => ${literal(dataType)} })(${literal(column.ormName)})`,
214
+ imports: ["customType"],
215
+ };
216
+ }
189
217
  if (provider === "sqlite") {
190
218
  if (column.type === "bool") {
191
219
  return {
@@ -26,6 +26,13 @@ describe("isDatabasePlugin", () => {
26
26
  ...plugin,
27
27
  models: { ...plugin.models, insights: undefined },
28
28
  };
29
+ const malformedInsights = {
30
+ ...plugin,
31
+ models: {
32
+ ...plugin.models,
33
+ insights: { ...plugin.models.insights, listEvents: null },
34
+ },
35
+ };
29
36
  const malformedChannels = {
30
37
  ...plugin,
31
38
  models: {
@@ -48,6 +55,7 @@ describe("isDatabasePlugin", () => {
48
55
  expect(isDatabasePlugin(factory)).toBe(false);
49
56
  expect(isDatabasePlugin(malformedBundles)).toBe(false);
50
57
  expect(isDatabasePlugin(missingInsights)).toBe(false);
58
+ expect(isDatabasePlugin(malformedInsights)).toBe(false);
51
59
  expect(isDatabasePlugin(malformedChannels)).toBe(false);
52
60
  expect(isDatabasePlugin(malformedApiKeys)).toBe(false);
53
61
  expect(isDatabasePlugin(malformedDispose)).toBe(false);
package/src/db/types.ts CHANGED
@@ -126,10 +126,16 @@ export function isDatabasePlugin(plugin: unknown): plugin is DatabasePlugin {
126
126
  "insights" in plugin.models &&
127
127
  typeof plugin.models.insights === "object" &&
128
128
  plugin.models.insights !== null &&
129
- "append" in plugin.models.insights &&
130
- typeof plugin.models.insights.append === "function" &&
131
- "scan" in plugin.models.insights &&
132
- typeof plugin.models.insights.scan === "function" &&
129
+ "record" in plugin.models.insights &&
130
+ typeof plugin.models.insights.record === "function" &&
131
+ "listEvents" in plugin.models.insights &&
132
+ typeof plugin.models.insights.listEvents === "function" &&
133
+ "findInstallations" in plugin.models.insights &&
134
+ typeof plugin.models.insights.findInstallations === "function" &&
135
+ "countEvents" in plugin.models.insights &&
136
+ typeof plugin.models.insights.countEvents === "function" &&
137
+ "countInstallations" in plugin.models.insights &&
138
+ typeof plugin.models.insights.countInstallations === "function" &&
133
139
  "apiKeys" in plugin.models &&
134
140
  typeof plugin.models.apiKeys === "object" &&
135
141
  plugin.models.apiKeys !== null &&
@@ -186,15 +186,13 @@ describe("Handler <-> Standalone Repository Integration", () => {
186
186
  }),
187
187
  }),
188
188
  );
189
- const overview = await api.handlers.admin(
190
- new Request(`${baseUrl}/installations/overview`),
191
- );
189
+ const events = await api.handlers.admin(new Request(`${baseUrl}/events`));
192
190
 
193
191
  expect(ingestion.status).toBe(204);
194
- expect(overview.status).toBe(200);
195
- await expect(overview.json()).resolves.toEqual({
196
- bundles: [{ bundleId, installations: 1 }],
197
- trackedInstallations: 1,
192
+ expect(events.status).toBe(200);
193
+ await expect(events.json()).resolves.toMatchObject({
194
+ data: [{ installId, toBundleId: bundleId, type: "UPDATE_APPLIED" }],
195
+ nextCursor: null,
198
196
  });
199
197
  });
200
198
 
package/src/index.ts CHANGED
@@ -4,9 +4,9 @@ export type {
4
4
  HotUpdaterHandler,
5
5
  HotUpdaterHandlers,
6
6
  } from "./handler";
7
- export { createInsightsProvider } from "./insights/bounded/provider";
7
+ export { createInsightsProvider } from "./insights/provider";
8
8
  export type * from "./insights/domain";
9
- export type { InsightsProvider } from "./insights/types";
9
+ export type * from "./insights/types";
10
10
  export {
11
11
  API_KEY_HEADER_NAME,
12
12
  createApiKey,