cratefield-import-supabase 0.2.2

Read-only inspection of a Supabase project and the migration report the Supabase importer is planned from (ADR 0026)
Documentation
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! The human rendering of a [`Report`]. The JSON is the source of truth
//! (ADR 0026, Decision 6); this is rendered from it and carries nothing it
//! does not.

use std::fmt::Write as _;

use crate::report::{Classification, Finding, Report, SourceStatus};

/// `1.5 MiB`, `812 B`.
#[must_use]
pub(crate) fn human_bytes(bytes: u64) -> String {
    const UNITS: &[&str] = &["B", "KiB", "MiB", "GiB", "TiB"];
    let mut value = bytes;
    let mut unit = 0;
    while value >= 1024 * 10 && unit < UNITS.len() - 1 {
        value /= 1024;
        unit += 1;
    }
    if unit == 0 && bytes >= 1024 {
        // One decimal for small KiB values.
        #[allow(clippy::cast_precision_loss)]
        return format!("{:.1} KiB", bytes as f64 / 1024.0);
    }
    format!("{value} {}", UNITS[unit])
}

fn duration(seconds: u64) -> String {
    match seconds {
        0..60 => format!("{seconds} s"),
        60..3600 => format!("{} min", seconds.div_ceil(60)),
        _ => format!("{:.1} h", seconds_to_hours(seconds)),
    }
}

#[allow(clippy::cast_precision_loss)]
fn seconds_to_hours(seconds: u64) -> f64 {
    seconds as f64 / 3600.0
}

/// One table cell: no pipes, no newlines.
fn cell(text: &str) -> String {
    text.split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
        .replace('|', "\\|")
}

fn code(text: &str) -> String {
    let text = cell(text);
    if text.contains('`') {
        format!("`` {text} ``")
    } else {
        format!("`{text}`")
    }
}

fn status(status: SourceStatus) -> &'static str {
    match status {
        SourceStatus::Inspected => "inspected",
        SourceStatus::NotInspected => "not inspected",
        SourceStatus::NotVisible => "not visible",
        SourceStatus::Failed => "failed (see warnings)",
    }
}

fn yes(value: bool) -> &'static str {
    if value { "yes" } else { "no" }
}

fn findings_table(out: &mut String, findings: &[&Finding], with_equivalent: bool) {
    if with_equivalent {
        out.push_str(
            "| Kind | Object | Phase | Why | Cratefield equivalent |\n|---|---|---|---|---|\n",
        );
    } else {
        out.push_str("| Kind | Object | Phase | Why |\n|---|---|---|---|\n");
    }
    for finding in findings {
        let _ = write!(
            out,
            "| {} | {} | {} | {}",
            finding.kind,
            code(&finding.object),
            finding.phase,
            cell(&finding.reason)
        );
        if with_equivalent {
            let _ = write!(out, " | {}", cell(&finding.cratefield_equivalent));
        }
        out.push_str(" |\n");
    }
    out.push('\n');
}

