This is an automated email from the ASF dual-hosted git repository.

abeizn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git

commit 7ec0ab9d9d69b9248a2c7158e00ed9bdc88c507d
Author: abeizn <[email protected]>
AuthorDate: Wed Jul 20 16:26:29 2022 +0800

    fix: generator adapt new migration
---
 generator/cmd/init_migration.go                       | 19 +++++++++++--------
 ...schema.go-template => add_init_tables.go-template} |  8 ++++----
 .../template/migrationscripts/register.go-template    |  2 +-
 generator/util/template.go                            |  4 ++--
 4 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/generator/cmd/init_migration.go b/generator/cmd/init_migration.go
index ebc4f17a..bed53ce2 100644
--- a/generator/cmd/init_migration.go
+++ b/generator/cmd/init_migration.go
@@ -20,13 +20,14 @@ package cmd
 import (
        "errors"
        "fmt"
+       "os"
+       "path/filepath"
+       "time"
+
        "github.com/apache/incubator-devlake/generator/util"
        "github.com/manifoldco/promptui"
        "github.com/spf13/cobra"
        "github.com/stoewer/go-strcase"
-       "os"
-       "path/filepath"
-       "time"
 )
 
 func init() {
@@ -61,17 +62,19 @@ Type in which plugin do you want init migrations in, then 
generator will create
                        cobra.CheckErr(errors.New(`migrationscripts inited or 
path read file`))
                }
 
+               // create vars
+               values := map[string]string{}
+               util.GenerateAllFormatVar(values, `plugin_name`, pluginName)
+               versionTimestamp := time.Now().Format(`20060102`)
+
                // read template
                templates := map[string]string{
-                       `initSchema.go`:     
util.ReadTemplate("generator/template/migrationscripts/init_schemas.go-template"),
+                       fmt.Sprintf("%s_add_init_tables.go", versionTimestamp): 
util.ReadTemplate("generator/template/migrationscripts/add_init_tables.go-template"),
                        `register.go`:       
util.ReadTemplate("generator/template/migrationscripts/register.go-template"),
                        `archived/.gitkeep`: ``,
                }
 
-               // create vars
-               values := map[string]string{}
-               util.GenerateAllFormatVar(values, `plugin_name`, pluginName)
-               values[`Date`] = time.Now().Format(`20060102`)
+               values[`Date`] = versionTimestamp
                values = util.DetectExistVars(templates, values)
                println(`vars in template:`, fmt.Sprint(values))
 
diff --git a/generator/template/migrationscripts/init_schema.go-template 
b/generator/template/migrationscripts/add_init_tables.go-template
similarity index 84%
rename from generator/template/migrationscripts/init_schema.go-template
rename to generator/template/migrationscripts/add_init_tables.go-template
index 1ab56bcc..5e4f68e6 100644
--- a/generator/template/migrationscripts/init_schema.go-template
+++ b/generator/template/migrationscripts/add_init_tables.go-template
@@ -22,18 +22,18 @@ import (
        "gorm.io/gorm"
 )
 
-type initSchemas struct {}
+type addInitTables struct {}
 
-func (u *initSchemas) Up(ctx context.Context, db *gorm.DB) error {
+func (u *addInitTables) Up(ctx context.Context, db *gorm.DB) error {
        return db.Migrator().AutoMigrate(
                // TODO add you models
        )
 }
 
-func (*initSchemas) Version() uint64 {
+func (*addInitTables) Version() uint64 {
        return {{ .Date }}000001
 }
 
-func (*initSchemas) Name() string {
+func (*addInitTables) Name() string {
        return "{{ .pluginName }} init schemas {{ .Date }}"
 }
diff --git a/generator/template/migrationscripts/register.go-template 
b/generator/template/migrationscripts/register.go-template
index 51152508..92e20c01 100644
--- a/generator/template/migrationscripts/register.go-template
+++ b/generator/template/migrationscripts/register.go-template
@@ -22,6 +22,6 @@ import "github.com/apache/incubator-devlake/migration"
 // All return all the migration scripts
 func All() []migration.Script {
        return []migration.Script{
-               new(initSchemas),
+               new(addInitTables),
        }
 }
diff --git a/generator/util/template.go b/generator/util/template.go
index d8b9896b..908caf70 100644
--- a/generator/util/template.go
+++ b/generator/util/template.go
@@ -47,10 +47,10 @@ func ReadTemplate(templateFile string) string {
 
 // WriteTemplates write some strings to files
 func WriteTemplates(path string, templates map[string]string) {
-       err := os.MkdirAll(path, 0600)
+       err := os.MkdirAll(path, 0755)
        cobra.CheckErr(err)
        for name, template := range templates {
-               err := os.MkdirAll(filepath.Dir(filepath.Join(path, name)), 
0600)
+               err := os.MkdirAll(filepath.Dir(filepath.Join(path, name)), 
0755)
                cobra.CheckErr(err)
                err = ioutil.WriteFile(filepath.Join(path, name), 
[]byte(template), 0600)
                cobra.CheckErr(err)

Reply via email to