CREATE EXTENSION IF NOT EXISTS pgcrypto; CREATE TABLE IF NOT EXISTS calibers ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name VARCHAR(40) NOT NULL UNIQUE, is_default BOOLEAN NOT NULL DEFAULT FALSE, is_active BOOLEAN NOT NULL DEFAULT TRUE, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS firearms ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), manufacturer VARCHAR(120) NOT NULL, model VARCHAR(120) NOT NULL, category VARCHAR(80) NOT NULL, caliber VARCHAR(40) NOT NULL, serial_number VARCHAR(120) NOT NULL UNIQUE, purchase_price NUMERIC(10, 2) NOT NULL DEFAULT 0, acquired_on DATE, image_url TEXT, notes TEXT, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS ammo_inventory ( caliber_id UUID PRIMARY KEY REFERENCES calibers(id) ON DELETE CASCADE, rounds_on_hand INT NOT NULL DEFAULT 0 CHECK (rounds_on_hand >= 0), cost_per_round NUMERIC(10, 2) NOT NULL DEFAULT 0, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); INSERT INTO calibers (name, is_default, is_active) VALUES ('9mm', TRUE, TRUE), ('.22 LR', TRUE, TRUE), ('5.56 NATO', TRUE, TRUE), ('.308 Win', TRUE, TRUE), ('12 Gauge', TRUE, TRUE), ('.45 ACP', TRUE, TRUE) ON CONFLICT (name) DO NOTHING; INSERT INTO ammo_inventory (caliber_id, rounds_on_hand, cost_per_round) SELECT id, 0, 0 FROM calibers ON CONFLICT (caliber_id) DO NOTHING;