update_apdex_ttl.go
023_update_apdex_ttl.go - Overview
This file defines a SQL migration to update the apdex settings and TTL status tables. It renames the old tables and migrates data from the old tables to the new tables, adding an organization ID (orgID) column and a UUID ID to the new tables.
Detailed Documentation
updateApdexTtl
struct
- Purpose: This struct holds the SQLStore instance and implements the
SQLMigration
interface. - Fields:
store
: An instance ofsqlstore.SQLStore
for database operations.
existingApdexSettings
struct
- Purpose: Represents the structure of the
apdex_settings
table before the migration. - Fields:
bun.BaseModel
: Base model for bun ORM.OrgID
: Organization ID (primary key).ServiceName
: Service name (primary key).Threshold
: Apdex threshold value.ExcludeStatusCodes
: Status codes to exclude from Apdex calculation.
newApdexSettings
struct
- Purpose: Represents the structure of the
apdex_setting
table after the migration. - Fields:
bun.BaseModel
: Base model for bun ORM.types.Identifiable
: Embeds an ID field.OrgID
: Organization ID.ServiceName
: Service name.Threshold
: Apdex threshold value.ExcludeStatusCodes
: Status codes to exclude from Apdex calculation.
existingTTLStatus
struct
- Purpose: Represents the structure of the
ttl_status
table before the migration. - Fields:
bun.BaseModel
: Base model for bun ORM.ID
: Unique ID (primary key, autoincrement).TransactionID
: Transaction ID.CreatedAt
: Creation timestamp.UpdatedAt
: Last updated timestamp.TableName
: Table name.TTL
: TTL value.ColdStorageTTL
: Cold storage TTL value.Status
: Status.
newTTLStatus
struct
- Purpose: Represents the structure of the
ttl_setting
table after the migration. - Fields:
bun.BaseModel
: Base model for bun ORM.types.Identifiable
: Embeds an ID field.types.TimeAuditable
: EmbedsCreatedAt
andUpdatedAt
fields.TransactionID
: Transaction ID.TableName
: Table name.TTL
: TTL value.ColdStorageTTL
: Cold storage TTL value.Status
: Status.OrgID
: Organization ID.
NewUpdateApdexTtlFactory(sqlstore sqlstore.SQLStore) factory.ProviderFactory[SQLMigration, Config]
- Purpose: Creates a factory for the
updateApdexTtl
migration. - Parameters:
sqlstore
: An instance ofsqlstore.SQLStore
.
- Returns: A
factory.ProviderFactory
for creatingSQLMigration
instances.
newUpdateApdexTtl(_ context.Context, _ factory.ProviderSettings, _ Config, store sqlstore.SQLStore) (SQLMigration, error)
- Purpose: Creates a new
updateApdexTtl
instance. - Parameters:
_
: Context (not used)._
: Provider settings (not used)._
: Configuration (not used).store
: An instance ofsqlstore.SQLStore
.
- Returns: A new
SQLMigration
instance and an error (if any).
(migration *updateApdexTtl) Register(migrations *migrate.Migrations) error
- Purpose: Registers the up and down migration functions.
- Parameters:
migrations
: Amigrate.Migrations
instance.
- Returns: An error if registration fails.
(migration *updateApdexTtl) Up(ctx context.Context, db *bun.DB) error
- Purpose: Implements the "up" migration logic, renaming tables and migrating data.
- Parameters:
ctx
: Context.db
: Abun.DB
instance.
- Returns: An error if the migration fails.
- The migration performs the following steps:
- Starts a transaction.
- Renames
apdex_settings
toapdex_setting
, migrates the data and adds an org_id - Creates a unique index on
apdex_setting
table - Renames
ttl_status
tottl_setting
, migrates the data and adds an org_id - Commits the transaction.
- The migration performs the following steps:
(migration *updateApdexTtl) Down(context.Context, *bun.DB) error
- Purpose: Implements the "down" migration logic (reverts the "up" migration). Currently, it does nothing.
- Parameters:
ctx
: Context.db
: Abun.DB
instance.
- Returns: Always returns
nil
.
(migration *updateApdexTtl) CopyExistingApdexSettingsToNewApdexSettings(existingApdexSettings []*existingApdexSettings) []*newApdexSettings
- Purpose: Copies data from the old
existingApdexSettings
to the newnewApdexSettings
format, generating UUIDs for the new IDs. - Parameters:
existingApdexSettings
: A slice ofexistingApdexSettings
structs.
- Returns: A slice of
newApdexSettings
structs.
(migration *updateApdexTtl) CopyExistingTTLStatusToNewTTLStatus(existingTTLStatus []*existingTTLStatus, orgID string) []*newTTLStatus
- Purpose: Copies data from the old
existingTTLStatus
to the newnewTTLStatus
format, generating UUIDs for the new IDs and assigning the providedorgID
. - Parameters:
existingTTLStatus
: A slice ofexistingTTLStatus
structs.orgID
: The organization ID to assign to the new records.
- Returns: A slice of
newTTLStatus
structs.
Code Examples
N/A
Clarity and Accuracy
The documentation is based solely on the provided code and aims for accuracy and conciseness.
Include in Getting Started: NO