Skip to main content

update_preferences.go

022_update_preferences.go - Overview

  1. Overview

This file defines a SQL migration to update the preferences tables by adding primary keys and migrating data to new tables. It includes functions to register the migration, perform the "Up" (migration) and "Down" (rollback) operations, and copy data from old preference tables to new ones.

  1. Detailed Documentation

updatePreferences

  • Purpose: Represents the SQL migration for updating preferences.
  • Parameters: None
  • Returns: None
type updatePreferences struct {
store sqlstore.SQLStore
}

existingOrgPreference

  • Purpose: Represents the structure of an existing organization preference in the database.
  • Parameters: None
  • Returns: None
type existingOrgPreference 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"`
}

newOrgPreference

  • Purpose: Represents the structure of a new organization preference in the database.
  • Parameters: None
  • Returns: None
type newOrgPreference struct {
bun.BaseModel `bun:"table:org_preference_new"`
types.Identifiable
PreferenceID string `bun:"preference_id,type:text,notnull"`
PreferenceValue string `bun:"preference_value,type:text,notnull"`
OrgID string `bun:"org_id,type:text,notnull"`
}

existingUserPreference

  • Purpose: Represents the structure of an existing user preference in the database.
  • Parameters: None
  • Returns: None
type existingUserPreference 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"`
}

newUserPreference

  • Purpose: Represents the structure of a new user preference in the database.
  • Parameters: None
  • Returns: None
type newUserPreference struct {
bun.BaseModel `bun:"table:user_preference_new"`
types.Identifiable
PreferenceID string `bun:"preference_id,type:text,notnull"`
PreferenceValue string `bun:"preference_value,type:text,notnull"`
UserID string `bun:"user_id,type:text,notnull"`
}

NewUpdatePreferencesFactory

  • Purpose: Creates a new factory for the updatePreferences SQL migration.
  • Parameters:
    • sqlstore (sqlstore.SQLStore): The SQL store instance.
  • Returns: factory.ProviderFactory[SQLMigration, Config]: The factory for creating the SQL migration.
func NewUpdatePreferencesFactory(sqlstore sqlstore.SQLStore) factory.ProviderFactory[SQLMigration, Config] {

newUpdatePreferences

  • Purpose: Creates a new updatePreferences instance.
  • Parameters:
    • _ (context.Context): Context.
    • _ (factory.ProviderSettings): Provider settings.
    • _ (Config): Configuration.
    • store (sqlstore.SQLStore): The SQL store instance.
  • Returns: (SQLMigration, error): The new updatePreferences instance and an error, if any.
func newUpdatePreferences(_ context.Context, _ factory.ProviderSettings, _ Config, store sqlstore.SQLStore) (SQLMigration, error) {

Register

  • Purpose: Registers the "Up" and "Down" migration functions.
  • Parameters:
    • migrations (*migrate.Migrations): The migrations instance.
  • Returns: error: An error, if any.
func (migration *updatePreferences) Register(migrations *migrate.Migrations) error {

Up

  • Purpose: Executes the migration to update the preferences tables. It creates new tables, migrates data, and adds primary keys.
  • Parameters:
    • ctx (context.Context): The context.
    • db (*bun.DB): The database connection.
  • Returns: error: An error, if any.
func (migration *updatePreferences) Up(ctx context.Context, db *bun.DB) error {

Down

  • Purpose: Executes the rollback operation. Currently, it does nothing.
  • Parameters:
    • ctx (context.Context): The context.
    • db (*bun.DB): The database connection.
  • Returns: error: An error, if any.
func (migration *updatePreferences) Down(context.Context, *bun.DB) error {

CopyOldOrgPreferencesToNewOrgPreferences

  • Purpose: Copies organization preferences from the old table structure to the new table structure.
  • Parameters:
    • existingOrgPreferences ([]*existingOrgPreference): A slice of existing organization preferences.
  • Returns: []*newOrgPreference: A slice of new organization preferences.
func (migration *updatePreferences) CopyOldOrgPreferencesToNewOrgPreferences(existingOrgPreferences []*existingOrgPreference) []*newOrgPreference {

CopyOldUserPreferencesToNewUserPreferences

  • Purpose: Copies user preferences from the old table structure to the new table structure.
  • Parameters:
    • existingUserPreferences ([]*existingUserPreference): A slice of existing user preferences.
  • Returns: []*newUserPreference: A slice of new user preferences.
func (migration *updatePreferences) CopyOldUserPreferencesToNewUserPreferences(existingUserPreferences []*existingUserPreference) []*newUserPreference {
  1. Code Examples

None

  1. Clarity and Accuracy

The documentation is based on the code provided.

  1. Markdown & MDX Perfection

No issues found.

  1. Edge Cases To Avoid Breaking MDX

No issues found.

  1. Getting Started Relevance

Include in Getting Started: NO