2025-01-20 17:45:33 +05:30
|
|
|
package sqlmigration
|
2025-01-17 16:52:55 +05:30
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2025-03-20 21:01:41 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/factory"
|
2025-01-17 16:52:55 +05:30
|
|
|
"github.com/uptrace/bun"
|
|
|
|
|
"github.com/uptrace/bun/migrate"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type addPreferences struct{}
|
|
|
|
|
|
2025-01-20 17:45:33 +05:30
|
|
|
func NewAddPreferencesFactory() factory.ProviderFactory[SQLMigration, Config] {
|
2025-01-17 16:52:55 +05:30
|
|
|
return factory.NewProviderFactory(factory.MustNewName("add_preferences"), newAddPreferences)
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 17:45:33 +05:30
|
|
|
func newAddPreferences(_ context.Context, _ factory.ProviderSettings, _ Config) (SQLMigration, error) {
|
2025-01-17 16:52:55 +05:30
|
|
|
return &addPreferences{}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (migration *addPreferences) Register(migrations *migrate.Migrations) error {
|
|
|
|
|
if err := migrations.Register(migration.Up, migration.Down); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (migration *addPreferences) Up(ctx context.Context, db *bun.DB) error {
|
|
|
|
|
// table:user_preference
|
2025-02-12 09:58:49 +05:30
|
|
|
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 {
|
2025-01-17 16:52:55 +05:30
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// table:org_preference
|
2025-02-12 09:58:49 +05:30
|
|
|
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 {
|
2025-01-17 16:52:55 +05:30
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (migration *addPreferences) Down(ctx context.Context, db *bun.DB) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|