diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 58ed593..6d1627e 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -34,8 +34,9 @@ jobs: - name: Download run: go mod download - - name: Verify - run: go mod verify + # Fixed in go 1.21: https://go.dev/issue/54372 + # - name: Verify + # run: go mod verify - name: Vet run: go vet ./... diff --git a/go.work b/go.work new file mode 100644 index 0000000..0aa07f2 --- /dev/null +++ b/go.work @@ -0,0 +1,6 @@ +go 1.19 + +use ( + . + ./gormlite +) diff --git a/gormlite/README.md b/gormlite/README.md index 94c01b7..62ce8ea 100644 --- a/gormlite/README.md +++ b/gormlite/README.md @@ -1,5 +1,7 @@ # GORM SQLite Driver +[![Go Reference](https://pkg.go.dev/badge/image)](https://pkg.go.dev/github.com/ncruces/go-sqlite3/gormlite) + ## Usage ```go diff --git a/gormlite/ddlmod.go b/gormlite/ddlmod.go index 3e35b45..69cd179 100644 --- a/gormlite/ddlmod.go +++ b/gormlite/ddlmod.go @@ -162,7 +162,7 @@ func parseDDL(strs ...string) (*ddl, error) { for _, column := range getAllColumns(matches[1]) { for idx, c := range result.columns { if c.NameValue.String == column { - c.UniqueValue = sql.NullBool{Bool: true, Valid: true} + c.UniqueValue = sql.NullBool{Bool: strings.ToUpper(strings.Fields(str)[1]) == "UNIQUE", Valid: true} result.columns[idx] = c } } diff --git a/gormlite/ddlmod_test.go b/gormlite/ddlmod_test.go index 62b3763..059adc1 100644 --- a/gormlite/ddlmod_test.go +++ b/gormlite/ddlmod_test.go @@ -79,6 +79,24 @@ func TestParseDDL(t *testing.T) { }, }, }, + { + "non-unique index", + []string{ + "CREATE TABLE `test-c` (`field` integer NOT NULL)", + "CREATE INDEX `idx_uq` ON `test-b`(`field`) WHERE field = 0", + }, + 1, + []migrator.ColumnType{ + { + NameValue: sql.NullString{String: "field", Valid: true}, + DataTypeValue: sql.NullString{String: "integer", Valid: true}, + ColumnTypeValue: sql.NullString{String: "integer", Valid: true}, + PrimaryKeyValue: sql.NullBool{Bool: false, Valid: true}, + UniqueValue: sql.NullBool{Bool: false, Valid: true}, + NullableValue: sql.NullBool{Bool: false, Valid: true}, + }, + }, + }, } for _, p := range params { diff --git a/gormlite/download.sh b/gormlite/download.sh new file mode 100755 index 0000000..fcf9a70 --- /dev/null +++ b/gormlite/download.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd -P -- "$(dirname -- "$0")" + +curl -#OL "https://github.com/go-gorm/sqlite/raw/master/ddlmod.go" +curl -#OL "https://github.com/go-gorm/sqlite/raw/master/ddlmod_test.go" +curl -#OL "https://github.com/go-gorm/sqlite/raw/master/error_translator.go" +curl -#OL "https://github.com/go-gorm/sqlite/raw/master/migrator.go" +curl -#OL "https://github.com/go-gorm/sqlite/raw/master/sqlite.go" +curl -#OL "https://github.com/go-gorm/sqlite/raw/master/sqlite_test.go" \ No newline at end of file diff --git a/gormlite/error_translator.go b/gormlite/error_translator.go index 46681eb..c807a6e 100644 --- a/gormlite/error_translator.go +++ b/gormlite/error_translator.go @@ -8,8 +8,14 @@ import ( ) func (dialector Dialector) Translate(err error) error { - if errors.Is(err, sqlite3.CONSTRAINT_UNIQUE) { + switch { + case + errors.Is(err, sqlite3.CONSTRAINT_UNIQUE), + errors.Is(err, sqlite3.CONSTRAINT_PRIMARYKEY): return gorm.ErrDuplicatedKey + case + errors.Is(err, sqlite3.CONSTRAINT_FOREIGNKEY): + return err // gorm.ErrForeignKeyViolated (gorm v1.25.2) } return err } diff --git a/gormlite/errors.go b/gormlite/errors.go deleted file mode 100644 index 2032658..0000000 --- a/gormlite/errors.go +++ /dev/null @@ -1,7 +0,0 @@ -package gormlite - -import "errors" - -var ( - ErrConstraintsNotImplemented = errors.New("constraints not implemented on sqlite, consider using DisableForeignKeyConstraintWhenMigrating, more details https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft#all-new-migrator") -) diff --git a/gormlite/migrator.go b/gormlite/migrator.go index 705ceb7..9e2c4c9 100644 --- a/gormlite/migrator.go +++ b/gormlite/migrator.go @@ -322,6 +322,9 @@ func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error var sql string m.DB.Raw("SELECT sql FROM sqlite_master WHERE type = ? AND tbl_name = ? AND name = ?", "index", stmt.Table, oldName).Row().Scan(&sql) if sql != "" { + if err := m.DropIndex(value, oldName); err != nil { + return err + } return m.DB.Exec(strings.Replace(sql, oldName, newName, 1)).Error } return fmt.Errorf("failed to find index with name %v", oldName)