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

Medzoner / medzoner-go / 21525866034

30 Jan 2026 06:10PM UTC coverage: 28.088% (+0.7%) from 27.342%
21525866034

push

travis-ci

Medzoner
rf pkg

0 of 25 new or added lines in 5 files covered. (0.0%)

216 of 769 relevant lines covered (28.09%)

1.01 hits per line

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

0.0
/pkg/database/db_sql_instance.go
1
package database
2

3
import (
4
        "fmt"
5

6
        "github.com/golang-migrate/migrate/v4/database"
7
        "github.com/golang-migrate/migrate/v4/database/mysql"
8
        "github.com/jmoiron/sqlx"
9
)
10

11
type Config struct {
12
        RootPath string `env:"ROOT_PATH" envDefault:"./"`
13
        Dsn      string `env:"DSN"    envDefault:"root:changeme@tcp(0.0.0.0:3306)"`
14
        Name     string `env:"NAME"   envDefault:"dev_medzoner"`
15
        Driver   string `env:"DRIVER" envDefault:"mysql"`
16
}
17

18
type DbSQLInstance struct {
19
        Connection   *sqlx.DB
20
        Dsn          string
21
        DatabaseName string
22
        DriverName   string
23
}
24

25
// NewDbSQLInstance is a function that returns a new DbSQLInstance
NEW
26
func NewDbSQLInstance(conf Config) *DbSQLInstance {
×
27
        d := &DbSQLInstance{
×
NEW
28
                Dsn:          conf.Dsn,
×
NEW
29
                DatabaseName: conf.Name,
×
NEW
30
                DriverName:   conf.Driver,
×
31
                Connection:   nil,
×
32
        }
×
33
        d.Connect()
×
34
        return d
×
35
}
×
36

37
const dsnOptions = "?multiStatements=true&parseTime=true"
38

39
// Connect Connect
40
func (d *DbSQLInstance) Connect() (db *sqlx.DB) {
×
41
        d.Connection = d.openDb(d.Dsn + "/" + d.DatabaseName + dsnOptions)
×
42
        return d.Connection
×
43
}
×
44

45
// GetConnection GetConnection
46
func (d *DbSQLInstance) GetConnection() (db *sqlx.DB) {
×
47
        return d.Connection
×
48
}
×
49

50
// CreateDatabase is a function that creates a database
51
func (d *DbSQLInstance) CreateDatabase(databaseName string) {
×
52
        if d.DriverName == "mysql" {
×
53
                dbCreate := d.openDb(d.Dsn + "/" + dsnOptions)
×
54
                dbCreate.MustExec("CREATE DATABASE IF NOT EXISTS " + databaseName)
×
55
        }
×
56
}
57

58
// DropDatabase DropDatabase
59
func (d *DbSQLInstance) DropDatabase(databaseName string) {
×
60
        if d.DriverName == "mysql" {
×
61
                dbDrop := d.openDb(d.Dsn + "/" + dsnOptions)
×
62
                dbDrop.MustExec("DROP DATABASE IF EXISTS " + databaseName)
×
63
        }
×
64
}
65

66
// GetDatabaseName GetDatabaseName
67
func (d *DbSQLInstance) GetDatabaseName() string {
×
68
        return d.DatabaseName
×
69
}
×
70

71
// GetDatabaseDriver is a function that returns the database driver
72
func (d *DbSQLInstance) GetDatabaseDriver() (database.Driver, error) {
×
73
        db, err := mysql.WithInstance(d.Connection.DB, &mysql.Config{})
×
74
        if err != nil {
×
75
                return nil, fmt.Errorf("error getting database driver: %w", err)
×
76
        }
×
77
        return db, nil
×
78
}
79

80
func (d *DbSQLInstance) openDb(dsn string) *sqlx.DB {
×
81
        dbDrop := sqlx.MustOpen(d.DriverName, dsn)
×
82
        return dbDrop
×
83
}
×
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