fix: move migrations to bun (#7055)

* fix: move migrations to bun

* fix: use anonymous structs and move modes to types package

* fix: minor changes after tests

* fix: remove bun relations and add foreign keys

* fix: minor changes

* Update pkg/types/agent.go

Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>

* fix: address minor comments

* fix: use bun create index

* fix: remove extra comma

---------

Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
This commit is contained in:
Nityananda Gohain 2025-02-12 09:58:49 +05:30 committed by GitHub
parent 42fad23cb0
commit 962e75c6d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 794 additions and 372 deletions

View File

@ -2,6 +2,7 @@ package sqlmigration
import ( import (
"context" "context"
"time"
"github.com/uptrace/bun" "github.com/uptrace/bun"
"github.com/uptrace/bun/migrate" "github.com/uptrace/bun/migrate"
@ -27,12 +28,16 @@ func (migration *addDataMigrations) Register(migrations *migrate.Migrations) err
func (migration *addDataMigrations) Up(ctx context.Context, db *bun.DB) error { func (migration *addDataMigrations) Up(ctx context.Context, db *bun.DB) error {
// table:data_migrations // table:data_migrations
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS data_migrations ( if _, err := db.NewCreateTable().
id SERIAL PRIMARY KEY, Model(&struct {
version VARCHAR(255) NOT NULL UNIQUE, bun.BaseModel `bun:"table:data_migrations"`
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, ID int `bun:"id,pk,autoincrement"`
succeeded BOOLEAN NOT NULL DEFAULT FALSE Version string `bun:"version,unique,notnull,type:VARCHAR(255)"`
);`); err != nil { CreatedAt time.Time `bun:"created_at,notnull,default:current_timestamp"`
Succeeded bool `bun:"succeeded,notnull,default:false"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }

View File

@ -2,6 +2,7 @@ package sqlmigration
import ( import (
"context" "context"
"time"
"github.com/uptrace/bun" "github.com/uptrace/bun"
"github.com/uptrace/bun/migrate" "github.com/uptrace/bun/migrate"
@ -27,92 +28,125 @@ func (migration *addOrganization) Register(migrations *migrate.Migrations) error
} }
func (migration *addOrganization) Up(ctx context.Context, db *bun.DB) error { func (migration *addOrganization) Up(ctx context.Context, db *bun.DB) error {
// table:invites
if _, err := db.NewRaw(`CREATE TABLE IF NOT EXISTS invites (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
token TEXT NOT NULL,
created_at INTEGER NOT NULL,
role TEXT NOT NULL,
org_id TEXT NOT NULL,
FOREIGN KEY(org_id) REFERENCES organizations(id)
)`).Exec(ctx); err != nil {
return err
}
// table:organizations // table:organizations
if _, err := db.NewRaw(`CREATE TABLE IF NOT EXISTS organizations ( if _, err := db.NewCreateTable().
id TEXT PRIMARY KEY, Model(&struct {
name TEXT NOT NULL, bun.BaseModel `bun:"table:organizations"`
created_at INTEGER NOT NULL, ID string `bun:"id,pk,type:text"`
is_anonymous INTEGER NOT NULL DEFAULT 0 CHECK(is_anonymous IN (0,1)), Name string `bun:"name,type:text,notnull"`
has_opted_updates INTEGER NOT NULL DEFAULT 1 CHECK(has_opted_updates IN (0,1)) CreatedAt int `bun:"created_at,notnull"`
)`).Exec(ctx); err != nil { IsAnonymous int `bun:"is_anonymous,notnull,default:0,CHECK(is_anonymous IN (0,1))"`
return err HasOptedUpdates int `bun:"has_opted_updates,notnull,default:1,CHECK(has_opted_updates IN (0,1))"`
} }{}).
IfNotExists().
// table:users Exec(ctx); err != nil {
if _, err := db.NewRaw(`CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
created_at INTEGER NOT NULL,
profile_picture_url TEXT,
group_id TEXT NOT NULL,
org_id TEXT NOT NULL,
FOREIGN KEY(group_id) REFERENCES groups(id),
FOREIGN KEY(org_id) REFERENCES organizations(id)
)`).Exec(ctx); err != nil {
return err return err
} }
// table:groups // table:groups
if _, err := db.NewRaw(`CREATE TABLE IF NOT EXISTS groups ( if _, err := db.NewCreateTable().
id TEXT PRIMARY KEY, Model(&struct {
name TEXT NOT NULL UNIQUE bun.BaseModel `bun:"table:groups"`
)`).Exec(ctx); err != nil { ID string `bun:"id,pk,type:text"`
Name string `bun:"name,type:text,notnull,unique"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err
}
// table:users
if _, err := db.NewCreateTable().
Model(&struct {
bun.BaseModel `bun:"table:users"`
ID string `bun:"id,pk,type:text"`
Name string `bun:"name,type:text,notnull"`
Email string `bun:"email,type:text,notnull,unique"`
Password string `bun:"password,type:text,notnull"`
CreatedAt int `bun:"created_at,notnull"`
ProfilePictureURL string `bun:"profile_picture_url,type:text"`
GroupID string `bun:"group_id,type:text,notnull"`
OrgID string `bun:"org_id,type:text,notnull"`
}{}).
ForeignKey(`("org_id") REFERENCES "organizations" ("id")`).
ForeignKey(`("group_id") REFERENCES "groups" ("id")`).
IfNotExists().
Exec(ctx); err != nil {
return err
}
// table:invites
if _, err := db.NewCreateTable().
Model(&struct {
bun.BaseModel `bun:"table:invites"`
ID int `bun:"id,pk,autoincrement"`
Name string `bun:"name,type:text,notnull"`
Email string `bun:"email,type:text,notnull,unique"`
Token string `bun:"token,type:text,notnull"`
CreatedAt int `bun:"created_at,notnull"`
Role string `bun:"role,type:text,notnull"`
OrgID string `bun:"org_id,type:text,notnull"`
}{}).
ForeignKey(`("org_id") REFERENCES "organizations" ("id")`).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
// table:reset_password_request // table:reset_password_request
if _, err := db.NewRaw(`CREATE TABLE IF NOT EXISTS reset_password_request ( if _, err := db.NewCreateTable().
id INTEGER PRIMARY KEY AUTOINCREMENT, Model(&struct {
user_id TEXT NOT NULL, bun.BaseModel `bun:"table:reset_password_request"`
token TEXT NOT NULL, ID int `bun:"id,pk,autoincrement"`
FOREIGN KEY(user_id) REFERENCES users(id) Token string `bun:"token,type:text,notnull"`
)`).Exec(ctx); err != nil { UserID string `bun:"user_id,type:text,notnull"`
}{}).
ForeignKey(`("user_id") REFERENCES "users" ("id")`).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
// table:user_flags // table:user_flags
if _, err := db.NewRaw(`CREATE TABLE IF NOT EXISTS user_flags ( if _, err := db.NewCreateTable().
user_id TEXT PRIMARY KEY, Model(&struct {
flags TEXT, bun.BaseModel `bun:"table:user_flags"`
FOREIGN KEY(user_id) REFERENCES users(id) UserID string `bun:"user_id,pk,type:text,notnull"`
)`).Exec(ctx); err != nil { Flags string `bun:"flags,type:text"`
}{}).
ForeignKey(`("user_id") REFERENCES "users" ("id")`).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
// table:apdex_settings // table:apdex_settings
if _, err := db.NewRaw(`CREATE TABLE IF NOT EXISTS apdex_settings ( if _, err := db.NewCreateTable().
service_name TEXT PRIMARY KEY, Model(&struct {
threshold FLOAT NOT NULL, bun.BaseModel `bun:"table:apdex_settings"`
exclude_status_codes TEXT NOT NULL ServiceName string `bun:"service_name,pk,type:text"`
)`).Exec(ctx); err != nil { Threshold float64 `bun:"threshold,type:float,notnull"`
ExcludeStatusCodes string `bun:"exclude_status_codes,type:text,notnull"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
// table:ingestion_keys // table:ingestion_keys
if _, err := db.NewRaw(`CREATE TABLE IF NOT EXISTS ingestion_keys ( if _, err := db.NewCreateTable().
key_id TEXT PRIMARY KEY, Model(&struct {
name TEXT, bun.BaseModel `bun:"table:ingestion_keys"`
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, KeyId string `bun:"key_id,pk,type:text"`
ingestion_key TEXT NOT NULL, Name string `bun:"name,type:text"`
ingestion_url TEXT NOT NULL, CreatedAt time.Time `bun:"created_at,default:current_timestamp"`
data_region TEXT NOT NULL IngestionKey string `bun:"ingestion_key,type:text,notnull"`
)`).Exec(ctx); err != nil { IngestionURL string `bun:"ingestion_url,type:text,notnull"`
DataRegion string `bun:"data_region,type:text,notnull"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }

View File

@ -28,24 +28,30 @@ func (migration *addPreferences) Register(migrations *migrate.Migrations) error
func (migration *addPreferences) Up(ctx context.Context, db *bun.DB) error { func (migration *addPreferences) Up(ctx context.Context, db *bun.DB) error {
// table:user_preference // table:user_preference
if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS user_preference ( if _, err := db.NewCreateTable().
preference_id TEXT NOT NULL, Model(&struct {
preference_value TEXT, bun.BaseModel `bun:"table:user_preference"`
user_id TEXT NOT NULL, PreferenceID string `bun:"preference_id,type:text,pk"`
PRIMARY KEY (preference_id,user_id), PreferenceValue string `bun:"preference_value,type:text"`
FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE UserID string `bun:"user_id,type:text,pk"`
)`); err != nil { }{}).
IfNotExists().
ForeignKey(`("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE`).
Exec(ctx); err != nil {
return err return err
} }
// table:org_preference // table:org_preference
if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS org_preference ( if _, err := db.NewCreateTable().
preference_id TEXT NOT NULL, Model(&struct {
preference_value TEXT, bun.BaseModel `bun:"table:org_preference"`
org_id TEXT NOT NULL, PreferenceID string `bun:"preference_id,pk,type:text,notnull"`
PRIMARY KEY (preference_id,org_id), PreferenceValue string `bun:"preference_value,type:text,notnull"`
FOREIGN KEY (org_id) REFERENCES organizations(id) ON UPDATE CASCADE ON DELETE CASCADE OrgID string `bun:"org_id,pk,type:text,notnull"`
);`); err != nil { }{}).
ForeignKey(`("org_id") REFERENCES "organizations" ("id") ON DELETE CASCADE ON UPDATE CASCADE`).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }

View File

@ -2,6 +2,7 @@ package sqlmigration
import ( import (
"context" "context"
"time"
"github.com/uptrace/bun" "github.com/uptrace/bun"
"github.com/uptrace/bun/migrate" "github.com/uptrace/bun/migrate"
@ -28,125 +29,91 @@ func (migration *addDashboards) Register(migrations *migrate.Migrations) error {
func (migration *addDashboards) Up(ctx context.Context, db *bun.DB) error { func (migration *addDashboards) Up(ctx context.Context, db *bun.DB) error {
// table:dashboards // table:dashboards
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS dashboards ( if _, err := db.NewCreateTable().
id INTEGER PRIMARY KEY AUTOINCREMENT, Model(&struct {
uuid TEXT NOT NULL UNIQUE, bun.BaseModel `bun:"table:dashboards"`
created_at datetime NOT NULL, ID int `bun:"id,pk,autoincrement"`
updated_at datetime NOT NULL, UUID string `bun:"uuid,type:text,notnull,unique"`
data TEXT NOT NULL CreatedAt time.Time `bun:"created_at,type:datetime,notnull"`
);`); err != nil { CreatedBy string `bun:"created_by,type:text,notnull"`
UpdatedAt time.Time `bun:"updated_at,type:datetime,notnull"`
UpdatedBy string `bun:"updated_by,type:text,notnull"`
Data string `bun:"data,type:text,notnull"`
Locked int `bun:"locked,notnull,default:0"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
// table:rules // table:rules
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS rules ( if _, err := db.NewCreateTable().
id INTEGER PRIMARY KEY AUTOINCREMENT, Model(&struct {
updated_at datetime NOT NULL, bun.BaseModel `bun:"table:rules"`
deleted INTEGER DEFAULT 0, ID int `bun:"id,pk,autoincrement"`
data TEXT NOT NULL CreatedAt time.Time `bun:"created_at,type:datetime,notnull"`
);`); err != nil { CreatedBy string `bun:"created_by,type:text,notnull"`
UpdatedAt time.Time `bun:"updated_at,type:datetime,notnull"`
UpdatedBy string `bun:"updated_by,type:text,notnull"`
Deleted int `bun:"deleted,notnull,default:0"`
Data string `bun:"data,type:text,notnull"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
// table:notification_channels // table:notification_channels
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS notification_channels ( if _, err := db.NewCreateTable().
id INTEGER PRIMARY KEY AUTOINCREMENT, Model(&struct {
created_at datetime NOT NULL, bun.BaseModel `bun:"table:notification_channels"`
updated_at datetime NOT NULL, ID int `bun:"id,pk,autoincrement"`
name TEXT NOT NULL UNIQUE, CreatedAt time.Time `bun:"created_at,type:datetime,notnull"`
type TEXT NOT NULL, UpdatedAt time.Time `bun:"updated_at,type:datetime,notnull"`
deleted INTEGER DEFAULT 0, Name string `bun:"name,type:text,notnull,unique"`
data TEXT NOT NULL Type string `bun:"type,type:text,notnull"`
);`); err != nil { Deleted int `bun:"deleted,notnull,default:0"`
Data string `bun:"data,type:text,notnull"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
// table:planned_maintenance // table:planned_maintenance
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS planned_maintenance ( if _, err := db.NewCreateTable().
id INTEGER PRIMARY KEY AUTOINCREMENT, Model(&struct {
name TEXT NOT NULL, bun.BaseModel `bun:"table:planned_maintenance"`
description TEXT, ID int `bun:"id,pk,autoincrement"`
alert_ids TEXT, Name string `bun:"name,type:text,notnull"`
schedule TEXT NOT NULL, Description string `bun:"description,type:text"`
created_at datetime NOT NULL, AlertIDs string `bun:"alert_ids,type:text"`
created_by TEXT NOT NULL, Schedule string `bun:"schedule,type:text,notnull"`
updated_at datetime NOT NULL, CreatedAt time.Time `bun:"created_at,type:datetime,notnull"`
updated_by TEXT NOT NULL CreatedBy string `bun:"created_by,type:text,notnull"`
);`); err != nil { UpdatedAt time.Time `bun:"updated_at,type:datetime,notnull"`
UpdatedBy string `bun:"updated_by,type:text,notnull"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
// table:ttl_status // table:ttl_status
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS ttl_status ( if _, err := db.NewCreateTable().
id INTEGER PRIMARY KEY AUTOINCREMENT, Model(&struct {
transaction_id TEXT NOT NULL, bun.BaseModel `bun:"table:ttl_status"`
created_at datetime NOT NULL, ID int `bun:"id,pk,autoincrement"`
updated_at datetime NOT NULL, TransactionID string `bun:"transaction_id,type:text,notnull"`
table_name TEXT NOT NULL, CreatedAt time.Time `bun:"created_at,type:datetime,notnull"`
ttl INTEGER DEFAULT 0, UpdatedAt time.Time `bun:"updated_at,type:datetime,notnull"`
cold_storage_ttl INTEGER DEFAULT 0, TableName string `bun:"table_name,type:text,notnull"`
status TEXT NOT NULL TTL int `bun:"ttl,notnull,default:0"`
);`); err != nil { ColdStorageTTL int `bun:"cold_storage_ttl,notnull,default:0"`
return err Status string `bun:"status,type:text,notnull"`
} }{}).
IfNotExists().
// table:rules op:add column created_at Exec(ctx); err != nil {
if _, err := db.
NewAddColumn().
Table("rules").
ColumnExpr("created_at datetime").
Apply(WrapIfNotExists(ctx, db, "rules", "created_at")).
Exec(ctx); err != nil && err != ErrNoExecute {
return err
}
// table:rules op:add column created_by
if _, err := db.
NewAddColumn().
Table("rules").
ColumnExpr("created_by TEXT").
Apply(WrapIfNotExists(ctx, db, "rules", "created_by")).
Exec(ctx); err != nil && err != ErrNoExecute {
return err
}
// table:rules op:add column updated_by
if _, err := db.
NewAddColumn().
Table("rules").
ColumnExpr("updated_by TEXT").
Apply(WrapIfNotExists(ctx, db, "rules", "updated_by")).
Exec(ctx); err != nil && err != ErrNoExecute {
return err
}
// table:dashboards op:add column created_by
if _, err := db.
NewAddColumn().
Table("dashboards").
ColumnExpr("created_by TEXT").
Apply(WrapIfNotExists(ctx, db, "dashboards", "created_by")).
Exec(ctx); err != nil && err != ErrNoExecute {
return err
}
// table:dashboards op:add column updated_by
if _, err := db.
NewAddColumn().
Table("dashboards").
ColumnExpr("updated_by TEXT").
Apply(WrapIfNotExists(ctx, db, "dashboards", "updated_by")).
Exec(ctx); err != nil && err != ErrNoExecute {
return err
}
// table:dashboards op:add column locked
if _, err := db.
NewAddColumn().
Table("dashboards").
ColumnExpr("locked INTEGER DEFAULT 0").
Apply(WrapIfNotExists(ctx, db, "dashboards", "locked")).
Exec(ctx); err != nil && err != ErrNoExecute {
return err return err
} }

View File

@ -2,6 +2,7 @@ package sqlmigration
import ( import (
"context" "context"
"time"
"github.com/uptrace/bun" "github.com/uptrace/bun"
"github.com/uptrace/bun/migrate" "github.com/uptrace/bun/migrate"
@ -28,19 +29,23 @@ func (migration *addSavedViews) Register(migrations *migrate.Migrations) error {
func (migration *addSavedViews) Up(ctx context.Context, db *bun.DB) error { func (migration *addSavedViews) Up(ctx context.Context, db *bun.DB) error {
// table:saved_views op:create // table:saved_views op:create
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS saved_views ( if _, err := db.NewCreateTable().
uuid TEXT PRIMARY KEY, Model(&struct {
name TEXT NOT NULL, bun.BaseModel `bun:"table:saved_views"`
category TEXT NOT NULL, UUID string `bun:"uuid,pk,type:text"`
created_at datetime NOT NULL, Name string `bun:"name,type:text,notnull"`
created_by TEXT, Category string `bun:"category,type:text,notnull"`
updated_at datetime NOT NULL, CreatedAt time.Time `bun:"created_at,type:datetime,notnull"`
updated_by TEXT, CreatedBy string `bun:"created_by,type:text"`
source_page TEXT NOT NULL, UpdatedAt time.Time `bun:"updated_at,type:datetime,notnull"`
tags TEXT, UpdatedBy string `bun:"updated_by,type:text"`
data TEXT NOT NULL, SourcePage string `bun:"source_page,type:text,notnull"`
extra_data TEXT Tags string `bun:"tags,type:text"`
);`); err != nil { Data string `bun:"data,type:text,notnull"`
ExtraData string `bun:"extra_data,type:text"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }

View File

@ -2,6 +2,7 @@ package sqlmigration
import ( import (
"context" "context"
"time"
"github.com/uptrace/bun" "github.com/uptrace/bun"
"github.com/uptrace/bun/migrate" "github.com/uptrace/bun/migrate"
@ -27,59 +28,69 @@ func (migration *addAgents) Register(migrations *migrate.Migrations) error {
} }
func (migration *addAgents) Up(ctx context.Context, db *bun.DB) error { func (migration *addAgents) Up(ctx context.Context, db *bun.DB) error {
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS agents ( if _, err := db.NewCreateTable().
agent_id TEXT PRIMARY KEY UNIQUE, Model(&struct {
started_at datetime NOT NULL, bun.BaseModel `bun:"table:agents"`
terminated_at datetime, AgentID string `bun:"agent_id,pk,type:text,unique"`
current_status TEXT NOT NULL, StartedAt time.Time `bun:"started_at,type:datetime,notnull"`
effective_config TEXT NOT NULL TerminatedAt time.Time `bun:"terminated_at,type:datetime"`
);`); err != nil { CurrentStatus string `bun:"current_status,type:text,notnull"`
EffectiveConfig string `bun:"effective_config,type:text,notnull"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS agent_config_versions( if _, err := db.NewCreateTable().
id TEXT PRIMARY KEY, Model(&struct {
created_by TEXT, bun.BaseModel `bun:"table:agent_config_versions"`
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, ID string `bun:"id,pk,type:text"`
updated_by TEXT, CreatedBy string `bun:"created_by,type:text"`
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, CreatedAt time.Time `bun:"created_at,default:CURRENT_TIMESTAMP"`
version INTEGER DEFAULT 1, UpdatedBy string `bun:"updated_by,type:text"`
active int, UpdatedAt time.Time `bun:"updated_at,default:CURRENT_TIMESTAMP"`
is_valid int, Version int `bun:"version,default:1,unique:element_version_idx"`
disabled int, Active int `bun:"active"`
element_type VARCHAR(120) NOT NULL, IsValid int `bun:"is_valid"`
deploy_status VARCHAR(80) NOT NULL DEFAULT 'DIRTY', Disabled int `bun:"disabled"`
deploy_sequence INTEGER, ElementType string `bun:"element_type,notnull,type:varchar(120),unique:element_version_idx"`
deploy_result TEXT, DeployStatus string `bun:"deploy_status,notnull,type:varchar(80),default:'DIRTY'"`
last_hash TEXT, DeploySequence int `bun:"deploy_sequence"`
last_config TEXT, DeployResult string `bun:"deploy_result,type:text"`
UNIQUE(element_type, version) LastHash string `bun:"last_hash,type:text"`
);`); err != nil { LastConfig string `bun:"last_config,type:text"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
if _, err := db.ExecContext(ctx, `CREATE UNIQUE INDEX IF NOT EXISTS agent_config_versions_u1 ON agent_config_versions(element_type, version);`); err != nil { // add an index on the last_hash column
if _, err := db.NewCreateIndex().
Table("agent_config_versions").
Column("last_hash").
Index("idx_agent_config_versions_last_hash").
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
if _, err := db.ExecContext(ctx, `CREATE INDEX IF NOT EXISTS agent_config_versions_nu1 ON agent_config_versions(last_hash);`); err != nil { if _, err := db.NewCreateTable().
return err Model(&struct {
} bun.BaseModel `bun:"table:agent_config_elements"`
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS agent_config_elements( ID string `bun:"id,pk,type:text"`
id TEXT PRIMARY KEY, CreatedBy string `bun:"created_by,type:text"`
created_by TEXT, CreatedAt time.Time `bun:"created_at,default:CURRENT_TIMESTAMP"`
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, UpdatedBy string `bun:"updated_by,type:text"`
updated_by TEXT, UpdatedAt time.Time `bun:"updated_at,default:CURRENT_TIMESTAMP"`
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, ElementID string `bun:"element_id,type:text,notnull,unique:agent_config_elements_u1"`
element_id TEXT NOT NULL, ElementType string `bun:"element_type,type:varchar(120),notnull,unique:agent_config_elements_u1"`
element_type VARCHAR(120) NOT NULL, VersionID string `bun:"version_id,type:text,notnull,unique:agent_config_elements_u1"`
version_id TEXT NOT NULL }{}).
);`); err != nil { IfNotExists().
return err Exec(ctx); err != nil {
}
if _, err := db.ExecContext(ctx, `CREATE UNIQUE INDEX IF NOT EXISTS agent_config_elements_u1 ON agent_config_elements(version_id, element_id, element_type);`); err != nil {
return err return err
} }

View File

@ -2,6 +2,7 @@ package sqlmigration
import ( import (
"context" "context"
"time"
"github.com/uptrace/bun" "github.com/uptrace/bun"
"github.com/uptrace/bun/migrate" "github.com/uptrace/bun/migrate"
@ -27,18 +28,22 @@ func (migration *addPipelines) Register(migrations *migrate.Migrations) error {
} }
func (migration *addPipelines) Up(ctx context.Context, db *bun.DB) error { func (migration *addPipelines) Up(ctx context.Context, db *bun.DB) error {
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS pipelines( if _, err := db.NewCreateTable().
id TEXT PRIMARY KEY, Model(&struct {
order_id INTEGER, bun.BaseModel `bun:"table:pipelines"`
enabled BOOLEAN, ID string `bun:"id,pk,type:text"`
created_by TEXT, OrderID int `bun:"order_id"`
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, Enabled bool `bun:"enabled"`
name VARCHAR(400) NOT NULL, CreatedBy string `bun:"created_by,type:text"`
alias VARCHAR(20) NOT NULL, CreatedAt time.Time `bun:"created_at,default:current_timestamp"`
description TEXT, Name string `bun:"name,type:varchar(400),notnull"`
filter TEXT NOT NULL, Alias string `bun:"alias,type:varchar(20),notnull"`
config_json TEXT Description string `bun:"description,type:text"`
);`); err != nil { Filter string `bun:"filter,type:text,notnull"`
ConfigJSON string `bun:"config_json,type:text"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }

View File

@ -2,6 +2,7 @@ package sqlmigration
import ( import (
"context" "context"
"time"
"github.com/uptrace/bun" "github.com/uptrace/bun"
"github.com/uptrace/bun/migrate" "github.com/uptrace/bun/migrate"
@ -27,35 +28,46 @@ func (migration *addIntegrations) Register(migrations *migrate.Migrations) error
} }
func (migration *addIntegrations) Up(ctx context.Context, db *bun.DB) error { func (migration *addIntegrations) Up(ctx context.Context, db *bun.DB) error {
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS integrations_installed( if _, err := db.NewCreateTable().
integration_id TEXT PRIMARY KEY, Model(&struct {
config_json TEXT, bun.BaseModel `bun:"table:integrations_installed"`
installed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);`); err != nil { IntegrationID string `bun:"integration_id,pk,type:text"`
ConfigJSON string `bun:"config_json,type:text"`
InstalledAt time.Time `bun:"installed_at,default:current_timestamp"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS cloud_integrations_accounts( if _, err := db.NewCreateTable().
cloud_provider TEXT NOT NULL, Model(&struct {
id TEXT NOT NULL, bun.BaseModel `bun:"table:cloud_integrations_accounts"`
config_json TEXT, CloudProvider string `bun:"cloud_provider,type:text,unique:cloud_provider_id"`
cloud_account_id TEXT, ID string `bun:"id,type:text,notnull,unique:cloud_provider_id"`
last_agent_report_json TEXT, ConfigJSON string `bun:"config_json,type:text"`
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, CloudAccountID string `bun:"cloud_account_id,type:text"`
removed_at TIMESTAMP, LastAgentReportJSON string `bun:"last_agent_report_json,type:text"`
UNIQUE(cloud_provider, id) CreatedAt time.Time `bun:"created_at,notnull,default:current_timestamp"`
)`); err != nil { RemovedAt time.Time `bun:"removed_at,type:timestamp"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS cloud_integrations_service_configs( if _, err := db.NewCreateTable().
cloud_provider TEXT NOT NULL, Model(&struct {
cloud_account_id TEXT NOT NULL, bun.BaseModel `bun:"table:cloud_integrations_service_configs"`
service_id TEXT NOT NULL, CloudProvider string `bun:"cloud_provider,type:text,notnull,unique:service_cloud_provider_account"`
config_json TEXT, CloudAccountID string `bun:"cloud_account_id,type:text,notnull,unique:service_cloud_provider_account"`
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, ServiceID string `bun:"service_id,type:text,notnull,unique:service_cloud_provider_account"`
UNIQUE(cloud_provider, cloud_account_id, service_id) ConfigJSON string `bun:"config_json,type:text"`
)`); err != nil { CreatedAt time.Time `bun:"created_at,default:current_timestamp"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }

View File

@ -2,6 +2,7 @@ package sqlmigration
import ( import (
"context" "context"
"time"
"github.com/uptrace/bun" "github.com/uptrace/bun"
"github.com/uptrace/bun/migrate" "github.com/uptrace/bun/migrate"
@ -27,42 +28,59 @@ func (migration *addLicenses) Register(migrations *migrate.Migrations) error {
} }
func (migration *addLicenses) Up(ctx context.Context, db *bun.DB) error { func (migration *addLicenses) Up(ctx context.Context, db *bun.DB) error {
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS licenses( if _, err := db.NewCreateTable().
key TEXT PRIMARY KEY, Model(&struct {
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, bun.BaseModel `bun:"table:licenses"`
updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, Key string `bun:"key,pk,type:text"`
planDetails TEXT, CreatedAt time.Time `bun:"createdAt,default:current_timestamp"`
activationId TEXT, UpdatedAt time.Time `bun:"updatedAt,default:current_timestamp"`
validationMessage TEXT, PlanDetails string `bun:"planDetails,type:text"`
lastValidated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ActivationID string `bun:"activationId,type:text"`
);`); err != nil { ValidationMessage string `bun:"validationMessage,type:text"`
LastValidated time.Time `bun:"lastValidated,default:current_timestamp"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS sites( if _, err := db.NewCreateTable().
uuid TEXT PRIMARY KEY, Model(&struct {
alias VARCHAR(180) DEFAULT 'PROD', bun.BaseModel `bun:"table:sites"`
url VARCHAR(300),
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP UUID string `bun:"uuid,pk,type:text"`
);`); err != nil { Alias string `bun:"alias,type:varchar(180),default:'PROD'"`
URL string `bun:"url,type:varchar(300)"`
CreatedAt time.Time `bun:"createdAt,default:current_timestamp"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS feature_status ( if _, err := db.NewCreateTable().
name TEXT PRIMARY KEY, Model(&struct {
active bool, bun.BaseModel `bun:"table:feature_status"`
usage INTEGER DEFAULT 0, Name string `bun:"name,pk,type:text"`
usage_limit INTEGER DEFAULT 0, Active bool `bun:"active"`
route TEXT Usage int `bun:"usage,default:0"`
);`); err != nil { UsageLimit int `bun:"usage_limit,default:0"`
Route string `bun:"route,type:text"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS licenses_v3 ( if _, err := db.NewCreateTable().
id TEXT PRIMARY KEY, Model(&struct {
key TEXT NOT NULL UNIQUE, bun.BaseModel `bun:"table:licenses_v3"`
data TEXT ID string `bun:"id,pk,type:text"`
);`); err != nil { Key string `bun:"key,type:text,notnull,unique"`
Data string `bun:"data,type:text"`
}{}).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }

View File

@ -27,77 +27,42 @@ func (migration *addPats) Register(migrations *migrate.Migrations) error {
} }
func (migration *addPats) Up(ctx context.Context, db *bun.DB) error { func (migration *addPats) Up(ctx context.Context, db *bun.DB) error {
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS org_domains( if _, err := db.NewCreateTable().
id TEXT PRIMARY KEY, Model(&struct {
org_id TEXT NOT NULL, bun.BaseModel `bun:"table:org_domains"`
name VARCHAR(50) NOT NULL UNIQUE,
created_at INTEGER NOT NULL, ID string `bun:"id,pk,type:text"`
updated_at INTEGER, OrgID string `bun:"org_id,type:text,notnull"`
data TEXT NOT NULL, Name string `bun:"name,type:varchar(50),notnull,unique"`
FOREIGN KEY(org_id) REFERENCES organizations(id) CreatedAt int `bun:"created_at,notnull"`
);`); err != nil { UpdatedAt int `bun:"updated_at,type:timestamp"`
Data string `bun:"data,type:text,notnull"`
}{}).
ForeignKey(`("org_id") REFERENCES "organizations" ("id")`).
IfNotExists().
Exec(ctx); err != nil {
return err return err
} }
if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS personal_access_tokens ( if _, err := db.NewCreateTable().
id INTEGER PRIMARY KEY AUTOINCREMENT, Model(&struct {
role TEXT NOT NULL, bun.BaseModel `bun:"table:personal_access_tokens"`
user_id TEXT NOT NULL,
token TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
created_at INTEGER NOT NULL,
expires_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
last_used INTEGER NOT NULL,
revoked BOOLEAN NOT NULL,
updated_by_user_id TEXT NOT NULL,
FOREIGN KEY(user_id) REFERENCES users(id)
);`); err != nil {
return err
}
if _, err := db. ID int `bun:"id,pk,autoincrement"`
NewAddColumn(). Role string `bun:"role,type:text,notnull,default:'ADMIN'"`
Table("personal_access_tokens"). UserID string `bun:"user_id,type:text,notnull"`
ColumnExpr("role TEXT NOT NULL DEFAULT 'ADMIN'"). Token string `bun:"token,type:text,notnull,unique"`
Apply(WrapIfNotExists(ctx, db, "personal_access_tokens", "role")). Name string `bun:"name,type:text,notnull"`
Exec(ctx); err != nil && err != ErrNoExecute { CreatedAt int `bun:"created_at,notnull,default:0"`
return err ExpiresAt int `bun:"expires_at,notnull,default:0"`
} UpdatedAt int `bun:"updated_at,notnull,default:0"`
LastUsed int `bun:"last_used,notnull,default:0"`
if _, err := db. Revoked bool `bun:"revoked,notnull,default:false"`
NewAddColumn(). UpdatedByUserID string `bun:"updated_by_user_id,type:text,notnull,default:''"`
Table("personal_access_tokens"). }{}).
ColumnExpr("updated_at INTEGER NOT NULL DEFAULT 0"). ForeignKey(`("user_id") REFERENCES "users" ("id")`).
Apply(WrapIfNotExists(ctx, db, "personal_access_tokens", "updated_at")). IfNotExists().
Exec(ctx); err != nil && err != ErrNoExecute { Exec(ctx); err != nil {
return err
}
if _, err := db.
NewAddColumn().
Table("personal_access_tokens").
ColumnExpr("last_used INTEGER NOT NULL DEFAULT 0").
Apply(WrapIfNotExists(ctx, db, "personal_access_tokens", "last_used")).
Exec(ctx); err != nil && err != ErrNoExecute {
return err
}
if _, err := db.
NewAddColumn().
Table("personal_access_tokens").
ColumnExpr("revoked BOOLEAN NOT NULL DEFAULT FALSE").
Apply(WrapIfNotExists(ctx, db, "personal_access_tokens", "revoked")).
Exec(ctx); err != nil && err != ErrNoExecute {
return err
}
if _, err := db.
NewAddColumn().
Table("personal_access_tokens").
ColumnExpr("updated_by_user_id TEXT NOT NULL DEFAULT ''").
Apply(WrapIfNotExists(ctx, db, "personal_access_tokens", "updated_by_user_id")).
Exec(ctx); err != nil && err != ErrNoExecute {
return err return err
} }

49
pkg/types/agent.go Normal file
View File

@ -0,0 +1,49 @@
package types
import (
"time"
"github.com/uptrace/bun"
)
type Agent struct {
bun.BaseModel `bun:"table:agents"`
AgentID string `bun:"agent_id,pk,type:text"`
StartedAt time.Time `bun:"started_at,type:datetime,notnull"`
TerminatedAt time.Time `bun:"terminated_at,type:datetime"`
CurrentStatus string `bun:"current_status,type:text,notnull"`
EffectiveConfig string `bun:"effective_config,type:text,notnull"`
}
type AgentConfigVersion struct {
bun.BaseModel `bun:"table:agent_config_versions"`
ID string `bun:"id,pk,type:text"`
CreatedBy string `bun:"created_by,type:text"`
CreatedAt time.Time `bun:"created_at,default:CURRENT_TIMESTAMP"`
UpdatedBy string `bun:"updated_by,type:text"`
UpdatedAt time.Time `bun:"updated_at,default:CURRENT_TIMESTAMP"`
Version int `bun:"version,default:1,unique:element_version_idx"`
Active int `bun:"active"`
IsValid int `bun:"is_valid"`
Disabled int `bun:"disabled"`
ElementType string `bun:"element_type,notnull,type:varchar(120),unique:element_version_idx"`
DeployStatus string `bun:"deploy_status,notnull,type:varchar(80),default:'DIRTY'"`
DeploySequence int `bun:"deploy_sequence"`
DeployResult string `bun:"deploy_result,type:text"`
LastHash string `bun:"last_hash,type:text"`
LastConfig string `bun:"last_config,type:text"`
}
type AgentConfigElement struct {
bun.BaseModel `bun:"table:agent_config_elements"`
ID string `bun:"id,pk,type:text"`
CreatedBy string `bun:"created_by,type:text"`
CreatedAt time.Time `bun:"created_at,default:CURRENT_TIMESTAMP"`
UpdatedBy string `bun:"updated_by,type:text"`
UpdatedAt time.Time `bun:"updated_at,default:CURRENT_TIMESTAMP"`
ElementID string `bun:"element_id,type:text,notnull,unique:agent_config_elements_u1"`
ElementType string `bun:"element_type,type:varchar(120),notnull,unique:agent_config_elements_u1"`
VersionID string `bun:"version_id,type:text,notnull,unique:agent_config_elements_u1"`
}

71
pkg/types/dashboard.go Normal file
View File

@ -0,0 +1,71 @@
package types
import (
"time"
"github.com/uptrace/bun"
)
type Dashboard struct {
bun.BaseModel `bun:"table:dashboards"`
ID int `bun:"id,pk,autoincrement"`
UUID string `bun:"uuid,type:text,notnull,unique"`
CreatedAt time.Time `bun:"created_at,type:datetime,notnull"`
CreatedBy string `bun:"created_by,type:text,notnull"`
UpdatedAt time.Time `bun:"updated_at,type:datetime,notnull"`
UpdatedBy string `bun:"updated_by,type:text,notnull"`
Data string `bun:"data,type:text,notnull"`
Locked int `bun:"locked,notnull,default:0"`
}
type Rule struct {
bun.BaseModel `bun:"table:rules"`
ID int `bun:"id,pk,autoincrement"`
CreatedAt time.Time `bun:"created_at,type:datetime,notnull"`
CreatedBy string `bun:"created_by,type:text,notnull"`
UpdatedAt time.Time `bun:"updated_at,type:datetime,notnull"`
UpdatedBy string `bun:"updated_by,type:text,notnull"`
Deleted int `bun:"deleted,notnull,default:0"`
Data string `bun:"data,type:text,notnull"`
}
type NotificationChannel struct {
bun.BaseModel `bun:"table:notification_channels"`
ID int `bun:"id,pk,autoincrement"`
CreatedAt time.Time `bun:"created_at,type:datetime,notnull"`
UpdatedAt time.Time `bun:"updated_at,type:datetime,notnull"`
Name string `bun:"name,type:text,notnull,unique"`
Type string `bun:"type,type:text,notnull"`
Deleted int `bun:"deleted,notnull,default:0"`
Data string `bun:"data,type:text,notnull"`
}
type PlannedMaintenance struct {
bun.BaseModel `bun:"table:planned_maintenance"`
ID int `bun:"id,pk,autoincrement"`
Name string `bun:"name,type:text,notnull"`
Description string `bun:"description,type:text"`
AlertIDs string `bun:"alert_ids,type:text"`
Schedule string `bun:"schedule,type:text,notnull"`
CreatedAt time.Time `bun:"created_at,type:datetime,notnull"`
CreatedBy string `bun:"created_by,type:text,notnull"`
UpdatedAt time.Time `bun:"updated_at,type:datetime,notnull"`
UpdatedBy string `bun:"updated_by,type:text,notnull"`
}
type TTLStatus struct {
bun.BaseModel `bun:"table:ttl_status"`
ID int `bun:"id,pk,autoincrement"`
TransactionID string `bun:"transaction_id,type:text,notnull"`
CreatedAt time.Time `bun:"created_at,type:datetime,notnull"`
UpdatedAt time.Time `bun:"updated_at,type:datetime,notnull"`
TableName string `bun:"table_name,type:text,notnull"`
TTL int `bun:"ttl,notnull,default:0"`
ColdStorageTTL int `bun:"cold_storage_ttl,notnull,default:0"`
Status string `bun:"status,type:text,notnull"`
}

View File

@ -0,0 +1,15 @@
package types
import (
"time"
"github.com/uptrace/bun"
)
type DataMigration struct {
bun.BaseModel `bun:"table:data_migrations"`
ID int `bun:"id,pk,autoincrement"`
Version string `bun:"version,unique,notnull,type:VARCHAR(255)"`
CreatedAt time.Time `bun:"created_at,notnull,default:current_timestamp"`
Succeeded bool `bun:"succeeded,notnull,default:false"`
}

37
pkg/types/integration.go Normal file
View File

@ -0,0 +1,37 @@
package types
import (
"time"
"github.com/uptrace/bun"
)
type Integration struct {
bun.BaseModel `bun:"table:integrations_installed"`
IntegrationID string `bun:"integration_id,pk,type:text"`
ConfigJSON string `bun:"config_json,type:text"`
InstalledAt time.Time `bun:"installed_at,default:current_timestamp"`
}
type CloudIntegrationAccount struct {
bun.BaseModel `bun:"table:cloud_integrations_accounts"`
CloudProvider string `bun:"cloud_provider,type:text,unique:cloud_provider_id"`
ID string `bun:"id,type:text,notnull,unique:cloud_provider_id"`
ConfigJSON string `bun:"config_json,type:text"`
CloudAccountID string `bun:"cloud_account_id,type:text"`
LastAgentReportJSON string `bun:"last_agent_report_json,type:text"`
CreatedAt time.Time `bun:"created_at,notnull,default:current_timestamp"`
RemovedAt time.Time `bun:"removed_at,type:timestamp"`
}
type CloudIntegrationServiceConfig struct {
bun.BaseModel `bun:"table:cloud_integrations_service_configs"`
CloudProvider string `bun:"cloud_provider,type:text,notnull,unique:service_cloud_provider_account"`
CloudAccountID string `bun:"cloud_account_id,type:text,notnull,unique:service_cloud_provider_account"`
ServiceID string `bun:"service_id,type:text,notnull,unique:service_cloud_provider_account"`
ConfigJSON string `bun:"config_json,type:text"`
CreatedAt time.Time `bun:"created_at,default:current_timestamp"`
}

46
pkg/types/license.go Normal file
View File

@ -0,0 +1,46 @@
package types
import (
"time"
"github.com/uptrace/bun"
)
type License struct {
bun.BaseModel `bun:"table:licenses"`
Key string `bun:"key,pk,type:text"`
CreatedAt time.Time `bun:"createdAt,default:current_timestamp"`
UpdatedAt time.Time `bun:"updatedAt,default:current_timestamp"`
PlanDetails string `bun:"planDetails,type:text"`
ActivationID string `bun:"activationId,type:text"`
ValidationMessage string `bun:"validationMessage,type:text"`
LastValidated time.Time `bun:"lastValidated,default:current_timestamp"`
}
type Site struct {
bun.BaseModel `bun:"table:sites"`
UUID string `bun:"uuid,pk,type:text"`
Alias string `bun:"alias,type:varchar(180),default:'PROD'"`
URL string `bun:"url,type:varchar(300)"`
CreatedAt time.Time `bun:"createdAt,default:current_timestamp"`
}
type FeatureStatus struct {
bun.BaseModel `bun:"table:feature_status"`
Name string `bun:"name,pk,type:text"`
Active bool `bun:"active"`
Usage int `bun:"usage,default:0"`
UsageLimit int `bun:"usage_limit,default:0"`
Route string `bun:"route,type:text"`
}
type LicenseV3 struct {
bun.BaseModel `bun:"table:licenses_v3"`
ID string `bun:"id,pk,type:text"`
Key string `bun:"key,type:text,notnull,unique"`
Data string `bun:"data,type:text"`
}

78
pkg/types/organization.go Normal file
View File

@ -0,0 +1,78 @@
package types
import (
"time"
"github.com/uptrace/bun"
)
// TODO: check constraints are not working
type Organization struct {
bun.BaseModel `bun:"table:organizations"`
ID string `bun:"id,pk,type:text"`
Name string `bun:"name,type:text,notnull"`
CreatedAt int `bun:"created_at,notnull"`
IsAnonymous int `bun:"is_anonymous,notnull,default:0,CHECK(is_anonymous IN (0,1))"`
HasOptedUpdates int `bun:"has_opted_updates,notnull,default:1,CHECK(has_opted_updates IN (0,1))"`
}
type Invite struct {
bun.BaseModel `bun:"table:invites"`
ID int `bun:"id,pk,autoincrement"`
Name string `bun:"name,type:text,notnull"`
Email string `bun:"email,type:text,notnull,unique"`
Token string `bun:"token,type:text,notnull"`
CreatedAt int `bun:"created_at,notnull"`
Role string `bun:"role,type:text,notnull"`
OrgID string `bun:"org_id,type:text,notnull"`
}
type Group struct {
bun.BaseModel `bun:"table:groups"`
ID string `bun:"id,pk,type:text"`
Name string `bun:"name,type:text,notnull,unique"`
}
type User struct {
bun.BaseModel `bun:"table:users"`
ID string `bun:"id,pk,type:text"`
Name string `bun:"name,type:text,notnull"`
Email string `bun:"email,type:text,notnull,unique"`
Password string `bun:"password,type:text,notnull"`
CreatedAt int `bun:"created_at,notnull"`
ProfilePictureURL string `bun:"profile_picture_url,type:text"`
GroupID string `bun:"group_id,type:text,notnull"`
OrgID string `bun:"org_id,type:text,notnull"`
}
type ResetPasswordRequest struct {
bun.BaseModel `bun:"table:reset_password_request"`
ID int `bun:"id,pk,autoincrement"`
Token string `bun:"token,type:text,notnull"`
UserID string `bun:"user_id,type:text,notnull"`
}
type UserFlags struct {
bun.BaseModel `bun:"table:user_flags"`
UserID string `bun:"user_id,pk,type:text,notnull"`
Flags string `bun:"flags,type:text"`
}
type ApdexSettings struct {
bun.BaseModel `bun:"table:apdex_settings"`
ServiceName string `bun:"service_name,pk,type:text"`
Threshold float64 `bun:"threshold,type:float,notnull"`
ExcludeStatusCodes string `bun:"exclude_status_codes,type:text,notnull"`
}
type IngestionKey struct {
bun.BaseModel `bun:"table:ingestion_keys"`
KeyId string `bun:"key_id,pk,type:text"`
Name string `bun:"name,type:text"`
CreatedAt time.Time `bun:"created_at,default:current_timestamp"`
IngestionKey string `bun:"ingestion_key,type:text,notnull"`
IngestionURL string `bun:"ingestion_url,type:text,notnull"`
DataRegion string `bun:"data_region,type:text,notnull"`
}

View File

@ -0,0 +1,32 @@
package types
import (
"github.com/uptrace/bun"
)
type PersonalAccessToken struct {
bun.BaseModel `bun:"table:personal_access_tokens"`
ID int `bun:"id,pk,autoincrement"`
Role string `bun:"role,type:text,notnull,default:'ADMIN'"`
UserID string `bun:"user_id,type:text,notnull"`
Token string `bun:"token,type:text,notnull,unique"`
Name string `bun:"name,type:text,notnull"`
CreatedAt int `bun:"created_at,notnull,default:0"`
ExpiresAt int `bun:"expires_at,notnull,default:0"`
UpdatedAt int `bun:"updated_at,notnull,default:0"`
LastUsed int `bun:"last_used,notnull,default:0"`
Revoked bool `bun:"revoked,notnull,default:false"`
UpdatedByUserID string `bun:"updated_by_user_id,type:text,notnull,default:''"`
}
type OrgDomain struct {
bun.BaseModel `bun:"table:org_domains"`
ID string `bun:"id,pk,type:text"`
OrgID string `bun:"org_id,type:text,notnull"`
Name string `bun:"name,type:varchar(50),notnull,unique"`
CreatedAt int `bun:"created_at,notnull"`
UpdatedAt int `bun:"updated_at,type:timestamp"`
Data string `bun:"data,type:text,notnull"`
}

22
pkg/types/pipeline.go Normal file
View File

@ -0,0 +1,22 @@
package types
import (
"time"
"github.com/uptrace/bun"
)
type Pipeline struct {
bun.BaseModel `bun:"table:pipelines"`
ID string `bun:"id,pk,type:text"`
OrderID int `bun:"order_id"`
Enabled bool `bun:"enabled"`
CreatedBy string `bun:"created_by,type:text"`
CreatedAt time.Time `bun:"created_at,default:current_timestamp"`
Name string `bun:"name,type:varchar(400),notnull"`
Alias string `bun:"alias,type:varchar(20),notnull"`
Description string `bun:"description,type:text"`
Filter string `bun:"filter,type:text,notnull"`
ConfigJSON string `bun:"config_json,type:text"`
}

21
pkg/types/preference.go Normal file
View File

@ -0,0 +1,21 @@
package types
import "github.com/uptrace/bun"
// on_delete:CASCADE,on_update:CASCADE not working
type UserPreference struct {
bun.BaseModel `bun:"table:user_preference"`
PreferenceID string `bun:"preference_id,type:text,pk"`
PreferenceValue string `bun:"preference_value,type:text"`
UserID string `bun:"user_id,type:text,pk"`
}
// on_delete:CASCADE,on_update:CASCADE not working
type OrgPreference struct {
bun.BaseModel `bun:"table:org_preference"`
PreferenceID string `bun:"preference_id,pk,type:text,notnull"`
PreferenceValue string `bun:"preference_value,type:text,notnull"`
OrgID string `bun:"org_id,pk,type:text,notnull"`
}

23
pkg/types/savedview.go Normal file
View File

@ -0,0 +1,23 @@
package types
import (
"time"
"github.com/uptrace/bun"
)
type SavedView struct {
bun.BaseModel `bun:"table:saved_views"`
UUID string `bun:"uuid,pk,type:text"`
Name string `bun:"name,type:text,notnull"`
Category string `bun:"category,type:text,notnull"`
CreatedAt time.Time `bun:"created_at,type:datetime,notnull"`
CreatedBy string `bun:"created_by,type:text"`
UpdatedAt time.Time `bun:"updated_at,type:datetime,notnull"`
UpdatedBy string `bun:"updated_by,type:text"`
SourcePage string `bun:"source_page,type:text,notnull"`
Tags string `bun:"tags,type:text"`
Data string `bun:"data,type:text,notnull"`
ExtraData string `bun:"extra_data,type:text"`
}