Writing a Professional Database Schema Migration in PostgreSQL
- Category SQL
- Type Query
- Platform Cross-platform
- Language SQL
- Price Free
- Copy 7 900
- Comments 0
Writing a Professional Database Schema Migration in PostgreSQL
-- --- UP MIGRATION (Apply Changes) ---
-- Create modern snippets table with strict foreign key constraints
CREATE TABLE snippets (
id SERIAL PRIMARY KEY,
user_id INT NOT NULL,
title VARCHAR(255) NOT NULL,
code_content TEXT NOT NULL,
slug VARCHAR(100) UNIQUE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
);
-- Add an index on the slug column for lighting-fast search queries
CREATE INDEX idx_snippets_slug ON snippets(slug);
-- --- DOWN MIGRATION (Rollback Changes) ---
-- DROP TABLE IF EXISTS snippets;
Free snippet — copy & paste!
Copy this snippet for free on Clayi Code. One click — no account required.

There are no comments yet :(