You've already forked pocketbase
mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-11-06 09:29:19 +02:00
[#5618] added support to conditionally reapply migrations
This commit is contained in:
36
migrations/1640988000_aux_init.go
Normal file
36
migrations/1640988000_aux_init.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
)
|
||||
|
||||
func init() {
|
||||
core.SystemMigrations.Add(&core.Migration{
|
||||
Up: func(txApp core.App) error {
|
||||
_, execErr := txApp.AuxDB().NewQuery(`
|
||||
CREATE TABLE IF NOT EXISTS {{_logs}} (
|
||||
[[id]] TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL,
|
||||
[[level]] INTEGER DEFAULT 0 NOT NULL,
|
||||
[[message]] TEXT DEFAULT "" NOT NULL,
|
||||
[[data]] JSON DEFAULT "{}" NOT NULL,
|
||||
[[created]] TEXT DEFAULT (strftime('%Y-%m-%d %H:%M:%fZ')) NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_logs_level on {{_logs}} ([[level]]);
|
||||
CREATE INDEX IF NOT EXISTS idx_logs_message on {{_logs}} ([[message]]);
|
||||
CREATE INDEX IF NOT EXISTS idx_logs_created_hour on {{_logs}} (strftime('%Y-%m-%d %H:00:00', [[created]]));
|
||||
`).Execute()
|
||||
|
||||
return execErr
|
||||
},
|
||||
Down: func(txApp core.App) error {
|
||||
_, err := txApp.AuxDB().DropTable("_logs").Execute()
|
||||
return err
|
||||
},
|
||||
ReapplyCondition: func(txApp core.App, runner *core.MigrationsRunner, fileName string) (bool, error) {
|
||||
// reapply only if the _logs table doesn't exist
|
||||
exists := txApp.AuxHasTable("_logs")
|
||||
return !exists, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -28,10 +28,6 @@ func Register(
|
||||
|
||||
func init() {
|
||||
core.SystemMigrations.Register(func(txApp core.App) error {
|
||||
if err := createLogsTable(txApp); err != nil {
|
||||
return fmt.Errorf("_logs error: %w", err)
|
||||
}
|
||||
|
||||
if err := createParamsTable(txApp); err != nil {
|
||||
return fmt.Errorf("_params exec error: %w", err)
|
||||
}
|
||||
@@ -86,11 +82,6 @@ func init() {
|
||||
|
||||
return nil
|
||||
}, func(txApp core.App) error {
|
||||
_, err := txApp.AuxDB().DropTable("_logs").Execute()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tables := []string{
|
||||
"users",
|
||||
core.CollectionNameSuperusers,
|
||||
@@ -124,24 +115,6 @@ func createParamsTable(txApp core.App) error {
|
||||
return execErr
|
||||
}
|
||||
|
||||
func createLogsTable(txApp core.App) error {
|
||||
_, execErr := txApp.AuxDB().NewQuery(`
|
||||
CREATE TABLE {{_logs}} (
|
||||
[[id]] TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL,
|
||||
[[level]] INTEGER DEFAULT 0 NOT NULL,
|
||||
[[message]] TEXT DEFAULT "" NOT NULL,
|
||||
[[data]] JSON DEFAULT "{}" NOT NULL,
|
||||
[[created]] TEXT DEFAULT (strftime('%Y-%m-%d %H:%M:%fZ')) NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX idx_logs_level on {{_logs}} ([[level]]);
|
||||
CREATE INDEX idx_logs_message on {{_logs}} ([[message]]);
|
||||
CREATE INDEX idx_logs_created_hour on {{_logs}} (strftime('%Y-%m-%d %H:00:00', [[created]]));
|
||||
`).Execute()
|
||||
|
||||
return execErr
|
||||
}
|
||||
|
||||
func createMFAsCollection(txApp core.App) error {
|
||||
col := core.NewBaseCollection(core.CollectionNameMFAs)
|
||||
col.System = true
|
||||
|
||||
@@ -2,6 +2,7 @@ package migrations
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -58,12 +59,9 @@ func init() {
|
||||
return fmt.Errorf("failed to create authOrigins collection: %w", err)
|
||||
}
|
||||
|
||||
if err = createLogsTable(txApp); err != nil {
|
||||
return fmt.Errorf("failed tocreate logs table: %w", err)
|
||||
}
|
||||
|
||||
if err = os.Remove(filepath.Join(txApp.DataDir(), "logs.db")); err != nil {
|
||||
txApp.Logger().Warn("Failed to delete old logs.db file")
|
||||
err = os.Remove(filepath.Join(txApp.DataDir(), "logs.db"))
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
txApp.Logger().Warn("Failed to delete old logs.db file", "error", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user