38 lines
1.1 KiB
MySQL
38 lines
1.1 KiB
MySQL
|
|
-- Template Module - Initial Migration
|
||
|
|
-- Opret basis tabeller for template module
|
||
|
|
|
||
|
|
-- Items tabel (eksempel)
|
||
|
|
CREATE TABLE IF NOT EXISTS template_items (
|
||
|
|
id SERIAL PRIMARY KEY,
|
||
|
|
name VARCHAR(255) NOT NULL,
|
||
|
|
description TEXT,
|
||
|
|
active BOOLEAN DEFAULT TRUE,
|
||
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||
|
|
);
|
||
|
|
|
||
|
|
-- Index for performance
|
||
|
|
CREATE INDEX IF NOT EXISTS idx_template_items_active ON template_items(active);
|
||
|
|
CREATE INDEX IF NOT EXISTS idx_template_items_created ON template_items(created_at DESC);
|
||
|
|
|
||
|
|
-- Trigger for updated_at
|
||
|
|
CREATE OR REPLACE FUNCTION update_template_items_updated_at()
|
||
|
|
RETURNS TRIGGER AS $$
|
||
|
|
BEGIN
|
||
|
|
NEW.updated_at = CURRENT_TIMESTAMP;
|
||
|
|
RETURN NEW;
|
||
|
|
END;
|
||
|
|
$$ LANGUAGE plpgsql;
|
||
|
|
|
||
|
|
CREATE TRIGGER trigger_template_items_updated_at
|
||
|
|
BEFORE UPDATE ON template_items
|
||
|
|
FOR EACH ROW
|
||
|
|
EXECUTE FUNCTION update_template_items_updated_at();
|
||
|
|
|
||
|
|
-- Indsæt test data (optional)
|
||
|
|
INSERT INTO template_items (name, description)
|
||
|
|
VALUES
|
||
|
|
('Test Item 1', 'This is a test item from template module'),
|
||
|
|
('Test Item 2', 'Another test item')
|
||
|
|
ON CONFLICT DO NOTHING;
|