[#7689] fixed indexes update collection error
This commit is contained in:
@@ -348,6 +348,7 @@ func (app *BaseApp) registerCollectionHooks() {
|
||||
}
|
||||
|
||||
// @todo experiment eventually replacing the rules *string with a struct?
|
||||
// @todo consider changing the Indexes field to a "getter" for the sqlite_master table?
|
||||
type baseCollection struct {
|
||||
BaseModel
|
||||
|
||||
@@ -820,6 +821,25 @@ func onCollectionSave(e *CollectionEvent) error {
|
||||
|
||||
e.Collection.updateGeneratedIdIfExists(e.App)
|
||||
|
||||
// normalize indexes table name
|
||||
for i, raw := range e.Collection.Indexes {
|
||||
parsed := dbutils.ParseIndex(raw)
|
||||
|
||||
// no need to normalize
|
||||
if parsed.TableName == e.Collection.Name {
|
||||
continue
|
||||
}
|
||||
|
||||
parsed.TableName = e.Collection.Name
|
||||
|
||||
normalized := parsed.Build()
|
||||
if normalized == "" {
|
||||
continue // leave to the model validator to decide whether to return an error
|
||||
}
|
||||
|
||||
e.Collection.Indexes[i] = normalized
|
||||
}
|
||||
|
||||
return e.Next()
|
||||
}
|
||||
|
||||
|
||||
@@ -1678,3 +1678,40 @@ func TestCollectionSaveViewWrapping(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectionSaveIndexesTableNameNormalization(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
dummyCollection := core.NewBaseCollection("new_test")
|
||||
dummyCollection.Fields.Add(&core.TextField{Name: "test"})
|
||||
dummyCollection.Indexes = []string{
|
||||
"create index `new_test_idx1` on `` (`test`) where 1=1",
|
||||
"create index `new_test_idx2` on `test` (`test`) where 1=2",
|
||||
"create index `new_test_idx3` on `someting_else` (`test`) where 1=3",
|
||||
}
|
||||
|
||||
err := app.Save(dummyCollection)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// refetch a clean state
|
||||
dummyCollection, err = app.FindCollectionByNameOrId(dummyCollection.Name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(dummyCollection.Indexes) != 3 {
|
||||
t.Fatalf("Expected 3 indexes, got %v", dummyCollection.Indexes)
|
||||
}
|
||||
|
||||
for _, raw := range dummyCollection.Indexes {
|
||||
parsed := dbutils.ParseIndex(raw)
|
||||
if parsed.TableName != dummyCollection.Name {
|
||||
t.Fatalf("Expected all indexes to have tableName %q, found %q:\n%s", dummyCollection.Name, parsed.TableName, raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -306,8 +306,10 @@ func dropCollectionIndexes(app App, collection *Collection) error {
|
||||
for _, raw := range collection.Indexes {
|
||||
parsed := dbutils.ParseIndex(raw)
|
||||
|
||||
if !parsed.IsValid() {
|
||||
continue
|
||||
// note: don't check IsValid because the index table name may not be populated
|
||||
// (https://github.com/pocketbase/pocketbase/issues/7689)
|
||||
if parsed.IndexName == "" {
|
||||
return fmt.Errorf("failed to dop index - missing index name: %s", raw)
|
||||
}
|
||||
|
||||
_, err := txApp.DB().NewQuery(fmt.Sprintf("DROP INDEX IF EXISTS [[%s]]", parsed.IndexName)).Execute()
|
||||
|
||||
@@ -294,3 +294,49 @@ func TestSingleVsMultipleValuesNormalization(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDropIndexWithoutTableName(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
properIndex := "CREATE INDEX `new_test_idx2` ON `new_test` (`test`)"
|
||||
indexWithoutTableName := "CREATE INDEX `new_test_idx2` ON `` (`test`)"
|
||||
|
||||
dummyCollection := core.NewBaseCollection("new_test")
|
||||
dummyCollection.Fields.Add(&core.TextField{Name: "test"})
|
||||
dummyCollection.Indexes = []string{properIndex}
|
||||
|
||||
err := app.Save(dummyCollection)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// resave without table name but without hooks to avoid the normalizations
|
||||
dummyCollection.Indexes[0] = indexWithoutTableName
|
||||
err = app.UnsafeWithoutHooks().Save(dummyCollection)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dummyCollection, err = app.FindCollectionByNameOrId(dummyCollection.Name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// resave should normalize the index
|
||||
err = app.Save(dummyCollection)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dummyCollection, err = app.FindCollectionByNameOrId(dummyCollection.Name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(dummyCollection.Indexes) != 1 || dummyCollection.Indexes[0] != properIndex {
|
||||
t.Fatalf("Expected exactly 1 index\n%s\ngot\n%v", properIndex, dummyCollection.Indexes)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user