|
| 1 | +package sqlmigration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/SigNoz/signoz/pkg/factory" |
| 9 | + "github.com/SigNoz/signoz/pkg/sqlstore" |
| 10 | + "github.com/SigNoz/signoz/pkg/types" |
| 11 | + "github.com/SigNoz/signoz/pkg/valuer" |
| 12 | + "github.com/uptrace/bun" |
| 13 | + "github.com/uptrace/bun/migrate" |
| 14 | +) |
| 15 | + |
| 16 | +type updateInvites struct { |
| 17 | + store sqlstore.SQLStore |
| 18 | +} |
| 19 | + |
| 20 | +type existingInvite struct { |
| 21 | + bun.BaseModel `bun:"table:invites"` |
| 22 | + |
| 23 | + OrgID string `bun:"org_id,type:text,notnull" json:"orgId"` |
| 24 | + ID int `bun:"id,pk,autoincrement" json:"id"` |
| 25 | + Name string `bun:"name,type:text,notnull" json:"name"` |
| 26 | + Email string `bun:"email,type:text,notnull,unique" json:"email"` |
| 27 | + Token string `bun:"token,type:text,notnull" json:"token"` |
| 28 | + CreatedAt time.Time `bun:"created_at,notnull" json:"createdAt"` |
| 29 | + Role string `bun:"role,type:text,notnull" json:"role"` |
| 30 | +} |
| 31 | + |
| 32 | +type newInvite struct { |
| 33 | + bun.BaseModel `bun:"table:user_invite"` |
| 34 | + |
| 35 | + types.Identifiable |
| 36 | + types.TimeAuditable |
| 37 | + Name string `bun:"name,type:text,notnull" json:"name"` |
| 38 | + Email string `bun:"email,type:text,notnull,unique" json:"email"` |
| 39 | + Token string `bun:"token,type:text,notnull" json:"token"` |
| 40 | + Role string `bun:"role,type:text,notnull" json:"role"` |
| 41 | + OrgID string `bun:"org_id,type:text,notnull" json:"orgId"` |
| 42 | +} |
| 43 | + |
| 44 | +func NewUpdateInvitesFactory(sqlstore sqlstore.SQLStore) factory.ProviderFactory[SQLMigration, Config] { |
| 45 | + return factory. |
| 46 | + NewProviderFactory( |
| 47 | + factory.MustNewName("update_invites"), |
| 48 | + func(ctx context.Context, ps factory.ProviderSettings, c Config) (SQLMigration, error) { |
| 49 | + return newUpdateInvites(ctx, ps, c, sqlstore) |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +func newUpdateInvites(_ context.Context, _ factory.ProviderSettings, _ Config, store sqlstore.SQLStore) (SQLMigration, error) { |
| 54 | + return &updateInvites{store: store}, nil |
| 55 | +} |
| 56 | + |
| 57 | +func (migration *updateInvites) Register(migrations *migrate.Migrations) error { |
| 58 | + if err := migrations. |
| 59 | + Register(migration.Up, migration.Down); err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func (migration *updateInvites) Up(ctx context.Context, db *bun.DB) error { |
| 67 | + tx, err := db. |
| 68 | + BeginTx(ctx, nil) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + defer tx.Rollback() |
| 74 | + |
| 75 | + err = migration. |
| 76 | + store. |
| 77 | + Dialect(). |
| 78 | + RenameTableAndModifyModel(ctx, tx, new(existingInvite), new(newInvite), func(ctx context.Context) error { |
| 79 | + existingInvites := make([]*existingInvite, 0) |
| 80 | + err = tx. |
| 81 | + NewSelect(). |
| 82 | + Model(&existingInvites). |
| 83 | + Scan(ctx) |
| 84 | + if err != nil { |
| 85 | + if err != sql.ErrNoRows { |
| 86 | + return err |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if err == nil && len(existingInvites) > 0 { |
| 91 | + newInvites := migration. |
| 92 | + CopyOldInvitesToNewInvites(existingInvites) |
| 93 | + _, err = tx. |
| 94 | + NewInsert(). |
| 95 | + Model(&newInvites). |
| 96 | + Exec(ctx) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + } |
| 101 | + return nil |
| 102 | + }) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + err = tx.Commit() |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func (migration *updateInvites) Down(context.Context, *bun.DB) error { |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func (migration *updateInvites) CopyOldInvitesToNewInvites(existingInvites []*existingInvite) []*newInvite { |
| 120 | + newInvites := make([]*newInvite, 0) |
| 121 | + for _, invite := range existingInvites { |
| 122 | + newInvites = append(newInvites, &newInvite{ |
| 123 | + Identifiable: types.Identifiable{ |
| 124 | + ID: valuer.GenerateUUID(), |
| 125 | + }, |
| 126 | + TimeAuditable: types.TimeAuditable{ |
| 127 | + CreatedAt: invite.CreatedAt, |
| 128 | + UpdatedAt: time.Now(), |
| 129 | + }, |
| 130 | + Name: invite.Name, |
| 131 | + Email: invite.Email, |
| 132 | + Token: invite.Token, |
| 133 | + Role: invite.Role, |
| 134 | + OrgID: invite.OrgID, |
| 135 | + }) |
| 136 | + } |
| 137 | + |
| 138 | + return newInvites |
| 139 | +} |
0 commit comments