From 962e75c6d48a0893d31a59966a3919a89efeaba3 Mon Sep 17 00:00:00 2001 From: Nityananda Gohain Date: Wed, 12 Feb 2025 09:58:49 +0530 Subject: [PATCH] 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> --- pkg/sqlmigration/000_add_data_migrations.go | 17 +- pkg/sqlmigration/001_add_organization.go | 162 ++++++++++-------- pkg/sqlmigration/002_add_preferences.go | 34 ++-- pkg/sqlmigration/003_add_dashboards.go | 173 ++++++++------------ pkg/sqlmigration/004_add_saved_views.go | 31 ++-- pkg/sqlmigration/005_add_agents.go | 97 ++++++----- pkg/sqlmigration/006_add_pipelines.go | 29 ++-- pkg/sqlmigration/007_add_integrations.go | 58 ++++--- pkg/sqlmigration/008_add_licenses.go | 72 +++++--- pkg/sqlmigration/009_add_pats.go | 99 ++++------- pkg/types/agent.go | 49 ++++++ pkg/types/dashboard.go | 71 ++++++++ pkg/types/datamigration.go | 15 ++ pkg/types/integration.go | 37 +++++ pkg/types/license.go | 46 ++++++ pkg/types/organization.go | 78 +++++++++ pkg/types/personal_access_token.go | 32 ++++ pkg/types/pipeline.go | 22 +++ pkg/types/preference.go | 21 +++ pkg/types/savedview.go | 23 +++ 20 files changed, 794 insertions(+), 372 deletions(-) create mode 100644 pkg/types/agent.go create mode 100644 pkg/types/dashboard.go create mode 100644 pkg/types/datamigration.go create mode 100644 pkg/types/integration.go create mode 100644 pkg/types/license.go create mode 100644 pkg/types/organization.go create mode 100644 pkg/types/personal_access_token.go create mode 100644 pkg/types/pipeline.go create mode 100644 pkg/types/preference.go create mode 100644 pkg/types/savedview.go diff --git a/pkg/sqlmigration/000_add_data_migrations.go b/pkg/sqlmigration/000_add_data_migrations.go index 6b138329be..a42495695d 100644 --- a/pkg/sqlmigration/000_add_data_migrations.go +++ b/pkg/sqlmigration/000_add_data_migrations.go @@ -2,6 +2,7 @@ package sqlmigration import ( "context" + "time" "github.com/uptrace/bun" "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 { // table:data_migrations - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS data_migrations ( - id SERIAL PRIMARY KEY, - version VARCHAR(255) NOT NULL UNIQUE, - created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - succeeded BOOLEAN NOT NULL DEFAULT FALSE - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } diff --git a/pkg/sqlmigration/001_add_organization.go b/pkg/sqlmigration/001_add_organization.go index 158fa5ecee..6f87c18334 100644 --- a/pkg/sqlmigration/001_add_organization.go +++ b/pkg/sqlmigration/001_add_organization.go @@ -2,6 +2,7 @@ package sqlmigration import ( "context" + "time" "github.com/uptrace/bun" "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 { - // 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 - if _, err := db.NewRaw(`CREATE TABLE IF NOT EXISTS organizations ( - id TEXT PRIMARY KEY, - name TEXT NOT NULL, - created_at INTEGER NOT NULL, - is_anonymous INTEGER NOT NULL DEFAULT 0 CHECK(is_anonymous IN (0,1)), - has_opted_updates INTEGER NOT NULL DEFAULT 1 CHECK(has_opted_updates IN (0,1)) - )`).Exec(ctx); err != nil { - return err - } - - // table:users - 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 { + if _, err := db.NewCreateTable(). + Model(&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))"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } // table:groups - if _, err := db.NewRaw(`CREATE TABLE IF NOT EXISTS groups ( - id TEXT PRIMARY KEY, - name TEXT NOT NULL UNIQUE - )`).Exec(ctx); err != nil { + if _, err := db.NewCreateTable(). + Model(&struct { + bun.BaseModel `bun:"table:groups"` + 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 } // table:reset_password_request - if _, err := db.NewRaw(`CREATE TABLE IF NOT EXISTS reset_password_request ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - user_id TEXT NOT NULL, - token TEXT NOT NULL, - FOREIGN KEY(user_id) REFERENCES users(id) - )`).Exec(ctx); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + ForeignKey(`("user_id") REFERENCES "users" ("id")`). + IfNotExists(). + Exec(ctx); err != nil { return err } // table:user_flags - if _, err := db.NewRaw(`CREATE TABLE IF NOT EXISTS user_flags ( - user_id TEXT PRIMARY KEY, - flags TEXT, - FOREIGN KEY(user_id) REFERENCES users(id) - )`).Exec(ctx); err != nil { + if _, err := db.NewCreateTable(). + Model(&struct { + bun.BaseModel `bun:"table:user_flags"` + UserID string `bun:"user_id,pk,type:text,notnull"` + Flags string `bun:"flags,type:text"` + }{}). + ForeignKey(`("user_id") REFERENCES "users" ("id")`). + IfNotExists(). + Exec(ctx); err != nil { return err } // table:apdex_settings - if _, err := db.NewRaw(`CREATE TABLE IF NOT EXISTS apdex_settings ( - service_name TEXT PRIMARY KEY, - threshold FLOAT NOT NULL, - exclude_status_codes TEXT NOT NULL - )`).Exec(ctx); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } // table:ingestion_keys - if _, err := db.NewRaw(`CREATE TABLE IF NOT EXISTS ingestion_keys ( - key_id TEXT PRIMARY KEY, - name TEXT, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - ingestion_key TEXT NOT NULL, - ingestion_url TEXT NOT NULL, - data_region TEXT NOT NULL - )`).Exec(ctx); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } diff --git a/pkg/sqlmigration/002_add_preferences.go b/pkg/sqlmigration/002_add_preferences.go index 436d7826fe..e1e646f1ed 100644 --- a/pkg/sqlmigration/002_add_preferences.go +++ b/pkg/sqlmigration/002_add_preferences.go @@ -28,24 +28,30 @@ func (migration *addPreferences) Register(migrations *migrate.Migrations) error func (migration *addPreferences) Up(ctx context.Context, db *bun.DB) error { // table:user_preference - if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS user_preference ( - preference_id TEXT NOT NULL, - preference_value TEXT, - user_id TEXT NOT NULL, - PRIMARY KEY (preference_id,user_id), - FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE - )`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + ForeignKey(`("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE`). + Exec(ctx); err != nil { return err } // table:org_preference - if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS org_preference ( - preference_id TEXT NOT NULL, - preference_value TEXT, - org_id TEXT NOT NULL, - PRIMARY KEY (preference_id,org_id), - FOREIGN KEY (org_id) REFERENCES organizations(id) ON UPDATE CASCADE ON DELETE CASCADE - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + ForeignKey(`("org_id") REFERENCES "organizations" ("id") ON DELETE CASCADE ON UPDATE CASCADE`). + IfNotExists(). + Exec(ctx); err != nil { return err } diff --git a/pkg/sqlmigration/003_add_dashboards.go b/pkg/sqlmigration/003_add_dashboards.go index 2424b89239..c3f1d4e613 100644 --- a/pkg/sqlmigration/003_add_dashboards.go +++ b/pkg/sqlmigration/003_add_dashboards.go @@ -2,6 +2,7 @@ package sqlmigration import ( "context" + "time" "github.com/uptrace/bun" "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 { // table:dashboards - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS dashboards ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - uuid TEXT NOT NULL UNIQUE, - created_at datetime NOT NULL, - updated_at datetime NOT NULL, - data TEXT NOT NULL - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } // table:rules - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS rules ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - updated_at datetime NOT NULL, - deleted INTEGER DEFAULT 0, - data TEXT NOT NULL - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } // table:notification_channels - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS notification_channels ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - created_at datetime NOT NULL, - updated_at datetime NOT NULL, - name TEXT NOT NULL UNIQUE, - type TEXT NOT NULL, - deleted INTEGER DEFAULT 0, - data TEXT NOT NULL - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } // table:planned_maintenance - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS planned_maintenance ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL, - description TEXT, - alert_ids TEXT, - schedule TEXT NOT NULL, - created_at datetime NOT NULL, - created_by TEXT NOT NULL, - updated_at datetime NOT NULL, - updated_by TEXT NOT NULL - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } // table:ttl_status - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS ttl_status ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - transaction_id TEXT NOT NULL, - created_at datetime NOT NULL, - updated_at datetime NOT NULL, - table_name TEXT NOT NULL, - ttl INTEGER DEFAULT 0, - cold_storage_ttl INTEGER DEFAULT 0, - status TEXT NOT NULL - );`); err != nil { - return err - } - - // table:rules op:add column created_at - 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 { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } diff --git a/pkg/sqlmigration/004_add_saved_views.go b/pkg/sqlmigration/004_add_saved_views.go index bec921c23b..20f817bc55 100644 --- a/pkg/sqlmigration/004_add_saved_views.go +++ b/pkg/sqlmigration/004_add_saved_views.go @@ -2,6 +2,7 @@ package sqlmigration import ( "context" + "time" "github.com/uptrace/bun" "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 { // table:saved_views op:create - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS saved_views ( - uuid TEXT PRIMARY KEY, - name TEXT NOT NULL, - category TEXT NOT NULL, - created_at datetime NOT NULL, - created_by TEXT, - updated_at datetime NOT NULL, - updated_by TEXT, - source_page TEXT NOT NULL, - tags TEXT, - data TEXT NOT NULL, - extra_data TEXT - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } diff --git a/pkg/sqlmigration/005_add_agents.go b/pkg/sqlmigration/005_add_agents.go index dac8168727..20ac7acbdc 100644 --- a/pkg/sqlmigration/005_add_agents.go +++ b/pkg/sqlmigration/005_add_agents.go @@ -2,6 +2,7 @@ package sqlmigration import ( "context" + "time" "github.com/uptrace/bun" "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 { - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS agents ( - agent_id TEXT PRIMARY KEY UNIQUE, - started_at datetime NOT NULL, - terminated_at datetime, - current_status TEXT NOT NULL, - effective_config TEXT NOT NULL - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&struct { + bun.BaseModel `bun:"table:agents"` + AgentID string `bun:"agent_id,pk,type:text,unique"` + 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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS agent_config_versions( - id TEXT PRIMARY KEY, - created_by TEXT, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_by TEXT, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - version INTEGER DEFAULT 1, - active int, - is_valid int, - disabled int, - element_type VARCHAR(120) NOT NULL, - deploy_status VARCHAR(80) NOT NULL DEFAULT 'DIRTY', - deploy_sequence INTEGER, - deploy_result TEXT, - last_hash TEXT, - last_config TEXT, - UNIQUE(element_type, version) - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { 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 } - if _, err := db.ExecContext(ctx, `CREATE INDEX IF NOT EXISTS agent_config_versions_nu1 ON agent_config_versions(last_hash);`); err != nil { - return err - } + if _, err := db.NewCreateTable(). + Model(&struct { + bun.BaseModel `bun:"table:agent_config_elements"` - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS agent_config_elements( - id TEXT PRIMARY KEY, - created_by TEXT, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_by TEXT, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - element_id TEXT NOT NULL, - element_type VARCHAR(120) NOT NULL, - version_id TEXT NOT NULL - );`); err != nil { - return err - } - - 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 { + 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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } diff --git a/pkg/sqlmigration/006_add_pipelines.go b/pkg/sqlmigration/006_add_pipelines.go index 8974754775..1cb8f5eadc 100644 --- a/pkg/sqlmigration/006_add_pipelines.go +++ b/pkg/sqlmigration/006_add_pipelines.go @@ -2,6 +2,7 @@ package sqlmigration import ( "context" + "time" "github.com/uptrace/bun" "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 { - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS pipelines( - id TEXT PRIMARY KEY, - order_id INTEGER, - enabled BOOLEAN, - created_by TEXT, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - name VARCHAR(400) NOT NULL, - alias VARCHAR(20) NOT NULL, - description TEXT, - filter TEXT NOT NULL, - config_json TEXT - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } diff --git a/pkg/sqlmigration/007_add_integrations.go b/pkg/sqlmigration/007_add_integrations.go index 9cfc5cf071..716f331356 100644 --- a/pkg/sqlmigration/007_add_integrations.go +++ b/pkg/sqlmigration/007_add_integrations.go @@ -2,6 +2,7 @@ package sqlmigration import ( "context" + "time" "github.com/uptrace/bun" "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 { - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS integrations_installed( - integration_id TEXT PRIMARY KEY, - config_json TEXT, - installed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS cloud_integrations_accounts( - cloud_provider TEXT NOT NULL, - id TEXT NOT NULL, - config_json TEXT, - cloud_account_id TEXT, - last_agent_report_json TEXT, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - removed_at TIMESTAMP, - UNIQUE(cloud_provider, id) - )`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS cloud_integrations_service_configs( - cloud_provider TEXT NOT NULL, - cloud_account_id TEXT NOT NULL, - service_id TEXT NOT NULL, - config_json TEXT, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - UNIQUE(cloud_provider, cloud_account_id, service_id) - )`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } diff --git a/pkg/sqlmigration/008_add_licenses.go b/pkg/sqlmigration/008_add_licenses.go index d50898c93a..0a723d946d 100644 --- a/pkg/sqlmigration/008_add_licenses.go +++ b/pkg/sqlmigration/008_add_licenses.go @@ -2,6 +2,7 @@ package sqlmigration import ( "context" + "time" "github.com/uptrace/bun" "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 { - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS licenses( - key TEXT PRIMARY KEY, - createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - planDetails TEXT, - activationId TEXT, - validationMessage TEXT, - lastValidated TIMESTAMP DEFAULT CURRENT_TIMESTAMP - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS sites( - uuid TEXT PRIMARY KEY, - alias VARCHAR(180) DEFAULT 'PROD', - url VARCHAR(300), - createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS feature_status ( - name TEXT PRIMARY KEY, - active bool, - usage INTEGER DEFAULT 0, - usage_limit INTEGER DEFAULT 0, - route TEXT - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS licenses_v3 ( - id TEXT PRIMARY KEY, - key TEXT NOT NULL UNIQUE, - data TEXT - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + IfNotExists(). + Exec(ctx); err != nil { return err } diff --git a/pkg/sqlmigration/009_add_pats.go b/pkg/sqlmigration/009_add_pats.go index ad6ae8f245..3190f5153a 100644 --- a/pkg/sqlmigration/009_add_pats.go +++ b/pkg/sqlmigration/009_add_pats.go @@ -27,77 +27,42 @@ func (migration *addPats) Register(migrations *migrate.Migrations) error { } func (migration *addPats) Up(ctx context.Context, db *bun.DB) error { - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS org_domains( - id TEXT PRIMARY KEY, - org_id TEXT NOT NULL, - name VARCHAR(50) NOT NULL UNIQUE, - created_at INTEGER NOT NULL, - updated_at INTEGER, - data TEXT NOT NULL, - FOREIGN KEY(org_id) REFERENCES organizations(id) - );`); err != nil { + if _, err := db.NewCreateTable(). + Model(&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"` + }{}). + ForeignKey(`("org_id") REFERENCES "organizations" ("id")`). + IfNotExists(). + Exec(ctx); err != nil { return err } - if _, err := db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS personal_access_tokens ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - role TEXT NOT NULL, - 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.NewCreateTable(). + Model(&struct { + bun.BaseModel `bun:"table:personal_access_tokens"` - if _, err := db. - NewAddColumn(). - Table("personal_access_tokens"). - ColumnExpr("role TEXT NOT NULL DEFAULT 'ADMIN'"). - Apply(WrapIfNotExists(ctx, db, "personal_access_tokens", "role")). - Exec(ctx); err != nil && err != ErrNoExecute { - return err - } - - if _, err := db. - NewAddColumn(). - Table("personal_access_tokens"). - ColumnExpr("updated_at INTEGER NOT NULL DEFAULT 0"). - Apply(WrapIfNotExists(ctx, db, "personal_access_tokens", "updated_at")). - Exec(ctx); err != nil && err != ErrNoExecute { - 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 { + 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:''"` + }{}). + ForeignKey(`("user_id") REFERENCES "users" ("id")`). + IfNotExists(). + Exec(ctx); err != nil { return err } diff --git a/pkg/types/agent.go b/pkg/types/agent.go new file mode 100644 index 0000000000..eb869a4983 --- /dev/null +++ b/pkg/types/agent.go @@ -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"` +} diff --git a/pkg/types/dashboard.go b/pkg/types/dashboard.go new file mode 100644 index 0000000000..4436b2d1ec --- /dev/null +++ b/pkg/types/dashboard.go @@ -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"` +} diff --git a/pkg/types/datamigration.go b/pkg/types/datamigration.go new file mode 100644 index 0000000000..eb70473144 --- /dev/null +++ b/pkg/types/datamigration.go @@ -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"` +} diff --git a/pkg/types/integration.go b/pkg/types/integration.go new file mode 100644 index 0000000000..9ba9d0e62c --- /dev/null +++ b/pkg/types/integration.go @@ -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"` +} diff --git a/pkg/types/license.go b/pkg/types/license.go new file mode 100644 index 0000000000..ddeadedfdb --- /dev/null +++ b/pkg/types/license.go @@ -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"` +} diff --git a/pkg/types/organization.go b/pkg/types/organization.go new file mode 100644 index 0000000000..51a87bbb45 --- /dev/null +++ b/pkg/types/organization.go @@ -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"` +} diff --git a/pkg/types/personal_access_token.go b/pkg/types/personal_access_token.go new file mode 100644 index 0000000000..a98721bbea --- /dev/null +++ b/pkg/types/personal_access_token.go @@ -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"` +} diff --git a/pkg/types/pipeline.go b/pkg/types/pipeline.go new file mode 100644 index 0000000000..c5a6a28469 --- /dev/null +++ b/pkg/types/pipeline.go @@ -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"` +} diff --git a/pkg/types/preference.go b/pkg/types/preference.go new file mode 100644 index 0000000000..7d98b4dbd3 --- /dev/null +++ b/pkg/types/preference.go @@ -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"` +} diff --git a/pkg/types/savedview.go b/pkg/types/savedview.go new file mode 100644 index 0000000000..8ec958ec27 --- /dev/null +++ b/pkg/types/savedview.go @@ -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"` +}