impl Report {
    /// The report as Markdown.
    #[must_use]
    #[allow(clippy::too_many_lines)] // one block per section, in order
    pub fn to_markdown(&self) -> String {
        let mut out = String::new();
        let s = &self.summary;
        let _ = writeln!(
            out,
            "# Supabase migration report: `{}`\n\nSource `{}:{}/{}`, PostgreSQL {}. Report \
             version {}, written by {} {}. Read-only: nothing was written to the project.\n",
            self.project.project_ref,
            self.project.host,
            self.project.port,
            self.project.database,
            self.project.server_version,
            self.report_version,
            self.tool.name,
            self.tool.version,
        );

        out.push_str("## Summary\n\n");
        let storage = match (s.storage_objects, s.storage_bytes) {
            (Some(objects), Some(bytes)) => format!("{objects} objects, {}", human_bytes(bytes)),
            _ => "not visible to the role".to_owned(),
        };
        let estimate_basis = if s.storage_bytes.is_some() {
            "data and storage only; index builds and verification are extra"
        } else {
            "data only — storage is not visible to the role, so it is not in this estimate; \
             index builds and verification are extra"
        };
        let _ = writeln!(
            out,
            "| | |\n|---|---|\n| Ready (no blockers) | **{}** |\n| Automatic | {} |\n| Needs \
             work | {} |\n| Blockers | {} |\n| Decided | {} |\n| Undecided | {} |\n| Tables | {} \
             (~{} rows) |\n| Data | {} (plus {} of \
             indexes, rebuilt on the target) |\n| Storage | {} |\n| Estimated \
             transfer | about {} at {} Mbit/s ({}) |\n",
            yes(s.ready),
            s.automatic,
            s.needs_work,
            s.blockers,
            s.decided,
            s.undecided,
            s.tables,
            s.estimated_rows,
            human_bytes(s.data_bytes),
            human_bytes(s.index_bytes),
            storage,
            duration(s.estimated_transfer_seconds),
            s.transfer_assumed_mbps,
            estimate_basis,
        );

        let r = &self.read_only;
        let _ = writeln!(
            out,
            "**Read-only evidence:** read-only transaction {}, read-only session {}, no \
             transaction id assigned {}; role `{}` (superuser {}, holds write privileges {}).\n",
            yes(r.transaction_read_only),
            yes(r.session_read_only),
            yes(r.no_transaction_id_assigned),
            r.role,
            yes(r.role_is_superuser),
            yes(r.role_can_write),
        );
        let _ = writeln!(
            out,
            "**Coverage:** database {}, Management API {}, policy classifier {}.\n",
            status(self.coverage.database),
            status(self.coverage.management_api),
            status(self.coverage.policy_classifier),
        );

        // Sections the role cannot see: unknown, never zero, with the SQL
        // that fixes them in one pasteable block (issue #723).
        let not_visible: Vec<_> = self
            .coverage
            .sections
            .iter()
            .filter(|section| section.coverage == SourceStatus::NotVisible)
            .collect();
        if !not_visible.is_empty() {
            let names: Vec<String> = not_visible
                .iter()
                .map(|section| format!("`{}`", section.section))
                .collect();
            let _ = writeln!(
                out,
                "**Not visible to `{}`:** {}. This is unknown, not zero: the counts below are \
                 omitted for those sections.\n",
                self.read_only.role,
                names.join(", ")
            );
            out.push_str("### Grants needed\n\n");
            out.push_str(
                "Run as the project's `postgres` role on the source, which can grant these:\n\n",
            );
            out.push_str("```sql\n");
            let mut lines: Vec<&str> = Vec::new();
            for section in &not_visible {
                for line in section.fix.lines() {
                    let line = line.trim();
                    if !line.is_empty() && !lines.contains(&line) {
                        lines.push(line);
                    }
                }
            }
            for line in lines {
                let _ = writeln!(out, "{line}");
            }
            out.push_str("```\n\n");
        }

        for (class, title, blurb, with_equivalent) in [
            (
                Classification::Blocker,
                "Blockers",
                "Each stops a later step until it is resolved.",
                true,
            ),
            (
                Classification::NeedsWork,
                "Needs work",
                "Moved or reported, but the venture writes or decides something. Each needs a \
                 `covered` or `waived` disposition before cutover.",
                true,
            ),
            (
                Classification::Automatic,
                "Automatic",
                "Moved by the importer with nothing to decide.",
                true,
            ),
        ] {
            let findings: Vec<&Finding> = self
                .findings
                .iter()
                .filter(|finding| finding.classification == class)
                .collect();
            let _ = writeln!(out, "## {title} ({})\n", findings.len());
            if findings.is_empty() {
                out.push_str("None.\n\n");
                continue;
            }
            let _ = writeln!(out, "{blurb}\n");
            findings_table(&mut out, &findings, with_equivalent);
        }

        let d = &self.dispositions;
        out.push_str("## Dispositions\n\n");
        if d.by_kind.is_empty() {
            out.push_str("Nothing needs a disposition.\n\n");
        } else {
            out.push_str(
                "Every needs-work item needs its own `covered` or `waived` entry (ADR 0026, \
                 Decision 5): `fz import supabase inspect --dispositions <FILE>`. Cutover \
                 refuses while `Undecided` is non-zero.\n\n| Kind | Covered | Waived | Undecided \
                 |\n|---|---|---|---|\n",
            );
            for kind in &d.by_kind {
                let _ = writeln!(
                    out,
                    "| {} | {} | {} | {} |",
                    kind.kind, kind.covered, kind.waived, kind.undecided
                );
            }
            out.push('\n');
            if !d.stale.is_empty() {
                out.push_str("**Stale entries** (they match no item that needs work):\n\n");
                for id in &d.stale {
                    let _ = writeln!(out, "- {}", code(id));
                }
                out.push('\n');
            }
        }

        let _ = writeln!(
            out,
            "## Row-level security ({} policies)\n",
            self.policies.len()
        );
        if self.policies.is_empty() {
            out.push_str("None.\n\n");
        } else {
            out.push_str(
                "RLS is neither copied nor translated (ADR 0026). The pattern and the suggested \
                 check are advice: a person writes the check, turns the policy's test stub (in \
                 the JSON report) into a passing test, and records the policy as covered or \
                 waived.\n\n| Table | Policy | Command | Roles | USING | WITH CHECK | Pattern \
                 | Confidence | Source | Suggested check |\n|---|---|---|---|---|---|---|---|---|---|\n",
            );
            for policy in &self.policies {
                let pattern = serde_json::to_value(policy.pattern)
                    .ok()
                    .and_then(|value| value.as_str().map(str::to_owned))
                    .unwrap_or_default();
                let source = serde_json::to_value(policy.source)
                    .ok()
                    .and_then(|value| value.as_str().map(str::to_owned))
                    .unwrap_or_default();
                let _ = writeln!(
                    out,
                    "| `{}.{}` | {} | {} | {} | {} | {} | {}{} | {:.2} | {} | {} |",
                    policy.schema,
                    policy.table,
                    cell(&policy.name),
                    policy.command,
                    policy.roles.join(", "),
                    policy.using.as_deref().map_or_else(|| "—".to_owned(), code),
                    policy
                        .with_check
                        .as_deref()
                        .map_or_else(|| "—".to_owned(), code),
                    pattern,
                    policy
                        .classifier_label
                        .as_deref()
                        .map_or_else(String::new, |label| format!(" (classifier said {label})")),
                    policy.confidence,
                    source,
                    cell(&policy.suggested_equivalent),
                );
            }
            out.push('\n');
        }

        let _ = writeln!(
            out,
            "## Supabase-managed policies ({})\n",
            self.managed_policies.len()
        );
        if self.managed_policies.is_empty() {
            out.push_str("None.\n\n");
        } else {
            let names = self
                .managed_policies
                .iter()
                .map(|policy| {
                    format!(
                        "`{}.{}.{}`",
                        policy.schema,
                        policy.table,
                        cell(&policy.name)
                    )
                })
                .collect::<Vec<_>>()
                .join(", ");
            let _ = writeln!(
                out,
                "On Supabase's own schemas: the platform's, not the venture's — never copied and \
                 never a finding: {names}.\n"
            );
        }

        let _ = writeln!(out, "## Tables ({})\n", self.tables.len());
        if !self.tables.is_empty() {
            out.push_str(
                "| Table | Rows (est.) | Data | Indexes | RLS | Primary key |\n|---|---|---|---|---|---|\n",
            );
            for table in &self.tables {
                let _ = writeln!(
                    out,
                    "| `{}.{}` | {} | {} | {} | {} | {} |",
                    table.schema,
                    table.name,
                    table
                        .estimated_rows
                        .map_or_else(|| "not analyzed".to_owned(), |rows| rows.to_string()),
                    human_bytes(table.data_bytes),
                    human_bytes(table.index_bytes),
                    yes(table.rls_enabled),
                    if table.primary_key.is_empty() {
                        "none".to_owned()
                    } else {
                        table.primary_key.join(", ")
                    },
                );
            }
            out.push('\n');
        }

        let a = &self.auth;
        out.push_str("## Auth\n\n");
        if a.present {
            if let Some(users) = a.users {
                let _ = writeln!(
                    out,
                    "{} users ({} without a password, {} unconfirmed, {} anonymous); {} MFA \
                     factors; {} SSO providers.\n",
                    users,
                    a.users_without_password.unwrap_or(0),
                    a.users_unconfirmed.unwrap_or(0),
                    a.anonymous_users.unwrap_or(0),
                    a.mfa_factors.unwrap_or(0),
                    a.sso_providers.unwrap_or(0),
                );
                let identities = a
                    .identities_by_provider
                    .as_deref()
                    .unwrap_or_default()
                    .iter()
                    .map(|count| format!("{} ({})", count.provider, count.identities))
                    .collect::<Vec<_>>();
                if !identities.is_empty() {
                    let _ = writeln!(out, "Identities by provider: {}.\n", identities.join(", "));
                }
            } else {
                let _ = writeln!(
                    out,
                    "Not visible to the role `{}`: the counts are unknown, not zero (see \
                     Coverage).\n",
                    self.read_only.role
                );
            }
            match &a.enabled_providers {
                Some(enabled) => {
                    let _ = writeln!(
                        out,
                        "Enabled in the auth configuration: {}; MFA: {}.\n",
                        if enabled.is_empty() {
                            "none".to_owned()
                        } else {
                            enabled.join(", ")
                        },
                        a.enabled_mfa
                            .as_ref()
                            .filter(|mfa| !mfa.is_empty())
                            .map_or_else(|| "none".to_owned(), |mfa| mfa.join(", "))
                    );
                }
                None => out
                    .push_str("The auth configuration was not read (no Management API token).\n\n"),
            }
        } else {
            out.push_str("No `auth` schema.\n\n");
        }

        out.push_str("## Storage\n\n");
        if self.storage.present {
            match &self.storage.buckets {
                None => {
                    let _ = writeln!(
                        out,
                        "Not visible to the role `{}`: the bucket list is unknown, not empty (see \
                         Coverage).\n",
                        self.read_only.role
                    );
                }
                Some(buckets) if buckets.is_empty() => out.push_str("No buckets.\n\n"),
                Some(buckets) => {
                    out.push_str(
                        "| Bucket | Public | Objects | Size | Over 10 MiB |\n|---|---|---|---|---|\n",
                    );
                    for bucket in buckets {
                        let objects = bucket
                            .objects
                            .map_or_else(|| "—".to_owned(), |count| count.to_string());
                        let size = bucket.bytes.map_or_else(|| "—".to_owned(), human_bytes);
                        let over = bucket
                            .objects_over_blob_cap
                            .map_or_else(|| "—".to_owned(), |count| count.to_string());
                        let _ = writeln!(
                            out,
                            "| `{}` | {} | {} | {} | {} |",
                            bucket.id,
                            yes(bucket.public),
                            objects,
                            size,
                            over
                        );
                    }
                    out.push('\n');
                    for bucket in buckets.iter().filter(|bucket| !bucket.policies.is_empty()) {
                        let _ = writeln!(out, "Bucket `{}` policies:", bucket.id);
                        for policy in &bucket.policies {
                            let scope = if policy.all_buckets {
                                " (every bucket)"
                            } else {
                                ""
                            };
                            let _ = writeln!(
                                out,
                                "- `{}` ({}, {} on `{}`{}) — {}",
                                policy.name,
                                policy.roles.join(", "),
                                policy.command,
                                policy.table,
                                scope,
                                cell(&policy.suggested_equivalent)
                            );
                        }
                        out.push('\n');
                    }
                }
            }
            for policy in &self.storage.unattached_policies {
                let reason = if self.storage.buckets.is_none() {
                    "the bucket list is not visible to the role, so it is not attached"
                } else {
                    "no bucket exists to attach it to"
                };
                let _ = writeln!(
                    out,
                    "Storage policy `{}` on `{}`: {} — {}",
                    policy.name,
                    policy.table,
                    reason,
                    cell(&policy.suggested_equivalent)
                );
            }
            if !self.storage.unattached_policies.is_empty() {
                out.push('\n');
            }
        } else {
            out.push_str("No `storage` schema.\n\n");
        }

        out.push_str("## Edge Functions\n\n");
        match self.edge_functions.status {
            SourceStatus::Inspected if self.edge_functions.functions.is_empty() => {
                out.push_str("None.\n\n");
            }
            SourceStatus::Inspected => {
                for function in &self.edge_functions.functions {
                    let _ = writeln!(out, "- `{}` ({})", function.slug, function.status);
                }
                out.push('\n');
            }
            _ => out.push_str(
                "Not inspected: unknown, not none. Pass a Management API token to list them.\n\n",
            ),
        }

        out.push_str("## Realtime and cron\n\n");
        if self.realtime.publications.is_empty() {
            out.push_str("No publications.\n");
        }
        for publication in &self.realtime.publications {
            let tables = if publication.all_tables {
                "all tables".to_owned()
            } else if publication.tables.is_empty() {
                "no tables".to_owned()
            } else {
                publication.tables.join(", ")
            };
            let _ = writeln!(out, "- Publication `{}`: {tables}.", publication.name);
        }
        match &self.cron_jobs {
            Some(jobs) => {
                for job in jobs {
                    let _ = writeln!(out, "- Cron job `{}` on `{}`.", job.name, job.schedule);
                }
            }
            None => {
                let _ = writeln!(
                    out,
                    "- Cron jobs: not visible to the role `{}` (unknown, not none).",
                    self.read_only.role
                );
            }
        }
        out.push('\n');

        if !self.warnings.is_empty() {
            out.push_str("## Warnings\n\n");
            for warning in &self.warnings {
                let _ = writeln!(out, "- {warning}");
            }
            out.push('\n');
        }

        out.push_str(
            "## Next\n\nResolve the blockers, decide each needs-work item, then run the later \
             steps (docs/import/supabase.md): auth users, schema and data into `app`, storage \
             into R2, verify, cut over.\n",
        );
        out
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sizes_read_naturally() {
        assert_eq!(human_bytes(812), "812 B");
        assert_eq!(human_bytes(1536), "1.5 KiB");
        assert_eq!(human_bytes(12 * 1024 * 1024), "12 MiB");
    }

    #[test]
    fn cells_cannot_break_the_table() {
        assert_eq!(cell("a | b\nc"), "a \\| b c");
    }
}