Compare commits

...

9 Commits

Author SHA1 Message Date
David Shrewsberry fe77499e59
Merge 003bcc3a35 into 6bfccf8afa 2024-11-22 01:10:32 +08:00
Jinzhu 6bfccf8afa Refactor all tests script 2024-11-21 17:03:31 +08:00
Dave Shrewsberry 003bcc3a35 more tracing 2024-08-01 17:26:20 -04:00
Dave Shrewsberry 3198b7c219 more tracing 2024-08-01 17:23:39 -04:00
Dave Shrewsberry 38fb45fea8 debug 2024-08-01 17:01:58 -04:00
Dave Shrewsberry e483cd9993 put check back in drop 2024-08-01 16:31:37 -04:00
Dave Shrewsberry 79e73f0d7d move to the caller 2024-08-01 16:17:38 -04:00
Dave Shrewsberry e55b2ba312 if exists doesn't work for mysql, so have to verify constraint exists. 2024-08-01 14:32:27 -04:00
Dave Shrewsberry 92bc52270e use IF EXISTS 2024-08-01 13:45:11 -04:00
7 changed files with 34 additions and 22 deletions

2
go.mod
View File

@ -5,5 +5,5 @@ go 1.18
require (
github.com/jinzhu/inflection v1.0.0
github.com/jinzhu/now v1.1.5
golang.org/x/text v0.14.0
golang.org/x/text v0.20.0
)

4
go.sum
View File

@ -2,5 +2,5 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=

View File

@ -183,7 +183,6 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) {
if config.Dialector != nil {
err = config.Dialector.Initialize(db)
if err != nil {
if db, _ := db.DB(); db != nil {
_ = db.Close()

View File

@ -710,6 +710,7 @@ func (m Migrator) GuessConstraintInterfaceAndTable(stmt *gorm.Statement, name st
for _, rel := range stmt.Schema.Relationships.Relations {
if constraint := rel.ParseConstraint(); constraint != nil && constraint.Name == name {
fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for relation 0 %v", constraint))
return constraint, getTable(rel)
}
}
@ -718,6 +719,7 @@ func (m Migrator) GuessConstraintInterfaceAndTable(stmt *gorm.Statement, name st
for k := range checkConstraints {
if checkConstraints[k].Field == field {
v := checkConstraints[k]
fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for check %v", &v))
return &v, stmt.Table
}
}
@ -725,12 +727,14 @@ func (m Migrator) GuessConstraintInterfaceAndTable(stmt *gorm.Statement, name st
for k := range uniqueConstraints {
if uniqueConstraints[k].Field == field {
v := uniqueConstraints[k]
fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for unique %v", &v))
return &v, stmt.Table
}
}
for _, rel := range stmt.Schema.Relationships.Relations {
if constraint := rel.ParseConstraint(); constraint != nil && rel.Field == field {
fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for relation %v", constraint))
return constraint, getTable(rel)
}
}
@ -757,7 +761,11 @@ func (m Migrator) CreateConstraint(value interface{}, name string) error {
// DropConstraint drop constraint
func (m Migrator) DropConstraint(value interface{}, name string) error {
fmt.Println(fmt.Sprintf("SHREWS --In Drop %q", name))
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
if !m.HasConstraint(value, name) {
return nil
}
constraint, table := m.GuessConstraintInterfaceAndTable(stmt, name)
if constraint != nil {
name = constraint.GetName()

View File

@ -8,7 +8,7 @@ require (
github.com/lib/pq v1.10.9
github.com/stretchr/testify v1.9.0
gorm.io/driver/mysql v1.5.7
gorm.io/driver/postgres v1.5.9
gorm.io/driver/postgres v1.5.10
gorm.io/driver/sqlite v1.5.6
gorm.io/driver/sqlserver v1.5.4
gorm.io/gorm v1.25.12
@ -25,12 +25,12 @@ require (
github.com/jackc/pgx/v5 v5.7.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-sqlite3 v1.14.23 // indirect
github.com/mattn/go-sqlite3 v1.14.24 // indirect
github.com/microsoft/go-mssqldb v1.7.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/text v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View File

@ -723,6 +723,10 @@ func TestMigrateConstraint(t *testing.T) {
DB.Migrator().CreateConstraint(&User{}, name)
}
if !DB.Migrator().HasConstraint(&User{}, name) {
t.Fatalf("failed to create constraint %v", name)
}
if err := DB.Migrator().DropConstraint(&User{}, name); err != nil {
t.Fatalf("failed to drop constraint %v, got error %v", name, err)
}

View File

@ -16,21 +16,22 @@ then
fi
# SqlServer for Mac M1
if [[ -z $GITHUB_ACTION ]]; then
if [ -d tests ]
then
cd tests
if [[ $(uname -a) == *" arm64" ]]; then
MSSQL_IMAGE=mcr.microsoft.com/azure-sql-edge docker compose up -d || true
go install github.com/microsoft/go-sqlcmd/cmd/sqlcmd@latest || true
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF DB_ID('gorm') IS NULL CREATE DATABASE gorm" > /dev/null || true
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF SUSER_ID (N'gorm') IS NULL CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';" > /dev/null || true
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF USER_ID (N'gorm') IS NULL CREATE USER gorm FROM LOGIN gorm; ALTER SERVER ROLE sysadmin ADD MEMBER [gorm];" > /dev/null || true
else
MSSQL_IMAGE=mcr.microsoft.com/mssql/server docker compose up -d
fi
cd ..
if [[ -z $GITHUB_ACTION && -d tests ]]; then
cd tests
if [[ $(uname -a) == *" arm64" ]]; then
MSSQL_IMAGE=mcr.microsoft.com/azure-sql-edge docker compose up -d --wait
go install github.com/microsoft/go-sqlcmd/cmd/sqlcmd@latest || true
for query in \
"IF DB_ID('gorm') IS NULL CREATE DATABASE gorm" \
"IF SUSER_ID (N'gorm') IS NULL CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';" \
"IF USER_ID (N'gorm') IS NULL CREATE USER gorm FROM LOGIN gorm; ALTER SERVER ROLE sysadmin ADD MEMBER [gorm];"
do
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "$query" > /dev/null || true
done
else
MSSQL_IMAGE=mcr.microsoft.com/mssql/server docker compose up -d --wait
fi
cd ..
fi