-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.go
47 lines (40 loc) · 1.08 KB
/
status.go
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
package gograte
import (
"context"
"database/sql"
"log"
)
func Status(args []string) {
driver := args[DRIVER]
dbURL := args[DB_URL]
if len(args) > 3 {
log.Fatalf("Invalid number of arguments.")
}
if !ValidateDbDriver(driver) {
log.Fatalf("Invalid database driver. Supported databases are: %s\n", DB_DRIVERS.String())
}
validatedURL, err := validateDbURL(dbURL)
if err != nil {
log.Fatalln("Invalid database URL. Please provide a valid URL.")
}
driver = GetSQLDriver(driver)
db, err := sql.Open(driver, validatedURL.String())
if err != nil {
log.Fatalf("Error opening database: %v\n", err)
}
defer db.Close()
conn, err := db.Conn(context.Background())
if err != nil {
log.Fatalf("Error database connection: %v\n", err)
}
defer conn.Close()
records, err := queryAllMigrations(db)
if err != nil {
log.Fatalf("Error querying applied migrations: %v\n", err)
}
log.Println("\tApplied \tMigration")
log.Println("\t==============================")
for _, record := range records {
log.Printf("\t%v\t\t%d_%s.sql\n", record.IsApplied, record.VersionID, record.Name)
}
}