2022-11-26 09:05:52 +02:00
|
|
|
package migratecmd
|
|
|
|
|
|
|
|
import (
|
2022-11-27 23:00:58 +02:00
|
|
|
"database/sql"
|
2022-11-26 09:05:52 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2022-12-02 11:36:13 +02:00
|
|
|
"github.com/pocketbase/dbx"
|
2022-11-26 09:05:52 +02:00
|
|
|
"github.com/pocketbase/pocketbase/core"
|
2022-12-02 11:36:13 +02:00
|
|
|
"github.com/pocketbase/pocketbase/daos"
|
2022-11-26 09:05:52 +02:00
|
|
|
"github.com/pocketbase/pocketbase/models"
|
2022-12-02 11:36:13 +02:00
|
|
|
"github.com/pocketbase/pocketbase/tools/migrate"
|
2022-11-26 09:05:52 +02:00
|
|
|
)
|
|
|
|
|
2022-11-28 19:59:17 +02:00
|
|
|
const collectionsCacheKey = "migratecmd_collections"
|
2022-11-26 09:05:52 +02:00
|
|
|
|
2022-11-26 22:33:27 +02:00
|
|
|
// onCollectionChange handles the automigration snapshot generation on
|
|
|
|
// collection change event (create/update/delete).
|
2022-11-27 23:00:58 +02:00
|
|
|
func (p *plugin) afterCollectionChange() func(*core.ModelEvent) error {
|
2022-11-26 09:05:52 +02:00
|
|
|
return func(e *core.ModelEvent) error {
|
|
|
|
if e.Model.TableName() != "_collections" {
|
|
|
|
return nil // not a collection
|
|
|
|
}
|
|
|
|
|
2022-11-28 19:59:17 +02:00
|
|
|
// @todo replace with the OldModel when added to the ModelEvent
|
2022-11-27 23:00:58 +02:00
|
|
|
oldCollections, err := p.getCachedCollections()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-11-26 09:05:52 +02:00
|
|
|
}
|
|
|
|
|
2022-11-29 22:28:38 +02:00
|
|
|
old := oldCollections[e.Model.GetId()]
|
2022-11-27 23:00:58 +02:00
|
|
|
|
|
|
|
new, err := p.app.Dao().FindCollectionByNameOrId(e.Model.GetId())
|
|
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return err
|
2022-11-26 09:05:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var template string
|
|
|
|
var templateErr error
|
2023-06-08 17:59:08 +03:00
|
|
|
if p.config.TemplateLang == TemplateLangJS {
|
2022-11-27 23:00:58 +02:00
|
|
|
template, templateErr = p.jsDiffTemplate(new, old)
|
2022-11-26 09:05:52 +02:00
|
|
|
} else {
|
2022-11-27 23:00:58 +02:00
|
|
|
template, templateErr = p.goDiffTemplate(new, old)
|
2022-11-26 09:05:52 +02:00
|
|
|
}
|
|
|
|
if templateErr != nil {
|
2022-12-02 11:36:13 +02:00
|
|
|
if errors.Is(templateErr, emptyTemplateErr) {
|
|
|
|
return nil // no changes
|
|
|
|
}
|
2022-11-30 17:23:00 +02:00
|
|
|
return fmt.Errorf("failed to resolve template: %w", templateErr)
|
2022-11-26 09:05:52 +02:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:59:17 +02:00
|
|
|
var action string
|
|
|
|
switch {
|
|
|
|
case new == nil:
|
|
|
|
action = "deleted_" + old.Name
|
|
|
|
case old == nil:
|
|
|
|
action = "created_" + new.Name
|
|
|
|
default:
|
|
|
|
action = "updated_" + old.Name
|
|
|
|
}
|
|
|
|
|
2023-06-27 00:35:17 +03:00
|
|
|
name := fmt.Sprintf("%d_%s.%s", time.Now().Unix(), action, p.config.TemplateLang)
|
2023-06-08 17:59:08 +03:00
|
|
|
filePath := filepath.Join(p.config.Dir, name)
|
2022-12-02 11:36:13 +02:00
|
|
|
|
|
|
|
return p.app.Dao().RunInTransaction(func(txDao *daos.Dao) error {
|
|
|
|
// insert the migration entry
|
|
|
|
_, err := txDao.DB().Insert(migrate.DefaultMigrationsTable, dbx.Params{
|
2023-06-27 00:35:17 +03:00
|
|
|
"file": name,
|
|
|
|
// use microseconds for more granular applied time in case
|
|
|
|
// multiple collection changes happens at the ~exact time
|
|
|
|
"applied": time.Now().UnixMicro(),
|
2022-12-02 11:36:13 +02:00
|
|
|
}).Execute()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that the local migrations dir exist
|
2023-06-08 17:59:08 +03:00
|
|
|
if err := os.MkdirAll(p.config.Dir, os.ModePerm); err != nil {
|
2022-12-02 11:36:13 +02:00
|
|
|
return fmt.Errorf("failed to create migration dir: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.WriteFile(filePath, []byte(template), 0644); err != nil {
|
|
|
|
return fmt.Errorf("failed to save automigrate file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.updateSingleCachedCollection(new, old)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-11-26 22:33:27 +02:00
|
|
|
|
2022-12-02 11:36:13 +02:00
|
|
|
func (p *plugin) updateSingleCachedCollection(new, old *models.Collection) {
|
|
|
|
cached, _ := p.app.Cache().Get(collectionsCacheKey).(map[string]*models.Collection)
|
2022-11-26 22:33:27 +02:00
|
|
|
|
2022-12-02 11:36:13 +02:00
|
|
|
switch {
|
|
|
|
case new == nil:
|
|
|
|
delete(cached, old.Id)
|
|
|
|
default:
|
|
|
|
cached[new.Id] = new
|
2022-11-26 09:05:52 +02:00
|
|
|
}
|
2022-12-02 11:36:13 +02:00
|
|
|
|
|
|
|
p.app.Cache().Set(collectionsCacheKey, cached)
|
2022-11-26 09:05:52 +02:00
|
|
|
}
|
|
|
|
|
2022-11-27 23:00:58 +02:00
|
|
|
func (p *plugin) refreshCachedCollections() error {
|
2022-11-28 19:59:17 +02:00
|
|
|
if p.app.Dao() == nil {
|
|
|
|
return errors.New("app is not initialized yet")
|
|
|
|
}
|
|
|
|
|
2022-11-27 23:00:58 +02:00
|
|
|
var collections []*models.Collection
|
|
|
|
if err := p.app.Dao().CollectionQuery().All(&collections); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-02 11:36:13 +02:00
|
|
|
cached := map[string]*models.Collection{}
|
2022-11-27 23:00:58 +02:00
|
|
|
for _, c := range collections {
|
2022-12-02 11:36:13 +02:00
|
|
|
cached[c.Id] = c
|
2022-11-27 23:00:58 +02:00
|
|
|
}
|
|
|
|
|
2022-12-02 11:36:13 +02:00
|
|
|
p.app.Cache().Set(collectionsCacheKey, cached)
|
2022-11-27 23:00:58 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *plugin) getCachedCollections() (map[string]*models.Collection, error) {
|
|
|
|
if !p.app.Cache().Has(collectionsCacheKey) {
|
|
|
|
if err := p.refreshCachedCollections(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result, _ := p.app.Cache().Get(collectionsCacheKey).(map[string]*models.Collection)
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|