Skip to content

Commit

Permalink
Move pkg/driver to pkg/icingadb/driver.go
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Mar 25, 2024
1 parent 5659749 commit 98fb197
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 19 deletions.
3 changes: 1 addition & 2 deletions cmd/icingadb-migrate/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/sha1"
"github.com/icinga/icingadb/pkg/contracts"
"github.com/icinga/icingadb/pkg/driver"
"github.com/icinga/icingadb/pkg/icingadb"
"github.com/icinga/icingadb/pkg/icingadb/objectpacker"
icingadbTypes "github.com/icinga/icingadb/pkg/types"
Expand Down Expand Up @@ -110,7 +109,7 @@ func sliceIdoHistory[Row any](
args["checkpoint"] = checkpoint
args["bulk"] = 20000

if ht.snapshot.DriverName() != driver.MySQL {
if ht.snapshot.DriverName() != icingadb.MySQL {
query = strings.ReplaceAll(query, " USE INDEX (PRIMARY)", "")
}

Expand Down
7 changes: 3 additions & 4 deletions pkg/config/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"database/sql/driver"
"fmt"
"github.com/go-sql-driver/mysql"
icingadbDriver "github.com/icinga/icingadb/pkg/driver"
"github.com/icinga/icingadb/pkg/icingadb"
"github.com/icinga/icingadb/pkg/logging"
"github.com/icinga/icingadb/pkg/utils"
Expand Down Expand Up @@ -40,7 +39,7 @@ type Database struct {
// calls sqlx.Open, but returns *icingadb.DB.
func (d *Database) Open(logger *logging.Logger) (*icingadb.DB, error) {
registerDriverOnce.Do(func() {
icingadbDriver.Register(logger)
icingadb.Register(logger)
})

var db *sqlx.DB
Expand Down Expand Up @@ -89,7 +88,7 @@ func (d *Database) Open(logger *logging.Logger) (*icingadb.DB, error) {
return setGaleraOpts(ctx, conn, wsrepSyncWait)
}

db = sqlx.NewDb(sql.OpenDB(icingadbDriver.NewConnector(c, logger, setWsrepSyncWait)), icingadbDriver.MySQL)
db = sqlx.NewDb(sql.OpenDB(icingadb.NewConnector(c, logger, setWsrepSyncWait)), icingadb.MySQL)
case "pgsql":
uri := &url.URL{
Scheme: "postgres",
Expand Down Expand Up @@ -143,7 +142,7 @@ func (d *Database) Open(logger *logging.Logger) (*icingadb.DB, error) {
return nil, errors.Wrap(err, "can't open pgsql database")
}

db = sqlx.NewDb(sql.OpenDB(icingadbDriver.NewConnector(connector, logger, nil)), icingadbDriver.PostgreSQL)
db = sqlx.NewDb(sql.OpenDB(icingadb.NewConnector(connector, logger, nil)), icingadb.PostgreSQL)
default:
return nil, unknownDbType(d.Type)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/icingadb/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/icinga/icingadb/internal"
"github.com/icinga/icingadb/pkg/com"
"github.com/icinga/icingadb/pkg/driver"
"github.com/icinga/icingadb/pkg/types"
"time"
)
Expand All @@ -20,10 +19,10 @@ type CleanupStmt struct {
// Build assembles the cleanup statement for the specified database driver with the given limit.
func (stmt *CleanupStmt) Build(driverName string, limit uint64) string {
switch driverName {
case driver.MySQL:
case MySQL:
return fmt.Sprintf(`DELETE FROM %[1]s WHERE environment_id = :environment_id AND %[2]s < :time
ORDER BY %[2]s LIMIT %[3]d`, stmt.Table, stmt.Column, limit)
case driver.PostgreSQL:
case PostgreSQL:
return fmt.Sprintf(`WITH rows AS (
SELECT %[1]s FROM %[2]s WHERE environment_id = :environment_id AND %[3]s < :time ORDER BY %[3]s LIMIT %[4]d
)
Expand Down
13 changes: 6 additions & 7 deletions pkg/icingadb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/icinga/icingadb/pkg/backoff"
"github.com/icinga/icingadb/pkg/com"
"github.com/icinga/icingadb/pkg/contracts"
"github.com/icinga/icingadb/pkg/driver"
"github.com/icinga/icingadb/pkg/logging"
"github.com/icinga/icingadb/pkg/periodic"
"github.com/icinga/icingadb/pkg/retry"
Expand Down Expand Up @@ -97,9 +96,9 @@ const (
func (db *DB) CheckSchema(ctx context.Context) error {
var expectedDbSchemaVersion uint16
switch db.DriverName() {
case driver.MySQL:
case MySQL:
expectedDbSchemaVersion = expectedMysqlSchemaVersion
case driver.PostgreSQL:
case PostgreSQL:
expectedDbSchemaVersion = expectedPostgresSchemaVersion
}

Expand Down Expand Up @@ -165,10 +164,10 @@ func (db *DB) BuildInsertIgnoreStmt(into interface{}) (string, int) {
var clause string

switch db.DriverName() {
case driver.MySQL:
case MySQL:
// MySQL treats UPDATE id = id as a no-op.
clause = fmt.Sprintf(`ON DUPLICATE KEY UPDATE "%s" = "%s"`, columns[0], columns[0])
case driver.PostgreSQL:
case PostgreSQL:
clause = fmt.Sprintf("ON CONFLICT ON CONSTRAINT pk_%s DO NOTHING", table)
}

Expand Down Expand Up @@ -228,10 +227,10 @@ func (db *DB) BuildUpsertStmt(subject interface{}) (stmt string, placeholders in

var clause, setFormat string
switch db.DriverName() {
case driver.MySQL:
case MySQL:
clause = "ON DUPLICATE KEY UPDATE"
setFormat = `"%[1]s" = VALUES("%[1]s")`
case driver.PostgreSQL:
case PostgreSQL:
clause = fmt.Sprintf("ON CONFLICT ON CONSTRAINT pk_%s DO UPDATE SET", table)
setFormat = `"%[1]s" = EXCLUDED."%[1]s"`
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/driver/driver.go → pkg/icingadb/driver.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package driver
package icingadb

import (
"context"
Expand Down
3 changes: 1 addition & 2 deletions pkg/icingadb/ha.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/icinga/icingadb/internal"
"github.com/icinga/icingadb/pkg/backoff"
"github.com/icinga/icingadb/pkg/com"
"github.com/icinga/icingadb/pkg/driver"
v1 "github.com/icinga/icingadb/pkg/icingadb/v1"
"github.com/icinga/icingadb/pkg/icingaredis"
icingaredisv1 "github.com/icinga/icingadb/pkg/icingaredis/v1"
Expand Down Expand Up @@ -251,7 +250,7 @@ func (h *HA) realize(ctx context.Context, s *icingaredisv1.IcingaStatus, t *type
isoLvl := sql.LevelSerializable
selectLock := ""

if h.db.DriverName() == driver.MySQL {
if h.db.DriverName() == MySQL {
// The RDBMS may actually be a Percona XtraDB Cluster which doesn't
// support serializable transactions, but only their following equivalent:
isoLvl = sql.LevelRepeatableRead
Expand Down

0 comments on commit 98fb197

Please sign in to comment.