zippa-db 0.1.0

A fast, lightweight, cross-platform database client for PostgreSQL, MySQL, and SQLite.
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
//! A table's own definition: columns, indexes, and foreign keys.
//!
//! TODO.md's "Table Schema Inspector & DDL Generator". Every section is now a
//! form that diffs itself against what was loaded and turns the difference
//! into statements (`sql.rs`), previewed and confirmed before they run —
//! unconditionally, on every `SafetyMode`, since a dropped column or foreign
//! key can lose data or break other tables in a way a single row's edit
//! cannot. Every section lays its rows out in a `gpui_kit::component::table`
//! `Table`, so the fields line up under column headings the way they do
//! elsewhere in the app. The rows stay form controls — text boxes, dropdowns,
//! and checkboxes — rather than a virtualized data grid, because a row here is
//! a handful of heterogeneous fields being edited, not one record among many.

use std::sync::Arc;

use anyhow::Context as _;
use gpui_kit::base::TestSupportExt;
use gpui_kit::component::button::{Button, ButtonVariants};
use gpui_kit::component::input::{InputEvent, InputState, Textarea, TextareaState};
use gpui_kit::component::scroll::ScrollableElement;
use gpui_kit::component::{ActiveTheme, Disableable, Sizable, h_flex, v_flex};
use gpui_kit::prelude::*;
use gpui_kit::{App, Context, Entity, Window, div, px};

use crate::db::{
    ColumnDef, Connection, DatabaseObject, Engine, ForeignKeyDef, IndexDef, ObjectKind,
    RebuildSource, ReferentialAction, TableSchema, runtime,
};

mod columns;
mod foreign_keys;
mod indexes;
mod layout;
mod rebuild;
mod sql;
#[cfg(test)]
mod test_support;

use rebuild::Change;
use sql::{ColumnEdit, ForeignKeyEdit, IndexEdit};

/// The fixed set of `ON DELETE`/`ON UPDATE` choices, for the action dropdowns.
fn referential_actions() -> [ReferentialAction; 5] {
    [
        ReferentialAction::NoAction,
        ReferentialAction::Restrict,
        ReferentialAction::Cascade,
        ReferentialAction::SetNull,
        ReferentialAction::SetDefault,
    ]
}

/// One column row as the form shows it: a stable identity (so a rename is
/// tracked by which row it is, not by matching names) plus the fields the
/// user can change. Name and default are kept as `InputState` directly —
/// there is nothing to mirror into a plain field, since the box already is
/// the value.
struct EditableColumn {
    id: usize,
    /// `None` for a column added by hand.
    original: Option<ColumnDef>,
    name: Entity<InputState>,
    type_name: String,
    nullable: bool,
    default: Entity<InputState>,
    /// Only ever set on a column that came from the server; a dropped new
    /// column is removed from the list outright instead.
    dropped: bool,
}

impl EditableColumn {
    fn snapshot(&self, cx: &App) -> ColumnEdit {
        let default = self.default.read(cx).value().trim().to_string();
        ColumnEdit {
            original: self.original.clone(),
            name: self.name.read(cx).value().trim().to_string(),
            type_name: self.type_name.clone(),
            nullable: self.nullable,
            default: (!default.is_empty()).then_some(default),
            dropped: self.dropped,
        }
    }
}

/// One index row: a whole index added or a whole one dropped, never
/// modified in place — see `sql::IndexEdit`, which this becomes for the
/// generator. Only a new row's column set is editable; an existing index's
/// composition is shown as loaded.
struct EditableIndex {
    id: usize,
    /// `None` for an index added by hand.
    original: Option<IndexDef>,
    name: Entity<InputState>,
    /// In the order picked — order matters for a composite index.
    columns: Vec<String>,
    unique: bool,
    primary_key: bool,
    /// Only ever set on an index that came from the server; a dropped new
    /// index is removed from the list outright instead.
    dropped: bool,
}

impl EditableIndex {
    fn snapshot(&self, cx: &App) -> IndexEdit {
        IndexEdit {
            original: self.original.clone(),
            name: self.name.read(cx).value().trim().to_string(),
            columns: self.columns.clone(),
            unique: self.unique,
            primary_key: self.primary_key,
            dropped: self.dropped,
        }
    }
}

