Skip to content

Commit 041a162

Browse files
committed
database: decaffeniate ops, document process
1 parent db90cff commit 041a162

File tree

11 files changed

+968
-270
lines changed

11 files changed

+968
-270
lines changed

src/packages/database/DB_DEVELOPMENT.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,56 @@ If the method needs access to instance properties, you have two options:
411411
- Document which pattern each function uses in the type definitions
412412
- **Always use `.call(this, opts)` in CoffeeScript wrappers to preserve instance context**
413413
414-
#### 2.3 Method Migration Priority
414+
#### 2.3 Ops Module Refactor (Example: `postgres/ops`)
415+
416+
When a migrated file grows large, split it into a subdirectory with smaller modules and an `index.ts` barrel. The `postgres/ops` migration used this pattern:
417+
418+
- **Move + split**:
419+
- `postgres/ops.ts` → `postgres/ops/backup.ts` and `postgres/ops/restore.ts`
420+
- Common types/helpers → `postgres/ops/utils.ts`
421+
- `postgres/ops/index.ts` re-exports `backup`, `restore`, and `utils`
422+
- **Update extender**:
423+
- `postgres-ops.ts` should import from `./postgres/ops` (directory barrel), not a single file
424+
- **Barrel + module resolution**:
425+
- Keep `postgres/ops/index.ts` as the public entry so `./postgres/ops` resolves to the directory (Node/TS will prefer `index.ts`)
426+
- This preserves existing import paths while allowing internal files to move under `postgres/ops/`
427+
- **Split tests**:
428+
- `postgres/ops.test.ts` → `postgres/ops/backup.test.ts` and `postgres/ops/restore.test.ts`
429+
- Keep tests close to their modules for easier review and targeted runs
430+
431+
**Mocking and test patterns used in ops:**
432+
433+
- **Mock `execute_code`** to avoid running shell commands:
434+
```ts
435+
jest.mock("@cocalc/backend/misc_node", () => ({
436+
execute_code: jest.fn(),
437+
}));
438+
const executeCode = execute_code as jest.MockedFunction<typeof execute_code>;
439+
```
440+
- **Mock `fs.readdirSync`** (non-configurable in Jest unless mocked at module level):
441+
```ts
442+
jest.mock("fs", () => ({
443+
...jest.requireActual("fs"),
444+
readdirSync: jest.fn(),
445+
}));
446+
const readdirSync = fs.readdirSync as unknown as jest.MockedFunction<
447+
(path: fs.PathLike) => string[]
448+
>;
449+
```
450+
- **Callback typing**: when stubbing callback-style APIs, pass `undefined` explicitly:
451+
```ts
452+
executeCode.mockImplementation((opts) => {
453+
opts.cb?.(undefined);
454+
});
455+
```
456+
457+
Suggested test runs for the split ops tests:
458+
459+
```bash
460+
pnpm test postgres/ops/backup.test.ts postgres/ops/restore.test.ts
461+
```
462+
463+
#### 2.4 Method Migration Priority
415464
416465
**Order of migration** (from foundational to dependent):
417466

src/packages/database/postgres-ops.coffee

Lines changed: 0 additions & 158 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of CoCalc: Copyright © 2020–2025 Sagemath, Inc.
3+
* License: MS-RSL – see LICENSE.md for details
4+
*/
5+
6+
import type { CB } from "@cocalc/util/types/database";
7+
8+
import {
9+
backupBupCB,
10+
backupTableCB,
11+
backupTablesCB,
12+
getBackupTables,
13+
restoreTableCB,
14+
restoreTablesCB,
15+
type BackupBupOptions,
16+
type BackupTableOptions,
17+
type BackupTables,
18+
type BackupTablesOptions,
19+
type RestoreTableOptions,
20+
type RestoreTablesOptions,
21+
} from "./postgres/ops";
22+
import type { PostgreSQL as PostgreSQLInterface } from "./postgres/types";
23+
24+
type PostgreSQLConstructor = new (...args: any[]) => PostgreSQLInterface;
25+
26+
export function extend_PostgreSQL<TBase extends PostgreSQLConstructor>(
27+
ext: TBase,
28+
): TBase {
29+
return class PostgreSQL extends ext {
30+
backup_tables(opts: BackupTablesOptions & { cb: CB }): void {
31+
backupTablesCB(this, opts);
32+
}
33+
34+
_backup_table(opts: BackupTableOptions & { cb: CB }): void {
35+
backupTableCB(this, opts);
36+
}
37+
38+
_backup_bup(opts: BackupBupOptions & { cb: CB }): void {
39+
backupBupCB(this, opts);
40+
}
41+
42+
_get_backup_tables(tables: BackupTables): string[] {
43+
return getBackupTables(tables);
44+
}
45+
46+
restore_tables(opts: RestoreTablesOptions & { cb: CB }): void {
47+
restoreTablesCB(this, opts);
48+
}
49+
50+
_restore_table(opts: RestoreTableOptions & { cb: CB }): void {
51+
restoreTableCB(this, opts);
52+
}
53+
};
54+
}

src/packages/database/postgres/ops.test.ts

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)