From a844d2b09118ad042b6f2231286a4705dbf4589e Mon Sep 17 00:00:00 2001 From: Yening Qin <710leo@gmail.com> Date: Fri, 21 Jun 2024 18:13:58 +0800 Subject: [PATCH] fix: use postgresql (#2008) --- .../initsql_for_postgres/a-n9e-for-Postgres.sql | 16 ++++++++-------- go.mod | 2 +- go.sum | 4 ++-- models/migrate/migrate.go | 14 +++++++------- models/task_tpl.go | 15 +++++++++++---- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/docker/compose-postgres/initsql_for_postgres/a-n9e-for-Postgres.sql b/docker/compose-postgres/initsql_for_postgres/a-n9e-for-Postgres.sql index 80d0215f5..7c5999f04 100644 --- a/docker/compose-postgres/initsql_for_postgres/a-n9e-for-Postgres.sql +++ b/docker/compose-postgres/initsql_for_postgres/a-n9e-for-Postgres.sql @@ -8,7 +8,7 @@ CREATE TABLE users ( portrait varchar(255) not null default '', roles varchar(255) not null, contacts varchar(1024), - maintainer boolean not null default false, + maintainer int not null default 0, belong varchar(16) not null default '', last_active_time bigint not null default 0, create_at bigint not null default 0, @@ -60,8 +60,8 @@ CREATE TABLE configs ( ckey varchar(191) not null, cval text not null default '', note varchar(1024) not null default '', - external boolean not null default false, - encrypted boolean not null default false, + external int not null default 0, + encrypted int not null default 0, create_at bigint not null default 0, create_by varchar(64) not null default '', update_at bigint not null default 0, @@ -378,7 +378,7 @@ COMMENT ON COLUMN alert_mute.disabled IS '0:enabled 1:disabled'; CREATE TABLE alert_subscribe ( id bigserial, name varchar(255) not null default '', - disabled boolean not null default false, + disabled int not null default 0, group_id bigint not null default 0, prod varchar(255) not null default '', cate varchar(128) not null, @@ -397,7 +397,7 @@ CREATE TABLE alert_subscribe ( rule_ids VARCHAR(1024) DEFAULT '', webhooks text not null, extra_config text not null, - redefine_webhooks boolean default false, + redefine_webhooks int default 0, for_duration bigint not null default 0, create_at bigint not null default 0, create_by varchar(64) not null default '', @@ -744,7 +744,7 @@ CREATE TABLE datasource status varchar(255) not null default '', http varchar(4096) not null default '', auth varchar(8192) not null default '', - is_default smallint not null default 0, + is_default boolean not null default false, created_at bigint not null default 0, created_by varchar(64) not null default '', updated_at bigint not null default 0, @@ -845,7 +845,7 @@ CREATE TABLE metric_filter ( update_by VARCHAR(191) NOT NULL DEFAULT '' ); -CREATE INDEX idx_name ON metric_filter (name); +CREATE INDEX idx_metric_filter_name ON metric_filter (name); CREATE TABLE board_busigroup ( busi_group_id BIGINT NOT NULL DEFAULT 0, @@ -882,6 +882,6 @@ CREATE TABLE builtin_payloads ( ); CREATE INDEX idx_component ON builtin_payloads (component); -CREATE INDEX idx_name ON builtin_payloads (name); +CREATE INDEX idx_builtin_payloads_name ON builtin_payloads (name); CREATE INDEX idx_cate ON builtin_payloads (cate); CREATE INDEX idx_type ON builtin_payloads (type); \ No newline at end of file diff --git a/go.mod b/go.mod index eb99e75ea..98f83d09b 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/expr-lang/expr v1.16.1 - github.com/flashcatcloud/ibex v1.3.4 + github.com/flashcatcloud/ibex v1.3.5 github.com/gin-contrib/pprof v1.4.0 github.com/gin-gonic/gin v1.9.1 github.com/go-ldap/ldap/v3 v3.4.4 diff --git a/go.sum b/go.sum index 7b36e483b..046f4363b 100644 --- a/go.sum +++ b/go.sum @@ -47,8 +47,8 @@ github.com/fatih/camelcase v1.0.0 h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8 github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc= github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= -github.com/flashcatcloud/ibex v1.3.4 h1:s5MgQmDIYR18liBKPNl96kC/h1jOTZjIOlUWeSx0710= -github.com/flashcatcloud/ibex v1.3.4/go.mod h1:T8hbMUySK2q6cXUaYp0AUVeKkU9Od2LjzwmB5lmTRBM= +github.com/flashcatcloud/ibex v1.3.5 h1:8GOOf5+aJT0TP/MC6izz7CO5JKJSdKVFBwL0vQp93Nc= +github.com/flashcatcloud/ibex v1.3.5/go.mod h1:T8hbMUySK2q6cXUaYp0AUVeKkU9Od2LjzwmB5lmTRBM= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= github.com/garyburd/redigo v1.6.2/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY= diff --git a/models/migrate/migrate.go b/models/migrate/migrate.go index 86888a248..b3c9c574a 100644 --- a/models/migrate/migrate.go +++ b/models/migrate/migrate.go @@ -9,7 +9,6 @@ import ( imodels "github.com/flashcatcloud/ibex/src/models" "github.com/toolkits/pkg/logger" "gorm.io/driver/mysql" - "gorm.io/driver/postgres" "gorm.io/gorm" ) @@ -23,9 +22,8 @@ func MigrateIbexTables(db *gorm.DB) { switch db.Dialector.(type) { case *mysql.Dialector: tableOptions = "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" - case *postgres.Dialector: - tableOptions = "ENCODING='UTF8'" } + if tableOptions != "" { db = db.Set("gorm:table_options", tableOptions) } @@ -52,8 +50,6 @@ func MigrateTables(db *gorm.DB) error { switch db.Dialector.(type) { case *mysql.Dialector: tableOptions = "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" - case *postgres.Dialector: - tableOptions = "ENCODING='UTF8'" } if tableOptions != "" { db = db.Set("gorm:table_options", tableOptions) @@ -62,12 +58,16 @@ func MigrateTables(db *gorm.DB) error { dts := []interface{}{&RecordingRule{}, &AlertRule{}, &AlertSubscribe{}, &AlertMute{}, &TaskRecord{}, &ChartShare{}, &Target{}, &Configs{}, &Datasource{}, &NotifyTpl{}, &Board{}, &BoardBusigroup{}, &Users{}, &SsoConfig{}, &models.BuiltinMetric{}, - &models.MetricFilter{}, &models.BuiltinComponent{}, &models.BuiltinPayload{}} + &models.MetricFilter{}, &models.BuiltinComponent{}} if !columnHasIndex(db, &AlertHisEvent{}, "last_eval_time") { dts = append(dts, &AlertHisEvent{}) } + if !db.Migrator().HasTable(&models.BuiltinPayload{}) { + dts = append(dts, &models.BuiltinPayload{}) + } + for _, dt := range dts { err := db.AutoMigrate(dt) if err != nil { @@ -209,7 +209,7 @@ type Target struct { } type Datasource struct { - IsDefault bool `gorm:"column:is_default;int;not null;default:0;comment:is default datasource"` + IsDefault bool `gorm:"column:is_default;type:boolean;not null;comment:is default datasource"` } type Configs struct { diff --git a/models/task_tpl.go b/models/task_tpl.go index 49091d934..096517c69 100644 --- a/models/task_tpl.go +++ b/models/task_tpl.go @@ -187,6 +187,11 @@ func (t *TaskTpl) CleanFields() error { return nil } +type TaskTplHost struct { + Id int64 `json:"id"` + Host string `json:"host"` +} + func (t *TaskTpl) Save(ctx *ctx.Context, hosts []string) error { if err := t.CleanFields(); err != nil { return err @@ -212,10 +217,12 @@ func (t *TaskTpl) Save(ctx *ctx.Context, hosts []string) error { continue } - err := tx.Table("task_tpl_host").Create(map[string]interface{}{ - "id": t.Id, - "host": host, - }).Error + taskTplHost := TaskTplHost{ + Id: t.Id, + Host: host, + } + + err := tx.Table("task_tpl_host").Create(&taskTplHost).Error if err != nil { return err