-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexec.go
58 lines (48 loc) · 1.49 KB
/
exec.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
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package main
import (
"fmt"
"github.com/go-vela/types/constants"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// Exec takes the provided configuration and attempts to
// run a series of different functions that will
// manipulate indexes, tables and columns.
func (d *db) Exec(c *cli.Context) error {
logrus.Debug("executing utility from provided configuration")
var err error
switch d.Driver {
case constants.DriverSqlite:
// create the new Postgres database client
//
// https://pkg.go.dev/gorm.io/gorm#Open
d.Gorm, err = gorm.Open(sqlite.Open(d.Address), &gorm.Config{})
if err != nil {
return fmt.Errorf("unable to connect to %s database: %v", constants.DriverSqlite, err)
}
case constants.DriverPostgres:
// create the new Postgres database client
//
// https://pkg.go.dev/gorm.io/gorm#Open
d.Gorm, err = gorm.Open(postgres.Open(d.Address), &gorm.Config{})
if err != nil {
return fmt.Errorf("unable to connect to %s database: %v", constants.DriverPostgres, err)
}
}
defer func() { _sql, _ := d.Gorm.DB(); _sql.Close() }()
// check if either the all or alter tables action was provided
if d.Actions.All || d.Actions.AlterTables {
// alter required tables in the database
err = d.Alter()
if err != nil {
return err
}
}
return nil
}