You've already forked pocketbase
mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-11-29 08:56:58 +02:00
tweaked automigrate to check for git status and extracted the base flags from the plugins
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -17,23 +18,10 @@ import (
|
||||
)
|
||||
|
||||
const migrationsTable = "_migrations"
|
||||
const automigrateSuffix = "_automigrate"
|
||||
|
||||
// tidyMigrationsTable cleanups the migrations table by removing all
|
||||
// entries with deleted migration files.
|
||||
func (p *plugin) tidyMigrationsTable() error {
|
||||
names, filesErr := p.getAllMigrationNames()
|
||||
if filesErr != nil {
|
||||
return fmt.Errorf("failed to fetch migration files list: %v", filesErr)
|
||||
}
|
||||
|
||||
_, tidyErr := p.app.Dao().DB().Delete(migrationsTable, dbx.NotIn("file", list.ToInterfaceSlice(names)...)).Execute()
|
||||
if tidyErr != nil {
|
||||
return fmt.Errorf("failed to delete last automigrates from the db: %v", tidyErr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// onCollectionChange handles the automigration snapshot generation on
|
||||
// collection change event (create/update/delete).
|
||||
func (p *plugin) onCollectionChange() func(*core.ModelEvent) error {
|
||||
return func(e *core.ModelEvent) error {
|
||||
if e.Model.TableName() != "_collections" {
|
||||
@@ -48,35 +36,11 @@ func (p *plugin) onCollectionChange() func(*core.ModelEvent) error {
|
||||
return errors.New("missing collections to automigrate")
|
||||
}
|
||||
|
||||
names, err := p.getAllMigrationNames()
|
||||
oldFiles, err := p.getAllMigrationNames()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch migration files list: %v", err)
|
||||
}
|
||||
|
||||
// delete last consequitive automigrates
|
||||
lastAutomigrates := []string{}
|
||||
for i := len(names) - 1; i >= 0; i-- {
|
||||
migrationFile := names[i]
|
||||
if !strings.Contains(migrationFile, "_automigrate.") {
|
||||
break
|
||||
}
|
||||
lastAutomigrates = append(lastAutomigrates, migrationFile)
|
||||
}
|
||||
if len(lastAutomigrates) > 0 {
|
||||
// delete last automigrates from the db
|
||||
_, err := p.app.Dao().DB().Delete(migrationsTable, dbx.In("file", list.ToInterfaceSlice(lastAutomigrates)...)).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete last automigrates from the db: %v", err)
|
||||
}
|
||||
|
||||
// delete last automigrates from the filesystem
|
||||
for _, f := range lastAutomigrates {
|
||||
if err := os.Remove(filepath.Join(p.options.Dir, f)); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("failed to delete last automigrates from the filesystem: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var template string
|
||||
var templateErr error
|
||||
if p.options.TemplateLang == TemplateLangJS {
|
||||
@@ -88,10 +52,6 @@ func (p *plugin) onCollectionChange() func(*core.ModelEvent) error {
|
||||
return fmt.Errorf("failed to resolve template: %v", templateErr)
|
||||
}
|
||||
|
||||
// add a comment to not edit the template
|
||||
template = ("// Do not edit by hand since this file is autogenerated and may get overwritten.\n" +
|
||||
"// If you want to do further changes, create a new non '_automigrate' file instead.\n" + template)
|
||||
|
||||
appliedTime := time.Now().Unix()
|
||||
fileDest := filepath.Join(p.options.Dir, fmt.Sprintf("%d_automigrate.%s", appliedTime, p.options.TemplateLang))
|
||||
|
||||
@@ -100,11 +60,37 @@ func (p *plugin) onCollectionChange() func(*core.ModelEvent) error {
|
||||
return fmt.Errorf("failed to create migration dir: %v", err)
|
||||
}
|
||||
|
||||
return os.WriteFile(fileDest, []byte(template), 0644)
|
||||
if err := os.WriteFile(fileDest, []byte(template), 0644); err != nil {
|
||||
return fmt.Errorf("failed to save automigrate file: %v", err)
|
||||
}
|
||||
|
||||
// remove the old untracked automigrate file
|
||||
// (only if the last one was automigrate!)
|
||||
if len(oldFiles) > 0 && strings.HasSuffix(oldFiles[len(oldFiles)-1], automigrateSuffix+"."+p.options.TemplateLang) {
|
||||
olfName := oldFiles[len(oldFiles)-1]
|
||||
oldPath := filepath.Join(p.options.Dir, olfName)
|
||||
|
||||
isUntracked := exec.Command(p.options.GitPath, "ls-files", "--error-unmatch", oldPath).Run() != nil
|
||||
if isUntracked {
|
||||
// delete the old automigrate from the db if it was already applied
|
||||
_, err := p.app.Dao().DB().Delete(migrationsTable, dbx.HashExp{"file": olfName}).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete last applied automigrate from the migration db: %v", err)
|
||||
}
|
||||
|
||||
// delete the old automigrate file from the filesystem
|
||||
if err := os.Remove(oldPath); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("failed to delete last automigrates from the filesystem: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// getAllMigrationNames return both applied and new local migration file names.
|
||||
// getAllMigrationNames return sorted slice with both applied and new
|
||||
// local migration file names.
|
||||
func (p *plugin) getAllMigrationNames() ([]string, error) {
|
||||
names := []string{}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user