Files
ackify-ce/migrations/0001_init.up.sql
Benjamin bde1049343 feat: implement standalone SQL migration system
- Add golang-migrate based migration CLI in cmd/migrate/
- Extract hardcoded SQL migrations from connection.go to proper migration files
- Create 0001_init migration with complete signatures table schema
- Remove embedded migrations from Go code for cleaner separation
- Support up/down/version/drop migration operations
- Migration files follow semantic versioning starting at 0001
2025-09-14 17:59:33 +02:00

35 lines
1013 B
PL/PgSQL

-- Create signatures table for Community Edition
CREATE TABLE signatures (
id BIGSERIAL PRIMARY KEY,
doc_id TEXT NOT NULL,
user_sub TEXT NOT NULL,
user_email TEXT NOT NULL,
user_name TEXT,
signed_at TIMESTAMPTZ NOT NULL,
payload_hash TEXT NOT NULL,
signature TEXT NOT NULL,
nonce TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
referer TEXT,
prev_hash TEXT,
UNIQUE (doc_id, user_sub)
);
-- Create index for efficient queries
CREATE INDEX idx_signatures_user ON signatures(user_sub);
-- Create trigger to prevent modification of created_at
CREATE OR REPLACE FUNCTION prevent_created_at_update()
RETURNS TRIGGER AS $$
BEGIN
IF OLD.created_at IS DISTINCT FROM NEW.created_at THEN
RAISE EXCEPTION 'Cannot modify created_at timestamp';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trigger_prevent_created_at_update
BEFORE UPDATE ON signatures
FOR EACH ROW
EXECUTE FUNCTION prevent_created_at_update();