File size: 4,870 Bytes
9d4a3b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { mkdtemp, readFile, readdir, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
import { DatabaseSync } from "node:sqlite";
import { promisify } from "node:util";

const root = process.cwd();
const execFileAsync = promisify(execFile);

async function applyMigrations(path) {
  const sqlite = new DatabaseSync(path);
  sqlite.exec("PRAGMA foreign_keys = ON;");
  const files = (await readdir(join(root, "drizzle"))).filter((file) => file.endsWith(".sql")).sort();
  for (const file of files) {
    const source = await readFile(join(root, "drizzle", file), "utf8");
    for (const statement of source.split("--> statement-breakpoint")) {
      if (statement.trim()) sqlite.exec(statement);
    }
  }
  sqlite.close();
}

test("SQLite migrations seed the default tenant and D1 batch rolls back partial account creation", async () => {
  const directory = await mkdtemp(join(tmpdir(), "linkedin-d1-contract-"));
  const path = join(directory, "linkedin.sqlite3");
  try {
    await applyMigrations(path);
    process.env.LINKEDIN_SQLITE_PATH = path;
    const [{ getDb }, schema] = await Promise.all([import("../db/index.ts"), import("../db/schema.ts")]);
    const db = getDb();
    const tenants = await db.select().from(schema.organizations);
    const users = await db.select().from(schema.users);
    const posts = await db.select().from(schema.posts);
    assert.ok(tenants.some((tenant) => tenant.slug === "northstar-systems"));
    assert.ok(users.some((user) => user.email === "maya@example.com"));
    assert.ok(posts.some((post) => post.id === "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"));
    assert.ok(users.find((user) => user.email === "maya@example.com")?.emailVerifiedAt instanceof Date);

    const rollbackUserId = "01234567-89ab-4cde-8fab-0123456789ab";
    await assert.rejects(db.batch([
      db.insert(schema.users).values({
        id: rollbackUserId,
        email: "rollback@example.test",
        username: "rollback-test",
        passwordHash: "not-used",
        emailVerifiedAt: new Date(),
        name: "Rollback Test",
      }),
      db.insert(schema.memberships).values({
        organizationId: "missing-tenant",
        userId: rollbackUserId,
        role: "member",
      }),
    ]));
    const sqlite = new DatabaseSync(path, { readOnly: true });
    assert.equal(sqlite.prepare("SELECT count(*) AS count FROM users WHERE id = ?").get(rollbackUserId).count, 0);
    sqlite.close();
  } finally {
    delete process.env.LINKEDIN_SQLITE_PATH;
    await rm(directory, { recursive: true, force: true });
  }
});

test("Harbor migrations freeze seeded and default timestamps across independent databases", async () => {
  const directory = await mkdtemp(join(tmpdir(), "linkedin-deterministic-time-"));
  const firstPath = join(directory, "first.sqlite3");
  const secondPath = join(directory, "second.sqlite3");
  const deterministicEpoch = 1767189679;
  try {
    for (const path of [firstPath, secondPath]) {
      await execFileAsync(process.execPath, [join(root, "scripts", "migrate-sqlite.mjs")], {
        cwd: root,
        env: {
          PATH: process.env.PATH,
          LINKEDIN_SQLITE_PATH: path,
          WEBSITEBENCH_DETERMINISTIC_EPOCH_SECONDS: String(deterministicEpoch),
        },
      });
      const sqlite = new DatabaseSync(path);
      sqlite.prepare("INSERT INTO organizations (id, name, slug) VALUES (?, ?, ?)")
        .run("12345678-1234-4123-8123-123456789abc", "Deterministic Test", "deterministic-test");
      assert.deepEqual(
        { ...sqlite.prepare("SELECT created_at, updated_at FROM organizations WHERE id = ?").get("55555555-5555-4555-8555-555555555555") },
        { created_at: 1764547200, updated_at: 1764547200 },
      );
      assert.deepEqual(
        { ...sqlite.prepare("SELECT created_at, updated_at FROM posts WHERE id = ?").get("aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa") },
        { created_at: 1764547200, updated_at: 1764547200 },
      );
      assert.deepEqual(
        { ...sqlite.prepare("SELECT published_at, created_at, updated_at FROM jobs WHERE id = ?").get("77777777-7777-4777-8777-777777777777") },
        { published_at: 1764547200, created_at: 1764547200, updated_at: 1764547200 },
      );
      assert.deepEqual(
        { ...sqlite.prepare("SELECT created_at, updated_at FROM organizations WHERE slug = ?").get("deterministic-test") },
        { created_at: deterministicEpoch, updated_at: deterministicEpoch },
      );
      assert.equal(
        sqlite.prepare("SELECT min(applied_at) AS applied_at FROM __drizzle_migrations").get().applied_at,
        deterministicEpoch,
      );
      sqlite.close();
    }
  } finally {
    await rm(directory, { recursive: true, force: true });
  }
});