feat(mixtape): schema migrations for collections and items
Two new tables (mixtape_collections, mixtape_collection_items) with CASCADE on delete and indexes for the two common query patterns. One additive column on session_queue_state for queue-source tracking. Idempotent (CREATE TABLE IF NOT EXISTS, ALTER only when missing), safe on every app start. The ALTER is skipped gracefully if session_queue_state hasn't been created yet. Adds LibraryDatabase::with_connection() helper (qbz-library) so src-tauri can run external DDL on the shared Connection without exposing it as a field. Migration is wired in session_lifecycle immediately after library.init_at() in both the online and offline activation paths. cc
This commit is contained in:
7 files changed
+185
No files matched your search
@@ -741,6 +741,19 @@ impl LibraryDatabase {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Provide raw connection access for external schema migrations.
|
||||||
|
///
|
||||||
|
/// This is intentionally narrow: callers receive a shared reference so
|
||||||
|
/// they can run DDL (CREATE TABLE, ALTER TABLE) but cannot move the
|
||||||
|
/// connection out or replace it. Use sparingly — prefer adding methods
|
||||||
|
/// to LibraryDatabase directly for DML queries.
|
||||||
|
pub fn with_connection<F, R>(&self, f: F) -> R
|
||||||
|
where
|
||||||
|
F: FnOnce(&Connection) -> R,
|
||||||
|
{
|
||||||
|
f(&self.conn)
|
||||||
|
}
|
||||||
|
|
||||||
// === Folder Management ===
|
// === Folder Management ===
|
||||||
|
|
||||||
/// Add a folder to the library with optional network info
|
/// Add a folder to the library with optional network info
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ pub mod logging;
|
|||||||
pub mod lyrics;
|
pub mod lyrics;
|
||||||
pub mod media_controls;
|
pub mod media_controls;
|
||||||
pub mod migration;
|
pub mod migration;
|
||||||
|
pub mod mixtape;
|
||||||
pub mod musicbrainz;
|
pub mod musicbrainz;
|
||||||
pub mod network;
|
pub mod network;
|
||||||
pub mod offline;
|
pub mod offline;
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
//! Populated in Task 3.1.
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
//! Mixtapes & Collections subsystem — schema, repository, enqueue.
|
||||||
|
//!
|
||||||
|
//! See spec: qbz-nix-docs/specs/2026-04-18-mixtapes-collections-design.md
|
||||||
|
|
||||||
|
pub mod schema;
|
||||||
|
pub mod repo;
|
||||||
|
pub mod enqueue;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
//! Populated in Task 2.1.
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
//! SQLite schema migrations for mixtape_collections + mixtape_collection_items,
|
||||||
|
//! plus one additive column on the existing session_queue_state table.
|
||||||
|
//!
|
||||||
|
//! Follows qbz-library's inline-ALTER pattern: additive-only, idempotent,
|
||||||
|
//! safe to run on every app start.
|
||||||
|
|
||||||
|
use rusqlite::{Connection, Result};
|
||||||
|
|
||||||
|
pub fn run_mixtape_migrations(conn: &Connection) -> Result<()> {
|
||||||
|
log::info!("[Mixtape/schema] running migrations");
|
||||||
|
|
||||||
|
conn.execute_batch(
|
||||||
|
r#"
|
||||||
|
CREATE TABLE IF NOT EXISTS mixtape_collections (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
kind TEXT NOT NULL,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
description TEXT,
|
||||||
|
source_type TEXT NOT NULL DEFAULT 'manual',
|
||||||
|
source_ref TEXT,
|
||||||
|
play_mode TEXT NOT NULL DEFAULT 'in_order',
|
||||||
|
custom_artwork_path TEXT,
|
||||||
|
position INTEGER NOT NULL DEFAULT 0,
|
||||||
|
hidden INTEGER NOT NULL DEFAULT 0,
|
||||||
|
last_played_at INTEGER,
|
||||||
|
play_count INTEGER NOT NULL DEFAULT 0,
|
||||||
|
last_synced_at INTEGER,
|
||||||
|
created_at INTEGER NOT NULL,
|
||||||
|
updated_at INTEGER NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS mixtape_collection_items (
|
||||||
|
collection_id TEXT NOT NULL REFERENCES mixtape_collections(id) ON DELETE CASCADE,
|
||||||
|
position INTEGER NOT NULL,
|
||||||
|
item_type TEXT NOT NULL,
|
||||||
|
source TEXT NOT NULL,
|
||||||
|
source_item_id TEXT NOT NULL,
|
||||||
|
title TEXT NOT NULL,
|
||||||
|
subtitle TEXT,
|
||||||
|
artwork_url TEXT,
|
||||||
|
year INTEGER,
|
||||||
|
track_count INTEGER,
|
||||||
|
added_at INTEGER NOT NULL,
|
||||||
|
PRIMARY KEY (collection_id, position)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_mixtape_items_collection
|
||||||
|
ON mixtape_collection_items(collection_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_mixtapes_kind_position
|
||||||
|
ON mixtape_collections(kind, position);
|
||||||
|
"#,
|
||||||
|
)?;
|
||||||
|
log::info!("[Mixtape/schema] mixtape_collections + items ensured");
|
||||||
|
|
||||||
|
// Additive column on session_queue_state (if that table exists in this DB).
|
||||||
|
// Some setups may not have session_queue_state yet; in that case we skip —
|
||||||
|
// the Mixtape enqueue context tracking gets persisted once session_queue_state
|
||||||
|
// is created by its own migration.
|
||||||
|
let session_table_exists: bool = conn
|
||||||
|
.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='session_queue_state'")?
|
||||||
|
.exists([])?;
|
||||||
|
|
||||||
|
if session_table_exists {
|
||||||
|
let has_col: bool = conn
|
||||||
|
.prepare(
|
||||||
|
"SELECT COUNT(*) FROM pragma_table_info('session_queue_state') \
|
||||||
|
WHERE name = 'source_collection_id'",
|
||||||
|
)?
|
||||||
|
.query_row([], |r| r.get::<_, i64>(0))?
|
||||||
|
> 0;
|
||||||
|
if !has_col {
|
||||||
|
conn.execute(
|
||||||
|
"ALTER TABLE session_queue_state ADD COLUMN source_collection_id TEXT",
|
||||||
|
[],
|
||||||
|
)?;
|
||||||
|
log::info!(
|
||||||
|
"[Mixtape/schema] added session_queue_state.source_collection_id"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log::info!(
|
||||||
|
"[Mixtape/schema] session_queue_state not present yet; skipping ALTER"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn migrations_are_idempotent() {
|
||||||
|
let conn = Connection::open_in_memory().unwrap();
|
||||||
|
run_mixtape_migrations(&conn).unwrap();
|
||||||
|
run_mixtape_migrations(&conn).unwrap();
|
||||||
|
|
||||||
|
// Both tables exist
|
||||||
|
let collections: i64 = conn.query_row(
|
||||||
|
"SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='mixtape_collections'",
|
||||||
|
[], |r| r.get(0),
|
||||||
|
).unwrap();
|
||||||
|
assert_eq!(collections, 1);
|
||||||
|
let items: i64 = conn.query_row(
|
||||||
|
"SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='mixtape_collection_items'",
|
||||||
|
[], |r| r.get(0),
|
||||||
|
).unwrap();
|
||||||
|
assert_eq!(items, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn adds_column_when_session_table_exists() {
|
||||||
|
let conn = Connection::open_in_memory().unwrap();
|
||||||
|
conn.execute(
|
||||||
|
"CREATE TABLE session_queue_state (user_id INTEGER, extra TEXT)",
|
||||||
|
[],
|
||||||
|
).unwrap();
|
||||||
|
run_mixtape_migrations(&conn).unwrap();
|
||||||
|
|
||||||
|
let has_col: i64 = conn.query_row(
|
||||||
|
"SELECT COUNT(*) FROM pragma_table_info('session_queue_state') WHERE name = 'source_collection_id'",
|
||||||
|
[], |r| r.get(0),
|
||||||
|
).unwrap();
|
||||||
|
assert_eq!(has_col, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tolerates_missing_session_table() {
|
||||||
|
let conn = Connection::open_in_memory().unwrap();
|
||||||
|
// Don't create session_queue_state
|
||||||
|
run_mixtape_migrations(&conn).unwrap(); // should not panic
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -119,6 +119,21 @@ pub async fn activate_session(app: &tauri::AppHandle, user_id: u64) -> Result<()
|
|||||||
allowed_origins.init_at(&data_dir)?;
|
allowed_origins.init_at(&data_dir)?;
|
||||||
updates.init_at(&data_dir)?;
|
updates.init_at(&data_dir)?;
|
||||||
library.init_at(&data_dir).await?;
|
library.init_at(&data_dir).await?;
|
||||||
|
|
||||||
|
// Run mixtape schema migrations on the library DB immediately after it opens.
|
||||||
|
// We hold the async Mutex briefly to get a raw-connection callback; the lock
|
||||||
|
// is dropped before returning so other commands are not blocked.
|
||||||
|
{
|
||||||
|
let db_guard = library.db.lock().await;
|
||||||
|
if let Some(db) = db_guard.as_ref() {
|
||||||
|
db.with_connection(|conn| {
|
||||||
|
if let Err(e) = crate::mixtape::schema::run_mixtape_migrations(conn) {
|
||||||
|
log::error!("[SessionLifecycle] Mixtape schema migration failed: {}", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
reco.init_at(&data_dir).await?;
|
reco.init_at(&data_dir).await?;
|
||||||
api_cache.init_at(&data_dir).await?;
|
api_cache.init_at(&data_dir).await?;
|
||||||
artist_vectors.init_at(&data_dir).await?;
|
artist_vectors.init_at(&data_dir).await?;
|
||||||
@@ -380,6 +395,19 @@ pub async fn activate_offline_session(app: &tauri::AppHandle) -> Result<(), Stri
|
|||||||
app.state::<crate::config::favorites_preferences::FavoritesPreferencesState>();
|
app.state::<crate::config::favorites_preferences::FavoritesPreferencesState>();
|
||||||
|
|
||||||
library.init_at(&data_dir).await?;
|
library.init_at(&data_dir).await?;
|
||||||
|
|
||||||
|
// Run mixtape schema migrations on the library DB (offline activation path).
|
||||||
|
{
|
||||||
|
let db_guard = library.db.lock().await;
|
||||||
|
if let Some(db) = db_guard.as_ref() {
|
||||||
|
db.with_connection(|conn| {
|
||||||
|
if let Err(e) = crate::mixtape::schema::run_mixtape_migrations(conn) {
|
||||||
|
log::error!("[SessionLifecycle] Mixtape schema migration failed (offline): {}", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
offline.init_at(&data_dir)?;
|
offline.init_at(&data_dir)?;
|
||||||
offline_cache.init_at(&cache_dir).await?;
|
offline_cache.init_at(&cache_dir).await?;
|
||||||
offline_cache.init_library_connection(&data_dir).await?;
|
offline_cache.init_library_connection(&data_dir).await?;
|
||||||
|
|||||||
Reference in new issue
Block a user