/// One foreign key row: a whole relationship added or a whole one dropped,
/// never modified in place — see `sql::ForeignKeyEdit`. Only a new row's
/// columns and reference are editable; an existing one is shown as loaded.
struct EditableForeignKey {
    id: usize,
    /// `None` for a foreign key added by hand.
    original: Option<ForeignKeyDef>,
    name: Entity<InputState>,
    /// Local columns, in the order picked — order matches `referenced_columns`.
    columns: Vec<String>,
    referenced_table: Option<DatabaseObject>,
    /// Columns of `referenced_table`, read once it is picked; empty until
    /// then or while that read is in flight.
    referenced_table_columns: Vec<String>,
    /// In the order picked, matching `columns` one for one.
    referenced_columns: Vec<String>,
    on_delete: ReferentialAction,
    on_update: ReferentialAction,
    /// Only ever set on a foreign key that came from the server; a dropped
    /// new one is removed from the list outright instead.
    dropped: bool,
}

impl EditableForeignKey {
    fn snapshot(&self, cx: &App) -> ForeignKeyEdit {
        ForeignKeyEdit {
            original: self.original.clone(),
            name: self.name.read(cx).value().trim().to_string(),
            columns: self.columns.clone(),
            referenced_schema: self
                .referenced_table
                .as_ref()
                .and_then(|t| t.schema.clone()),
            referenced_table: self
                .referenced_table
                .as_ref()
                .map(|t| t.name.clone())
                .unwrap_or_default(),
            referenced_columns: self.referenced_columns.clone(),
            on_delete: self.on_delete,
            on_update: self.on_update,
            dropped: self.dropped,
        }
    }
}

pub struct SchemaView {
    connection: Arc<Connection>,
    object: DatabaseObject,
    /// As loaded, kept for reference: it is what a foreign key's own row
    /// picks its referenced columns from once the target is this table.
    schema: Option<TableSchema>,
    /// As SQLite stores it, read alongside `schema`: what a rebuild has to put
    /// back on top of the table (its indexes, triggers, and views). `None` on
    /// the other engines, and for a view.
    rebuild_source: Option<RebuildSource>,
    /// The Columns section's own editable state, seeded from `schema` each
    /// time it (re)loads.
    columns: Vec<EditableColumn>,
    /// The Indexes section's own editable state, seeded the same way.
    indexes: Vec<EditableIndex>,
    /// The Foreign Keys section's own editable state, seeded the same way.
    foreign_keys: Vec<EditableForeignKey>,
    /// Other tables a new foreign key can reference, read once alongside the
    /// structure rather than reusing the sidebar's list, so this view stays
    /// self-contained.
    tables: Vec<DatabaseObject>,
    next_id: usize,
    loading: bool,
    applying: bool,
    error: Option<String>,
    notice: Option<String>,
    /// The generated statements, shown in `preview` and waiting for "Apply?"
    /// — asked unconditionally, regardless of `SafetyMode`. `None` when
    /// nothing is waiting.
    pending: Option<Change>,
    preview: Entity<TextareaState>,
}

impl SchemaView {
    pub fn new(
        connection: Arc<Connection>,
        object: DatabaseObject,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        let preview = cx.new(|cx| TextareaState::new(window, cx));

        let mut view = Self {
            connection,
            object,
            schema: None,
            rebuild_source: None,
            columns: Vec::new(),
            indexes: Vec::new(),
            foreign_keys: Vec::new(),
            tables: Vec::new(),
            next_id: 0,
            loading: false,
            applying: false,
            error: None,
            notice: None,
            pending: None,
            preview,
        };
        view.reload(cx);
        view
    }

    pub fn object(&self) -> &DatabaseObject {
        &self.object
    }

    /// Unapplied edits, or a preview waiting on "Apply?", would be lost by
    /// closing the tab.
    pub(crate) fn is_dirty(&self, cx: &App) -> bool {
        self.pending.is_some() || self.has_changes(cx)
    }

    /// Point the view at a new connection, as when the database is switched.
    pub(crate) fn set_connection(&mut self, connection: Arc<Connection>, cx: &mut Context<Self>) {
        self.connection = connection;
        self.schema = None;
        self.pending = None;
        self.notice = None;
        self.reload(cx);
    }

    /// Read the structure fresh from the server.
    ///
    /// Leaves `notice` alone: `apply`'s "Changes applied" is set right before
    /// this runs and is meant to survive it, since the reload it is
    /// reporting on is this same one.
    fn reload(&mut self, cx: &mut Context<Self>) {
        self.loading = true;
        self.error = None;
        self.pending = None;
        self.rebuild_source = None;
        cx.notify();

        let connection = self.connection.clone();
        let object = self.object.clone();
        // Only SQLite needs the stored text a rebuild works from, and only a
        // table has one.
        let wants_source =
            connection.config.engine == Engine::Sqlite && object.kind == ObjectKind::Table;
        let task = runtime::spawn(async move {
            let schema = connection.table_schema(&object).await;
            let source = if wants_source {
                connection.rebuild_source(&object).await.ok()
            } else {
                None
            };
            (schema, connection.objects().await, source)
        });

        cx.spawn(async move |this, cx| {
            let result = task.await;
            this.update_in(cx, |this, window, cx| {
                this.loading = false;
                match result {
                    Ok((Ok(schema), objects, source)) => {
                        let mut columns = Vec::with_capacity(schema.columns.len());
                        for column in schema.columns.clone() {
                            columns.push(this.new_row(Some(column), window, cx));
                        }
                        this.columns = columns;

                        let mut indexes = Vec::with_capacity(schema.indexes.len());
                        for index in schema.indexes.clone() {
                            indexes.push(this.new_index_row(Some(index), window, cx));
                        }
                        this.indexes = indexes;

                        let mut foreign_keys = Vec::with_capacity(schema.foreign_keys.len());
                        for key in schema.foreign_keys.clone() {
                            foreign_keys.push(this.new_foreign_key_row(Some(key), window, cx));
                        }
                        this.foreign_keys = foreign_keys;

                        // Other tables a new foreign key could reference; a
                        // view cannot be one, and referencing this table
                        // itself is left out to keep the picker simple.
                        this.tables = objects
                            .unwrap_or_default()
                            .into_iter()
                            .filter(|candidate| {
                                candidate.kind == ObjectKind::Table && candidate != &this.object
                            })
                            .collect();

                        this.schema = Some(schema);
                        this.rebuild_source = source;
                    }
                    Ok((Err(error), _, _)) => this.error = Some(format!("{error:#}")),
                    Err(_) => {
                        this.error = Some("reading the table's structure was cancelled".into())
                    }
                }
                cx.notify();
            })
            .ok();
        })
        .detach();
    }

    /// Build a row's inputs, seeded from `original` when there is one.
    fn new_row(
        &mut self,
        original: Option<ColumnDef>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> EditableColumn {
        let id = self.next_id;
        self.next_id += 1;

        let name = cx.new(|cx| {
            InputState::new(window, cx).default_value(
                original
                    .as_ref()
                    .map(|c| c.name.as_str())
                    .unwrap_or_default(),
            )
        });
        cx.subscribe_in(&name, window, Self::on_field_event)
            .detach();

        let default = cx.new(|cx| {
            InputState::new(window, cx)
                .placeholder("Default")
                .default_value(
                    original
                        .as_ref()
                        .and_then(|c| c.default.as_deref())
                        .unwrap_or_default(),
                )
        });
        cx.subscribe_in(&default, window, Self::on_field_event)
            .detach();

        let type_name = original
            .as_ref()
            .map(|c| c.type_name.clone())
            .unwrap_or_else(|| sql::common_types(self.connection.config.engine)[0].to_string());
        let nullable = original.as_ref().map(|c| c.nullable).unwrap_or(true);

        EditableColumn {
            id,
            original,
            name,
            type_name,
            nullable,
            default,
            dropped: false,
        }
    }

    /// A row's text box changed; the view has no mirrored copy of it to
    /// update, but the preview and the change summary need to redraw.
    fn on_field_event(
        &mut self,
        _input: &Entity<InputState>,
        event: &InputEvent,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if matches!(event, InputEvent::Change) {
            self.pending = None;
            cx.notify();
        }
    }

    /// Whether this connection and object allow changing columns at all.
    fn is_editable(&self) -> bool {
        !self.connection.config.safety.is_read_only() && self.object.kind == ObjectKind::Table
    }

    /// Why editing is off, when it is, for the footer.
    fn read_only_reason(&self) -> Option<&'static str> {
        if self.connection.config.safety.is_read_only() {
            return Some("this connection is read-only");
        }
        if self.object.kind == ObjectKind::View {
            return Some("a view has no columns of its own to change");
        }
        None
    }

