Implement non-destructive Remove & Archive lifecycle system across assets, locations, tags, and shortlinks

This commit is contained in:
lucio committed 2026-05-21 18:31:24 -07:00
1 parent 3391257f6c
commit 960c375557
20 files changed
+1751 -185

No files matched your search

+31
View File
@@ -48,6 +48,37 @@ func migrate(conn *sql.DB) error {
return fmt.Errorf("running schema migrations: %w", err)
}
// Ensure lifecycle_state column exists in items, locations, tags, links (for existing databases)
for _, tableName := range []string{"items", "locations", "tags", "links"} {
rows, err := conn.Query(fmt.Sprintf("PRAGMA table_info(%s)", tableName))
if err != nil {
return fmt.Errorf("pragma table_info for %s: %w", tableName, err)
}
hasLifecycleState := false
for rows.Next() {
var cid int
var name, ctype string
var notnull, pk int
var dfltValue interface{}
if err := rows.Scan(&cid, &name, &ctype, &notnull, &dfltValue, &pk); err != nil {
rows.Close()
return fmt.Errorf("scanning table_info for %s: %w", tableName, err)
}
if name == "lifecycle_state" {
hasLifecycleState = true
}
}
rows.Close()
if !hasLifecycleState {
alterQuery := fmt.Sprintf("ALTER TABLE %s ADD COLUMN lifecycle_state TEXT NOT NULL DEFAULT 'active'", tableName)
if _, err := conn.Exec(alterQuery); err != nil {
return fmt.Errorf("adding lifecycle_state column to %s: %w", tableName, err)
}
}
}
// Check if locations count is 0
var count int
err = conn.QueryRow("SELECT COUNT(*) FROM locations").Scan(&count)
+29 -8
View File
@@ -23,6 +23,7 @@ type Item struct {
CustomFields string // JSON text
ImagePath string
ReceiptPath string
LifecycleState string // 'active', 'archived', 'removed'
CreatedAt time.Time
UpdatedAt time.Time
Tags []Tag // Loaded on demand
@@ -86,7 +87,7 @@ func (d *DB) GetItem(id int64) (*Item, error) {
i.id, i.name, i.description, i.quantity, i.model_number, i.serial_number, i.status,
i.location_id, l.name AS location_name, i.purchase_price, i.purchase_date, i.warranty_months,
i.supplier, i.custom_fields, i.image_path, i.receipt_path, i.created_at, i.updated_at,
COALESCE(lk.slug, '') AS short_slug
COALESCE(lk.slug, '') AS short_slug, i.lifecycle_state
FROM items i
LEFT JOIN locations l ON i.location_id = l.id
LEFT JOIN links lk ON lk.item_id = i.id
@@ -160,18 +161,38 @@ func (d *DB) DeleteItem(id int64) error {
return err
}
func (d *DB) ListItems() ([]Item, error) {
rows, err := d.Conn.Query(`
func (d *DB) SoftRemoveItem(id int64) error {
_, err := d.Conn.Exec("UPDATE items SET lifecycle_state = 'removed', updated_at = datetime('now') WHERE id = ?", id)
return err
}
func (d *DB) ArchiveItem(id int64) error {
_, err := d.Conn.Exec("UPDATE items SET lifecycle_state = 'archived', updated_at = datetime('now') WHERE id = ?", id)
return err
}
func (d *DB) RestoreItem(id int64) error {
_, err := d.Conn.Exec("UPDATE items SET lifecycle_state = 'active', updated_at = datetime('now') WHERE id = ?", id)
return err
}
func (d *DB) ListItems(showRemoved bool) ([]Item, error) {
query := `
SELECT
i.id, i.name, i.description, i.quantity, i.model_number, i.serial_number, i.status,
i.location_id, l.name AS location_name, i.purchase_price, i.purchase_date, i.warranty_months,
i.supplier, i.custom_fields, i.image_path, i.receipt_path, i.created_at, i.updated_at,
COALESCE(lk.slug, '') AS short_slug
COALESCE(lk.slug, '') AS short_slug, i.lifecycle_state
FROM items i
LEFT JOIN locations l ON i.location_id = l.id
LEFT JOIN links lk ON lk.item_id = i.id
ORDER BY i.id DESC`,
)
`
if !showRemoved {
query += " WHERE i.lifecycle_state != 'removed'"
}
query += " ORDER BY CASE i.lifecycle_state WHEN 'archived' THEN 1 WHEN 'removed' THEN 2 ELSE 0 END, i.id DESC"
rows, err := d.Conn.Query(query)
if err != nil {
return nil, err
}
@@ -191,7 +212,7 @@ func (d *DB) ListItems() ([]Item, error) {
err := rows.Scan(
&i.ID, &i.Name, &i.Description, &i.Quantity, &i.ModelNumber, &i.SerialNumber, &i.Status,
&locID, &locName, &price, &date, &warranty, &i.Supplier, &i.CustomFields, &i.ImagePath,
&i.ReceiptPath, &created, &updated, &i.ShortSlug,
&i.ReceiptPath, &created, &updated, &i.ShortSlug, &i.LifecycleState,
)
if err != nil {
return nil, err
@@ -312,7 +333,7 @@ func scanItem(row interface {
err := row.Scan(
&i.ID, &i.Name, &i.Description, &i.Quantity, &i.ModelNumber, &i.SerialNumber, &i.Status,
&locID, &locName, &price, &date, &warranty, &i.Supplier, &i.CustomFields, &i.ImagePath,
&i.ReceiptPath, &created, &updated, &i.ShortSlug,
&i.ReceiptPath, &created, &updated, &i.ShortSlug, &i.LifecycleState,
)
if err != nil {
return nil, err
+35 -13
View File
@@ -6,13 +6,14 @@ import (
)
type Link struct {
ID int64
Slug string
URL string
ItemID sql.NullInt64
CreatedBy string
Clicks int64
CreatedAt time.Time
ID int64
Slug string
URL string
ItemID sql.NullInt64
CreatedBy string
Clicks int64
LifecycleState string // 'active', 'archived', 'removed'
CreatedAt time.Time
}
func (d *DB) CreateLink(slug, url, createdBy string, itemID sql.NullInt64) (*Link, error) {
@@ -35,12 +36,12 @@ func (d *DB) CreateLink(slug, url, createdBy string, itemID sql.NullInt64) (*Lin
}
func (d *DB) GetLinkBySlug(slug string) (*Link, error) {
row := d.Conn.QueryRow("SELECT id, slug, url, item_id, created_by, clicks, created_at FROM links WHERE slug = ?", slug)
row := d.Conn.QueryRow("SELECT id, slug, url, item_id, created_by, clicks, lifecycle_state, created_at FROM links WHERE slug = ?", slug)
return scanLink(row)
}
func (d *DB) GetLinkByItemID(itemID int64) (*Link, error) {
row := d.Conn.QueryRow("SELECT id, slug, url, item_id, created_by, clicks, created_at FROM links WHERE item_id = ?", itemID)
row := d.Conn.QueryRow("SELECT id, slug, url, item_id, created_by, clicks, lifecycle_state, created_at FROM links WHERE item_id = ?", itemID)
return scanLink(row)
}
@@ -49,8 +50,29 @@ func (d *DB) IncrementLinkClicks(slug string) error {
return err
}
func (d *DB) ListLinks() ([]Link, error) {
rows, err := d.Conn.Query("SELECT id, slug, url, item_id, created_by, clicks, created_at FROM links ORDER BY id DESC")
func (d *DB) SoftRemoveLink(id int64) error {
_, err := d.Conn.Exec("UPDATE links SET lifecycle_state = 'removed' WHERE id = ?", id)
return err
}
func (d *DB) ArchiveLink(id int64) error {
_, err := d.Conn.Exec("UPDATE links SET lifecycle_state = 'archived' WHERE id = ?", id)
return err
}
func (d *DB) RestoreLink(id int64) error {
_, err := d.Conn.Exec("UPDATE links SET lifecycle_state = 'active' WHERE id = ?", id)
return err
}
func (d *DB) ListLinks(showRemoved bool) ([]Link, error) {
query := "SELECT id, slug, url, item_id, created_by, clicks, lifecycle_state, created_at FROM links"
if !showRemoved {
query += " WHERE lifecycle_state != 'removed'"
}
query += " ORDER BY CASE lifecycle_state WHEN 'archived' THEN 1 WHEN 'removed' THEN 2 ELSE 0 END, id DESC"
rows, err := d.Conn.Query(query)
if err != nil {
return nil, err
}
@@ -61,7 +83,7 @@ func (d *DB) ListLinks() ([]Link, error) {
var l Link
var itemID sql.NullInt64
var ts string
if err := rows.Scan(&l.ID, &l.Slug, &l.URL, &itemID, &l.CreatedBy, &l.Clicks, &ts); err != nil {
if err := rows.Scan(&l.ID, &l.Slug, &l.URL, &itemID, &l.CreatedBy, &l.Clicks, &l.LifecycleState, &ts); err != nil {
return nil, err
}
l.ItemID = itemID
@@ -93,7 +115,7 @@ func scanLink(row interface {
var l Link
var itemID sql.NullInt64
var ts string
if err := row.Scan(&l.ID, &l.Slug, &l.URL, &itemID, &l.CreatedBy, &l.Clicks, &ts); err != nil {
if err := row.Scan(&l.ID, &l.Slug, &l.URL, &itemID, &l.CreatedBy, &l.Clicks, &l.LifecycleState, &ts); err != nil {
return nil, err
}
l.ItemID = itemID
+43 -17
View File
@@ -7,14 +7,15 @@ import (
)
type Location struct {
ID int64
Name string
Description string
ParentID sql.NullInt64
ParentName sql.NullString // Resolved parent location name
ImagePath string
CreatedAt time.Time
UpdatedAt time.Time
ID int64
Name string
Description string
ParentID sql.NullInt64
ParentName sql.NullString // Resolved parent location name
ImagePath string
LifecycleState string // 'active', 'archived', 'removed'
CreatedAt time.Time
UpdatedAt time.Time
}
func (d *DB) CreateLocation(name, description string, parentID sql.NullInt64, imagePath string) (*Location, error) {
@@ -63,7 +64,7 @@ func (d *DB) CreateLocation(name, description string, parentID sql.NullInt64, im
func (d *DB) GetLocation(id int64) (*Location, error) {
row := d.Conn.QueryRow(`
SELECT l.id, l.name, l.description, l.parent_id, p.name AS parent_name, l.image_path, l.created_at, l.updated_at
SELECT l.id, l.name, l.description, l.parent_id, p.name AS parent_name, l.image_path, l.lifecycle_state, l.created_at, l.updated_at
FROM locations l
LEFT JOIN locations p ON l.parent_id = p.id
WHERE l.id = ?`,
@@ -174,13 +175,38 @@ func (d *DB) DeleteLocation(id int64) error {
return tx.Commit()
}
func (d *DB) ListLocations() ([]Location, error) {
rows, err := d.Conn.Query(`
SELECT l.id, l.name, l.description, l.parent_id, p.name AS parent_name, l.image_path, l.created_at, l.updated_at
func (d *DB) SoftRemoveLocation(id int64) error {
if id == 1 {
return fmt.Errorf("cannot remove the Default Room")
}
_, err := d.Conn.Exec("UPDATE locations SET lifecycle_state = 'removed', updated_at = datetime('now') WHERE id = ?", id)
return err
}
func (d *DB) ArchiveLocation(id int64) error {
if id == 1 {
return fmt.Errorf("cannot archive the Default Room")
}
_, err := d.Conn.Exec("UPDATE locations SET lifecycle_state = 'archived', updated_at = datetime('now') WHERE id = ?", id)
return err
}
func (d *DB) RestoreLocation(id int64) error {
_, err := d.Conn.Exec("UPDATE locations SET lifecycle_state = 'active', updated_at = datetime('now') WHERE id = ?", id)
return err
}
func (d *DB) ListLocations(showRemoved bool) ([]Location, error) {
query := `
SELECT l.id, l.name, l.description, l.parent_id, p.name AS parent_name, l.image_path, l.lifecycle_state, l.created_at, l.updated_at
FROM locations l
LEFT JOIN locations p ON l.parent_id = p.id
ORDER BY l.name ASC`,
)
LEFT JOIN locations p ON l.parent_id = p.id`
if !showRemoved {
query += " WHERE l.lifecycle_state != 'removed'"
}
query += " ORDER BY CASE l.lifecycle_state WHEN 'archived' THEN 1 WHEN 'removed' THEN 2 ELSE 0 END, l.name ASC"
rows, err := d.Conn.Query(query)
if err != nil {
return nil, err
}
@@ -192,7 +218,7 @@ func (d *DB) ListLocations() ([]Location, error) {
var parentID sql.NullInt64
var parentName sql.NullString
var created, updated string
if err := rows.Scan(&l.ID, &l.Name, &l.Description, &parentID, &parentName, &l.ImagePath, &created, &updated); err != nil {
if err := rows.Scan(&l.ID, &l.Name, &l.Description, &parentID, &parentName, &l.ImagePath, &l.LifecycleState, &created, &updated); err != nil {
return nil, err
}
l.ParentID = parentID
@@ -211,7 +237,7 @@ func scanLocation(row interface {
var parentID sql.NullInt64
var parentName sql.NullString
var created, updated string
if err := row.Scan(&l.ID, &l.Name, &l.Description, &parentID, &parentName, &l.ImagePath, &created, &updated); err != nil {
if err := row.Scan(&l.ID, &l.Name, &l.Description, &parentID, &parentName, &l.ImagePath, &l.LifecycleState, &created, &updated); err != nil {
return nil, err
}
l.ParentID = parentID
+23 -19
View File
@@ -3,24 +3,26 @@
-- 1. Locations Table (Supports hierarchical locations via parent_id)
CREATE TABLE IF NOT EXISTS locations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
parent_id INTEGER REFERENCES locations(id) ON DELETE SET NULL,
image_path TEXT NOT NULL DEFAULT '',
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
parent_id INTEGER REFERENCES locations(id) ON DELETE SET NULL,
image_path TEXT NOT NULL DEFAULT '',
lifecycle_state TEXT NOT NULL DEFAULT 'active',
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_locations_parent ON locations(parent_id);
-- 2. Tags Table (Sleek labels for grouping items)
CREATE TABLE IF NOT EXISTS tags (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
color TEXT NOT NULL DEFAULT '#e2e8f0', -- Hex code for pill color
description TEXT NOT NULL DEFAULT '',
created_at TEXT NOT NULL DEFAULT (datetime('now'))
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
color TEXT NOT NULL DEFAULT '#e2e8f0', -- Hex code for pill color
description TEXT NOT NULL DEFAULT '',
lifecycle_state TEXT NOT NULL DEFAULT 'active',
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
-- 3. Items Table (Asset entries with purchase records and JSON custom fields)
@@ -40,6 +42,7 @@ CREATE TABLE IF NOT EXISTS items (
custom_fields TEXT NOT NULL DEFAULT '{}', -- JSON text field containing arbitrary key-value custom metadata
image_path TEXT NOT NULL DEFAULT '',
receipt_path TEXT NOT NULL DEFAULT '',
lifecycle_state TEXT NOT NULL DEFAULT 'active',
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
@@ -55,13 +58,14 @@ CREATE TABLE IF NOT EXISTS item_tags (
-- 5. Links Table (Ported and enhanced from Glimmer)
CREATE TABLE IF NOT EXISTS links (
id INTEGER PRIMARY KEY AUTOINCREMENT,
slug TEXT NOT NULL UNIQUE,
url TEXT NOT NULL, -- Redirect target (internal, e.g., '/items/12', or external)
item_id INTEGER REFERENCES items(id) ON DELETE CASCADE, -- Nullable; links specific assets to short URLs
created_by TEXT NOT NULL DEFAULT 'admin',
clicks INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
id INTEGER PRIMARY KEY AUTOINCREMENT,
slug TEXT NOT NULL UNIQUE,
url TEXT NOT NULL, -- Redirect target (internal, e.g., '/items/12', or external)
item_id INTEGER REFERENCES items(id) ON DELETE CASCADE, -- Nullable; links specific assets to short URLs
created_by TEXT NOT NULL DEFAULT 'admin',
clicks INTEGER NOT NULL DEFAULT 0,
lifecycle_state TEXT NOT NULL DEFAULT 'active',
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_links_slug ON links(slug);
+43 -11
View File
@@ -6,11 +6,12 @@ import (
)
type Tag struct {
ID int64
Name string
Color string // Hex representation, e.g. "#3b82f6"
Description string
CreatedAt time.Time
ID int64
Name string
Color string // Hex representation, e.g. "#3b82f6"
Description string
LifecycleState string // 'active', 'archived', 'removed'
CreatedAt time.Time
}
func (d *DB) CreateTag(name, color, description string) (*Tag, error) {
@@ -35,12 +36,12 @@ func (d *DB) CreateTag(name, color, description string) (*Tag, error) {
}
func (d *DB) GetTag(id int64) (*Tag, error) {
row := d.Conn.QueryRow("SELECT id, name, color, description, created_at FROM tags WHERE id = ?", id)
row := d.Conn.QueryRow("SELECT id, name, color, description, lifecycle_state, created_at FROM tags WHERE id = ?", id)
return scanTag(row)
}
func (d *DB) GetTagByName(name string) (*Tag, error) {
row := d.Conn.QueryRow("SELECT id, name, color, description, created_at FROM tags WHERE name = ?", name)
row := d.Conn.QueryRow("SELECT id, name, color, description, lifecycle_state, created_at FROM tags WHERE name = ?", name)
return scanTag(row)
}
@@ -74,8 +75,39 @@ func (d *DB) DeleteTag(id int64) error {
return err
}
func (d *DB) ListTags() ([]Tag, error) {
rows, err := d.Conn.Query("SELECT id, name, color, description, created_at FROM tags ORDER BY name ASC")
func (d *DB) SoftRemoveTag(id int64) error {
var name string
err := d.Conn.QueryRow("SELECT name FROM tags WHERE id = ?", id).Scan(&name)
if err == nil && name == "Default Room" {
return fmt.Errorf("cannot remove the Default Room tag")
}
_, err = d.Conn.Exec("UPDATE tags SET lifecycle_state = 'removed' WHERE id = ?", id)
return err
}
func (d *DB) ArchiveTag(id int64) error {
var name string
err := d.Conn.QueryRow("SELECT name FROM tags WHERE id = ?", id).Scan(&name)
if err == nil && name == "Default Room" {
return fmt.Errorf("cannot archive the Default Room tag")
}
_, err = d.Conn.Exec("UPDATE tags SET lifecycle_state = 'archived' WHERE id = ?", id)
return err
}
func (d *DB) RestoreTag(id int64) error {
_, err := d.Conn.Exec("UPDATE tags SET lifecycle_state = 'active' WHERE id = ?", id)
return err
}
func (d *DB) ListTags(showRemoved bool) ([]Tag, error) {
query := "SELECT id, name, color, description, lifecycle_state, created_at FROM tags"
if !showRemoved {
query += " WHERE lifecycle_state != 'removed'"
}
query += " ORDER BY CASE lifecycle_state WHEN 'archived' THEN 1 WHEN 'removed' THEN 2 ELSE 0 END, name ASC"
rows, err := d.Conn.Query(query)
if err != nil {
return nil, err
}
@@ -85,7 +117,7 @@ func (d *DB) ListTags() ([]Tag, error) {
for rows.Next() {
var t Tag
var created string
if err := rows.Scan(&t.ID, &t.Name, &t.Color, &t.Description, &created); err != nil {
if err := rows.Scan(&t.ID, &t.Name, &t.Color, &t.Description, &t.LifecycleState, &created); err != nil {
return nil, err
}
t.CreatedAt, _ = time.Parse("2006-01-02 15:04:05", created)
@@ -99,7 +131,7 @@ func scanTag(row interface {
}) (*Tag, error) {
var t Tag
var created string
if err := row.Scan(&t.ID, &t.Name, &t.Color, &t.Description, &created); err != nil {
if err := row.Scan(&t.ID, &t.Name, &t.Color, &t.Description, &t.LifecycleState, &created); err != nil {
return nil, err
}
t.CreatedAt, _ = time.Parse("2006-01-02 15:04:05", created)