1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-11-24 23:24:00 +02:00

[#5618] added support to conditionally reapply migrations

This commit is contained in:
Gani Georgiev
2024-10-08 16:23:58 +03:00
parent ed1dc54f27
commit 646331bfa2
13 changed files with 320 additions and 207 deletions

View File

@@ -7,20 +7,6 @@ import (
"github.com/pocketbase/dbx"
)
// HasTable checks if a table (or view) with the provided name exists (case insensitive).
func (app *BaseApp) HasTable(tableName string) bool {
var exists bool
err := app.DB().Select("(1)").
From("sqlite_schema").
AndWhere(dbx.HashExp{"type": []any{"table", "view"}}).
AndWhere(dbx.NewExp("LOWER([[name]])=LOWER({:tableName})", dbx.Params{"tableName": tableName})).
Limit(1).
Row(&exists)
return err == nil && exists
}
// TableColumns returns all column names of a single table by its name.
func (app *BaseApp) TableColumns(tableName string) ([]string, error) {
columns := []string{}
@@ -109,6 +95,31 @@ func (app *BaseApp) DeleteTable(tableName string) error {
return err
}
// HasTable checks if a table (or view) with the provided name exists (case insensitive).
// in the current app.DB() instance.
func (app *BaseApp) HasTable(tableName string) bool {
return app.hasTable(app.DB(), tableName)
}
// AuxHasTable checks if a table (or view) with the provided name exists (case insensitive)
// in the current app.AuxDB() instance.
func (app *BaseApp) AuxHasTable(tableName string) bool {
return app.hasTable(app.AuxDB(), tableName)
}
func (app *BaseApp) hasTable(db dbx.Builder, tableName string) bool {
var exists bool
err := db.Select("(1)").
From("sqlite_schema").
AndWhere(dbx.HashExp{"type": []any{"table", "view"}}).
AndWhere(dbx.NewExp("LOWER([[name]])=LOWER({:tableName})", dbx.Params{"tableName": tableName})).
Limit(1).
Row(&exists)
return err == nil && exists
}
// Vacuum executes VACUUM on the current app.DB() instance
// in order to reclaim unused data db disk space.
func (app *BaseApp) Vacuum() error {