1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-11-28 10:03:42 +02:00
pocketbase/migrations/logs/1660821103_add_user_ip_column.go
2022-08-21 14:47:33 +03:00

58 lines
1.5 KiB
Go

package logs
import (
"github.com/pocketbase/dbx"
)
func init() {
LogsMigrations.Register(func(db dbx.Builder) error {
// delete old index (don't check for error because of backward compatibility with old installations)
db.DropIndex("_requests", "_request_ip_idx").Execute()
// rename ip -> remoteIp
if _, err := db.RenameColumn("_requests", "ip", "remoteIp").Execute(); err != nil {
return err
}
// add new userIp column
if _, err := db.AddColumn("_requests", "userIp", `TEXT DEFAULT "127.0.0.1" NOT NULL`).Execute(); err != nil {
return err
}
// add new indexes
if _, err := db.CreateIndex("_requests", "_request_remote_ip_idx", "remoteIp").Execute(); err != nil {
return err
}
if _, err := db.CreateIndex("_requests", "_request_user_ip_idx", "userIp").Execute(); err != nil {
return err
}
return nil
}, func(db dbx.Builder) error {
// delete new indexes
if _, err := db.DropIndex("_requests", "_request_remote_ip_idx").Execute(); err != nil {
return err
}
if _, err := db.DropIndex("_requests", "_request_user_ip_idx").Execute(); err != nil {
return err
}
// drop userIp column
if _, err := db.DropColumn("_requests", "userIp").Execute(); err != nil {
return err
}
// restore original remoteIp column name
if _, err := db.RenameColumn("_requests", "remoteIp", "ip").Execute(); err != nil {
return err
}
// restore original index
if _, err := db.CreateIndex("_requests", "_request_ip_idx", "ip").Execute(); err != nil {
return err
}
return nil
})
}