aboutsummaryrefslogtreecommitdiffstats
path: root/internal/commands/create.go
blob: ab37ecab88c8189067b968984292e8a3b3e07bce (plain) (blame)
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
package commands

import (
	"context"
	"flag"
	"fmt"
	"os"

	"github.com/eugenetriguba/bolt/internal/db"
	"github.com/google/subcommands"
)

type CreateCmd struct{}

func (*CreateCmd) Name() string     { return "create" }
func (*CreateCmd) Synopsis() string { return "Create a new database migration." }
func (*CreateCmd) Usage() string {
	return `create:
	Create a new database migration.
  `
}

func (m *CreateCmd) SetFlags(f *flag.FlagSet) {}

func (m *CreateCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
	db, err := db.Connect()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return subcommands.ExitFailure
	}
	defer db.Close()

	sqlStatement := `
		CREATE TABLE IF NOT EXISTS applied_migration(
			version CHARACTER(32) PRIMARY KEY NOT NULL
		);
	`
	_, err = db.Exec(sqlStatement)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return subcommands.ExitFailure
	}

	return subcommands.ExitSuccess
}