Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

코드 포맷합니다. #98

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ format\:install:
# https://github.,com/segmentio/golines
go install github.com/segmentio/golines@latest
format:
gofumpt -l -w .
golines -w .
gofumpt -l -w .

lint\:install:
# golangci-lint
Expand Down
7 changes: 6 additions & 1 deletion api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ func (l *PaginatedView[T]) CalcLastPage() {
}
}

func writePayload(w http.ResponseWriter, headers map[string]string, payload interface{}, statusCode int) error {
func writePayload(
w http.ResponseWriter,
headers map[string]string,
payload interface{},
statusCode int,
) error {
setHeaders(w, headers)

w.WriteHeader(statusCode)
Expand Down
17 changes: 13 additions & 4 deletions api/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ func ParseOptionalUUIDQuery(c echo.Context, query string) (uuid.NullUUID, *AppEr

id, err := uuid.Parse(queryStr)
if err != nil {
return uuid.NullUUID{}, ErrInvalidQuery(fmt.Errorf("expected valid UUID for query: %s", query))
return uuid.NullUUID{}, ErrInvalidQuery(
fmt.Errorf("expected valid UUID for query: %s", query),
)
}

return uuid.NullUUID{UUID: id, Valid: true}, nil
Expand Down Expand Up @@ -79,7 +81,10 @@ func ParseOptionalStringQuery(c echo.Context, query string) *string {
}

// ParsePaginationQueries parses pagination parameters from query string: page, size.
func ParsePaginationQueries(c echo.Context, defaultPage, defaultLimit int) (page, size int, err *AppError) {
func ParsePaginationQueries(
c echo.Context,
defaultPage, defaultLimit int,
) (page, size int, err *AppError) {
pageQuery := c.QueryParam("page")
sizeQuery := c.QueryParam("size")

Expand All @@ -90,15 +95,19 @@ func ParsePaginationQueries(c echo.Context, defaultPage, defaultLimit int) (page
var atoiError error
page, atoiError = strconv.Atoi(pageQuery)
if atoiError != nil || page <= 0 {
return 0, 0, ErrInvalidPagination(errors.New("expected integer value bigger than 0 for query: page"))
return 0, 0, ErrInvalidPagination(
errors.New("expected integer value bigger than 0 for query: page"),
)
}
}

if sizeQuery != "" {
var atoiError error
size, atoiError = strconv.Atoi(sizeQuery)
if atoiError != nil || size <= 0 {
return 0, 0, ErrInvalidPagination(errors.New("expected integer value bigger than 0 for query: size"))
return 0, 0, ErrInvalidPagination(
errors.New("expected integer value bigger than 0 for query: size"),
)
}
}

Expand Down
13 changes: 11 additions & 2 deletions cmd/import_breeds/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,20 @@ func importBreed(
}, nil
}

func importBreeds(ctx context.Context, conn *database.DB, petType commonvo.PetType, rows *[]breedsimporterservice.Row) {
func importBreeds(
ctx context.Context,
conn *database.DB,
petType commonvo.PetType,
rows *[]breedsimporterservice.Row,
) {
for _, row := range *rows {
breedData, err := importBreed(ctx, conn, petType, row)
if err != nil {
log.Printf("Failed to import breed with pet_type: %s, name: %s to database", breedData.PetType, breedData.Name)
log.Printf(
"Failed to import breed with pet_type: %s, name: %s to database",
breedData.PetType,
breedData.Name,
)
}
}
}
183 changes: 156 additions & 27 deletions cmd/migrateuuids/uuidmigrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,55 +35,135 @@ type FK struct {

var Targets = []Target{
{"users", "uuid", []FK{
{Column: "profile_image_id", UUIDColumn: "profile_image_uuid", ReferencedTable: "media", ReferencedColumn: "uuid"},
{
Column: "profile_image_id",
UUIDColumn: "profile_image_uuid",
ReferencedTable: "media",
ReferencedColumn: "uuid",
},
}},
{"media", "uuid", []FK{}},
{"breeds", "uuid", []FK{}},
{"pets", "uuid", []FK{
{Column: "owner_id", UUIDColumn: "owner_uuid", ReferencedTable: "users", ReferencedColumn: "uuid"},
{Column: "profile_image_id", UUIDColumn: "profile_image_uuid", ReferencedTable: "media", ReferencedColumn: "uuid"},
{
Column: "owner_id",
UUIDColumn: "owner_uuid",
ReferencedTable: "users",
ReferencedColumn: "uuid",
},
{
Column: "profile_image_id",
UUIDColumn: "profile_image_uuid",
ReferencedTable: "media",
ReferencedColumn: "uuid",
},
}},
{"base_posts", "uuid", []FK{
{Column: "author_id", UUIDColumn: "author_uuid", ReferencedTable: "users", ReferencedColumn: "uuid"},
{
Column: "author_id",
UUIDColumn: "author_uuid",
ReferencedTable: "users",
ReferencedColumn: "uuid",
},
}},
{"sos_posts", "uuid", []FK{
{Column: "thumbnail_id", UUIDColumn: "thumbnail_uuid", ReferencedTable: "media", ReferencedColumn: "uuid"},
{
Column: "thumbnail_id",
UUIDColumn: "thumbnail_uuid",
ReferencedTable: "media",
ReferencedColumn: "uuid",
},
}},
{"sos_dates", "uuid", []FK{}},
{"sos_posts_dates", "uuid", []FK{
{Column: "sos_post_id", UUIDColumn: "sos_post_uuid", ReferencedTable: "sos_posts", ReferencedColumn: "uuid"},
{Column: "sos_dates_id", UUIDColumn: "sos_dates_uuid", ReferencedTable: "sos_dates", ReferencedColumn: "uuid"},
{
Column: "sos_post_id",
UUIDColumn: "sos_post_uuid",
ReferencedTable: "sos_posts",
ReferencedColumn: "uuid",
},
{
Column: "sos_dates_id",
UUIDColumn: "sos_dates_uuid",
ReferencedTable: "sos_dates",
ReferencedColumn: "uuid",
},
}},
{"sos_conditions", "uuid", []FK{}},
{"sos_posts_conditions", "uuid", []FK{
{Column: "sos_post_id", UUIDColumn: "sos_post_uuid", ReferencedTable: "sos_posts", ReferencedColumn: "uuid"},
{
Column: "sos_post_id",
UUIDColumn: "sos_post_uuid",
ReferencedTable: "sos_posts",
ReferencedColumn: "uuid",
},
{
Column: "sos_condition_id", UUIDColumn: "sos_condition_uuid", ReferencedTable: "sos_conditions",
ReferencedColumn: "uuid",
},
}},
{"sos_posts_pets", "uuid", []FK{
{Column: "sos_post_id", UUIDColumn: "sos_post_uuid", ReferencedTable: "sos_posts", ReferencedColumn: "uuid"},
{Column: "pet_id", UUIDColumn: "pet_uuid", ReferencedTable: "pets", ReferencedColumn: "uuid"},
{
Column: "sos_post_id",
UUIDColumn: "sos_post_uuid",
ReferencedTable: "sos_posts",
ReferencedColumn: "uuid",
},
{
Column: "pet_id",
UUIDColumn: "pet_uuid",
ReferencedTable: "pets",
ReferencedColumn: "uuid",
},
}},
{
"resource_media", "uuid", []FK{
{Column: "media_id", UUIDColumn: "media_uuid", ReferencedTable: "media", ReferencedColumn: "uuid"},
{
Column: "media_id",
UUIDColumn: "media_uuid",
ReferencedTable: "media",
ReferencedColumn: "uuid",
},
// Conditional FK -- Thankfully, we only have SOS post for resource
{Column: "resource_id", UUIDColumn: "resource_uuid", ReferencedTable: "sos_posts", ReferencedColumn: "uuid"},
{
Column: "resource_id",
UUIDColumn: "resource_uuid",
ReferencedTable: "sos_posts",
ReferencedColumn: "uuid",
},
},
},
{"chat_rooms", "uuid", []FK{}},
{
"chat_messages", "uuid", []FK{
{Column: "room_id", UUIDColumn: "room_uuid", ReferencedTable: "chat_rooms", ReferencedColumn: "uuid"},
{Column: "user_id", UUIDColumn: "user_uuid", ReferencedTable: "users", ReferencedColumn: "uuid"},
{
Column: "room_id",
UUIDColumn: "room_uuid",
ReferencedTable: "chat_rooms",
ReferencedColumn: "uuid",
},
{
Column: "user_id",
UUIDColumn: "user_uuid",
ReferencedTable: "users",
ReferencedColumn: "uuid",
},
},
},
{
"user_chat_rooms", "uuid", []FK{
{Column: "room_id", UUIDColumn: "room_uuid", ReferencedTable: "chat_rooms", ReferencedColumn: "uuid"},
{Column: "user_id", UUIDColumn: "user_uuid", ReferencedTable: "users", ReferencedColumn: "uuid"},
{
Column: "room_id",
UUIDColumn: "room_uuid",
ReferencedTable: "chat_rooms",
ReferencedColumn: "uuid",
},
{
Column: "user_id",
UUIDColumn: "user_uuid",
ReferencedTable: "users",
ReferencedColumn: "uuid",
},
},
},
}
Expand Down Expand Up @@ -146,7 +226,9 @@ func MigrateUUID(tx *database.Tx, options MigrateOptions) *pnd.AppError {
var count int
err := tx.QueryRow("SELECT COUNT(*) FROM " + target.Table).Scan(&count)
if err != nil {
return pnd.ErrUnknown(fmt.Errorf("error counting rows from table %s due to %w", target.Table, err))
return pnd.ErrUnknown(
fmt.Errorf("error counting rows from table %s due to %w", target.Table, err),
)
}
log.Printf(" - Total rows: %d\n", count)
total += count
Expand All @@ -169,7 +251,12 @@ func MigrateUUID(tx *database.Tx, options MigrateOptions) *pnd.AppError {
err = rows.Scan(&row.ID, &row.UUID)
if err != nil {
return pnd.ErrUnknown(
fmt.Errorf("error scanning row from table %s with id %d due to %w", target.Table, row.ID, err),
fmt.Errorf(
"error scanning row from table %s with id %d due to %w",
target.Table,
row.ID,
err,
),
)
}
rowData = append(rowData, row)
Expand All @@ -194,10 +281,19 @@ func MigrateUUID(tx *database.Tx, options MigrateOptions) *pnd.AppError {
}

if !options.ReadOnly {
_, err = tx.Exec("UPDATE "+target.Table+" SET "+target.UUIDColumn+" = $1 WHERE id = $2", newUUID, row.ID)
_, err = tx.Exec(
"UPDATE "+target.Table+" SET "+target.UUIDColumn+" = $1 WHERE id = $2",
newUUID,
row.ID,
)
if err != nil {
return pnd.ErrUnknown(
fmt.Errorf("error updating UUID column in table %s with id %d due to %w", target.Table, row.ID, err),
fmt.Errorf(
"error updating UUID column in table %s with id %d due to %w",
target.Table,
row.ID,
err,
),
)
}
log.Printf(" - Updated row %d with UUID %s\n", row.ID, newUUID)
Expand Down Expand Up @@ -241,7 +337,12 @@ func MigrateFK(tx *database.Tx, options MigrateOptions) *pnd.AppError {
err = rows.Scan(&row.ID, &fkValue)
if err != nil {
return pnd.ErrUnknown(
fmt.Errorf("error scanning row from table %s with id %d due to %w", target.Table, row.ID, err),
fmt.Errorf(
"error scanning row from table %s with id %d due to %w",
target.Table,
row.ID,
err,
),
)
}

Expand All @@ -255,7 +356,11 @@ func MigrateFK(tx *database.Tx, options MigrateOptions) *pnd.AppError {
for _, row := range rowData {
// Skip if FK is not set
if row.FKValue == nil {
log.Printf(" - Skipping row %d with FK %s due to null value\n", row.ID, fk.Column)
log.Printf(
" - Skipping row %d with FK %s due to null value\n",
row.ID,
fk.Column,
)
skipped++
continue
}
Expand All @@ -266,31 +371,55 @@ func MigrateFK(tx *database.Tx, options MigrateOptions) *pnd.AppError {
row.FKValue).Scan(&uuidValue)
if err != nil && err.Error() != "sql: no rows in result set" {
return pnd.ErrUnknown(
fmt.Errorf("error selecting UUID from table %s with id %d due to %w", fk.ReferencedTable, row.FKValue, err),
fmt.Errorf(
"error selecting UUID from table %s with id %d due to %w",
fk.ReferencedTable,
row.FKValue,
err,
),
)
}

// Skip if UUID is not set
if uuidValue == nil {
// Perhaps you forgot to run MigrateUUID() before MigrateFK()
log.Printf(" - Skipping row %d with FK %s due to missing UUID\n", row.ID, fk.Column)
log.Printf(
" - Skipping row %d with FK %s due to missing UUID\n",
row.ID,
fk.Column,
)
skipped++
continue
}

if !options.ReadOnly {
_, err = tx.Exec("UPDATE "+target.Table+" SET "+fk.UUIDColumn+" = $1 WHERE id = $2", *uuidValue, row.ID)
_, err = tx.Exec(
"UPDATE "+target.Table+" SET "+fk.UUIDColumn+" = $1 WHERE id = $2",
*uuidValue,
row.ID,
)
if err != nil {
return pnd.ErrUnknown(
fmt.Errorf("error updating UUID column in table %s with id %d due to %w", target.Table, row.ID, err),
fmt.Errorf(
"error updating UUID column in table %s with id %d due to %w",
target.Table,
row.ID,
err,
),
)
}
}

succeeded++
}

log.Printf(" - FK %s, Total rows: %d, Succeeded: %d, Skipped: %d\n", fk.Column, total, succeeded, skipped)
log.Printf(
" - FK %s, Total rows: %d, Succeeded: %d, Skipped: %d\n",
fk.Column,
total,
succeeded,
skipped,
)
}
}

Expand Down
Loading