Пример #1
0
func Sync(config *ServerConfig, force bool) error {
	core, err := config.GetCore()
	if err != nil {
		return err
	}
	db, err := core.OpenDatabaseConnection(nil)
	if err != nil {
		return err
	}
	defer db.Close()
	mig, err := sync.BuildMigration(db, core.GetModel())
	if err != nil {
		return err
	}

	e, err := json.Marshal(mig)
	if err != nil {
		return err
	}
	fmt.Println(string(e))

	if force {
		err := mig.Run(db)
		if err != nil {
			return err
		}
	}
	return nil
}
Пример #2
0
func main() {
	flag.Parse()
	if len(modelFilename) < 1 {
		fmt.Println("No model filename specified (use --model)")
		os.Exit(1)
		return
	}

	model, err := databath.ReadModelFromFileForSync(modelFilename)
	if err != nil {
		fmt.Printf("Error reading model: %s\n", err.Error())
		os.Exit(2)
		return
	}

	db, err := sql.Open("mysql", dsn)
	if err != nil {
		fmt.Printf("Error opening database %s: %s\n", dsn, err.Error())
		os.Exit(3)
		return
	}
	defer db.Close()
	mig, err := sync.BuildMigration(db, model)
	if err != nil {
		fmt.Println(err)
		os.Exit(4)
		return
	}

	e, err := json.Marshal(mig)
	if err != nil {
		fmt.Println(err)
		os.Exit(4)
		return
	}
	fmt.Println(string(e))

	if force {
		err := mig.Run(db)
		if err != nil {
			fmt.Println(err)
			os.Exit(5)
		}
	}

	if len(setUser) > 0 {
		parts := strings.Split(setUser, ":")
		if len(parts) != 2 {
			fmt.Fprintln(os.Stderr, "setuser must be in the form username:password")
			os.Exit(1)
		}

		err = SetUser(db, "user", parts[0], parts[1])
		if err != nil {
			fmt.Println(err.Error())
			os.Exit(5)
			return
		}
	}
}