Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/icingadb-migrate: avoid unnecessary config option #541

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 5 additions & 24 deletions cmd/icingadb-migrate/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type commentRow = struct {
}

func convertCommentRows(
env string, envId, endpointId icingadbTypes.Binary,
env string, envId icingadbTypes.Binary,
_ func(interface{}, string, ...interface{}), _ *sqlx.Tx, idoRows []commentRow,
) (icingaDbInserts, _ [][]contracts.Entity, checkpoint any) {
var commentHistory, acknowledgementHistory, allHistoryComment, allHistoryAck []contracts.Entity
Expand All @@ -71,7 +71,6 @@ func convertCommentRows(
CommentHistoryEntity: history.CommentHistoryEntity{CommentId: id},
HistoryTableMeta: history.HistoryTableMeta{
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand All @@ -93,7 +92,6 @@ func convertCommentRows(
HistoryMeta: history.HistoryMeta{
HistoryEntity: history.HistoryEntity{Id: hashAny([]string{env, "comment_add", row.Name})},
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand All @@ -111,7 +109,6 @@ func convertCommentRows(
HistoryMeta: history.HistoryMeta{
HistoryEntity: history.HistoryEntity{Id: hashAny([]string{env, "comment_remove", row.Name})},
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand Down Expand Up @@ -143,7 +140,6 @@ func convertCommentRows(
},
HistoryTableMeta: history.HistoryTableMeta{
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand Down Expand Up @@ -175,7 +171,6 @@ func convertCommentRows(
Id: hashAny([]any{env, "ack_set", name, setTs}),
},
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand All @@ -196,7 +191,6 @@ func convertCommentRows(
Id: hashAny([]any{env, "ack_clear", name, setTs}),
},
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand Down Expand Up @@ -240,7 +234,7 @@ type downtimeRow = struct {
}

func convertDowntimeRows(
env string, envId, endpointId icingadbTypes.Binary,
env string, envId icingadbTypes.Binary,
_ func(interface{}, string, ...interface{}), _ *sqlx.Tx, idoRows []downtimeRow,
) (icingaDbInserts, _ [][]contracts.Entity, checkpoint any) {
var downtimeHistory, allHistory, sla []contracts.Entity
Expand Down Expand Up @@ -287,7 +281,6 @@ func convertDowntimeRows(
DowntimeHistoryEntity: history.DowntimeHistoryEntity{DowntimeId: id},
HistoryTableMeta: history.HistoryTableMeta{
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand All @@ -313,7 +306,6 @@ func convertDowntimeRows(
HistoryMeta: history.HistoryMeta{
HistoryEntity: history.HistoryEntity{Id: hashAny([]string{env, "downtime_start", row.Name})},
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand All @@ -331,7 +323,6 @@ func convertDowntimeRows(
HistoryMeta: history.HistoryMeta{
HistoryEntity: history.HistoryEntity{Id: hashAny([]string{env, "downtime_end", row.Name})},
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand All @@ -352,7 +343,6 @@ func convertDowntimeRows(
DowntimeHistoryEntity: history.DowntimeHistoryEntity{DowntimeId: id},
HistoryTableMeta: history.HistoryTableMeta{
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand Down Expand Up @@ -385,7 +375,7 @@ type flappingRow = struct {
}

func convertFlappingRows(
env string, envId, endpointId icingadbTypes.Binary,
env string, envId icingadbTypes.Binary,
selectCache func(dest interface{}, query string, args ...interface{}), _ *sqlx.Tx, idoRows []flappingRow,
) (icingaDbInserts, icingaDbUpserts [][]contracts.Entity, checkpoint any) {
if len(idoRows) < 1 {
Expand Down Expand Up @@ -446,7 +436,6 @@ func convertFlappingRows(
},
HistoryTableMeta: history.HistoryTableMeta{
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand All @@ -466,7 +455,6 @@ func convertFlappingRows(
Id: hashAny([]interface{}{env, "flapping_end", name, startTime}),
},
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand All @@ -486,7 +474,6 @@ func convertFlappingRows(
},
HistoryTableMeta: history.HistoryTableMeta{
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand All @@ -505,7 +492,6 @@ func convertFlappingRows(
Id: hashAny([]interface{}{env, "flapping_start", name, startTime}),
},
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand Down Expand Up @@ -540,7 +526,7 @@ type notificationRow = struct {
}

func convertNotificationRows(
env string, envId, endpointId icingadbTypes.Binary,
env string, envId icingadbTypes.Binary,
selectCache func(dest interface{}, query string, args ...interface{}), ido *sqlx.Tx, idoRows []notificationRow,
) (icingaDbInserts, _ [][]contracts.Entity, checkpoint any) {
if len(idoRows) < 1 {
Expand Down Expand Up @@ -632,7 +618,6 @@ func convertNotificationRows(
},
HistoryTableMeta: history.HistoryTableMeta{
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand All @@ -655,7 +640,6 @@ func convertNotificationRows(
HistoryMeta: history.HistoryMeta{
HistoryEntity: history.HistoryEntity{Id: id},
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand Down Expand Up @@ -735,7 +719,7 @@ type stateRow = struct {
}

func convertStateRows(
env string, envId, endpointId icingadbTypes.Binary,
env string, envId icingadbTypes.Binary,
selectCache func(dest interface{}, query string, args ...interface{}), _ *sqlx.Tx, idoRows []stateRow,
) (icingaDbInserts, _ [][]contracts.Entity, checkpoint any) {
if len(idoRows) < 1 {
Expand Down Expand Up @@ -782,7 +766,6 @@ func convertStateRows(
},
HistoryTableMeta: history.HistoryTableMeta{
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand All @@ -804,7 +787,6 @@ func convertStateRows(
HistoryMeta: history.HistoryMeta{
HistoryEntity: history.HistoryEntity{Id: id},
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand All @@ -825,7 +807,6 @@ func convertStateRows(
},
HistoryTableMeta: history.HistoryTableMeta{
EnvironmentId: envId,
EndpointId: endpointId,
ObjectType: typ,
HostId: hostId,
ServiceId: serviceId,
Expand Down
15 changes: 4 additions & 11 deletions cmd/icingadb-migrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"crypto/sha1"
"database/sql"
_ "embed"
"encoding/hex"
Expand Down Expand Up @@ -52,8 +51,6 @@ type Config struct {
Icinga2 struct {
// Env specifies the environment ID, hex.
Env string `yaml:"env"`
// Endpoint specifies the name on the main endpoint writing to IDO.
Endpoint string `yaml:"endpoint"`
} `yaml:"icinga2"`
}

Expand Down Expand Up @@ -354,24 +351,22 @@ func fillCache() {

// migrate does the actual migration.
func migrate(c *Config, idb *icingadb.DB, envId []byte) {
endpointId := sha1.Sum([]byte(c.Icinga2.Endpoint))

progress := mpb.New()
for _, ht := range types {
ht.setupBar(progress, ht.total)
}

types.forEach(func(ht *historyType) {
ht.migrate(c, idb, envId, endpointId, ht)
ht.migrate(c, idb, envId, ht)
})

progress.Wait()
}

// migrate does the actual migration for one history type.
func migrateOneType[IdoRow any](
c *Config, idb *icingadb.DB, envId []byte, endpointId [sha1.Size]byte, ht *historyType,
convertRows func(env string, envId, endpointId icingadbTypes.Binary,
c *Config, idb *icingadb.DB, envId []byte, ht *historyType,
convertRows func(env string, envId icingadbTypes.Binary,
selectCache func(dest interface{}, query string, args ...interface{}), ido *sqlx.Tx,
idoRows []IdoRow) (icingaDbInserts, icingaDbUpserts [][]contracts.Entity, checkpoint any),
) {
Expand Down Expand Up @@ -428,9 +423,7 @@ func migrateOneType[IdoRow any](
ht, ht.migrationQuery, args, ht.lastId,
func(idoRows []IdoRow) (checkpoint interface{}) {
// ... convert them, ...
inserts, upserts, lastIdoId := convertRows(
c.Icinga2.Env, envId, endpointId[:], selectCache, ht.snapshot, idoRows,
)
inserts, upserts, lastIdoId := convertRows(c.Icinga2.Env, envId, selectCache, ht.snapshot, idoRows)

// ... and insert them:

Expand Down
22 changes: 11 additions & 11 deletions cmd/icingadb-migrate/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ type historyType struct {
// migrationQuery SELECTs source data for actual migration.
migrationQuery string
// migrate does the actual migration.
migrate func(c *Config, idb *icingadb.DB, envId []byte, endpointId [sha1.Size]byte, ht *historyType)
migrate func(c *Config, idb *icingadb.DB, envId []byte, ht *historyType)

// cacheFile locates <name>.sqlite3.
cacheFile string
Expand Down Expand Up @@ -246,8 +246,8 @@ var types = historyTypes{
// Manual deletion time wins vs. time of expiration which never happens due to manual deletion.
idoEndColumns: []string{"deletion_time", "expiration_time"},
migrationQuery: commentMigrationQuery,
migrate: func(c *Config, idb *icingadb.DB, envId []byte, endpId [20]byte, ht *historyType) {
migrateOneType(c, idb, envId, endpId, ht, convertCommentRows)
migrate: func(c *Config, idb *icingadb.DB, envId []byte, ht *historyType) {
migrateOneType(c, idb, envId, ht, convertCommentRows)
},
},
{
Expand All @@ -258,8 +258,8 @@ var types = historyTypes{
idoStartColumns: []string{"actual_start_time", "scheduled_start_time"},
idoEndColumns: []string{"actual_end_time", "scheduled_end_time"},
migrationQuery: downtimeMigrationQuery,
migrate: func(c *Config, idb *icingadb.DB, envId []byte, endpId [20]byte, ht *historyType) {
migrateOneType(c, idb, envId, endpId, ht, convertDowntimeRows)
migrate: func(c *Config, idb *icingadb.DB, envId []byte, ht *historyType) {
migrateOneType(c, idb, envId, ht, convertDowntimeRows)
},
},
{
Expand All @@ -276,8 +276,8 @@ var types = historyTypes{
})
},
migrationQuery: flappingMigrationQuery,
migrate: func(c *Config, idb *icingadb.DB, envId []byte, endpId [20]byte, ht *historyType) {
migrateOneType(c, idb, envId, endpId, ht, convertFlappingRows)
migrate: func(c *Config, idb *icingadb.DB, envId []byte, ht *historyType) {
migrateOneType(c, idb, envId, ht, convertFlappingRows)
},
},
{
Expand All @@ -294,8 +294,8 @@ var types = historyTypes{
},
cacheLimitQuery: "SELECT MAX(history_id) FROM previous_hard_state",
migrationQuery: notificationMigrationQuery,
migrate: func(c *Config, idb *icingadb.DB, envId []byte, endpId [20]byte, ht *historyType) {
migrateOneType(c, idb, envId, endpId, ht, convertNotificationRows)
migrate: func(c *Config, idb *icingadb.DB, envId []byte, ht *historyType) {
migrateOneType(c, idb, envId, ht, convertNotificationRows)
},
},
{
Expand All @@ -310,8 +310,8 @@ var types = historyTypes{
},
cacheLimitQuery: "SELECT MAX(history_id) FROM previous_hard_state",
migrationQuery: stateMigrationQuery,
migrate: func(c *Config, idb *icingadb.DB, envId []byte, endpId [20]byte, ht *historyType) {
migrateOneType(c, idb, envId, endpId, ht, convertStateRows)
migrate: func(c *Config, idb *icingadb.DB, envId []byte, ht *historyType) {
migrateOneType(c, idb, envId, ht, convertStateRows)
},
},
}
2 changes: 0 additions & 2 deletions doc/06-Migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ Create a YAML file like this somewhere:
icinga2:
# Content of /var/lib/icinga2/icingadb.env
env: "da39a3ee5e6b4b0d3255bfef95601890afBADHEX"
# Name of the main Icinga 2 endpoint writing to IDO
endpoint: master-1
# IDO database
ido:
type: pgsql
Expand Down