This commit is contained in:
+20
-28
@@ -35,13 +35,11 @@ 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.
|
||||
// migratePosts creates the unified post model.
|
||||
func migratePosts(ctx context.Context, exec execContext) error {
|
||||
steps := []struct {
|
||||
name string
|
||||
sql string
|
||||
args []any
|
||||
}{
|
||||
{name: "create posts", sql: `
|
||||
CREATE TABLE IF NOT EXISTS posts (
|
||||
@@ -78,33 +76,9 @@ CREATE TABLE IF NOT EXISTS post_votes (
|
||||
value INTEGER NOT NULL CHECK (value IN (-1, 1)),
|
||||
PRIMARY KEY (user_id, post_id)
|
||||
)`},
|
||||
{name: "copy questions", sql: `
|
||||
INSERT INTO posts (
|
||||
id, parent_id, author_id, title, body, city, post_date, post_state, created_at, updated_at
|
||||
)
|
||||
SELECT
|
||||
id, NULL, author_id, title, body, city, hunt_date,
|
||||
CASE WHEN hidden = 0 THEN $1 ELSE $2 END,
|
||||
created_at, created_at
|
||||
FROM questions
|
||||
ON CONFLICT (id) DO NOTHING`, args: []any{string(PostStateVisible), string(PostStateHidden)}},
|
||||
{name: "copy answers", sql: `
|
||||
INSERT INTO posts (
|
||||
id, parent_id, author_id, title, body, city, post_date, post_state, created_at, updated_at
|
||||
)
|
||||
SELECT
|
||||
'answer:' || question_id, question_id, author_id, '', body, '', '',
|
||||
$1, created_at, updated_at
|
||||
FROM answers
|
||||
ON CONFLICT (id) DO NOTHING`, args: []any{string(PostStateVisible)}},
|
||||
{name: "copy votes", sql: `
|
||||
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, step.args...); err != nil {
|
||||
if _, err := exec.ExecContext(ctx, step.sql); err != nil {
|
||||
return fmt.Errorf("%s: %w", step.name, err)
|
||||
}
|
||||
}
|
||||
@@ -129,6 +103,23 @@ CREATE INDEX IF NOT EXISTS idx_posts_author_created
|
||||
return nil
|
||||
}
|
||||
|
||||
func migrateDropLegacyPostTables(ctx context.Context, exec execContext) error {
|
||||
steps := []struct {
|
||||
name string
|
||||
sql string
|
||||
}{
|
||||
{"drop legacy answers", `DROP TABLE IF EXISTS answers`},
|
||||
{"drop legacy votes", `DROP TABLE IF EXISTS votes`},
|
||||
{"drop legacy questions", `DROP TABLE IF EXISTS questions`},
|
||||
}
|
||||
for _, step := range steps {
|
||||
if _, err := exec.ExecContext(ctx, step.sql); err != nil {
|
||||
return fmt.Errorf("%s: %w", step.name, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func migratePostDate(ctx context.Context, exec execContext) error {
|
||||
steps := []struct {
|
||||
name string
|
||||
@@ -319,6 +310,7 @@ CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
{"006_post_date", migratePostDate},
|
||||
{"007_post_state", migratePostState},
|
||||
{"008_post_author_index", migratePostAuthorIndex},
|
||||
{"009_drop_legacy_post_tables", migrateDropLegacyPostTables},
|
||||
}
|
||||
for _, m := range migrations {
|
||||
if applied[m.version] {
|
||||
|
||||
Reference in New Issue
Block a user