• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

a1y-developer / doc-formatter / 20615518441

31 Dec 2025 08:45AM UTC coverage: 65.274% (-2.5%) from 67.802%
20615518441

Pull #23

github

web-flow
Merge 927ec1ce0 into 3f7df65bc
Pull Request #23: feat: upload to s3

293 of 507 new or added lines in 30 files covered. (57.79%)

3 existing lines in 2 files now uncovered.

906 of 1388 relevant lines covered (65.27%)

0.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.04
/cmd/storage/options/database_options.go
1
package options
2

3
import (
4
        "fmt"
5
        "strconv"
6

7
        "github.com/a1y/doc-formatter/cmd/auth/util"
8
        storageinfra "github.com/a1y/doc-formatter/internal/storage/infra/persistence"
9
        "github.com/pkg/errors"
10
        "github.com/sirupsen/logrus"
11

12
        "github.com/spf13/pflag"
13
        "gorm.io/driver/postgres"
14
        "gorm.io/gorm"
15
)
16

17
var (
18
        ErrDBHostNotSpecified = errors.New("--db-host must be specified")
19
        ErrDBNameNotSpecified = errors.New("--db-name must be specified")
20
        ErrDBUserNotSpecified = errors.New("--db-user must be specified")
21
        ErrDBPortNotSpecified = errors.New("--db-port must be specified")
22
)
23

24
// DatabaseOptions holds the database access layer configurations.
25
type DatabaseOptions struct {
26
        DBName      string `json:"dbName,omitempty" yaml:"dbName,omitempty"`
27
        DBUser      string `json:"dbUser,omitempty" yaml:"dbUser,omitempty"`
28
        DBPassword  string `json:"dbPassword,omitempty" yaml:"dbPassword,omitempty"`
29
        DBHost      string `json:"dbHost,omitempty" yaml:"dbHost,omitempty"`
30
        DBPort      int    `json:"dbPort,omitempty" yaml:"dbPort,omitempty"`
31
        AutoMigrate bool   `json:"autoMigrate,omitempty" yaml:"autoMigrate,omitempty"`
32
}
33

34
// InstallDB uses the run options to generate and open a db session.
35
func (o *DatabaseOptions) InstallDB() (*gorm.DB, error) {
1✔
36
        dsn := fmt.Sprintf(
1✔
37
                "host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s",
1✔
38
                o.DBHost, o.DBUser, o.DBPassword, o.DBName,
1✔
39
                o.DBPort, "disable", "Asia/Ho_Chi_Minh",
1✔
40
        )
1✔
41
        return gorm.Open(postgres.Open(dsn), &gorm.Config{})
1✔
42
}
1✔
43

44
// ApplyTo uses the run options to generate and open a db session.
45
func (o *DatabaseOptions) ApplyTo(db **gorm.DB) error {
1✔
46
        d, err := o.InstallDB()
1✔
47
        if err != nil {
2✔
48
                return err
1✔
49
        }
1✔
NEW
50
        *db = d
×
NEW
51

×
NEW
52
        if *db != nil && o.AutoMigrate {
×
NEW
53
                if err := storageinfra.AutoMigrate(*db); err != nil {
×
NEW
54
                        logrus.Fatalf("Failed to auto migrate: %+v", err)
×
NEW
55
                }
×
56
        }
NEW
57
        return nil
×
58
}
59

60
// Validate checks validation of DatabaseOptions
61
func (o *DatabaseOptions) Validate() error {
1✔
62
        var errs []error
1✔
63
        if len(o.DBHost) == 0 {
2✔
64
                errs = append(errs, ErrDBHostNotSpecified)
1✔
65
        }
1✔
66
        if len(o.DBName) == 0 {
2✔
67
                errs = append(errs, ErrDBNameNotSpecified)
1✔
68
        }
1✔
69
        if len(o.DBUser) == 0 {
2✔
70
                errs = append(errs, ErrDBUserNotSpecified)
1✔
71
        }
1✔
72
        if o.DBPort == 0 {
2✔
73
                errs = append(errs, ErrDBPortNotSpecified)
1✔
74
        }
1✔
75
        if errs != nil {
2✔
76
                err := util.AggregateError(errs)
1✔
77
                return errors.Wrap(err, "invalid db options")
1✔
78
        }
1✔
79
        return nil
1✔
80
}
81

82
// AddFlags adds flags related to DB to a specified FlagSet
83
func (o *DatabaseOptions) AddFlags(fs *pflag.FlagSet) {
1✔
84
        fs.StringVar(&o.DBName, "db-name", DBNameEnv, "the database name")
1✔
85
        fs.StringVar(&o.DBUser, "db-user", DBUserEnv, "the user name used to access database")
1✔
86
        fs.StringVar(&o.DBPassword, "db-pass", DBPassEnv, "the user password used to access database")
1✔
87
        fs.StringVar(&o.DBHost, "db-host", DBHostEnv, "database host")
1✔
88
        dbPort, err := strconv.Atoi(DBPortEnv)
1✔
89
        if err != nil {
2✔
90
                dbPort = DefaultDBPort
1✔
91
        }
1✔
92
        fs.IntVar(&o.DBPort, "db-port", dbPort, "database port")
1✔
93
        autoMigrate, err := strconv.ParseBool(AutoMigrateEnv)
1✔
94
        if err != nil {
2✔
95
                autoMigrate = false
1✔
96
        }
1✔
97
        fs.BoolVar(&o.AutoMigrate, "auto-migrate", autoMigrate, "whether to enable automatic migration")
1✔
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc