From cd4096b2ff0791501e4f869216a3f5c5ee65f584 Mon Sep 17 00:00:00 2001 From: Robert Perce Date: Mon, 19 Jan 2026 21:38:23 -0600 Subject: [PATCH] refactor: demo.db migration structure --- migrations/demo.db/0001_contact-tables.sql | 12 ------------ .../demo.db/0002_journal-entry-tables.sql | 13 ------------- migrations/demo.db/0004_user-settings.sql | 6 ------ migrations/demo.db/0005_address-tables.sql | 6 ------ migrations/demo.db/0006_contact-text-body.sql | 3 --- .../{demo.db/0003_demo-data.sql => demo.sql} | 0 migrations/each_user/0007_contact-groups.sql | 2 +- src/db.rs | 12 +++++++----- src/web/contact.rs | 18 ++++++++++++++++++ 9 files changed, 26 insertions(+), 46 deletions(-) delete mode 100644 migrations/demo.db/0001_contact-tables.sql delete mode 100644 migrations/demo.db/0002_journal-entry-tables.sql delete mode 100644 migrations/demo.db/0004_user-settings.sql delete mode 100644 migrations/demo.db/0005_address-tables.sql delete mode 100644 migrations/demo.db/0006_contact-text-body.sql rename migrations/{demo.db/0003_demo-data.sql => demo.sql} (100%) diff --git a/migrations/demo.db/0001_contact-tables.sql b/migrations/demo.db/0001_contact-tables.sql deleted file mode 100644 index f88f0cd..0000000 --- a/migrations/demo.db/0001_contact-tables.sql +++ /dev/null @@ -1,12 +0,0 @@ -create table if not exists contacts ( - id integer primary key autoincrement, - birthday text, - manually_freshened_at date -- text, iso8601 date -); - -create table if not exists names ( - id integer primary key, - contact_id integer not null references contacts(id) on delete cascade, - sort integer not null, - name text not null -); diff --git a/migrations/demo.db/0002_journal-entry-tables.sql b/migrations/demo.db/0002_journal-entry-tables.sql deleted file mode 100644 index 3b66cb7..0000000 --- a/migrations/demo.db/0002_journal-entry-tables.sql +++ /dev/null @@ -1,13 +0,0 @@ -create table if not exists journal_entries ( - id integer primary key autoincrement, - value text not null, - date text not null -); - -create table if not exists contact_mentions ( - entry_id integer not null references journal_entries(id) on delete cascade, - contact_id integer not null references contacts(id) on delete cascade, - input_text text not null, - byte_range_start integer not null, - byte_range_end integer not null -); diff --git a/migrations/demo.db/0004_user-settings.sql b/migrations/demo.db/0004_user-settings.sql deleted file mode 100644 index 035a34f..0000000 --- a/migrations/demo.db/0004_user-settings.sql +++ /dev/null @@ -1,6 +0,0 @@ -create table if not exists settings ( - id integer primary key, - ics_path text -); - -insert into settings (id) values (1) on conflict (id) do nothing; diff --git a/migrations/demo.db/0005_address-tables.sql b/migrations/demo.db/0005_address-tables.sql deleted file mode 100644 index 138382a..0000000 --- a/migrations/demo.db/0005_address-tables.sql +++ /dev/null @@ -1,6 +0,0 @@ -create table if not exists addresses ( - id integer primary key, - contact_id integer not null references contacts(id) on delete cascade, - label text, - value text not null -); diff --git a/migrations/demo.db/0006_contact-text-body.sql b/migrations/demo.db/0006_contact-text-body.sql deleted file mode 100644 index 65eb200..0000000 --- a/migrations/demo.db/0006_contact-text-body.sql +++ /dev/null @@ -1,3 +0,0 @@ -alter table contacts - add column - text_body text; diff --git a/migrations/demo.db/0003_demo-data.sql b/migrations/demo.sql similarity index 100% rename from migrations/demo.db/0003_demo-data.sql rename to migrations/demo.sql diff --git a/migrations/each_user/0007_contact-groups.sql b/migrations/each_user/0007_contact-groups.sql index f695b85..ca70062 100644 --- a/migrations/each_user/0007_contact-groups.sql +++ b/migrations/each_user/0007_contact-groups.sql @@ -1,4 +1,4 @@ create table if not exists groups ( - contact_id number not null references contacts(id), + contact_id integer not null references contacts(id) on delete cascade, name text not null ); diff --git a/src/db.rs b/src/db.rs index 8b3dd18..d844b6b 100644 --- a/src/db.rs +++ b/src/db.rs @@ -23,12 +23,14 @@ impl Database { let pool = SqlitePoolOptions::new().connect_with(db_options).await?; - let migrator = if user.username == "demo" { - sqlx::migrate!("./migrations/demo.db/") - } else { - sqlx::migrate!("./migrations/each_user/") + sqlx::migrate!("./migrations/each_user/") + .run(&pool) + .await?; + if user.username == "demo" { + sqlx::query_file!("./migrations/demo.sql") + .execute(&pool) + .await?; }; - migrator.run(&pool).await?; Ok(Self { pool }) } diff --git a/src/web/contact.rs b/src/web/contact.rs index 889a5cb..c391ca7 100644 --- a/src/web/contact.rs +++ b/src/web/contact.rs @@ -227,6 +227,16 @@ mod get { .clone() .map_or("".to_string(), |m| m.to_rfc3339()); + let groups: Vec = sqlx::query_as!( + Group, + "select * from groups where contact_id = $1", + contact_id) + .fetch_all(pool) + .await? + .into_iter() + .map(|group| group.name) + .collect(); + let text_body: String = sqlx::query!("select text_body from contacts where id = $1", contact_id) .fetch_one(pool) @@ -287,6 +297,14 @@ mod get { } input type="button" value="Add" x-on:click="addresses.push({ label: new_label, value: new_address }); new_label = ''; new_address = ''"; } + label { "groups" } + #groups x-data=(json!({ "groups": groups, "new_group": "" })) { + template x-for="(group, index) in groups" x-bind:key="index" { + input name="group" x-model="group" placeholder="group name"; + } + input name="group" x-model="new_group" placeholder="group name"; + input type="button" value="Add" x-on:click="groups.push(new_group); new_group = ''"; + } } div #text_body { div { "Free text (supports markdown)" }