38 lines
1.1 KiB
MySQL
38 lines
1.1 KiB
MySQL
|
|
-- Migration: 001_initial_schema.sql
|
||
|
|
-- Description: Initial database schema with example items table
|
||
|
|
-- To customize: Rename tables/columns and add your own migrations
|
||
|
|
|
||
|
|
-- Create items table (example)
|
||
|
|
CREATE TABLE IF NOT EXISTS items (
|
||
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||
|
|
name VARCHAR(255) NOT NULL,
|
||
|
|
description TEXT,
|
||
|
|
status VARCHAR(50) NOT NULL DEFAULT 'active',
|
||
|
|
metadata JSONB,
|
||
|
|
created TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||
|
|
modified TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||
|
|
);
|
||
|
|
|
||
|
|
-- Create index on status for faster filtering
|
||
|
|
CREATE INDEX IF NOT EXISTS idx_items_status ON items(status);
|
||
|
|
|
||
|
|
-- Create index on created for sorting
|
||
|
|
CREATE INDEX IF NOT EXISTS idx_items_created ON items(created);
|
||
|
|
|
||
|
|
-- Optional: Trigger to auto-update modified timestamp
|
||
|
|
CREATE OR REPLACE FUNCTION update_modified_column()
|
||
|
|
RETURNS TRIGGER AS $$
|
||
|
|
BEGIN
|
||
|
|
NEW.modified = NOW();
|
||
|
|
RETURN NEW;
|
||
|
|
END;
|
||
|
|
$$ language 'plpgsql';
|
||
|
|
|
||
|
|
CREATE OR REPLACE TRIGGER update_items_modtime
|
||
|
|
BEFORE UPDATE ON items
|
||
|
|
FOR EACH ROW
|
||
|
|
EXECUTE FUNCTION update_modified_column();
|
||
|
|
|
||
|
|
-- Add table comment
|
||
|
|
COMMENT ON TABLE items IS 'Example items table - replace with your own schema';
|