Skip to content

Commit

Permalink
fix: use postgresql (#2008)
Browse files Browse the repository at this point in the history
  • Loading branch information
710leo authored Jun 21, 2024
1 parent 69ca7f3 commit a844d2b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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 '',
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
14 changes: 7 additions & 7 deletions models/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 11 additions & 4 deletions models/task_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit a844d2b

Please sign in to comment.