budgit/internal/db/migrations/00006_create_features_tables.sql
2026-01-14 18:19:13 +00:00

48 lines
1.7 KiB
SQL

-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS tags (
id TEXT PRIMARY KEY NOT NULL,
space_id TEXT NOT NULL,
name TEXT NOT NULL,
color TEXT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (space_id, name),
FOREIGN KEY (space_id) REFERENCES spaces(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS shopping_lists (
id TEXT PRIMARY KEY NOT NULL,
space_id TEXT NOT NULL,
name TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (space_id) REFERENCES spaces(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS list_items (
id TEXT PRIMARY KEY NOT NULL,
list_id TEXT NOT NULL,
name TEXT NOT NULL,
is_checked BOOLEAN NOT NULL DEFAULT FALSE,
created_by TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (list_id) REFERENCES shopping_lists(id) ON DELETE CASCADE,
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_tags_space_id ON tags(space_id);
CREATE INDEX IF NOT EXISTS idx_shopping_lists_space_id ON shopping_lists(space_id);
CREATE INDEX IF NOT EXISTS idx_list_items_list_id ON list_items(list_id);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP INDEX IF EXISTS idx_list_items_list_id;
DROP INDEX IF EXISTS idx_shopping_lists_space_id;
DROP INDEX IF EXISTS idx_tags_space_id;
DROP TABLE IF EXISTS list_items;
DROP TABLE IF EXISTS shopping_lists;
DROP TABLE IF EXISTS tags;
-- +goose StatementEnd