|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { Cause, ConfigProvider, Effect, Exit, Layer, Option } from "effect"; |
| 3 | +import { vi } from "vitest"; |
| 4 | + |
| 5 | +vi.mock("node:net", (importOriginal) => |
| 6 | + Promise.all([ |
| 7 | + import("../../../../../tests/helpers/refuse-invalid-db-hosts.ts"), |
| 8 | + importOriginal<typeof import("node:net")>(), |
| 9 | + ]).then(([helper, net]) => helper.refuseInvalidDbHosts(net)), |
| 10 | +); |
| 11 | + |
| 12 | +import { mockOutput } from "../../../../../tests/helpers/mocks.ts"; |
| 13 | +import { |
| 14 | + buildTestRuntime, |
| 15 | + jsonResponse, |
| 16 | + mockCommandPlatformApi, |
| 17 | + mockCommandSettings, |
| 18 | + mockTelemetryStateTracked, |
| 19 | +} from "../../../../../tests/helpers/command-mocks.ts"; |
| 20 | +import { configValuesLayer } from "../../../../../tests/helpers/config-values-layer.ts"; |
| 21 | +import { DbConfigResolver } from "../../../../command-internal/db-config.service.ts"; |
| 22 | +import type { |
| 23 | + DbConnectionInput, |
| 24 | + ResolvedDbConnection, |
| 25 | +} from "../../../../command-internal/db-config.types.ts"; |
| 26 | +import { |
| 27 | + DbConnection, |
| 28 | + type PgConnInput, |
| 29 | +} from "../../../../command-internal/db-connection.service.ts"; |
| 30 | +import { DnsResolverFlag, OutputFlag, YesFlag } from "../../../../command-internal/global-flags.ts"; |
| 31 | +import { TargetPolicy } from "../../../../command-internal/target/target-policy.ts"; |
| 32 | +import type { InspectConnectionFlags } from "../inspect-query.ts"; |
| 33 | +import { inspectDbCollationDrift } from "./collation-drift.handler.ts"; |
| 34 | + |
| 35 | +const LOCAL_CONN: PgConnInput = { |
| 36 | + host: "127.0.0.1", |
| 37 | + port: 54322, |
| 38 | + user: "postgres", |
| 39 | + password: "postgres", |
| 40 | + database: "postgres", |
| 41 | +}; |
| 42 | + |
| 43 | +const DRIFT_ROW = { |
| 44 | + database: "postgres", |
| 45 | + name: "public.users_email_key", |
| 46 | + table: "public.users", |
| 47 | + columns: "email", |
| 48 | + collation: "default", |
| 49 | + stored_version: "2.36", |
| 50 | + current_version: "2.39", |
| 51 | + key_type: "UNIQUE", |
| 52 | + size: "16 kB", |
| 53 | + can_refresh: true, |
| 54 | +}; |
| 55 | + |
| 56 | +const flags: InspectConnectionFlags = { |
| 57 | + dbUrl: Option.none(), |
| 58 | + remote: Option.none(), |
| 59 | + linked: false, |
| 60 | + local: false, |
| 61 | + projectRef: Option.none(), |
| 62 | +}; |
| 63 | + |
| 64 | +function setup(opts: { |
| 65 | + serverVersionNum: number; |
| 66 | + rows: ReadonlyArray<Record<string, unknown>>; |
| 67 | + format?: "text" | "json"; |
| 68 | + connectFails?: boolean; |
| 69 | +}) { |
| 70 | + const out = mockOutput({ format: opts.format ?? "text" }); |
| 71 | + const queries: string[] = []; |
| 72 | + const connection = Layer.succeed(DbConnection, { |
| 73 | + connect: () => |
| 74 | + opts.connectFails === true |
| 75 | + ? Effect.die("connect must not be reached") |
| 76 | + : Effect.succeed({ |
| 77 | + exec: () => Effect.void, |
| 78 | + execBatch: () => Effect.void, |
| 79 | + extensionExists: () => Effect.succeed(false), |
| 80 | + queryRaw: () => Effect.succeed({ fields: [], rows: [], commandTag: "" }), |
| 81 | + copyToCsv: () => Effect.succeed(new Uint8Array()), |
| 82 | + query: (sql: string) => { |
| 83 | + queries.push(sql); |
| 84 | + return Effect.succeed( |
| 85 | + sql.includes("server_version_num") |
| 86 | + ? [{ server_version_num: opts.serverVersionNum }] |
| 87 | + : opts.rows, |
| 88 | + ); |
| 89 | + }, |
| 90 | + }), |
| 91 | + }); |
| 92 | + const resolver = Layer.succeed(DbConfigResolver, { |
| 93 | + resolveConnection: (input: DbConnectionInput) => |
| 94 | + Effect.succeed({ |
| 95 | + conn: LOCAL_CONN, |
| 96 | + isLocal: true, |
| 97 | + target: input.target, |
| 98 | + } satisfies ResolvedDbConnection), |
| 99 | + resolvePoolerFallback: () => Effect.succeed(Option.none()), |
| 100 | + }); |
| 101 | + const runtime = buildTestRuntime({ |
| 102 | + out, |
| 103 | + api: mockCommandPlatformApi({ |
| 104 | + handler: (request) => Effect.succeed(jsonResponse(request, 200, [])), |
| 105 | + }), |
| 106 | + cliSettings: mockCommandSettings({ |
| 107 | + workdir: "/tmp/collation-drift-test", |
| 108 | + projectId: Option.none(), |
| 109 | + }), |
| 110 | + telemetry: mockTelemetryStateTracked().layer, |
| 111 | + }); |
| 112 | + const layer = Layer.mergeAll( |
| 113 | + runtime, |
| 114 | + resolver, |
| 115 | + connection, |
| 116 | + Layer.succeed(DnsResolverFlag, "native"), |
| 117 | + Layer.succeed(YesFlag, false), |
| 118 | + Layer.succeed(TargetPolicy, { group: "selectable", consent: "none" }), |
| 119 | + ConfigProvider.layer(ConfigProvider.fromEnvRecord({}, { preserveEmptyStrings: true })), |
| 120 | + configValuesLayer(), |
| 121 | + ); |
| 122 | + return { layer, out, queries }; |
| 123 | +} |
| 124 | + |
| 125 | +describe("inspect db collation-drift", () => { |
| 126 | + it.effect("PG15+ reads the default collation version and prints remediation steps", () => { |
| 127 | + const { layer, out, queries } = setup({ |
| 128 | + serverVersionNum: 150000, |
| 129 | + rows: [DRIFT_ROW], |
| 130 | + }); |
| 131 | + return Effect.gen(function* () { |
| 132 | + yield* inspectDbCollationDrift(flags); |
| 133 | + expect(queries[1]).toContain("pg_database_collation_actual_version"); |
| 134 | + expect(out.stdoutText).toContain("public.users_email_key"); |
| 135 | + expect(out.stdoutText).toContain("bt_index_check"); |
| 136 | + expect(out.stdoutText).toContain("REINDEX INDEX CONCURRENTLY public.users_email_key;"); |
| 137 | + expect(out.stdoutText).toContain('ALTER DATABASE "postgres" REFRESH COLLATION VERSION;'); |
| 138 | + }).pipe(Effect.provide(layer)); |
| 139 | + }); |
| 140 | + |
| 141 | + it.effect("pre-15 skips the default collation check and prints the healthy callout", () => { |
| 142 | + const { layer, out, queries } = setup({ |
| 143 | + serverVersionNum: 140000, |
| 144 | + rows: [], |
| 145 | + }); |
| 146 | + return Effect.gen(function* () { |
| 147 | + yield* inspectDbCollationDrift(flags); |
| 148 | + expect(queries[1]).not.toContain("datcollversion"); |
| 149 | + expect(out.stdoutText).toContain("No recorded collation version mismatch"); |
| 150 | + expect(out.stdoutText.replace(/\s+/g, " ")).toContain("not checked before PostgreSQL 15"); |
| 151 | + }).pipe(Effect.provide(layer)); |
| 152 | + }); |
| 153 | + |
| 154 | + it.effect("json mode carries raw rows and the report severity", () => { |
| 155 | + const { layer, out } = setup({ |
| 156 | + serverVersionNum: 160000, |
| 157 | + rows: [DRIFT_ROW], |
| 158 | + format: "json", |
| 159 | + }); |
| 160 | + return Effect.gen(function* () { |
| 161 | + yield* inspectDbCollationDrift(flags); |
| 162 | + expect(out.messages).toContainEqual( |
| 163 | + expect.objectContaining({ |
| 164 | + type: "success", |
| 165 | + message: "inspect db collation-drift", |
| 166 | + data: { |
| 167 | + rows: [DRIFT_ROW], |
| 168 | + report: expect.objectContaining({ |
| 169 | + command: "collation-drift", |
| 170 | + severity: "critical", |
| 171 | + }), |
| 172 | + }, |
| 173 | + }), |
| 174 | + ); |
| 175 | + expect(out.stdoutText).toBe(""); |
| 176 | + }).pipe(Effect.provide(layer)); |
| 177 | + }); |
| 178 | + |
| 179 | + it.effect("rejects -o/--output before connecting", () => { |
| 180 | + const { layer } = setup({ serverVersionNum: 170000, rows: [], connectFails: true }); |
| 181 | + return Effect.gen(function* () { |
| 182 | + const exit = yield* Effect.exit(inspectDbCollationDrift(flags)); |
| 183 | + expect(Exit.isFailure(exit)).toBe(true); |
| 184 | + if (Exit.isFailure(exit)) { |
| 185 | + expect(Cause.pretty(exit.cause)).toContain( |
| 186 | + "the -o/--output flag is not supported by inspect db collation-drift; use --output-format json|stream-json instead.", |
| 187 | + ); |
| 188 | + expect(Cause.pretty(exit.cause)).not.toContain("connect must not be reached"); |
| 189 | + } |
| 190 | + }).pipe(Effect.provide(Layer.mergeAll(layer, Layer.succeed(OutputFlag, Option.some("json"))))); |
| 191 | + }); |
| 192 | +}); |
0 commit comments