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

fix: word check on migration error #1013

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
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
Loading