From adc9288e2ff6669b972c27d10b870f0a8a086c00 Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Mon, 11 Aug 2014 11:33:53 +0800 Subject: [PATCH] skeleton code for 'migrate' command --- bee.go | 1 + migrate.go | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 migrate.go diff --git a/bee.go b/bee.go index bf9029b..2e8770c 100644 --- a/bee.go +++ b/bee.go @@ -83,6 +83,7 @@ var commands = []*Command{ cmdVersion, cmdGenerate, cmdRundocs, + cmdMigrate, } func main() { diff --git a/migrate.go b/migrate.go new file mode 100644 index 0000000..1e5c37a --- /dev/null +++ b/migrate.go @@ -0,0 +1,90 @@ +// Copyright 2013 bee authors +// +// Licensed under the Apache License, Version 2.0 (the "License"): you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + +package main + +import "os" + +var cmdMigrate = &Command{ + UsageLine: "migrate [Command]", + Short: "run database migrations", + Long: ` +bee migrate + run all outstanding migrations + +bee migrate rollback + rollback the last migration operation + +bee migrate reset + rollback all migrations + +bee migrate refresh + rollback all migrations and run them all again +`, +} + +func init() { + cmdMigrate.Run = runMigration +} + +func runMigration(cmd *Command, args []string) { + //curpath, _ := os.Getwd() + + gopath := os.Getenv("GOPATH") + Debugf("gopath:%s", gopath) + if gopath == "" { + ColorLog("[ERRO] $GOPATH not found\n") + ColorLog("[HINT] Set $GOPATH in your environment vairables\n") + os.Exit(2) + } + + if len(args) == 0 { + // run all outstanding migrations + ColorLog("[INFO] running all outstanding migrations\n") + migrateUpdate() + } else { + mcmd := args[0] + switch mcmd { + case "rollback": + ColorLog("[INFO] rolling back the last migration operation\n") + migrateRollback() + case "reset": + ColorLog("[INFO] reseting all migrations\n") + migrateReset() + case "refresh": + ColorLog("[INFO] refreshing all migrations\n") + migrateReset() + default: + ColorLog("[ERRO] command is missing\n") + os.Exit(2) + } + ColorLog("[SUCC] migration successful!\n") + } +} + +func migrateUpdate() { + println("=>update") +} + +func migrateRollback() { + println("=>rollback") +} + +func migrateReset() { + println("=>reset") +} + +func migrateRefresh() { + println("=>refresh") +}