File size: 1,516 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"os/exec"
	"sort"
	"strings"
	"time"
)

const stampFormat = "20060102150405"

func runCmd(name string, args ...string) string {
	cmd := exec.Command(name, args...)
	cmd.Stderr = os.Stderr
	fmt.Println("+ ", name, strings.Join(args, " "))
	data, err := cmd.Output()
	if err != nil {
		log.Fatal(err)
	}
	return strings.TrimSpace(string(data))
}
func setTime(s string, t time.Time) string {
	return s[:19] + t.Format(stampFormat) + s[33:]
}

func main() {
	check := flag.Bool("check", false, "Exit with error status on wrong order, but don't actually rename anything.")
	flag.Parse()
	runCmd("git", "fetch", "--no-tags", "origin", "+refs/heads/master:")
	masterMigrations := strings.Split(runCmd("git", "ls-tree", "-r", "--name-only", "origin/master", "--", "migrate/migrations"), "\n")
	newMigrations := strings.Split(runCmd("git", "diff", "--name-only", "origin/master", "--", "migrate/migrations"), "\n")

	sort.Strings(masterMigrations)
	sort.Strings(newMigrations)

	if len(newMigrations) == 0 || len(newMigrations) == 1 && newMigrations[0] == "" {
		return
	}

	if newMigrations[0] > masterMigrations[len(masterMigrations)-1] {
		// already in order
		return
	}

	if *check {
		log.Println(newMigrations[0], "<=", masterMigrations[len(masterMigrations)-1])
		log.Fatal("found new migrations before those in master")
	}

	t := time.Now().Add(time.Minute)
	for _, m := range newMigrations {
		runCmd("git", "mv", m, setTime(m, t))
		t = t.Add(time.Minute)
	}

}