Skip to main content

add_preferences.go

002_add_preferences.go - Overview

  1. Overview This file defines a SQL migration to add tables user_preference and org_preference to the database.

  2. Detailed Documentation

type addPreferences struct{}

type addPreferences struct{}
  • Purpose: Represents the migration for adding preference tables.

func NewAddPreferencesFactory() factory.ProviderFactory[SQLMigration, Config]

func NewAddPreferencesFactory() factory.ProviderFactory[SQLMigration, Config] {
return factory.NewProviderFactory(factory.MustNewName("add_preferences"), newAddPreferences)
}
  • Purpose: Creates a new factory for the addPreferences migration.
  • Returns: A factory.ProviderFactory that creates SQLMigration instances.

func newAddPreferences(_ context.Context, _ factory.ProviderSettings, _ Config) (SQLMigration, error)

func newAddPreferences(_ context.Context, _ factory.ProviderSettings, _ Config) (SQLMigration, error) {
return &addPreferences{}, nil
}
  • Purpose: Creates a new addPreferences instance.
  • Parameters:
    • _: A context.Context (unused).
    • _: A factory.ProviderSettings (unused).
    • _: A Config (unused).
  • Returns: A new addPreferences instance and an error (always nil).

func (migration *addPreferences) Register(migrations *migrate.Migrations) error

func (migration *addPreferences) Register(migrations *migrate.Migrations) error {
if err := migrations.Register(migration.Up, migration.Down); err != nil {
return err
}

return nil
}
  • Purpose: Registers the Up and Down migration functions with the migrate.Migrations object.
  • Parameters:
    • migrations: A pointer to a migrate.Migrations object.
  • Returns: An error if registration fails, nil otherwise.

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
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.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
}

return nil
}
  • Purpose: Creates the user_preference and org_preference tables in the database.
  • Parameters:
    • ctx: A context.Context for the database operation.
    • db: A pointer to a bun.DB object representing the database connection.
  • Returns: An error if table creation fails, nil otherwise.

func (migration *addPreferences) Down(ctx context.Context, db *bun.DB) error

func (migration *addPreferences) Down(ctx context.Context, db *bun.DB) error {
return nil
}
  • Purpose: Currently a no-op, it's intended to revert the Up migration, but currently doesn't drop the tables.
  • Parameters:
    • ctx: A context.Context for the database operation.
    • db: A pointer to a bun.DB object representing the database connection.
  • Returns: Always nil.

Include in Getting Started: NO