budgit/internal/db/migrations/00010_create_payment_methods_table.sql
juancwu 3de76916c9
All checks were successful
Deploy / build-and-deploy (push) Successful in 1m1s
feat: payment methods
2026-02-13 21:55:10 +00:00

23 lines
1,010 B
SQL

-- +goose Up
CREATE TABLE payment_methods (
id TEXT PRIMARY KEY NOT NULL,
space_id TEXT NOT NULL,
name TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('credit', 'debit')),
last_four TEXT NOT NULL,
created_by TEXT NOT NULL,
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,
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE
);
ALTER TABLE expenses ADD COLUMN payment_method_id TEXT REFERENCES payment_methods(id) ON DELETE SET NULL;
CREATE INDEX idx_payment_method_space_id ON payment_methods(space_id);
CREATE INDEX idx_expenses_payment_method_id ON expenses(payment_method_id);
-- +goose Down
DROP INDEX IF EXISTS idx_expenses_payment_method_id;
DROP INDEX IF EXISTS idx_payment_method_space_id;
ALTER TABLE expenses DROP COLUMN IF EXISTS payment_method_id;
DROP TABLE IF EXISTS payment_methods;