@profullstack/sh1pt-migrate 0.0.0-stage → 0.1.15

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.
Files changed (78) hide show
  1. package/README.md +133 -2
  2. package/dist/apply.d.ts +65 -0
  3. package/dist/apply.d.ts.map +1 -0
  4. package/dist/apply.js +143 -0
  5. package/dist/apply.js.map +1 -0
  6. package/dist/engines/files.d.ts +37 -0
  7. package/dist/engines/files.d.ts.map +1 -0
  8. package/dist/engines/files.js +137 -0
  9. package/dist/engines/files.js.map +1 -0
  10. package/dist/engines/index.d.ts +20 -0
  11. package/dist/engines/index.d.ts.map +1 -0
  12. package/dist/engines/index.js +35 -0
  13. package/dist/engines/index.js.map +1 -0
  14. package/dist/engines/mysql.d.ts +17 -0
  15. package/dist/engines/mysql.d.ts.map +1 -0
  16. package/dist/engines/mysql.js +171 -0
  17. package/dist/engines/mysql.js.map +1 -0
  18. package/dist/engines/object-storage.d.ts +42 -0
  19. package/dist/engines/object-storage.d.ts.map +1 -0
  20. package/dist/engines/object-storage.js +197 -0
  21. package/dist/engines/object-storage.js.map +1 -0
  22. package/dist/engines/postgres.d.ts +34 -0
  23. package/dist/engines/postgres.d.ts.map +1 -0
  24. package/dist/engines/postgres.js +309 -0
  25. package/dist/engines/postgres.js.map +1 -0
  26. package/dist/engines/redis.d.ts +3 -0
  27. package/dist/engines/redis.d.ts.map +1 -0
  28. package/dist/engines/redis.js +87 -0
  29. package/dist/engines/redis.js.map +1 -0
  30. package/dist/engines/sqlite.d.ts +3 -0
  31. package/dist/engines/sqlite.d.ts.map +1 -0
  32. package/dist/engines/sqlite.js +113 -0
  33. package/dist/engines/sqlite.js.map +1 -0
  34. package/dist/exec.d.ts +31 -0
  35. package/dist/exec.d.ts.map +1 -0
  36. package/dist/exec.js +119 -0
  37. package/dist/exec.js.map +1 -0
  38. package/dist/index.d.ts +35 -0
  39. package/dist/index.d.ts.map +1 -0
  40. package/dist/index.js +31 -0
  41. package/dist/index.js.map +1 -0
  42. package/dist/plan.d.ts +115 -0
  43. package/dist/plan.d.ts.map +1 -0
  44. package/dist/plan.js +339 -0
  45. package/dist/plan.js.map +1 -0
  46. package/dist/platforms/index.d.ts +29 -0
  47. package/dist/platforms/index.d.ts.map +1 -0
  48. package/dist/platforms/index.js +51 -0
  49. package/dist/platforms/index.js.map +1 -0
  50. package/dist/platforms/managed.d.ts +41 -0
  51. package/dist/platforms/managed.d.ts.map +1 -0
  52. package/dist/platforms/managed.js +163 -0
  53. package/dist/platforms/managed.js.map +1 -0
  54. package/dist/platforms/railway.d.ts +29 -0
  55. package/dist/platforms/railway.d.ts.map +1 -0
  56. package/dist/platforms/railway.js +135 -0
  57. package/dist/platforms/railway.js.map +1 -0
  58. package/dist/platforms/ssh.d.ts +56 -0
  59. package/dist/platforms/ssh.d.ts.map +1 -0
  60. package/dist/platforms/ssh.js +151 -0
  61. package/dist/platforms/ssh.js.map +1 -0
  62. package/dist/platforms/supabase.d.ts +57 -0
  63. package/dist/platforms/supabase.d.ts.map +1 -0
  64. package/dist/platforms/supabase.js +102 -0
  65. package/dist/platforms/supabase.js.map +1 -0
  66. package/dist/staging.d.ts +27 -0
  67. package/dist/staging.d.ts.map +1 -0
  68. package/dist/staging.js +98 -0
  69. package/dist/staging.js.map +1 -0
  70. package/dist/transforms.d.ts +113 -0
  71. package/dist/transforms.d.ts.map +1 -0
  72. package/dist/transforms.js +179 -0
  73. package/dist/transforms.js.map +1 -0
  74. package/dist/types.d.ts +238 -0
  75. package/dist/types.d.ts.map +1 -0
  76. package/dist/types.js +79 -0
  77. package/dist/types.js.map +1 -0
  78. package/package.json +33 -4
