Merge branch 'fix/migration-file'
All checks were successful
Deploy / build-and-deploy (push) Successful in 2m16s
All checks were successful
Deploy / build-and-deploy (push) Successful in 2m16s
This commit is contained in:
commit
b9976aef9c
1 changed files with 71 additions and 17 deletions
|
|
@ -1,4 +1,31 @@
|
||||||
-- +goose Up
|
-- +goose Up
|
||||||
|
-- Drop index before rename (SQLite keeps index names after table rename)
|
||||||
|
DROP INDEX IF EXISTS idx_budgets_space_id;
|
||||||
|
|
||||||
|
-- Rename old budgets table so we can recreate it without tag_id
|
||||||
|
ALTER TABLE budgets RENAME TO budgets_old;
|
||||||
|
|
||||||
|
-- Recreate budgets table without tag_id (SQLite can't DROP COLUMN with constraints)
|
||||||
|
CREATE TABLE budgets (
|
||||||
|
id TEXT PRIMARY KEY NOT NULL,
|
||||||
|
space_id TEXT NOT NULL,
|
||||||
|
amount_cents INTEGER NOT NULL,
|
||||||
|
period TEXT NOT NULL CHECK (period IN ('weekly', 'monthly', 'yearly')),
|
||||||
|
start_date DATE NOT NULL,
|
||||||
|
end_date DATE,
|
||||||
|
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||||
|
created_by 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,
|
||||||
|
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO budgets (id, space_id, amount_cents, period, start_date, end_date, is_active, created_by, created_at, updated_at)
|
||||||
|
SELECT id, space_id, amount_cents, period, start_date, end_date, is_active, created_by, created_at, updated_at FROM budgets_old;
|
||||||
|
|
||||||
|
CREATE INDEX idx_budgets_space_id ON budgets(space_id);
|
||||||
|
|
||||||
-- Create budget_tags join table for many-to-many relationship
|
-- Create budget_tags join table for many-to-many relationship
|
||||||
CREATE TABLE budget_tags (
|
CREATE TABLE budget_tags (
|
||||||
budget_id TEXT NOT NULL,
|
budget_id TEXT NOT NULL,
|
||||||
|
|
@ -12,27 +39,54 @@ CREATE INDEX idx_budget_tags_tag_id ON budget_tags(tag_id);
|
||||||
|
|
||||||
-- Migrate existing tag_id data to the join table
|
-- Migrate existing tag_id data to the join table
|
||||||
INSERT INTO budget_tags (budget_id, tag_id)
|
INSERT INTO budget_tags (budget_id, tag_id)
|
||||||
SELECT id, tag_id FROM budgets WHERE tag_id IS NOT NULL;
|
SELECT id, tag_id FROM budgets_old WHERE tag_id IS NOT NULL;
|
||||||
|
|
||||||
-- Drop the unique constraint and FK that reference tag_id, then drop the column
|
-- Drop the old table (nothing references it now)
|
||||||
ALTER TABLE budgets DROP CONSTRAINT budgets_space_id_tag_id_period_key;
|
DROP TABLE budgets_old;
|
||||||
ALTER TABLE budgets DROP CONSTRAINT budgets_tag_id_fkey;
|
|
||||||
ALTER TABLE budgets DROP COLUMN tag_id;
|
|
||||||
|
|
||||||
-- +goose Down
|
-- +goose Down
|
||||||
-- Add tag_id column back
|
-- Drop budget_tags first (it references budgets)
|
||||||
ALTER TABLE budgets ADD COLUMN tag_id TEXT;
|
DROP INDEX IF EXISTS idx_budget_tags_tag_id;
|
||||||
|
|
||||||
-- Copy first tag back from budget_tags
|
-- Save tag mappings before dropping budget_tags
|
||||||
UPDATE budgets SET tag_id = (
|
CREATE TEMP TABLE budget_tag_mappings AS
|
||||||
SELECT tag_id FROM budget_tags WHERE budget_tags.budget_id = budgets.id LIMIT 1
|
SELECT budget_id, tag_id FROM budget_tags;
|
||||||
|
|
||||||
|
DROP TABLE budget_tags;
|
||||||
|
|
||||||
|
-- Drop index before rename (SQLite keeps index names after table rename)
|
||||||
|
DROP INDEX IF EXISTS idx_budgets_space_id;
|
||||||
|
|
||||||
|
-- Rename current budgets out of the way
|
||||||
|
ALTER TABLE budgets RENAME TO budgets_new;
|
||||||
|
|
||||||
|
-- Recreate budgets table with tag_id column
|
||||||
|
CREATE TABLE budgets (
|
||||||
|
id TEXT PRIMARY KEY NOT NULL,
|
||||||
|
space_id TEXT NOT NULL,
|
||||||
|
tag_id TEXT NOT NULL,
|
||||||
|
amount_cents INTEGER NOT NULL,
|
||||||
|
period TEXT NOT NULL CHECK (period IN ('weekly', 'monthly', 'yearly')),
|
||||||
|
start_date DATE NOT NULL,
|
||||||
|
end_date DATE,
|
||||||
|
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||||
|
created_by TEXT NOT NULL,
|
||||||
|
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
UNIQUE(space_id, tag_id, period),
|
||||||
|
FOREIGN KEY (space_id) REFERENCES spaces(id) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Re-add FK and unique constraint
|
-- Copy data back, restoring first tag from saved mappings
|
||||||
ALTER TABLE budgets ADD CONSTRAINT budgets_tag_id_fkey
|
INSERT INTO budgets (id, space_id, tag_id, amount_cents, period, start_date, end_date, is_active, created_by, created_at, updated_at)
|
||||||
FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE;
|
SELECT b.id, b.space_id,
|
||||||
ALTER TABLE budgets ADD CONSTRAINT budgets_space_id_tag_id_period_key
|
COALESCE((SELECT m.tag_id FROM budget_tag_mappings m WHERE m.budget_id = b.id LIMIT 1), ''),
|
||||||
UNIQUE (space_id, tag_id, period);
|
b.amount_cents, b.period, b.start_date, b.end_date, b.is_active, b.created_by, b.created_at, b.updated_at
|
||||||
|
FROM budgets_new b;
|
||||||
|
|
||||||
DROP INDEX IF EXISTS idx_budget_tags_tag_id;
|
CREATE INDEX idx_budgets_space_id ON budgets(space_id);
|
||||||
DROP TABLE IF EXISTS budget_tags;
|
|
||||||
|
DROP TABLE budgets_new;
|
||||||
|
DROP TABLE budget_tag_mappings;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue