skip empty automigrate templates
This commit is contained in:
parent
6400924d29
commit
d2028143df
|
@ -8,8 +8,11 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pocketbase/dbx"
|
||||||
"github.com/pocketbase/pocketbase/core"
|
"github.com/pocketbase/pocketbase/core"
|
||||||
|
"github.com/pocketbase/pocketbase/daos"
|
||||||
"github.com/pocketbase/pocketbase/models"
|
"github.com/pocketbase/pocketbase/models"
|
||||||
|
"github.com/pocketbase/pocketbase/tools/migrate"
|
||||||
)
|
)
|
||||||
|
|
||||||
const collectionsCacheKey = "migratecmd_collections"
|
const collectionsCacheKey = "migratecmd_collections"
|
||||||
|
@ -43,6 +46,9 @@ func (p *plugin) afterCollectionChange() func(*core.ModelEvent) error {
|
||||||
template, templateErr = p.goDiffTemplate(new, old)
|
template, templateErr = p.goDiffTemplate(new, old)
|
||||||
}
|
}
|
||||||
if templateErr != nil {
|
if templateErr != nil {
|
||||||
|
if errors.Is(templateErr, emptyTemplateErr) {
|
||||||
|
return nil // no changes
|
||||||
|
}
|
||||||
return fmt.Errorf("failed to resolve template: %w", templateErr)
|
return fmt.Errorf("failed to resolve template: %w", templateErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,23 +63,48 @@ func (p *plugin) afterCollectionChange() func(*core.ModelEvent) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
appliedTime := time.Now().Unix()
|
appliedTime := time.Now().Unix()
|
||||||
fileDest := filepath.Join(p.options.Dir, fmt.Sprintf("%d_%s.%s", appliedTime, action, p.options.TemplateLang))
|
name := fmt.Sprintf("%d_%s.%s", appliedTime, action, p.options.TemplateLang)
|
||||||
|
filePath := filepath.Join(p.options.Dir, name)
|
||||||
|
|
||||||
|
return p.app.Dao().RunInTransaction(func(txDao *daos.Dao) error {
|
||||||
|
// insert the migration entry
|
||||||
|
_, err := txDao.DB().Insert(migrate.DefaultMigrationsTable, dbx.Params{
|
||||||
|
"file": name,
|
||||||
|
"applied": appliedTime,
|
||||||
|
}).Execute()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// ensure that the local migrations dir exist
|
// ensure that the local migrations dir exist
|
||||||
if err := os.MkdirAll(p.options.Dir, os.ModePerm); err != nil {
|
if err := os.MkdirAll(p.options.Dir, os.ModePerm); err != nil {
|
||||||
return fmt.Errorf("failed to create migration dir: %w", err)
|
return fmt.Errorf("failed to create migration dir: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.WriteFile(fileDest, []byte(template), 0644); err != nil {
|
if err := os.WriteFile(filePath, []byte(template), 0644); err != nil {
|
||||||
return fmt.Errorf("failed to save automigrate file: %w", err)
|
return fmt.Errorf("failed to save automigrate file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
p.refreshCachedCollections()
|
p.updateSingleCachedCollection(new, old)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *plugin) updateSingleCachedCollection(new, old *models.Collection) {
|
||||||
|
cached, _ := p.app.Cache().Get(collectionsCacheKey).(map[string]*models.Collection)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case new == nil:
|
||||||
|
delete(cached, old.Id)
|
||||||
|
default:
|
||||||
|
cached[new.Id] = new
|
||||||
|
}
|
||||||
|
|
||||||
|
p.app.Cache().Set(collectionsCacheKey, cached)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *plugin) refreshCachedCollections() error {
|
func (p *plugin) refreshCachedCollections() error {
|
||||||
if p.app.Dao() == nil {
|
if p.app.Dao() == nil {
|
||||||
return errors.New("app is not initialized yet")
|
return errors.New("app is not initialized yet")
|
||||||
|
@ -84,12 +115,12 @@ func (p *plugin) refreshCachedCollections() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
mapped := map[string]*models.Collection{}
|
cached := map[string]*models.Collection{}
|
||||||
for _, c := range collections {
|
for _, c := range collections {
|
||||||
mapped[c.Id] = c
|
cached[c.Id] = c
|
||||||
}
|
}
|
||||||
|
|
||||||
p.app.Cache().Set(collectionsCacheKey, mapped)
|
p.app.Cache().Set(collectionsCacheKey, cached)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,8 @@ const (
|
||||||
TemplateLangGo = "go"
|
TemplateLangGo = "go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var emptyTemplateErr = errors.New("empty template")
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
// JavaScript templates
|
// JavaScript templates
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
|
@ -271,6 +273,10 @@ func (p *plugin) jsDiffTemplate(new *models.Collection, old *models.Collection)
|
||||||
|
|
||||||
// -----------------------------------------------------------------
|
// -----------------------------------------------------------------
|
||||||
|
|
||||||
|
if len(upParts) == 0 && len(downParts) == 0 {
|
||||||
|
return "", emptyTemplateErr
|
||||||
|
}
|
||||||
|
|
||||||
up := strings.Join(upParts, "\n ")
|
up := strings.Join(upParts, "\n ")
|
||||||
down := strings.Join(downParts, "\n ")
|
down := strings.Join(downParts, "\n ")
|
||||||
|
|
||||||
|
@ -646,6 +652,10 @@ func (p *plugin) goDiffTemplate(new *models.Collection, old *models.Collection)
|
||||||
}
|
}
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
|
|
||||||
|
if len(upParts) == 0 && len(downParts) == 0 {
|
||||||
|
return "", emptyTemplateErr
|
||||||
|
}
|
||||||
|
|
||||||
up := strings.Join(upParts, "\n\t\t")
|
up := strings.Join(upParts, "\n\t\t")
|
||||||
down := strings.Join(downParts, "\n\t\t")
|
down := strings.Join(downParts, "\n\t\t")
|
||||||
combined := up + down
|
combined := up + down
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
)
|
)
|
||||||
|
|
||||||
const migrationsTable = "_migrations"
|
const DefaultMigrationsTable = "_migrations"
|
||||||
|
|
||||||
// Runner defines a simple struct for managing the execution of db migrations.
|
// Runner defines a simple struct for managing the execution of db migrations.
|
||||||
type Runner struct {
|
type Runner struct {
|
||||||
|
@ -24,7 +24,7 @@ func NewRunner(db *dbx.DB, migrationsList MigrationsList) (*Runner, error) {
|
||||||
runner := &Runner{
|
runner := &Runner{
|
||||||
db: db,
|
db: db,
|
||||||
migrationsList: migrationsList,
|
migrationsList: migrationsList,
|
||||||
tableName: migrationsTable,
|
tableName: DefaultMigrationsTable,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := runner.createMigrationsTable(); err != nil {
|
if err := runner.createMigrationsTable(); err != nil {
|
||||||
|
|
Loading…
Reference in New Issue