diff --git a/CHANGELOG.md b/CHANGELOG.md index 07754529..8309fdf3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,10 @@ - Added option to explicitly set the record id from the Admin UI ([#2118](https://github.com/pocketbase/pocketbase/issues/2118)). +- **!** Renamed `daos.GetTableColumns()` to `daos.TableColumns()` for consistency with the other Dao table related helpers. + +- **!** Renamed `daos.GetTableInfo()` to `daos.TableInfo()` for consistency with the other Dao table related helpers. + - **!** Changed `types.JsonArray` to support specifying a generic type, aka. `types.JsonArray[T]`. If you have previously used `types.JsonArray`, you'll have to update it to `types.JsonArray[any]`. diff --git a/daos/collection_test.go b/daos/collection_test.go index 97b9807f..c98e1548 100644 --- a/daos/collection_test.go +++ b/daos/collection_test.go @@ -259,7 +259,7 @@ func TestSaveCollectionCreate(t *testing.T) { } // check if the records table has the schema fields - columns, err := app.Dao().GetTableColumns(collection.Name) + columns, err := app.Dao().TableColumns(collection.Name) if err != nil { t.Fatal(err) } @@ -298,7 +298,7 @@ func TestSaveCollectionUpdate(t *testing.T) { // check if the records table has the schema fields expectedColumns := []string{"id", "created", "updated", "title_update", "test", "files"} - columns, err := app.Dao().GetTableColumns(collection.Name) + columns, err := app.Dao().TableColumns(collection.Name) if err != nil { t.Fatal(err) } diff --git a/daos/record_table_sync_test.go b/daos/record_table_sync_test.go index f231d42b..5e829cf8 100644 --- a/daos/record_table_sync_test.go +++ b/daos/record_table_sync_test.go @@ -112,7 +112,7 @@ func TestSyncRecordTableSchema(t *testing.T) { t.Errorf("[%s] Expected table %s to exist", s.name, s.newCollection.Name) } - cols, _ := app.Dao().GetTableColumns(s.newCollection.Name) + cols, _ := app.Dao().TableColumns(s.newCollection.Name) if len(cols) != len(s.expectedColumns) { t.Errorf("[%s] Expected columns %v, got %v", s.name, s.expectedColumns, cols) } diff --git a/daos/table.go b/daos/table.go index 9854c954..1c2c2ac3 100644 --- a/daos/table.go +++ b/daos/table.go @@ -21,10 +21,8 @@ func (dao *Dao) HasTable(tableName string) bool { return err == nil && exists } -// @todo rename to TableColumns -// -// GetTableColumns returns all column names of a single table by its name. -func (dao *Dao) GetTableColumns(tableName string) ([]string, error) { +// TableColumns returns all column names of a single table by its name. +func (dao *Dao) TableColumns(tableName string) ([]string, error) { columns := []string{} err := dao.DB().NewQuery("SELECT name FROM PRAGMA_TABLE_INFO({:tableName})"). @@ -34,10 +32,8 @@ func (dao *Dao) GetTableColumns(tableName string) ([]string, error) { return columns, err } -// @todo rename to TableInfo -// -// GetTableInfo returns the `table_info` pragma result for the specified table. -func (dao *Dao) GetTableInfo(tableName string) ([]*models.TableInfoRow, error) { +// TableInfo returns the `table_info` pragma result for the specified table. +func (dao *Dao) TableInfo(tableName string) ([]*models.TableInfoRow, error) { info := []*models.TableInfoRow{} err := dao.DB().NewQuery("SELECT * FROM PRAGMA_TABLE_INFO({:tableName})"). diff --git a/daos/table_test.go b/daos/table_test.go index acfce0d8..90fad2ef 100644 --- a/daos/table_test.go +++ b/daos/table_test.go @@ -35,7 +35,7 @@ func TestHasTable(t *testing.T) { } } -func TestGetTableColumns(t *testing.T) { +func TestTableColumns(t *testing.T) { app, _ := tests.NewTestApp() defer app.Cleanup() @@ -48,7 +48,7 @@ func TestGetTableColumns(t *testing.T) { } for i, s := range scenarios { - columns, _ := app.Dao().GetTableColumns(s.tableName) + columns, _ := app.Dao().TableColumns(s.tableName) if len(columns) != len(s.expected) { t.Errorf("[%d] Expected columns %v, got %v", i, s.expected, columns) @@ -63,7 +63,7 @@ func TestGetTableColumns(t *testing.T) { } } -func TestGetTableInfo(t *testing.T) { +func TestTableInfo(t *testing.T) { app, _ := tests.NewTestApp() defer app.Cleanup() @@ -80,7 +80,7 @@ func TestGetTableInfo(t *testing.T) { } for i, s := range scenarios { - rows, _ := app.Dao().GetTableInfo(s.tableName) + rows, _ := app.Dao().TableInfo(s.tableName) raw, _ := json.Marshal(rows) rawStr := string(raw) diff --git a/daos/view.go b/daos/view.go index 25b0fc39..5e7bdbcf 100644 --- a/daos/view.go +++ b/daos/view.go @@ -63,7 +63,7 @@ func (dao *Dao) SaveView(name string, selectQuery string) error { // fetch the view table info to ensure that the view was created // because missing tables or columns won't return an error - if _, err := txDao.GetTableInfo(name); err != nil { + if _, err := txDao.TableInfo(name); err != nil { // manually cleanup previously created view in case the func // is called in a nested transaction and the error is discarded txDao.DeleteView(name) @@ -99,7 +99,7 @@ func (dao *Dao) CreateViewSchema(selectQuery string) (schema.Schema, error) { defer txDao.DeleteView(tempView) // extract the generated view table info - info, err := txDao.GetTableInfo(tempView) + info, err := txDao.TableInfo(tempView) if err != nil { return err } diff --git a/daos/view_test.go b/daos/view_test.go index 54affd24..fecdbbb0 100644 --- a/daos/view_test.go +++ b/daos/view_test.go @@ -159,7 +159,7 @@ func TestSaveView(t *testing.T) { continue } - infoRows, err := app.Dao().GetTableInfo(s.viewName) + infoRows, err := app.Dao().TableInfo(s.viewName) if err != nil { t.Errorf("[%s] Failed to fetch table info for %s: %v", s.scenarioName, s.viewName, err) continue diff --git a/forms/collection_upsert.go b/forms/collection_upsert.go index af556b92..223c9db1 100644 --- a/forms/collection_upsert.go +++ b/forms/collection_upsert.go @@ -158,9 +158,7 @@ func (form *CollectionUpsert) Validate() error { validation.When(isView, validation.Nil), validation.By(form.checkRule), ), - validation.Field(&form.Indexes, - validation.When(isView, validation.Length(0, 0)).Else(validation.By(form.checkIndexes)), - ), + validation.Field(&form.Indexes, validation.By(form.checkIndexes)), validation.Field(&form.Options, validation.By(form.checkOptions)), ) } @@ -390,6 +388,13 @@ func (form *CollectionUpsert) checkRule(value any) error { func (form *CollectionUpsert) checkIndexes(value any) error { v, _ := value.(types.JsonArray[string]) + if form.Type == models.CollectionTypeView && len(v) > 0 { + return validation.NewError( + "validation_indexes_not_supported", + fmt.Sprintf("The collection doesn't support indexes."), + ) + } + for i, rawIndex := range v { parsed := dbutils.ParseIndex(rawIndex) diff --git a/migrations/1679986800_add_indexes_column.go b/migrations/1679986800_add_indexes_column.go index ae8f88e0..3d6aa96d 100644 --- a/migrations/1679986800_add_indexes_column.go +++ b/migrations/1679986800_add_indexes_column.go @@ -18,7 +18,7 @@ func init() { AppMigrations.Register(func(db dbx.Builder) error { dao := daos.New(db) - cols, err := dao.GetTableColumns("_collections") + cols, err := dao.TableColumns("_collections") if err != nil { return err }