@@ -0,0 +1,179 @@
1
+ /**
2
+ * Rewriting absolute URLs that point at the place you just left.
3
+ *
4
+ * This is the failure that makes migrations dangerous months after they look
5
+ * successful. An app stores an uploaded file and, instead of keeping a key,
6
+ * writes the whole URL into a row:
7
+ *
8
+ * https://ywcizjsgrcmhgyplldac.supabase.co/storage/v1/object/public/ads/x.png
9
+ *
10
+ * Migrate the bucket and the database, cut DNS over, check the site: every
11
+ * image loads. They load because the OLD account still exists and is still
12
+ * serving them. The day that account is closed — which is the whole point of
13
+ * migrating, and which happens weeks later once everyone is confident — every
14
+ * one of those rows 404s at once, and nothing connects the outage to the
15
+ * migration.
16
+ *
17
+ * crawlproof.com had 2,928 such rows across four tables. They were found by
18
+ * looking, not by anything failing.
19
+ *
20
+ * So this is a first-class step rather than a footnote: find every column that
21
+ * could hold one, report what is there, and rewrite it inside the same
22
+ * transaction that a person can roll back.
23
+ *
24
+ * Nothing here executes SQL. It builds statements and the caller runs them,
25
+ * which keeps it testable and keeps the generated SQL reviewable before it
26
+ * touches a database.
27
+ */
28
+ /** Text-ish column types worth scanning. */
29
+ const TEXT_TYPES = new Set(['text', 'character varying', 'character', 'json', 'jsonb']);
30
+ /**
31
+ * The query that finds columns which could hold a URL.
32
+ *
33
+ * Every text-ish column in a user schema. Deliberately broad: a column called
34
+ * `notes` holding a pasted URL breaks exactly as badly as one called
35
+ * `image_url`, and guessing from names is how the four crawlproof tables would
36
+ * have been missed. The count query that follows narrows it cheaply.
37
+ */
38
+ export function findTextColumnsSql() {
39
+ return `select table_schema, table_name, column_name, data_type
40
+ from information_schema.columns
41
+ where table_schema not in ('pg_catalog', 'information_schema')
42
+ and data_type in ('text', 'character varying', 'character', 'json', 'jsonb')
43
+ order by table_schema, table_name, column_name`;
44
+ }
45
+ /** True when a column's type can hold a URL. */
46
+ export function isScannable(dataType) {
47
+ return TEXT_TYPES.has(dataType.toLowerCase());
48
+ }
49
+ /**
50
+ * Postgres identifiers are quoted rather than interpolated bare.
51
+ *
52
+ * These names come from information_schema, so they are real identifiers, but
53
+ * a table legitimately named `user` or `order` is a reserved word and an
54
+ * unquoted reference is a syntax error partway through a migration. Doubling
55
+ * any embedded quote is the standard escape.
56
+ */
57
+ export function quoteIdent(name) {
58
+ return `"${name.replace(/"/g, '""')}"`;
59
+ }
60
+ /** A single-quoted SQL string literal. */
61
+ export function quoteLiteral(value) {
62
+ return `'${value.replace(/'/g, "''")}'`;
63
+ }
64
+ /** Fully-qualified, safely quoted. */
65
+ export function qualify(c) {
66
+ return `${quoteIdent(c.schema)}.${quoteIdent(c.table)}`;
67
+ }
68
+ /**
69
+ * Count rows in one column that mention the old host.
70
+ *
71
+ * Run before rewriting anything: it turns "this might be a problem" into "2,928
72
+ * rows in four tables", which is what makes the step reviewable. A cast to text
73
+ * lets one statement cover json and jsonb alongside the plain text types.
74
+ */
75
+ export function countMatchesSql(c, host) {
76
+ return `select ${quoteLiteral(`${c.schema}.${c.table}.${c.column}`)} as ref, count(*) as n
77
+ from ${qualify(c)}
78
+ where ${quoteIdent(c.column)}::text like ${quoteLiteral(`%${host}%`)}`;
79
+ }
80
+ /**
81
+ * Rewrite one column.
82
+ *
83
+ * `replace` on the text form rather than a regex: the host is a literal, a
84
+ * regex would need escaping, and `replace` is index-friendly and predictable.
85
+ * The `where` clause means untouched rows are not rewritten, which keeps the
86
+ * update small and leaves `updated_at` triggers alone on rows that did not
87
+ * change.
88
+ *
89
+ * json and jsonb are cast out to text and back, which is lossy for jsonb key
90
+ * order but not for content — and jsonb does not preserve key order anyway.
91
+ */
92
+ export function rewriteColumnSql(c, rewrite) {
93
+ const from = quoteLiteral(`https://${rewrite.from}`);
94
+ const to = quoteLiteral(rewrite.to.replace(/\/+$/, ''));
95
+ const col = quoteIdent(c.column);
96
+ const type = c.dataType.toLowerCase();
97
+ const expr = type === 'json' || type === 'jsonb'
98
+ ? `replace(${col}::text, ${from}, ${to})::${type}`
99
+ : `replace(${col}, ${from}, ${to})`;
100
+ return `update ${qualify(c)}
101
+ set ${col} = ${expr}
102
+ where ${col}::text like ${quoteLiteral(`%${rewrite.from}%`)}`;
103
+ }
104
+ /**
105
+ * The whole rewrite as one transaction.
106
+ *
107
+ * One transaction so a failure halfway leaves nothing half-rewritten, and so
108
+ * the whole thing can be rolled back by a person watching it. The trailing
109
+ * verification select is what the caller asserts on: after a correct rewrite it
110
+ * returns zero rows, and the crawlproof runbook asserted exactly that.
111
+ */
112
+ export function rewritePlanSql(columns, rewrites) {
113
+ const statements = ['begin;'];
114
+ for (const rewrite of rewrites) {
115
+ for (const c of columns) {
116
+ if (!isScannable(c.dataType))
117
+ continue;
118
+ statements.push(`${rewriteColumnSql(c, rewrite)};`);
119
+ }
120
+ }
121
+ statements.push('commit;');
122
+ return statements.join('\n');
123
+ }
124
+ /**
125
+ * The assertion that the rewrite worked.
126
+ *
127
+ * Returns one row per column that still mentions an old host. Zero rows is the
128
+ * pass condition. Run it AFTER committing: a migration that reports success
129
+ * while rows still point at an account about to be closed is worse than one
130
+ * that fails loudly.
131
+ */
132
+ export function remainingMatchesSql(columns, hosts) {
133
+ const parts = [];
134
+ for (const host of hosts) {
135
+ for (const c of columns) {
136
+ if (!isScannable(c.dataType))
137
+ continue;
138
+ parts.push(countMatchesSql(c, host));
139
+ }
140
+ }
141
+ if (!parts.length)
142
+ return 'select null::text as ref, 0::bigint as n where false';
143
+ /*
144
+ * The union goes in a subquery and the filter is a WHERE on the outside.
145
+ *
146
+ * A trailing `having count(*) > 0` looks like it filters the whole thing and
147
+ * does not: in a UNION chain it binds to the final SELECT only, so every
148
+ * other column would be reported regardless of its count and the one real
149
+ * offender could be buried. Filtering outside the subquery applies to all of
150
+ * them, which is the point of the assertion.
151
+ */
152
+ return `select ref, n from (\n${parts.join('\nunion all\n')}\n) as remaining where n > 0 order by n desc`;
153
+ }
154
+ /**
155
+ * Turn `--rewrite-host old=new` into a rewrite.
156
+ *
157
+ * Accepts a bare host on the left (`abc.supabase.co`) and a full origin on the
158
+ * right (`https://cdn.example.com`). A missing scheme on the right is the easy
159
+ * mistake and produces a corrupt URL rather than an error at runtime, so it is
160
+ * rejected here.
161
+ */
162
+ export function parseRewrite(spec) {
163
+ const eq = spec.indexOf('=');
164
+ if (eq < 1) {
165
+ throw new Error(`--rewrite-host wants old-host=new-origin, got '${spec}'`);
166
+ }
167
+ const from = spec.slice(0, eq).trim().replace(/^https?:\/\//, '').replace(/\/+$/, '');
168
+ const to = spec.slice(eq + 1).trim().replace(/\/+$/, '');
169
+ if (!from)
170
+ throw new Error(`--rewrite-host has an empty old host: '${spec}'`);
171
+ if (!/^https?:\/\//.test(to)) {
172
+ throw new Error(`--rewrite-host needs a scheme on the new origin, got '${to}'. Use https://${to}`);
173
+ }
174
+ if (from.includes('/')) {
175
+ throw new Error(`--rewrite-host old side should be a bare host, got '${from}'`);
176
+ }
177
+ return { from, to };
178
+ }
179
+ //# sourceMappingURL=transforms.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transforms.js","sourceRoot":"","sources":["../src/transforms.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAUH,4CAA4C;AAC5C,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AASxF;;;;;;;GAOG;AACH,MAAM,UAAU,kBAAkB;IAChC,OAAO;;;;mDAI0C,CAAC;AACpD,CAAC;AAED,gDAAgD;AAChD,MAAM,UAAU,WAAW,CAAC,QAAgB;IAC1C,OAAO,UAAU,CAAC,GAAG,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC;AAChD,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC;AACzC,CAAC;AAED,0CAA0C;AAC1C,MAAM,UAAU,YAAY,CAAC,KAAa;IACxC,OAAO,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC;AAC1C,CAAC;AAED,sCAAsC;AACtC,MAAM,UAAU,OAAO,CAAC,CAAsC;IAC5D,OAAO,GAAG,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,IAA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K,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IACtF,MAAM,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAEzD,IAAI,CAAC,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,0CAA0C,IAAI,GAAG,CAAC,CAAC;IAC9E,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK,CACb,yDAAyD,EAAE,kBAAkB,EAAE,EAAE,CAClF,CAAC;IACJ,CAAC;IACD,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,uDAAuD,IAAI,GAAG,CAAC,CAAC;IAClF,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;AACtB,CAAC"}
@@ -0,0 +1,238 @@
1
+ /**
2
+ * Moving an application and its data from one platform to another.
3
+ *
4
+ * `packages/cloud/*` already answers "give me a machine": connect, quote,
5
+ * provision, destroy. That is not this. Provisioning the destination is the
6
+ * easy half of a migration; the half that goes wrong is the data — the dump
7
+ * that silently omitted an extension, the storage bucket whose objects are
8
+ * referenced by absolute URL in a thousand rows, the cron jobs that start
9
+ * firing from two places at once because the cutover happened in the wrong
10
+ * order.
11
+ *
12
+ * The shape here comes from an actual migration rather than a whiteboard:
13
+ * crawlproof.com off Railway and Supabase cloud onto a dedicated box, which
14
+ * moved a 4.7 GB database, 8,410 storage objects across three buckets, ten
15
+ * pg_cron jobs and a realtime publication, and which would have quietly broken
16
+ * the site months later over 2,928 rows holding absolute storage URLs.
17
+ *
18
+ * ## Why this is not N×M adapters
19
+ *
20
+ * The naive shape is one adapter per (source, target) pair, which is why most
21
+ * migration tooling supports exactly one direction. The split that avoids it:
22
+ *
23
+ * **Platforms** answer "what have I got, and what are the credentials?"
24
+ * Railway, Supabase, Turso, Fly, Neon, Vercel, a plain VPS over ssh. A
25
+ * platform does not know how to move a byte. It does an `inventory()` and
26
+ * hands back resources with connection details attached.
27
+ *
28
+ * **Engines** move the bytes. Postgres, MySQL, SQLite/libSQL, Redis,
29
+ * S3-compatible object storage, plain files. An engine does not know or care
30
+ * which vendor either side is.
31
+ *
32
+ * So Railway→dedicated and dedicated→Railway are the same code path, and a new
33
+ * platform costs one `inventory()` rather than one adapter per existing
34
+ * platform. Direction is not a property of the system; it is which platform
35
+ * you named first.
36
+ *
37
+ * Every resource carries the engine that can move it. A migration is possible
38
+ * exactly when, for each resource the source lists, the target can accept that
39
+ * engine — which is a check the planner can make before touching anything.
40
+ */
41
+ /**
42
+ * What kind of thing is being moved, which is the same as asking which engine
43
+ * moves it.
44
+ *
45
+ * Deliberately about storage shape rather than vendor: Supabase's database and
46
+ * Neon's are both `postgres`, and the code that moves one moves the other. A
47
+ * vendor difference that genuinely matters (Supabase's auth schema, its
48
+ * storage metadata tables) is a `quirk` on the resource, not a new kind.
49
+ */
50
+ export type ResourceKind = 'postgres' | 'mysql' | 'sqlite' | 'redis' | 'object-storage' | 'files' | 'env' | 'cron' | 'dns';
51
+ /** Every engine id, for exhaustiveness checks and for the CLI's help text. */
52
+ export declare const RESOURCE_KINDS: readonly ResourceKind[];
53
+ /**
54
+ * A credential or connection string.
55
+ *
56
+ * Kept as a getter rather than a value so a plan can be printed, stored and
57
+ * reviewed without a password ever being serialised into it. `describe()` is
58
+ * what goes in the plan file; `reveal()` is called only inside an engine, at
59
+ * the moment it runs.
60
+ */
61
+ export interface Secretish {
62
+ /** Safe for logs, plan files and terminal output. Never the secret. */
63
+ describe(): string;
64
+ /** The actual value. Call as late as possible, never log the result. */
65
+ reveal(): string;
66
+ }
67
+ /**
68
+ * A secret that is safe to print because it is not one — a hostname, a bucket
69
+ * name, a database name.
70
+ */
71
+ export declare function plain(value: string): Secretish;
72
+ /**
73
+ * Wrap a real credential. `describe()` shows enough to tell two apart without
74
+ * showing either: scheme and host for a URL, a short prefix otherwise.
75
+ */
76
+ export declare function secret(value: string): Secretish;
77
+ /**
78
+ * One movable thing on a platform.
79
+ *
80
+ * `id` is the platform's own identifier and `name` is what a person calls it.
81
+ * `sizeBytes` and `itemCount` are best-effort: they drive the plan's estimate
82
+ * and the progress output, and being wrong is not fatal. `quirks` is where a
83
+ * vendor's non-portable detail is recorded so the planner can warn about it
84
+ * rather than discovering it halfway through a restore.
85
+ */
86
+ export interface Resource {
87
+ kind: ResourceKind;
88
+ id: string;
89
+ name: string;
90
+ /** How to reach it. Engine-specific; see each engine for what it needs. */
91
+ connection: Record<string, Secretish>;
92
+ sizeBytes?: number;
93
+ itemCount?: number;
94
+ /**
95
+ * Vendor specifics that survive or do not survive a move: postgres
96
+ * extensions, a Supabase auth schema, pg_cron jobs, a realtime publication.
97
+ * The planner turns these into warnings and extra steps.
98
+ */
99
+ quirks?: string[];
100
+ metadata?: Record<string, string | number | boolean>;
101
+ }
102
+ /** What a platform reported when asked what it holds. */
103
+ export interface Inventory {
104
+ platform: string;
105
+ /** What the platform calls this deployment: a project, an app, an account. */
106
+ scope: string;
107
+ resources: Resource[];
108
+ /** Anything the platform could not enumerate and a human should check. */
109
+ notes?: string[];
110
+ }
111
+ export interface PlatformContext {
112
+ secret(key: string): string | undefined;
113
+ log(msg: string, level?: 'info' | 'warn' | 'error'): void;
114
+ /** True when nothing may be mutated anywhere. */
115
+ dryRun: boolean;
116
+ }
117
+ /**
118
+ * A platform: somewhere an app lives. Implementations resolve credentials and
119
+ * enumerate resources; they never move data.
120
+ *
121
+ * `role` is honest about what a platform can do rather than aspirational.
122
+ * Railway can be read from and written to; a managed provider that offers no
123
+ * import path is `source` only, and saying so lets the planner refuse early
124
+ * with a clear message instead of failing at the last step.
125
+ */
126
+ export interface Platform<Config = unknown> {
127
+ id: string;
128
+ label: string;
129
+ role: 'source' | 'target' | 'both';
130
+ /** Kinds this platform can hold. The planner intersects source and target. */
131
+ supports: ResourceKind[];
132
+ /** Enumerate what is there. Read-only; safe to run against production. */
133
+ inventory(ctx: PlatformContext, config: Config): Promise<Inventory>;
134
+ /**
135
+ * Make a place for an incoming resource and return the connection an engine
136
+ * should write to. Absent on `source`-only platforms.
137
+ */
138
+ provision?(ctx: PlatformContext, resource: Resource, config: Config): Promise<Resource>;
139
+ /** Cheap credentials check, so a three-hour migration fails in the first second. */
140
+ check?(ctx: PlatformContext, config: Config): Promise<void>;
141
+ }
142
+ /** Where an engine stages bytes between export and import. */
143
+ export interface Staging {
144
+ /** Absolute path to a directory the engine may write into. */
145
+ dir: string;
146
+ /** Record an artifact so a resumed run can find it again. */
147
+ record(artifact: Artifact): Promise<void>;
148
+ /** Artifacts already produced for this resource, if the run is resuming. */
149
+ existing(resourceId: string): Promise<Artifact[]>;
150
+ }
151
+ /** Something an export produced: a dump file, a manifest, a directory of objects. */
152
+ export interface Artifact {
153
+ resourceId: string;
154
+ kind: ResourceKind;
155
+ /** Path relative to the staging dir. */
156
+ path: string;
157
+ sizeBytes?: number;
158
+ /** Set once the artifact is complete; a partial artifact has no checksum. */
159
+ sha256?: string;
160
+ metadata?: Record<string, string | number | boolean>;
161
+ }
162
+ export interface EngineContext {
163
+ log(msg: string, level?: 'info' | 'warn' | 'error'): void;
164
+ dryRun: boolean;
165
+ staging: Staging;
166
+ /**
167
+ * Run a command. Injected rather than imported so tests drive an engine
168
+ * without a database, and so a dry run can record commands instead of
169
+ * running them.
170
+ */
171
+ exec(cmd: string, args: string[], opts?: ExecOptions): Promise<ExecResult>;
172
+ }
173
+ export interface ExecOptions {
174
+ /** Extra environment. Secrets belong here, never in `args`, which is logged. */
175
+ env?: Record<string, string>;
176
+ cwd?: string;
177
+ /** Fail the step if the command exits non-zero. Default true. */
178
+ check?: boolean;
179
+ timeoutMs?: number;
180
+ /**
181
+ * Send stdout to this absolute path instead of buffering it.
182
+ *
183
+ * Some tools only dump to stdout — `sqlite3 .dump`, `turso db shell .dump` —
184
+ * and a multi-gigabyte dump must not be held in a string. Expressed as an
185
+ * option rather than a shell redirect because `exec` runs without a shell,
186
+ * so `>` would be passed to the program as a literal argument.
187
+ */
188
+ stdoutFile?: string;
189
+ /** Feed this file to stdin. The load half of the same problem. */
190
+ stdinFile?: string;
191
+ }
192
+ export interface ExecResult {
193
+ code: number;
194
+ stdout: string;
195
+ stderr: string;
196
+ }
197
+ /**
198
+ * An engine moves one kind of resource between two connections.
199
+ *
200
+ * Split into export and import rather than a single `copy` so a migration can
201
+ * stage everything, be inspected, and then be cut over — which is what makes
202
+ * the delta sync and the rollback possible. A direct streaming copy is an
203
+ * optimisation an engine may offer via `copy`, not the contract.
204
+ */
205
+ export interface Engine {
206
+ kind: ResourceKind;
207
+ /** Binaries that must exist for this engine to run, e.g. ['pg_dump']. */
208
+ requires: string[];
209
+ /** Read the source into staging. */
210
+ export(ctx: EngineContext, from: Resource): Promise<Artifact[]>;
211
+ /**
212
+ * Write staged artifacts into the target.
213
+ *
214
+ * `from` is the source resource, passed because not every engine stages the
215
+ * bytes themselves. Object storage is the case that forces it: pulling ten
216
+ * gigabytes down and pushing them back up doubles the transfer for no
217
+ * benefit, so its export writes only a manifest and its import runs a
218
+ * remote-to-remote copy, which means it still needs to know where the
219
+ * objects came from.
220
+ */
221
+ import(ctx: EngineContext, to: Resource, artifacts: Artifact[], from?: Resource): Promise<void>;
222
+ /**
223
+ * Re-read only what changed since a timestamp. This is what makes a cutover
224
+ * short: the bulk copy happens while the source is live, and only the delta
225
+ * is moved during the window where writes are stopped. An engine that cannot
226
+ * do this omits it, and the planner says the cutover needs full downtime.
227
+ */
228
+ delta?(ctx: EngineContext, from: Resource, since: Date): Promise<Artifact[]>;
229
+ /** Compare source and target after the fact. Row counts, object counts. */
230
+ verify?(ctx: EngineContext, from: Resource, to: Resource): Promise<VerifyResult>;
231
+ }
232
+ export interface VerifyResult {
233
+ ok: boolean;
234
+ /** One line per check, e.g. "public.users: 113 → 113". */
235
+ checks: string[];
236
+ problems: string[];
237
+ }
238
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH;;;;;;;;GAQG;AACH,MAAM,MAAM,YAAY,GACpB,UAAU,GACV,OAAO,GACP,QAAQ,GACR,OAAO,GACP,gBAAgB,GAChB,OAAO,GACP,KAAK,GACL,MAAM,GACN,KAAK,CAAC;AAEV,8EAA8E;AAC9E,eAAO,MAAM,cAAc,EAAE,SAAS,YAAY,EAUxC,CAAC;AAEX;;;;;;;GAOG;AACH,MAAM,WAAW,SAAS;IACxB,uEAAuE;IACvE,QAAQ,IAAI,MAAM,CAAC;IACnB,wEAAwE;IACxE,MAAM,IAAI,MAAM,CAAC;CAClB;AAED;;;GAGG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAE9C;AAED;;;GAGG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAa/C;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,YAAY,CAAC;IACnB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,2EAA2E;IAC3E,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACtC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC;CACtD;AAED,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AAM,CAAC;CAChB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,MAAM;IACrB,IAAI,EAAE,YAAY,CAAC;IACnB,yEAAyE;IACzE,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,oCAAoC;IACpC,MAAM,CAAC,GAAG,EAAE,aAAa,EAAE,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;IAChE;;;;;;;;;OASG;IACH,MAAM,CAAC,GAAG,EAAE,aAAa,EAAE,EAAE,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,EAAE,IAAI,CAAC,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAChG;;;;;OAKG;IACH,KAAK,CAAC,CAAC,GAAG,EAAE,aAAa,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC7E,2EAA2E;IAC3E,MAAM,CAAC,CAAC,GAAG,EAAE,aAAa,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,EAAE,QAAQ,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;CAClF;AAED,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,OAAO,CAAC;IACZ,0DAA0D;IAC1D,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB"}
package/dist/types.js ADDED
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Moving an application and its data from one platform to another.
3
+ *
4
+ * `packages/cloud/*` already answers "give me a machine": connect, quote,
5
+ * provision, destroy. That is not this. Provisioning the destination is the
6
+ * easy half of a migration; the half that goes wrong is the data — the dump
7
+ * that silently omitted an extension, the storage bucket whose objects are
8
+ * referenced by absolute URL in a thousand rows, the cron jobs that start
9
+ * firing from two places at once because the cutover happened in the wrong
10
+ * order.
11
+ *
12
+ * The shape here comes from an actual migration rather than a whiteboard:
13
+ * crawlproof.com off Railway and Supabase cloud onto a dedicated box, which
14
+ * moved a 4.7 GB database, 8,410 storage objects across three buckets, ten
15
+ * pg_cron jobs and a realtime publication, and which would have quietly broken
16
+ * the site months later over 2,928 rows holding absolute storage URLs.
17
+ *
18
+ * ## Why this is not N×M adapters
19
+ *
20
+ * The naive shape is one adapter per (source, target) pair, which is why most
21
+ * migration tooling supports exactly one direction. The split that avoids it:
22
+ *
23
+ * **Platforms** answer "what have I got, and what are the credentials?"
24
+ * Railway, Supabase, Turso, Fly, Neon, Vercel, a plain VPS over ssh. A
25
+ * platform does not know how to move a byte. It does an `inventory()` and
26
+ * hands back resources with connection details attached.
27
+ *
28
+ * **Engines** move the bytes. Postgres, MySQL, SQLite/libSQL, Redis,
29
+ * S3-compatible object storage, plain files. An engine does not know or care
30
+ * which vendor either side is.
31
+ *
32
+ * So Railway→dedicated and dedicated→Railway are the same code path, and a new
33
+ * platform costs one `inventory()` rather than one adapter per existing
34
+ * platform. Direction is not a property of the system; it is which platform
35
+ * you named first.
36
+ *
37
+ * Every resource carries the engine that can move it. A migration is possible
38
+ * exactly when, for each resource the source lists, the target can accept that
39
+ * engine — which is a check the planner can make before touching anything.
40
+ */
41
+ /** Every engine id, for exhaustiveness checks and for the CLI's help text. */
42
+ export const RESOURCE_KINDS = [
43
+ 'postgres',
44
+ 'mysql',
45
+ 'sqlite',
46
+ 'redis',
47
+ 'object-storage',
48
+ 'files',
49
+ 'env',
50
+ 'cron',
51
+ 'dns',
52
+ ];
53
+ /**
54
+ * A secret that is safe to print because it is not one — a hostname, a bucket
55
+ * name, a database name.
56
+ */
57
+ export function plain(value) {
58
+ return { describe: () => value, reveal: () => value };
59
+ }
60
+ /**
61
+ * Wrap a real credential. `describe()` shows enough to tell two apart without
62
+ * showing either: scheme and host for a URL, a short prefix otherwise.
63
+ */
64
+ export function secret(value) {
65
+ return {
66
+ reveal: () => value,
67
+ describe: () => {
68
+ try {
69
+ const u = new URL(value);
70
+ const user = u.username ? `${u.username}:***@` : '';
71
+ return `${u.protocol}//${user}${u.host}${u.pathname}`;
72
+ }
73
+ catch {
74
+ return value.length <= 4 ? '***' : `${value.slice(0, 4)}***`;
75
+ }
76
+ },
77
+ };
78
+ }
79
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAsBH,8EAA8E;AAC9E,MAAM,CAAC,MAAM,cAAc,GAA4B;IACrD,UAAU;IACV,OAAO;IACP,QAAQ;IACR,OAAO;IACP,gBAAgB;IAChB,OAAO;IACP,KAAK;IACL,MAAM;IACN,KAAK;CACG,CAAC;AAiBX;;;GAGG;AACH,MAAM,UAAU,KAAK,CAAC,KAAa;IACjC,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC;AACxD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,MAAM,CAAC,KAAa;IAClC,OAAO;QACL,MAAM,EAAE,GAAG,EAAE,CAAC,KAAK;QACnB,QAAQ,EAAE,GAAG,EAAE;YACb,IAAI,CAAC;gBACH,MAAM,CAAC,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;gBACzB,MAAM,IAAI,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,QAAQ,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;gBACpD,OAAO,GAAG,CAAC,CAAC,QAAQ,KAAK,IAAI,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC;YACxD,CAAC;YAAC,MAAM,CAAC;gBACP,OAAO,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC;YAC/D,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,35 @@
1
1
  {
2
2
  "name": "@profullstack/sh1pt-migrate",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.15",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "scripts": {
7
+ "build": "tsc -p tsconfig.json",
8
+ "typecheck": "tsc -p tsconfig.json --noEmit"
9
+ },
10
+ "dependencies": {
11
+ "@profullstack/sh1pt-core": "0.3.7"
12
+ },
13
+ "license": "MIT",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/profullstack/sh1pt.git",
17
+ "directory": "packages/migrate"
18
+ },
19
+ "homepage": "https://sh1pt.com",
20
+ "bugs": "https://github.com/profullstack/sh1pt/issues",
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "types": "./dist/index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js",
29
+ "default": "./dist/index.js"
30
+ }
31
+ },
32
+ "publishConfig": {
33
+ "access": "public"
34
+ }
35
+ }