add_preferences.go
002_add_preferences.go - Overview
-
Overview This file defines a SQL migration to add tables
user_preference
andorg_preference
to the database. -
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 createsSQLMigration
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:
_
: Acontext.Context
(unused)._
: Afactory.ProviderSettings
(unused)._
: AConfig
(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 amigrate.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
andorg_preference
tables in the database. - Parameters:
ctx
: Acontext.Context
for the database operation.db
: A pointer to abun.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
: Acontext.Context
for the database operation.db
: A pointer to abun.DB
object representing the database connection.
- Returns: Always nil.
Include in Getting Started: NO