Skip to content

Commit fb03bc0

Browse files
committed
feat(seeder): add truncate before seeding for clean state
Add TRUNCATE TABLE statements with RESTART IDENTITY CASCADE to all seeder SQL files to ensure tables are emptied and primary keys reset before inserting seed data. This guarantees a clean database state for each migration:fresh run. Also update registration_events seeder to include "id" in insert columns and values for consistency.
1 parent 0e36789 commit fb03bc0

File tree

7 files changed

+48
-5
lines changed

7 files changed

+48
-5
lines changed

app/blog_post/delivery/http/http.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,42 @@ func (h Handler) GetAllBlogPosts(w http.ResponseWriter, r *http.Request) {
112112
return
113113
}
114114

115+
type response struct {
116+
Id int `json:"id" gorm:"primaryKey"`
117+
Title string `json:"title"`
118+
Excerpt string `json:"excerpt"`
119+
Author domain.Author `json:"author" gorm:"foreignKey:AuthorID;references:UserId"`
120+
AuthorID int `json:"author_id" gorm:"column:author_id"`
121+
PublishedAt time.Time `json:"published_at"`
122+
UpdatedAt time.Time `json:"updated_at"`
123+
Tags []string `json:"tags" gorm:"-"`
124+
Category string `json:"category"`
125+
Status string `json:"status" gorm:"type:enum('draft', 'published', 'archived')"`
126+
Slug string `json:"slug"`
127+
}
128+
129+
responseDTO := []response{}
130+
for _, post := range data {
131+
resp := response{
132+
Id: post.Id,
133+
Title: post.Title,
134+
Excerpt: post.Excerpt,
135+
Author: post.Author,
136+
AuthorID: post.AuthorID,
137+
PublishedAt: post.PublishedAt,
138+
UpdatedAt: post.PublishedAt,
139+
Tags: post.Tags,
140+
Category: post.Category,
141+
Status: post.Status,
142+
Slug: post.Slug,
143+
}
144+
responseDTO = append(responseDTO, resp)
145+
}
146+
115147
utils.Response(domain.HttpResponse{
116148
Code: http.StatusOK,
117149
Message: "Blog posts retrieved successfully",
118-
Data: data,
150+
Data: responseDTO,
119151
Pagination: paginationResponse,
120152
}, w)
121153
}

database/seeder/20250522114625_seed_users.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
-- +goose Up
22
-- +goose StatementBegin
3+
4+
TRUNCATE TABLE "public"."users" RESTART IDENTITY CASCADE;
5+
36
INSERT INTO "public"."users" (
47
"username", "email", "password", "role", "fullname",
58
"date_of_birth", "gender", "phone_number", "address",

database/seeder/20250522114645_seed_events.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
-- +goose Up
22
-- +goose StatementBegin
3+
TRUNCATE TABLE "public"."events" RESTART IDENTITY CASCADE;
4+
35
INSERT INTO "public"."events" (
46
"id", "title", "description", "author", "image", "date",
57
"reservation_start_date", "reservation_end_date", "type",

database/seeder/20250522114656_seed_images.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
-- +goose Up
22
-- +goose StatementBegin
3+
Truncate Table "public"."images" Restart Identity Cascade;
4+
35
INSERT INTO "public"."images" (
46
"file_name", "file_path", "format", "content_type", "is_used", "file_size"
57
) VALUES

database/seeder/20250522114706_seed_tags.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
-- +goose Up
22
-- +goose StatementBegin
3+
Truncate Table "public"."event_tags" Restart Identity Cascade;
34
INSERT INTO "public"."event_tags" (
45
"event_id", "tag"
56
) VALUES

database/seeder/20250522114720_seed_registration_events.sql

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
-- +goose Up
22
-- +goose StatementBegin
3+
34
INSERT INTO "public"."registration_events" (
4-
"order_no", "event_id", "user_id", "name", "email", "phone_number",
5+
"id", "order_no", "event_id", "user_id", "name", "email", "phone_number",
56
"image_proof_payment", "payment_date", "status", "up_to_you", "created_by_user_id"
67
) VALUES
7-
('ORD-001', 1, '2', 'John Doe', '[email protected]', '987654321',
8+
(1, 'ORD-001', 1, '2', 'John Doe', '[email protected]', '987654321',
89
'payment_proof1.jpg', '2025-06-01 14:30:00', 'confirmed', 'Looking forward to it!', 2),
910

10-
('ORD-002', 2, '3', 'Jane Doe', '[email protected]', '555123456',
11+
(2, 'ORD-002', 2, '3', 'Jane Doe', '[email protected]', '555123456',
1112
'payment_proof2.jpg', '2025-07-05 10:15:00', 'confirmed', 'Excited to learn!', 3),
1213

13-
('ORD-003', 3, '2', 'John Doe', '[email protected]', '987654321',
14+
(3, 'ORD-003', 3, '2', 'John Doe', '[email protected]', '987654321',
1415
NULL, NULL, 'pending', 'Will bring my team', 2);
1516
-- +goose StatementEnd
1617

database/seeder/20250522114740_seed_event_pays.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
-- +goose Up
22
-- +goose StatementBegin
3+
TRUNCATE TABLE "public"."event_pays" RESTART IDENTITY CASCADE;
4+
35
INSERT INTO "public"."event_pays" (
46
"order_no", "status", "registration_event_id", "event_id",
57
"image_proof_payment", "net_amount"

0 commit comments

Comments
 (0)