1010 SELECT 1 FROM information_schema .columns
1111 WHERE table_name= ' events' AND column_name= ' author_id'
1212 ) THEN
13- -- Rename and change type to INT if needed
14- ALTER TABLE events RENAME COLUMN author TO author_id;
15- ALTER TABLE events ALTER COLUMN author_id TYPE INT USING (author_id::integer );
13+ -- Add the new author_id column first
14+ ALTER TABLE events ADD COLUMN author_id INT ;
15+
16+ -- You'll need to populate author_id based on your business logic
17+ -- For example, if you have a users table:
18+ -- UPDATE events SET author_id = users.id FROM users WHERE events.author = users.name;
19+
20+ -- Or set a default value for now:
21+ -- UPDATE events SET author_id = 1 WHERE author_id IS NULL;
22+
23+ -- Drop the old author column after migration
24+ -- ALTER TABLE events DROP COLUMN author;
25+
1626 ELSIF NOT EXISTS (
1727 SELECT 1 FROM information_schema .columns
1828 WHERE table_name= ' events' AND column_name= ' author_id'
@@ -33,6 +43,21 @@ END $$;
3343
3444-- +goose Down
3545-- +goose StatementBegin
36- ALTER TABLE events RENAME COLUMN author_id TO author;
37- ALTER TABLE events DROP COLUMN IF EXISTS session_type;
38- -- +goose StatementEnd
46+ DO $$
47+ BEGIN
48+ -- Add back the author column if it doesn't exist
49+ IF NOT EXISTS (
50+ SELECT 1 FROM information_schema .columns
51+ WHERE table_name= ' events' AND column_name= ' author'
52+ ) THEN
53+ ALTER TABLE events ADD COLUMN author TEXT ;
54+
55+ -- You might want to populate it from a users table:
56+ -- UPDATE events SET author = users.name FROM users WHERE events.author_id = users.id;
57+ END IF;
58+
59+ -- Drop the columns we added
60+ ALTER TABLE events DROP COLUMN IF EXISTS author_id;
61+ ALTER TABLE events DROP COLUMN IF EXISTS session_type;
62+ END $$;
63+ -- +goose StatementEnd
0 commit comments