Skip to content

Commit 274b906

Browse files
committed
feat(migration): add created_at and is_deleted columns
Update blog_posts table migration to add created_at and is_deleted columns safely if they do not exist. Adjust down migration to drop only these columns. Update events seeder to reset events_id_seq after inserts.
1 parent b3bc4d8 commit 274b906

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
-- +goose Up
22
-- +goose StatementBegin
3-
SELECT 'up SQL query';
4-
5-
ALTER TABLE blog_posts
6-
ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
7-
ADD COLUMN is_deleted BOOLEAN DEFAULT FALSE;
8-
9-
10-
3+
DO $$
4+
BEGIN
5+
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='blog_posts' AND column_name='created_at') THEN
6+
ALTER TABLE blog_posts ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
7+
END IF;
8+
9+
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='blog_posts' AND column_name='is_deleted') THEN
10+
ALTER TABLE blog_posts ADD COLUMN is_deleted BOOLEAN DEFAULT FALSE;
11+
END IF;
12+
END $$;
1113
-- +goose StatementEnd
1214

1315
-- +goose Down
14-
-- +goose StatementBegin
15-
DROP TABLE IF EXISTS blog_posts CASCADE;
16-
-- +goose StatementEnd
16+
ALTER TABLE blog_posts DROP COLUMN IF EXISTS created_at;
17+
ALTER TABLE blog_posts DROP COLUMN IF EXISTS is_deleted;

database/seeder/20250522114645_seed_events.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ INSERT INTO "public"."events" (
2525
'2025-07-01 00:00:00', '2025-08-05 23:59:59', 'hackathon',
2626
'Innovation Labs', '48 hours', 'upcoming', 50.00, 100,
2727
'https://example.com/register/app-hackathon', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
28+
29+
SELECT setval('events_id_seq', (SELECT MAX(id) FROM events), true);
2830
-- +goose StatementEnd
2931

3032
-- +goose Down

0 commit comments

Comments
 (0)