1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-12-01 09:16:40 +02:00

updated automigrate templates, added js bindings tests and updated models IsNew behavior

This commit is contained in:
Gani Georgiev
2022-12-05 13:57:09 +02:00
parent 604009bd10
commit b8cd686b32
21 changed files with 546 additions and 213 deletions

View File

@@ -136,3 +136,19 @@ func (p *plugin) getCachedCollections() (map[string]*models.Collection, error) {
return result, nil
}
func (p *plugin) hasCustomMigrations() bool {
files, err := os.ReadDir(p.options.Dir)
if err != nil {
return false
}
for _, f := range files {
if f.IsDir() {
continue
}
return true
}
return false
}

View File

@@ -82,6 +82,15 @@ func Register(app core.App, rootCmd *cobra.Command, options *Options) error {
// when migrations are applied on server start
p.app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
p.refreshCachedCollections()
cachedCollections, _ := p.getCachedCollections()
// create a full initial snapshot, if there are no custom
// migrations but there is already at least 1 collection created,
// to ensure that the automigrate will work with up-to-date collections data
if !p.hasCustomMigrations() && len(cachedCollections) > 1 {
p.migrateCollectionsHandler(nil, false)
}
return nil
})
@@ -114,11 +123,11 @@ func (p *plugin) createCommand() *cobra.Command {
switch cmd {
case "create":
if err := p.migrateCreateHandler("", args[1:]); err != nil {
if err := p.migrateCreateHandler("", args[1:], true); err != nil {
log.Fatal(err)
}
case "collections":
if err := p.migrateCollectionsHandler(args[1:]); err != nil {
if err := p.migrateCollectionsHandler(args[1:], true); err != nil {
log.Fatal(err)
}
default:
@@ -137,7 +146,7 @@ func (p *plugin) createCommand() *cobra.Command {
return command
}
func (p *plugin) migrateCreateHandler(template string, args []string) error {
func (p *plugin) migrateCreateHandler(template string, args []string, interactive bool) error {
if len(args) < 1 {
return fmt.Errorf("Missing migration file name")
}
@@ -150,14 +159,16 @@ func (p *plugin) migrateCreateHandler(template string, args []string) error {
fmt.Sprintf("%d_%s.%s", time.Now().Unix(), inflector.Snakecase(name), p.options.TemplateLang),
)
confirm := false
prompt := &survey.Confirm{
Message: fmt.Sprintf("Do you really want to create migration %q?", resultFilePath),
}
survey.AskOne(prompt, &confirm)
if !confirm {
fmt.Println("The command has been cancelled")
return nil
if interactive {
confirm := false
prompt := &survey.Confirm{
Message: fmt.Sprintf("Do you really want to create migration %q?", resultFilePath),
}
survey.AskOne(prompt, &confirm)
if !confirm {
fmt.Println("The command has been cancelled")
return nil
}
}
// get default create template
@@ -183,11 +194,14 @@ func (p *plugin) migrateCreateHandler(template string, args []string) error {
return fmt.Errorf("Failed to save migration file %q: %v\n", resultFilePath, err)
}
fmt.Printf("Successfully created file %q\n", resultFilePath)
if interactive {
fmt.Printf("Successfully created file %q\n", resultFilePath)
}
return nil
}
func (p *plugin) migrateCollectionsHandler(args []string) error {
func (p *plugin) migrateCollectionsHandler(args []string, interactive bool) error {
createArgs := []string{"collections_snapshot"}
createArgs = append(createArgs, args...)
@@ -207,5 +221,5 @@ func (p *plugin) migrateCollectionsHandler(args []string) error {
return fmt.Errorf("Failed to resolve template: %v", templateErr)
}
return p.migrateCreateHandler(template, createArgs)
return p.migrateCreateHandler(template, createArgs, interactive)
}

View File

@@ -23,7 +23,7 @@ func TestAutomigrateCollectionCreate(t *testing.T) {
migratecmd.TemplateLangJS,
`
migrate((db) => {
const collection = unmarshal({
const collection = new Collection({
"id": "new_id",
"created": "2022-01-01 00:00:00.000Z",
"updated": "2022-01-01 00:00:00.000Z",
@@ -46,7 +46,7 @@ migrate((db) => {
"onlyEmailDomains": null,
"requireEmail": false
}
}, new Collection());
});
return Dao(db).saveCollection(collection);
}, (db) => {
@@ -193,7 +193,7 @@ migrate((db) => {
return dao.deleteCollection(collection);
}, (db) => {
const collection = unmarshal({
const collection = new Collection({
"id": "test123",
"created": "2022-01-01 00:00:00.000Z",
"updated": "2022-01-01 00:00:00.000Z",
@@ -216,7 +216,7 @@ migrate((db) => {
"onlyEmailDomains": null,
"requireEmail": false
}
}, new Collection());
});
return Dao(db).saveCollection(collection);
})
@@ -372,7 +372,7 @@ migrate((db) => {
collection.schema.removeField("f3_id")
// add
collection.schema.addField(unmarshal({
collection.schema.addField(new SchemaField({
"system": false,
"id": "f4_id",
"name": "f4_name",
@@ -384,10 +384,10 @@ migrate((db) => {
"max": null,
"pattern": "` + "`" + `test backtick` + "`" + `123"
}
}, new SchemaField()))
}))
// update
collection.schema.addField(unmarshal({
collection.schema.addField(new SchemaField({
"system": false,
"id": "f2_id",
"name": "f2_name_new",
@@ -398,7 +398,7 @@ migrate((db) => {
"min": 10,
"max": null
}
}, new SchemaField()))
}))
return dao.saveCollection(collection)
}, (db) => {
@@ -421,7 +421,7 @@ migrate((db) => {
}
// add
collection.schema.addField(unmarshal({
collection.schema.addField(new SchemaField({
"system": false,
"id": "f3_id",
"name": "f3_name",
@@ -429,13 +429,13 @@ migrate((db) => {
"required": false,
"unique": false,
"options": {}
}, new SchemaField()))
}))
// remove
collection.schema.removeField("f4_id")
// update
collection.schema.addField(unmarshal({
collection.schema.addField(new SchemaField({
"system": false,
"id": "f2_id",
"name": "f2_name",
@@ -446,7 +446,7 @@ migrate((db) => {
"min": 10,
"max": null
}
}, new SchemaField()))
}))
return dao.saveCollection(collection)
})

View File

@@ -43,7 +43,7 @@ func (p *plugin) jsSnapshotTemplate(collections []*models.Collection) (string, e
const template = `migrate((db) => {
const snapshot = %s;
const collections = snapshot.map((item) => unmarshal(item, new Collection()));
const collections = snapshot.map((item) => new Collection(item));
return Dao(db).importCollections(collections, true, null);
}, (db) => {
@@ -61,7 +61,7 @@ func (p *plugin) jsCreateTemplate(collection *models.Collection) (string, error)
}
const template = `migrate((db) => {
const collection = unmarshal(%s, new Collection());
const collection = new Collection(%s);
return Dao(db).saveCollection(collection);
}, (db) => {
@@ -87,7 +87,7 @@ func (p *plugin) jsDeleteTemplate(collection *models.Collection) (string, error)
return dao.deleteCollection(collection);
}, (db) => {
const collection = unmarshal(%s, new Collection());
const collection = new Collection(%s);
return Dao(db).saveCollection(collection);
})
@@ -222,7 +222,7 @@ func (p *plugin) jsDiffTemplate(new *models.Collection, old *models.Collection)
upParts = append(upParts, fmt.Sprintf("%s.schema.removeField(%q)\n", varName, oldField.Id))
downParts = append(downParts, "// add")
downParts = append(downParts, fmt.Sprintf("%s.schema.addField(unmarshal(%s, new SchemaField()))\n", varName, rawOldField))
downParts = append(downParts, fmt.Sprintf("%s.schema.addField(new SchemaField(%s))\n", varName, rawOldField))
}
// created fields
@@ -237,7 +237,7 @@ func (p *plugin) jsDiffTemplate(new *models.Collection, old *models.Collection)
}
upParts = append(upParts, "// add")
upParts = append(upParts, fmt.Sprintf("%s.schema.addField(unmarshal(%s, new SchemaField()))\n", varName, rawNewField))
upParts = append(upParts, fmt.Sprintf("%s.schema.addField(new SchemaField(%s))\n", varName, rawNewField))
downParts = append(downParts, "// remove")
downParts = append(downParts, fmt.Sprintf("%s.schema.removeField(%q)\n", varName, newField.Id))
@@ -265,10 +265,10 @@ func (p *plugin) jsDiffTemplate(new *models.Collection, old *models.Collection)
}
upParts = append(upParts, "// update")
upParts = append(upParts, fmt.Sprintf("%s.schema.addField(unmarshal(%s, new SchemaField()))\n", varName, rawNewField))
upParts = append(upParts, fmt.Sprintf("%s.schema.addField(new SchemaField(%s))\n", varName, rawNewField))
downParts = append(downParts, "// update")
downParts = append(downParts, fmt.Sprintf("%s.schema.addField(unmarshal(%s, new SchemaField()))\n", varName, rawOldField))
downParts = append(downParts, fmt.Sprintf("%s.schema.addField(new SchemaField(%s))\n", varName, rawOldField))
}
// -----------------------------------------------------------------