Add mage command to create a new migration

This commit is contained in:
kolaente 2020-09-04 10:15:33 +02:00
parent e4dd314079
commit 05e237560d
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
2 changed files with 83 additions and 4 deletions

View file

@ -34,6 +34,7 @@ There are multiple categories of subcommands in the magefile:
* `check`: Contains commands to statically check the source code * `check`: Contains commands to statically check the source code
* `release`: Contains commands to release Vikunja with everything that's required * `release`: Contains commands to release Vikunja with everything that's required
* `test`: Contains commands to run all kinds of tests * `test`: Contains commands to run all kinds of tests
* `dev`: Contains commands to run development tasks
* `misc`: Commands which do not belong in either of the other categories * `misc`: Commands which do not belong in either of the other categories
## CI ## CI
@ -162,6 +163,17 @@ mage test:integration
Runs all integration tests. Runs all integration tests.
## Dev
### Create a new migration
{{< highlight bash >}}
mage dev:create-migration
{{< /highlight >}}
Creates a new migration with the current date.
Will ask for the name of the struct you want to create a migration for.
## Misc ## Misc
### Format the code ### Format the code

View file

@ -32,6 +32,7 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"time"
) )
const ( const (
@ -56,10 +57,11 @@ var (
// Aliases are mage aliases of targets // Aliases are mage aliases of targets
Aliases = map[string]interface{}{ Aliases = map[string]interface{}{
"do-the-swag": DoTheSwag, "do-the-swag": DoTheSwag,
"check:go-sec": Check.GoSec, "check:go-sec": Check.GoSec,
"check:got-swag": Check.GotSwag, "check:got-swag": Check.GotSwag,
"release:os-package": Release.OsPackage, "release:os-package": Release.OsPackage,
"dev:create-migration": Dev.CreateMigration,
} }
) )
@ -712,3 +714,68 @@ func (Release) Deb() {
func (Release) Reprepro() { func (Release) Reprepro() {
runAndStreamOutput("reprepro_expect", "debian", "includedeb", "strech", RootPath+"/"+Executable+"-"+Version+"_amd64.deb") runAndStreamOutput("reprepro_expect", "debian", "includedeb", "strech", RootPath+"/"+Executable+"-"+Version+"_amd64.deb")
} }
type Dev mg.Namespace
// Creates a new bare db migration skeleton in pkg/migration with the current date
func (Dev) CreateMigration() error {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter the name of the struct: ")
str, _ := reader.ReadString('\n')
str = strings.Trim(str, "\n")
date := time.Now().Format("20060102150405")
migration := `// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package migration
import (
"src.techknowlogick.com/xormigrate"
"xorm.io/xorm"
)
type ` + str + date + ` struct {
}
func (` + str + date + `) TableName() string {
return "` + str + `"
}
func init() {
migrations = append(migrations, &xormigrate.Migration{
ID: "` + date + `",
Description: "",
Migrate: func(tx *xorm.Engine) error {
return tx.Sync2(` + str + date + `{})
},
Rollback: func(tx *xorm.Engine) error {
return nil
},
})
}
`
f, err := os.Create(RootPath + "/pkg/migration/" + date + ".go")
defer f.Close()
if err != nil {
return err
}
_, err = f.WriteString(migration)
return err
}