Add unified posts database groundwork (#2)
Adds self-referencing post and post-vote tables, generated schema models, and an idempotent snapshot migration that preserves the legacy tables during the staged application cutover. Co-authored-by: codegirl-007 <s.raide@gmail.com>
This commit was merged in pull request #2.
This commit is contained in:
@@ -35,6 +35,75 @@ CREATE UNIQUE INDEX IF NOT EXISTS users_email_lower_uidx
|
||||
return nil
|
||||
}
|
||||
|
||||
// migratePosts creates the unified post model and snapshots legacy content.
|
||||
// Legacy tables remain in place until the application cutover is complete.
|
||||
func migratePosts(ctx context.Context, exec execContext) error {
|
||||
steps := []struct {
|
||||
name string
|
||||
sql string
|
||||
}{
|
||||
{"create posts", `
|
||||
CREATE TABLE IF NOT EXISTS posts (
|
||||
id TEXT PRIMARY KEY,
|
||||
parent_id TEXT REFERENCES posts(id) ON DELETE CASCADE,
|
||||
author_id TEXT NOT NULL REFERENCES users(id),
|
||||
title TEXT NOT NULL DEFAULT '',
|
||||
body TEXT NOT NULL,
|
||||
city TEXT NOT NULL DEFAULT '',
|
||||
post_date TEXT NOT NULL DEFAULT '',
|
||||
hidden INTEGER NOT NULL DEFAULT 0,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL,
|
||||
CHECK (
|
||||
(parent_id IS NULL AND title <> '' AND post_date <> '')
|
||||
OR
|
||||
(parent_id IS NOT NULL AND title = '' AND city = '' AND post_date = '' AND hidden = 0)
|
||||
)
|
||||
)`},
|
||||
{"index post replies", `
|
||||
CREATE INDEX IF NOT EXISTS idx_posts_parent_created
|
||||
ON posts(parent_id, created_at, id)`},
|
||||
{"index root posts", `
|
||||
CREATE INDEX IF NOT EXISTS idx_posts_root_date
|
||||
ON posts(post_date, hidden)
|
||||
WHERE parent_id IS NULL`},
|
||||
{"create post votes", `
|
||||
CREATE TABLE IF NOT EXISTS post_votes (
|
||||
user_id TEXT NOT NULL REFERENCES users(id),
|
||||
post_id TEXT NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
|
||||
value INTEGER NOT NULL CHECK (value IN (-1, 1)),
|
||||
PRIMARY KEY (user_id, post_id)
|
||||
)`},
|
||||
{"copy questions", `
|
||||
INSERT INTO posts (
|
||||
id, parent_id, author_id, title, body, city, post_date, hidden, created_at, updated_at
|
||||
)
|
||||
SELECT
|
||||
id, NULL, author_id, title, body, city, hunt_date, hidden, created_at, created_at
|
||||
FROM questions
|
||||
ON CONFLICT (id) DO NOTHING`},
|
||||
{"copy answers", `
|
||||
INSERT INTO posts (
|
||||
id, parent_id, author_id, title, body, city, post_date, hidden, created_at, updated_at
|
||||
)
|
||||
SELECT
|
||||
'answer:' || question_id, question_id, author_id, '', body, '', '', 0, created_at, updated_at
|
||||
FROM answers
|
||||
ON CONFLICT (id) DO NOTHING`},
|
||||
{"copy votes", `
|
||||
INSERT INTO post_votes (user_id, post_id, value)
|
||||
SELECT user_id, question_id, value
|
||||
FROM votes
|
||||
ON CONFLICT (user_id, post_id) DO NOTHING`},
|
||||
}
|
||||
for _, step := range steps {
|
||||
if _, err := exec.ExecContext(ctx, step.sql); err != nil {
|
||||
return fmt.Errorf("%s: %w", step.name, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type execContext interface {
|
||||
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
|
||||
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
|
||||
@@ -81,6 +150,7 @@ CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
}},
|
||||
{"002_user_profile_columns", migrateUserProfileColumns},
|
||||
{"003_user_email", migrateUserEmail},
|
||||
{"004_posts", migratePosts},
|
||||
}
|
||||
for _, m := range migrations {
|
||||
if applied[m.version] {
|
||||
|
||||
Reference in New Issue
Block a user