This commit is contained in:
2025-10-24 21:06:39 +08:00
parent 45fb8dfeb8
commit 2f56813874
6 changed files with 84 additions and 285 deletions

View File

@@ -1,25 +1,16 @@
-- Files table: stores uploaded file metadata
CREATE TABLE IF NOT EXISTS files (
id BIGSERIAL PRIMARY KEY,
id BIGSERIAL,
file_name VARCHAR(255) NOT NULL,
file_key VARCHAR(500) NOT NULL UNIQUE,
byte_size BIGINT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
-- Index for looking up files by key (most common query)
CREATE INDEX IF NOT EXISTS idx_files_file_key ON files(file_key);
COMMENT ON TABLE files IS 'Files table for storing uploaded files';
-- Index for sorting files by creation date
CREATE INDEX IF NOT EXISTS idx_files_created_at ON files(created_at DESC);
COMMENT ON COLUMN files.id IS 'Unique identifier for the file';
COMMENT ON COLUMN files.file_name IS 'Name of the uploaded file';
COMMENT ON COLUMN files.file_key IS 'Unique key for the file storage';
COMMENT ON COLUMN files.created_at IS 'Timestamp when the file was uploaded';
-- Index for file size queries
CREATE INDEX IF NOT EXISTS idx_files_byte_size ON files(byte_size);
-- Comments on columns
COMMENT ON TABLE files IS 'File metadata table for uploaded files';
COMMENT ON COLUMN files.id IS 'Primary key, auto-incrementing file ID';
COMMENT ON COLUMN files.file_name IS 'Original name of the uploaded file';
COMMENT ON COLUMN files.file_key IS 'Unique storage key/path for the file';
COMMENT ON COLUMN files.byte_size IS 'File size in bytes';
COMMENT ON COLUMN files.created_at IS 'Timestamp when the file was uploaded';
CREATE INDEX idx_files_created_at ON files(created_at);

View File

@@ -1,23 +1,15 @@
-- Hashtags table: stores unique hashtag information
CREATE TABLE IF NOT EXISTS hashtags (
id BIGSERIAL PRIMARY KEY,
id BIGSERIAL,
name VARCHAR(50) NOT NULL UNIQUE,
usage_count BIGINT DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
-- Index for looking up hashtags by name (most common query)
CREATE INDEX IF NOT EXISTS idx_hashtags_name ON hashtags(name);
COMMENT ON TABLE hashtags IS 'Hashtags table for storing unique hashtag names';
-- Index for sorting hashtags by popularity
CREATE INDEX IF NOT EXISTS idx_hashtags_usage_count ON hashtags(usage_count DESC);
COMMENT ON COLUMN hashtags.id IS 'Unique identifier for the hashtag';
COMMENT ON COLUMN hashtags.name IS 'Name of the hashtag';
COMMENT ON COLUMN hashtags.created_at IS 'Timestamp when the hashtag was created';
-- Index for trending hashtags (recent + popular)
CREATE INDEX IF NOT EXISTS idx_hashtags_created_usage ON hashtags(created_at DESC, usage_count DESC);
-- Comments on columns
COMMENT ON TABLE hashtags IS 'Hashtags table for categorizing posts';
COMMENT ON COLUMN hashtags.id IS 'Primary key, auto-incrementing hashtag ID';
COMMENT ON COLUMN hashtags.name IS 'Unique hashtag name';
COMMENT ON COLUMN hashtags.usage_count IS 'Number of times the hashtag has been used';
COMMENT ON COLUMN hashtags.created_at IS 'Timestamp when the hashtag was first created';
CREATE INDEX idx_hashtags_name ON hashtags(name);
CREATE INDEX idx_hashtags_created_at ON hashtags(created_at);

View File

@@ -1,23 +1,16 @@
-- Post_hashtags junction table: many-to-many relationship between posts and hashtags
CREATE TABLE IF NOT EXISTS post_hashtags (
post_id BIGINT NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
hashtag_id BIGINT NOT NULL REFERENCES hashtags(id) ON DELETE CASCADE,
post_id BIGINT NOT NULL,
hashtag_id BIGINT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (post_id, hashtag_id)
);
-- Index for finding all posts with a specific hashtag
CREATE INDEX IF NOT EXISTS idx_post_hashtags_hashtag_id ON post_hashtags(hashtag_id, post_id);
COMMENT ON TABLE post_hashtags IS 'Junction table linking posts and hashtags for many-to-many relationship';
-- Index for finding all hashtags for a specific post
CREATE INDEX IF NOT EXISTS idx_post_hashtags_post_id ON post_hashtags(post_id, hashtag_id);
COMMENT ON COLUMN post_hashtags.post_id IS 'Reference to the post id';
COMMENT ON COLUMN post_hashtags.hashtag_id IS 'Reference to the hashtag id';
COMMENT ON COLUMN post_hashtags.created_at IS 'Timestamp when the association was created';
-- Index for sorting by creation date
CREATE INDEX IF NOT EXISTS idx_post_hashtags_created_at ON post_hashtags(created_at DESC);
-- Comments on columns
COMMENT ON TABLE post_hashtags IS 'Junction table linking posts and hashtags';
COMMENT ON COLUMN post_hashtags.post_id IS 'Foreign key referencing posts table';
COMMENT ON COLUMN post_hashtags.hashtag_id IS 'Foreign key referencing hashtags table';
COMMENT ON COLUMN post_hashtags.created_at IS 'Timestamp when the hashtag was associated with the post';
CREATE INDEX idx_post_hashtags_post_id ON post_hashtags(post_id);
CREATE INDEX idx_post_hashtags_hashtag_id ON post_hashtags(hashtag_id);
CREATE INDEX idx_post_hashtags_created_at ON post_hashtags(created_at);

View File

@@ -1,30 +1,21 @@
-- Posts table: stores blog post information
CREATE TABLE IF NOT EXISTS posts (
id BIGSERIAL PRIMARY KEY,
id BIGSERIAL,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
view_count BIGINT DEFAULT 0,
cover_id BIGINT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
-- Index for sorting posts by creation date
CREATE INDEX IF NOT EXISTS idx_posts_created_at ON posts(created_at DESC);
COMMENT ON TABLE posts IS 'Blog posts table';
-- Index for sorting posts by view count (popular posts)
CREATE INDEX IF NOT EXISTS idx_posts_view_count ON posts(view_count DESC);
-- Index for sorting posts by update date
CREATE INDEX IF NOT EXISTS idx_posts_updated_at ON posts(updated_at DESC);
-- Combined index for pagination queries
CREATE INDEX IF NOT EXISTS idx_posts_created_view ON posts(created_at DESC, view_count DESC);
-- Comments on columns
COMMENT ON TABLE posts IS 'Blog posts table containing article information';
COMMENT ON COLUMN posts.id IS 'Primary key, auto-incrementing post ID';
COMMENT ON COLUMN posts.title IS 'Post title';
COMMENT ON COLUMN posts.content IS 'Post content in text format';
COMMENT ON COLUMN posts.view_count IS 'Number of times the post has been viewed';
COMMENT ON COLUMN posts.id IS 'Unique identifier for the post';
COMMENT ON COLUMN posts.title IS 'Title of the blog post';
COMMENT ON COLUMN posts.content IS 'Content of the blog post';
COMMENT ON COLUMN posts.cover_id IS 'Reference to the cover image file id';
COMMENT ON COLUMN posts.created_at IS 'Timestamp when the post was created';
COMMENT ON COLUMN posts.updated_at IS 'Timestamp when the post was last updated';
COMMENT ON COLUMN posts.updated_at IS 'Timestamp when the post was last updated';
CREATE INDEX idx_posts_created_at ON posts(created_at);
CREATE INDEX idx_posts_cover_id ON posts(cover_id);

21
ddl/tmpfiles.sql Normal file
View File

@@ -0,0 +1,21 @@
CREATE TABLE IF NOT EXISTS tmpfiles (
id BIGSERIAL,
file_name VARCHAR(255) NOT NULL,
key VARCHAR(500) NOT NULL UNIQUE,
presigned_url TEXT NOT NULL,
expiration TIMESTAMP WITH TIME ZONE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
COMMENT ON TABLE tmpfiles IS 'Temporary files table for storing files with presigned URLs and expiration times';
COMMENT ON COLUMN tmpfiles.id IS 'Unique identifier for the temporary file';
COMMENT ON COLUMN tmpfiles.file_name IS 'Name of the temporary file';
COMMENT ON COLUMN tmpfiles.key IS 'Unique key for the temporary file';
COMMENT ON COLUMN tmpfiles.presigned_url IS 'Presigned URL for accessing the temporary file';
COMMENT ON COLUMN tmpfiles.expiration IS 'Expiration timestamp for the temporary file';
COMMENT ON COLUMN tmpfiles.created_at IS 'Timestamp when the temporary file was created';
CREATE INDEX idx_tmpfiles_expiration ON tmpfiles(expiration);
CREATE INDEX idx_tmpfiles_created_at ON tmpfiles(created_at);