-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathsqlite-config-blob-migration.test.ts
More file actions
192 lines (172 loc) · 6.76 KB
/
Copy pathsqlite-config-blob-migration.test.ts
File metadata and controls
192 lines (172 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { describe, expect, it } from "@effect/vitest";
import { Effect, Schema } from "effect";
import { collectTables } from "./executor";
import { createSqliteTestFumaDb } from "./sqlite-test-db";
import { runSqliteConfigBlobMigration } from "./sqlite-config-blob-migration";
import { runSqliteDataMigrations, type SqliteDataMigrationClient } from "./sqlite-data-migrations";
const OPENAPI_OPTIONS = {
migrationName: "test-spec-to-blob",
pluginId: "openapi",
inlineField: "spec",
hashField: "specHash",
blobKeyPrefix: "spec",
} as const;
const decodeConfigJson = Schema.decodeUnknownSync(
Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)),
);
const insertIntegration = async (
client: SqliteDataMigrationClient,
row: { rowId: string; tenant: string; slug: string; pluginId: string; config: unknown },
) => {
await client.execute({
sql: `INSERT INTO integration (row_id, tenant, slug, plugin_id, description, config, can_remove, can_refresh, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, 1, 0, ?, ?)`,
args: [
row.rowId,
row.tenant,
row.slug,
row.pluginId,
row.slug,
JSON.stringify(row.config),
Date.now(),
Date.now(),
],
});
};
describe("runSqliteConfigBlobMigration", () => {
it.effect("moves inline spec text to a blob row and rewrites the config pointer", () =>
Effect.gen(function* () {
const db = yield* Effect.promise(() => createSqliteTestFumaDb({ tables: collectTables() }));
const spec = JSON.stringify({ openapi: "3.0.0", info: { title: "T", version: "1" } });
yield* Effect.promise(() =>
insertIntegration(db.client, {
rowId: "r1",
tenant: "t1",
slug: "legacy_api",
pluginId: "openapi",
config: { spec, sourceUrl: "https://example.com/spec.json" },
}),
);
const moved = yield* runSqliteConfigBlobMigration(db.client, OPENAPI_OPTIONS);
expect(moved).toBe(1);
const integration = yield* Effect.promise(() =>
db.client.execute("SELECT config FROM integration WHERE row_id = 'r1'"),
);
const config = decodeConfigJson(String(integration.rows[0]!.config));
expect(config.spec).toBeUndefined();
expect(typeof config.specHash).toBe("string");
// Untouched fields survive the rewrite.
expect(config.sourceUrl).toBe("https://example.com/spec.json");
// The blob row uses the runtime's exact naming: namespace
// `o:<tenant>/<pluginId>`, key `spec/<hash>`, id JSON.stringify pair.
const blob = yield* Effect.promise(() =>
db.client.execute({
sql: "SELECT namespace, key, value FROM blob WHERE id = ?",
args: [JSON.stringify([`o:t1/openapi`, `spec/${config.specHash}`])],
}),
);
expect(blob.rows).toHaveLength(1);
expect(blob.rows[0]!.value).toBe(spec);
yield* Effect.promise(() => db.close());
}),
);
it.effect("is idempotent and leaves pointer-shaped and foreign rows alone", () =>
Effect.gen(function* () {
const db = yield* Effect.promise(() => createSqliteTestFumaDb({ tables: collectTables() }));
yield* Effect.promise(() =>
insertIntegration(db.client, {
rowId: "r1",
tenant: "t1",
slug: "already_migrated",
pluginId: "openapi",
config: { specHash: "abc123" },
}),
);
yield* Effect.promise(() =>
insertIntegration(db.client, {
rowId: "r2",
tenant: "t1",
slug: "mcp_thing",
pluginId: "mcp",
config: { endpoint: "https://mcp.example.com" },
}),
);
yield* Effect.promise(() =>
insertIntegration(db.client, {
rowId: "r3",
tenant: "t1",
slug: "legacy_api",
pluginId: "openapi",
config: { spec: "{}" },
}),
);
expect(yield* runSqliteConfigBlobMigration(db.client, OPENAPI_OPTIONS)).toBe(1);
// Second run: everything is pointer-shaped now.
expect(yield* runSqliteConfigBlobMigration(db.client, OPENAPI_OPTIONS)).toBe(0);
const mcpRow = yield* Effect.promise(() =>
db.client.execute("SELECT config FROM integration WHERE row_id = 'r2'"),
);
expect(decodeConfigJson(String(mcpRow.rows[0]!.config))).toEqual({
endpoint: "https://mcp.example.com",
});
yield* Effect.promise(() => db.close());
}),
);
it.effect("identical specs across integrations share one blob per tenant", () =>
Effect.gen(function* () {
const db = yield* Effect.promise(() => createSqliteTestFumaDb({ tables: collectTables() }));
const spec = JSON.stringify({ openapi: "3.0.0", info: { title: "Shared", version: "1" } });
for (const [rowId, slug] of [
["r1", "api_a"],
["r2", "api_b"],
] as const) {
yield* Effect.promise(() =>
insertIntegration(db.client, {
rowId,
tenant: "t1",
slug,
pluginId: "openapi",
config: { spec },
}),
);
}
expect(yield* runSqliteConfigBlobMigration(db.client, OPENAPI_OPTIONS)).toBe(2);
const blobs = yield* Effect.promise(() => db.client.execute("SELECT id FROM blob"));
expect(blobs.rows).toHaveLength(1);
yield* Effect.promise(() => db.close());
}),
);
it.effect("returns 0 when the integration table does not exist yet", () =>
Effect.gen(function* () {
// The ledger may run against a brand-new database before any schema
// bring-up; an absent table is "nothing to migrate", not an error.
const db = yield* Effect.promise(() => createSqliteTestFumaDb({ tables: collectTables() }));
yield* Effect.promise(() => db.client.execute("DROP TABLE integration"));
expect(yield* runSqliteConfigBlobMigration(db.client, OPENAPI_OPTIONS)).toBe(0);
yield* Effect.promise(() => db.close());
}),
);
it.effect("runs once under the ledger and is stamped", () =>
Effect.gen(function* () {
const db = yield* Effect.promise(() => createSqliteTestFumaDb({ tables: collectTables() }));
yield* Effect.promise(() =>
insertIntegration(db.client, {
rowId: "r1",
tenant: "t1",
slug: "legacy_api",
pluginId: "openapi",
config: { spec: "{}" },
}),
);
const entry = {
name: "test-spec-to-blob",
run: (client: SqliteDataMigrationClient) =>
runSqliteConfigBlobMigration(client, OPENAPI_OPTIONS).pipe(Effect.asVoid),
};
expect(yield* runSqliteDataMigrations(db.client, [entry])).toEqual(["test-spec-to-blob"]);
// Stamped: the second boot doesn't re-run it.
expect(yield* runSqliteDataMigrations(db.client, [entry])).toEqual([]);
yield* Effect.promise(() => db.close());
}),
);
});