    fn edits(&self, cx: &App) -> Vec<ColumnEdit> {
        self.columns
            .iter()
            .map(|column| column.snapshot(cx))
            .collect()
    }

    fn has_changes(&self, cx: &App) -> bool {
        let columns_changed = self.edits(cx).iter().any(|edit| {
            edit.changed() || edit.dropped || (edit.is_new() && !edit.name.trim().is_empty())
        });
        columns_changed || self.has_index_changes(cx) || self.has_foreign_key_changes(cx)
    }

    /// Generate the statements for what has changed and show them, or say
    /// why not.
    fn preview(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        if !self.is_editable() {
            return;
        }
        self.notice = None;

        let engine = self.connection.config.engine;
        let change = rebuild::plan(
            engine,
            &self.object,
            &self.edits(cx),
            &self.index_edits(cx),
            &self.foreign_key_edits(cx),
            self.rebuild_source.as_ref(),
        );

        match change {
            Ok(change) if change.is_empty() => {
                self.error = Some("nothing has changed".into());
                self.pending = None;
            }
            Ok(change) => {
                self.error = None;
                let text = change.statements().join(";\n") + ";";
                self.preview
                    .update(cx, |input, cx| input.set_value(text, window, cx));
                self.pending = Some(change);
            }
            Err(message) => {
                self.error = Some(message);
                self.pending = None;
            }
        }
        cx.notify();
    }

    /// Leave the preview without running it; the edits stay staged to try
    /// again.
    fn cancel_preview(&mut self, cx: &mut Context<Self>) {
        self.pending = None;
        cx.notify();
    }

    /// Run the previewed change, then reload from the server — a rename or a
    /// retype changes identity, so nothing here is patched in place.
    fn apply(&mut self, cx: &mut Context<Self>) {
        let Some(change) = self.pending.take() else {
            return;
        };

        self.applying = true;
        self.error = None;
        self.notice = None;
        cx.notify();

        let connection = self.connection.clone();
        let task = runtime::spawn(async move {
            match change {
                Change::Statements(statements) => {
                    connection.execute_script(&statements).await?;
                }
                // A rebuild is all or nothing: one connection, one
                // transaction, foreign keys checked before it commits.
                Change::Rebuild(statements) => connection
                    .rebuild_table(statements)
                    .await
                    .context("the table rebuild failed")?,
            }
            Ok::<(), anyhow::Error>(())
        });

        cx.spawn(async move |this, cx| {
            let result = task.await;
            this.update(cx, |this, cx| {
                this.applying = false;
                match result {
                    Ok(Ok(())) => {
                        this.notice = Some("Changes applied".into());
                        this.reload(cx);
                    }
                    Ok(Err(error)) => this.error = Some(format!("{error:#}")),
                    Err(_) => this.error = Some("applying the changes was cancelled".into()),
                }
                cx.notify();
            })
            .ok();
        })
        .detach();
    }

    fn render_header(&self, cx: &Context<Self>) -> impl IntoElement {
        let kind = match self.object.kind {
            ObjectKind::Table => "table",
            ObjectKind::View => "view",
        };

        h_flex()
            .gap_2()
            .child(div().text_sm().child(self.object.label()))
            .child(
                div()
                    .text_xs()
                    .text_color(cx.theme().muted_foreground)
                    .child(kind),
            )
    }

