Skip to content

Commit

Permalink
fix: word check on migration error
Browse files Browse the repository at this point in the history
  • Loading branch information
fmartingr committed Nov 16, 2024
1 parent 6b6d5f3 commit 67d9a76
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
10 changes: 10 additions & 0 deletions internal/database/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ import (
"embed"
"fmt"
"path"
"regexp"
"strings"

"github.com/blang/semver"
)

// compareWordsInString is a helper function to compare words in two strings without special characters.
func compareWordsInString(input, expected string) bool {
re := regexp.MustCompile(`\b\w+\b`)
cleaned := strings.Join(re.FindAllString(input, -1), " ")
fmt.Println(cleaned)
return strings.Contains(expected, cleaned)
}

//go:embed migrations/*
var migrationFiles embed.FS

Expand Down
92 changes: 92 additions & 0 deletions internal/database/migrations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package database

import "testing"

func TestCompareWordsInString(t *testing.T) {
tests := []struct {
name string
input string
expected string
want bool
}{
{
name: "equal",
input: "hello world",
expected: "hello world",
want: true,
},
{
name: "not equal",
input: "hello world",
expected: "hello",
want: false,
},
{
name: "equal with special characters",
input: "hello, world",
expected: "hello world",
want: true,
},
{
name: "not equal with special characters",
input: "hello, world",
expected: "hello",
want: false,
},
{
name: "equal with special characters and spaces",
input: "hello, world",
expected: "hello world",
want: true,
},
{
name: "not equal with special characters and spaces",
input: "hello, world",
expected: "hello",
want: false,
},
{
name: "equal with special characters and spaces",
input: "hello, world",
expected: "hello world",
want: true,
},
{
name: "not equal with special characters and spaces",
input: "hello, 'world'",
expected: "hello",
want: false,
},
{
name: "equal with special characters and spaces",
input: `hello, "world"`,
expected: "hello world",
want: true,
},
{
name: "not equal with special characters and spaces",
input: "hello, world",
expected: "hello",
want: false,
},
{
name: "migration string",
input: `pq: column "has_content" of relation "bookmark" already exists`,
expected: "pq column has_content of relation bookmark already exists",
want: true,
},
{
name: "migration string",
input: `pq: column »has_content« of relation »bookmark« already exists`,
expected: "pq column has_content of relation bookmark already exists",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := compareWordsInString(tt.input, tt.expected); got != tt.want {
t.Errorf("compareWordsInString() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 2 additions & 2 deletions internal/database/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var postgresMigrations = []migration{
defer tx.Rollback()

_, err = tx.Exec(`ALTER TABLE bookmark ADD COLUMN has_content BOOLEAN DEFAULT FALSE NOT NULL`)
if err != nil && strings.Contains(err.Error(), `column "has_content" of relation "bookmark" already exists`) {
if err != nil && compareWordsInString(err.Error(), `pq column has_content of relation bookmark already exists`) {
tx.Rollback()
} else if err != nil {
return fmt.Errorf("failed to add has_content column to bookmark table: %w", err)
Expand All @@ -45,7 +45,7 @@ var postgresMigrations = []migration{
defer tx.Rollback()

_, err = tx.Exec(`ALTER TABLE account ADD COLUMN config JSONB NOT NULL DEFAULT '{}'`)
if err != nil && strings.Contains(err.Error(), `column "config" of relation "account" already exists`) {
if err != nil && compareWordsInString(err.Error(), `pq column config of relation account already exists`) {
tx.Rollback()
} else if err != nil {
return fmt.Errorf("failed to add config column to account table: %w", err)
Expand Down

0 comments on commit 67d9a76

Please sign in to comment.