@app-vitals/agent-admin 1.126.1 → 1.127.0
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.
- package/package.json +2 -2
- package/prisma/client/internal/class.ts +4 -4
- package/prisma/client/internal/prismaNamespace.ts +2 -0
- package/prisma/client/internal/prismaNamespaceBrowser.ts +2 -0
- package/prisma/client/models/SessionAlertState.ts +37 -2
- package/prisma/client/models/SessionFollow.ts +37 -2
- package/prisma/migrations/20261203000000_add_account_id_to_session_follow_and_alert_state/migration.sql +34 -0
- package/prisma/schema/coding.prisma +6 -0
- package/src/session-follow-account-id-migration.integration.test.ts +126 -0
- package/src/session-follow-account-id-migration.unit.test.ts +49 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/session-follow-account-id-migration.integration.test.ts
|
|
3
|
+
* Migration + backfill test for 20261203000000_add_account_id_to_session_follow_and_alert_state:
|
|
4
|
+
* members and owners get their account, users with no account stay NULL,
|
|
5
|
+
* existing unique constraints survive, and re-running is a no-op.
|
|
6
|
+
*
|
|
7
|
+
* Requires DATABASE_URL_ADMIN_TEST; skips otherwise.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
11
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
12
|
+
import { Client } from "pg";
|
|
13
|
+
|
|
14
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
15
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
16
|
+
|
|
17
|
+
const MIGRATIONS_DIR = new URL("../prisma/migrations/", import.meta.url);
|
|
18
|
+
const TARGET =
|
|
19
|
+
"20261203000000_add_account_id_to_session_follow_and_alert_state";
|
|
20
|
+
|
|
21
|
+
const ALL_MIGRATIONS = readdirSync(MIGRATIONS_DIR, { withFileTypes: true })
|
|
22
|
+
.filter((d) => d.isDirectory())
|
|
23
|
+
.map((d) => d.name)
|
|
24
|
+
.sort();
|
|
25
|
+
|
|
26
|
+
const migrationSql = (dir: string): string =>
|
|
27
|
+
readFileSync(new URL(`${dir}/migration.sql`, MIGRATIONS_DIR), "utf8");
|
|
28
|
+
|
|
29
|
+
describeOrSkip("accountId on SessionFollow and SessionAlertState", () => {
|
|
30
|
+
const schema = `session_acct_${process.pid}_${Date.now()}`;
|
|
31
|
+
let client: Client;
|
|
32
|
+
const notices: string[] = [];
|
|
33
|
+
|
|
34
|
+
beforeEach(async () => {
|
|
35
|
+
client = new Client({ connectionString: TEST_DB as string });
|
|
36
|
+
client.on("notice", (n) => notices.push(n.message ?? ""));
|
|
37
|
+
await client.connect();
|
|
38
|
+
await client.query(`CREATE SCHEMA "${schema}"`);
|
|
39
|
+
await client.query(`SET search_path TO "${schema}"`);
|
|
40
|
+
for (const dir of ALL_MIGRATIONS.slice(0, ALL_MIGRATIONS.indexOf(TARGET))) {
|
|
41
|
+
await client.query(migrationSql(dir));
|
|
42
|
+
}
|
|
43
|
+
await client.query(
|
|
44
|
+
`INSERT INTO "Account" ("id","name","updatedAt") VALUES ('acc1','Acme',now())`,
|
|
45
|
+
);
|
|
46
|
+
await client.query(
|
|
47
|
+
`INSERT INTO "AccountMember" ("id","accountId","email","role")
|
|
48
|
+
VALUES ('m1','acc1','owner@example.com','owner'),
|
|
49
|
+
('m2','acc1','member@example.com','member')`,
|
|
50
|
+
);
|
|
51
|
+
// Distinct shapes: owner, member (mixed-case email), no account.
|
|
52
|
+
for (const email of [
|
|
53
|
+
"owner@example.com",
|
|
54
|
+
"Member@Example.com",
|
|
55
|
+
"nobody@example.com",
|
|
56
|
+
]) {
|
|
57
|
+
await client.query(
|
|
58
|
+
`INSERT INTO "SessionFollow" ("id","userEmail","sessionSlug","updatedAt")
|
|
59
|
+
VALUES ($1,$2,'s1',now())`,
|
|
60
|
+
[`f-${email}`, email],
|
|
61
|
+
);
|
|
62
|
+
await client.query(
|
|
63
|
+
`INSERT INTO "SessionAlertState" ("id","userEmail","sessionSlug","updatedAt")
|
|
64
|
+
VALUES ($1,$2,'s1',now())`,
|
|
65
|
+
[`a-${email}`, email],
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
afterEach(async () => {
|
|
71
|
+
await client.query(`DROP SCHEMA IF EXISTS "${schema}" CASCADE`);
|
|
72
|
+
await client.end();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const accounts = async (table: string) =>
|
|
76
|
+
(
|
|
77
|
+
await client.query(
|
|
78
|
+
`SELECT "userEmail","accountId" FROM "${table}" ORDER BY "userEmail"`,
|
|
79
|
+
)
|
|
80
|
+
).rows;
|
|
81
|
+
|
|
82
|
+
it("backfills members and owners, leaves no-account users NULL, reports the count", async () => {
|
|
83
|
+
await client.query(migrationSql(TARGET));
|
|
84
|
+
const expected = [
|
|
85
|
+
{ userEmail: "Member@Example.com", accountId: "acc1" },
|
|
86
|
+
{ userEmail: "nobody@example.com", accountId: null },
|
|
87
|
+
{ userEmail: "owner@example.com", accountId: "acc1" },
|
|
88
|
+
];
|
|
89
|
+
expect(await accounts("SessionFollow")).toEqual(expected);
|
|
90
|
+
expect(await accounts("SessionAlertState")).toEqual(expected);
|
|
91
|
+
expect(
|
|
92
|
+
notices.some((m) =>
|
|
93
|
+
m.includes("1 SessionFollow and 1 SessionAlertState rows left NULL"),
|
|
94
|
+
),
|
|
95
|
+
).toBe(true);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("is idempotent and does not overwrite an existing accountId", async () => {
|
|
99
|
+
await client.query(migrationSql(TARGET));
|
|
100
|
+
await client.query(
|
|
101
|
+
`UPDATE "SessionFollow" SET "accountId"='other' WHERE "userEmail"='owner@example.com'`,
|
|
102
|
+
);
|
|
103
|
+
await client.query(migrationSql(TARGET));
|
|
104
|
+
const rows = await accounts("SessionFollow");
|
|
105
|
+
expect(
|
|
106
|
+
rows.find((r) => r.userEmail === "owner@example.com")?.accountId,
|
|
107
|
+
).toBe("other");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("keeps the old unique constraint and adds the new one", async () => {
|
|
111
|
+
await client.query(migrationSql(TARGET));
|
|
112
|
+
await expect(
|
|
113
|
+
client.query(
|
|
114
|
+
`INSERT INTO "SessionFollow" ("id","userEmail","sessionSlug","updatedAt")
|
|
115
|
+
VALUES ('dup','owner@example.com','s1',now())`,
|
|
116
|
+
),
|
|
117
|
+
).rejects.toThrow(/SessionFollow_userEmail_sessionSlug_key/);
|
|
118
|
+
await client.query(`DROP INDEX "SessionFollow_userEmail_sessionSlug_key"`);
|
|
119
|
+
await expect(
|
|
120
|
+
client.query(
|
|
121
|
+
`INSERT INTO "SessionFollow" ("id","userEmail","accountId","sessionSlug","updatedAt")
|
|
122
|
+
VALUES ('dup','owner@example.com','acc1','s1',now())`,
|
|
123
|
+
),
|
|
124
|
+
).rejects.toThrow(/SessionFollow_userEmail_accountId_sessionSlug_key/);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/session-follow-account-id-migration.unit.test.ts
|
|
3
|
+
* Static checks on the accountId migration SQL: idempotency, additivity, and
|
|
4
|
+
* the backfill mapping (lowercased email -> AccountMember, only NULL rows).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, expect, it } from "bun:test";
|
|
8
|
+
import { readFileSync } from "node:fs";
|
|
9
|
+
|
|
10
|
+
const sql = readFileSync(
|
|
11
|
+
new URL(
|
|
12
|
+
"../prisma/migrations/20261203000000_add_account_id_to_session_follow_and_alert_state/migration.sql",
|
|
13
|
+
import.meta.url,
|
|
14
|
+
),
|
|
15
|
+
"utf8",
|
|
16
|
+
);
|
|
17
|
+
const code = sql.replace(/^--.*$/gm, "");
|
|
18
|
+
const TABLES = ["SessionFollow", "SessionAlertState"];
|
|
19
|
+
|
|
20
|
+
describe("accountId migration SQL", () => {
|
|
21
|
+
it.each(TABLES)("%s: idempotent nullable column and unique index", (t) => {
|
|
22
|
+
expect(code).toContain(
|
|
23
|
+
`ALTER TABLE "${t}" ADD COLUMN IF NOT EXISTS "accountId" TEXT;`,
|
|
24
|
+
);
|
|
25
|
+
expect(code).toContain(
|
|
26
|
+
`CREATE UNIQUE INDEX IF NOT EXISTS "${t}_userEmail_accountId_sessionSlug_key" ON "${t}"("userEmail", "accountId", "sessionSlug");`,
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it.each(TABLES)(
|
|
31
|
+
"%s: backfill maps lower(userEmail) and skips set rows",
|
|
32
|
+
(t) => {
|
|
33
|
+
const stmt = code
|
|
34
|
+
.split(";")
|
|
35
|
+
.find((s) => s.includes(`UPDATE "${t}"`)) as string;
|
|
36
|
+
expect(stmt).toContain(`"AccountMember"`);
|
|
37
|
+
expect(stmt).toContain(`t."accountId" IS NULL`);
|
|
38
|
+
expect(stmt).toContain(`lower(t."userEmail") = m."email"`);
|
|
39
|
+
},
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
it("is additive: no drops, no FKs, no NOT NULL", () => {
|
|
43
|
+
expect(code).not.toMatch(/DROP |FOREIGN KEY|NOT NULL|REFERENCES/i);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("reports the rows left NULL", () => {
|
|
47
|
+
expect(code).toContain("RAISE NOTICE");
|
|
48
|
+
});
|
|
49
|
+
});
|