Skip to main content

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 of sqlstore.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: Embeds CreatedAt and UpdatedAt 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 of sqlstore.SQLStore.
  • Returns: A factory.ProviderFactory for creating SQLMigration 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 of sqlstore.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: A migrate.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: A bun.DB instance.
  • Returns: An error if the migration fails.
    • The migration performs the following steps:
      1. Starts a transaction.
      2. Renames apdex_settings to apdex_setting, migrates the data and adds an org_id
      3. Creates a unique index on apdex_setting table
      4. Renames ttl_status to ttl_setting, migrates the data and adds an org_id
      5. Commits the transaction.

(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: A bun.DB instance.
  • Returns: Always returns nil.

(migration *updateApdexTtl) CopyExistingApdexSettingsToNewApdexSettings(existingApdexSettings []*existingApdexSettings) []*newApdexSettings

  • Purpose: Copies data from the old existingApdexSettings to the new newApdexSettings format, generating UUIDs for the new IDs.
  • Parameters:
    • existingApdexSettings: A slice of existingApdexSettings structs.
  • Returns: A slice of newApdexSettings structs.

(migration *updateApdexTtl) CopyExistingTTLStatusToNewTTLStatus(existingTTLStatus []*existingTTLStatus, orgID string) []*newTTLStatus

  • Purpose: Copies data from the old existingTTLStatus to the new newTTLStatus format, generating UUIDs for the new IDs and assigning the provided orgID.
  • Parameters:
    • existingTTLStatus: A slice of existingTTLStatus 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