Skip to content
46 changes: 46 additions & 0 deletions core/crates/storage/migrations/20261108000000_favourites.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
-- Folders and keywords on saved queries, and the tables and databases a
-- user marks as favourites (core ADR 0017).

CREATE TABLE saved_query_folders (
id INTEGER PRIMARY KEY AUTOINCREMENT,
connection_id TEXT NOT NULL,
name TEXT NOT NULL,
-- A deleted folder takes the folders inside it.
parent_id INTEGER REFERENCES saved_query_folders (id) ON DELETE CASCADE,
sort_order INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);

-- No two siblings share a name in any case. A top-level folder has no
-- parent, which a unique index would treat as always different.
CREATE UNIQUE INDEX saved_query_folders_name_idx
ON saved_query_folders (connection_id, COALESCE(parent_id, 0), name COLLATE NOCASE);

ALTER TABLE saved_queries ADD COLUMN keyword TEXT;
-- A deleted folder leaves its queries at the top.
ALTER TABLE saved_queries ADD COLUMN folder_id INTEGER REFERENCES saved_query_folders (id) ON DELETE SET NULL;
ALTER TABLE saved_queries ADD COLUMN sort_order INTEGER NOT NULL DEFAULT 0;

CREATE UNIQUE INDEX saved_queries_keyword_idx
ON saved_queries (connection_id, keyword COLLATE NOCASE) WHERE keyword IS NOT NULL;

-- A table is named by its database and schema where the engine has them,
-- and by the empty string where it does not, so the key is never NULL.
CREATE TABLE favourite_tables (
connection_id TEXT NOT NULL,
database TEXT NOT NULL DEFAULT '',
schema TEXT NOT NULL DEFAULT '',
name TEXT NOT NULL,
created_at INTEGER NOT NULL,
PRIMARY KEY (connection_id, database, schema, name)
);

CREATE TABLE favourite_databases (
connection_id TEXT NOT NULL,
database TEXT NOT NULL,
environment TEXT NOT NULL DEFAULT 'unassigned'
CHECK (environment IN ('development', 'testing', 'production', 'unassigned')),
created_at INTEGER NOT NULL,
PRIMARY KEY (connection_id, database)
);
23 changes: 23 additions & 0 deletions core/crates/storage/migrations/20261109000000_favourites_order.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
WITH positions AS (
SELECT id, ROW_NUMBER() OVER (
PARTITION BY connection_id, folder_id
ORDER BY sort_order, name COLLATE NOCASE, id
) - 1 AS position
FROM saved_queries
)
UPDATE saved_queries SET sort_order = positions.position
FROM positions WHERE saved_queries.id = positions.id;

WITH positions AS (
SELECT id, ROW_NUMBER() OVER (
PARTITION BY connection_id, parent_id
ORDER BY sort_order, name COLLATE NOCASE, id
) - 1 AS position
FROM saved_query_folders
)
UPDATE saved_query_folders SET sort_order = positions.position
FROM positions WHERE saved_query_folders.id = positions.id;

CREATE INDEX saved_queries_folder_order_idx ON saved_queries (connection_id, folder_id, sort_order, id);
CREATE INDEX saved_query_folders_parent_idx ON saved_query_folders (parent_id);
CREATE INDEX saved_query_folders_order_idx ON saved_query_folders (connection_id, parent_id, sort_order, id);
20 changes: 20 additions & 0 deletions core/crates/storage/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ pub enum StorageError {
#[error("another tag has that name")]
TagNameTaken,

#[error("another saved query of the connection has that keyword")]
KeywordTaken,

#[error("a keyword is one word of at most {max} characters, with no spaces")]
KeywordInvalid { max: usize },

#[error("the folder name contains a control character")]
FolderNameInvalid,

#[error("another folder in the same place has that name")]
FolderNameTaken,

#[error("the folder {id} cannot move inside itself")]
FolderCycle { id: i64 },

#[error("export encoding failed: {0}")]
Encode(#[from] tablepro_core::export::RenderError),

Expand Down Expand Up @@ -104,6 +119,11 @@ impl StorageError {
Self::EmptyName => "storage.empty_name",
Self::NameTaken => "storage.name_taken",
Self::TagNameTaken => "storage.tag_name_taken",
Self::KeywordTaken => "storage.keyword_taken",
Self::KeywordInvalid { .. } => "storage.keyword_invalid",
Self::FolderNameInvalid => "storage.folder_name_invalid",
Self::FolderNameTaken => "storage.folder_name_taken",
Self::FolderCycle { .. } => "storage.folder_cycle",
Self::Encode(..) => "storage.encode",
Self::HistoryNewerThanApp { .. } => "storage.history_newer_than_app",
Self::HistoryMigration(..) => "storage.history_migration",
Expand Down
Loading
Loading