    fn render_section_heading(&self, title: &'static str, cx: &Context<Self>) -> impl IntoElement {
        div()
            .px_1()
            .text_xs()
            .text_color(cx.theme().muted_foreground)
            .child(title)
    }

    fn render_confirm(&self, cx: &mut Context<Self>) -> impl IntoElement {
        h_flex()
            .flex_none()
            .px_3()
            .py_2()
            .gap_2()
            .justify_between()
            .border_t_1()
            .border_color(cx.theme().border)
            .bg(cx.theme().secondary)
            .child(
                div()
                    .text_xs()
                    .text_color(cx.theme().warning)
                    .child("Apply these changes?"),
            )
            .child(
                h_flex()
                    .gap_2()
                    .child(
                        Button::new("cancel-schema-apply")
                            .ghost()
                            .xsmall()
                            .label("Cancel")
                            .on_click(cx.listener(|this, _, _window, cx| this.cancel_preview(cx))),
                    )
                    .child(
                        Button::new("confirm-schema-apply")
                            .primary()
                            .xsmall()
                            .label("Apply")
                            .on_click(cx.listener(|this, _, _window, cx| this.apply(cx))),
                    ),
            )
    }

    fn render_footer(&self, cx: &mut Context<Self>) -> impl IntoElement {
        let message = match (&self.error, self.loading, self.applying, &self.notice) {
            (Some(error), _, _, _) => (format!("Error: {error}"), cx.theme().danger),
            (None, _, true, _) => ("Applying…".to_string(), cx.theme().muted_foreground),
            (None, true, _, _) => ("Loading…".to_string(), cx.theme().muted_foreground),
            (None, false, false, Some(notice)) => (notice.clone(), cx.theme().muted_foreground),
            (None, false, false, None) => (String::new(), cx.theme().muted_foreground),
        };

        h_flex()
            .flex_none()
            .px_3()
            .py_1()
            .gap_3()
            .justify_between()
            .border_t_1()
            .border_color(cx.theme().border)
            .bg(cx.theme().status_bar)
            .child(div().text_xs().text_color(message.1).child(message.0))
            .child(
                h_flex()
                    .gap_2()
                    .when_some(self.read_only_reason(), |this, reason| {
                        this.child(
                            div()
                                .text_xs()
                                .text_color(cx.theme().muted_foreground)
                                .child(format!("Read-only: {reason}")),
                        )
                    })
                    .when(self.is_editable() && self.pending.is_none(), |this| {
                        this.child(
                            Button::new("preview-schema-changes")
                                .primary()
                                .xsmall()
                                .label("Preview changes")
                                .disabled(self.applying || !self.has_changes(cx))
                                .on_click(cx.listener(|this, _, window, cx| {
                                    this.preview(window, cx);
                                })),
                        )
                    }),
            )
    }
}

impl Render for SchemaView {
    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        v_flex()
            .id("schema-view")
            .test_support()
            .size_full()
            .child(
                div()
                    .id("schema-sections")
                    .flex_1()
                    .min_h_0()
                    .overflow_y_scrollbar()
                    .child(
                        v_flex()
                            .p_3()
                            .gap_4()
                            .child(self.render_header(cx))
                            .child(self.render_columns(cx))
                            .child(self.render_indexes(cx))
                            .child(self.render_foreign_keys(cx)),
                    ),
            )
            .when(self.pending.is_some(), |this| {
                this.when(
                    self.pending.as_ref().is_some_and(Change::is_rebuild),
                    |this| {
                        this.child(
                            div()
                                .flex_none()
                                .px_3()
                                .pt_2()
                                .text_xs()
                                .text_color(cx.theme().muted_foreground)
                                .child(
                                    "This rebuilds the table: its rows are copied into a new one \
                                     and swapped in, in a single transaction, with the foreign keys \
                                     checked before it commits.",
                                ),
                        )
                    },
                )
                .child(
                    Textarea::new(&self.preview)
                        .flex_none()
                        .mx_3()
                        .mb_2()
                        .h(px(120.))
                        .readonly(true)
                        .font_family(crate::settings::grid_font(cx)),
                )
                .child(self.render_confirm(cx))
            })
            .child(self.render_footer(cx))
    }
}