91 рядки
3.0 KiB
Go
91 рядки
3.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func Migrate(db *gorm.DB) error {
|
|
return db.AutoMigrate(
|
|
&UserModel{}, &ListModel{}, &ListShareModel{}, &PendingShareModel{},
|
|
&ItemModel{}, &BotAccessModel{},
|
|
)
|
|
}
|
|
|
|
type UserModel struct {
|
|
ID int64 `gorm:"primaryKey"`
|
|
TelegramID int64 `gorm:"not null;uniqueIndex"`
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
func (UserModel) TableName() string { return "users" }
|
|
|
|
type ListModel struct {
|
|
ID int64 `gorm:"primaryKey"`
|
|
OwnerID int64 `gorm:"not null;index"`
|
|
Title string `gorm:"not null;size:120"`
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
DeletedAt gorm.DeletedAt `gorm:"index"`
|
|
}
|
|
|
|
func (ListModel) TableName() string { return "lists" }
|
|
|
|
type ListShareModel struct {
|
|
ID int64 `gorm:"primaryKey"`
|
|
ListID int64 `gorm:"not null;uniqueIndex:idx_list_user"`
|
|
UserID int64 `gorm:"not null;uniqueIndex:idx_list_user"`
|
|
Role string `gorm:"not null;size:16"`
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
func (ListShareModel) TableName() string { return "list_shares" }
|
|
|
|
// PendingShareModel holds a share whose target has bot access but no local
|
|
// user row yet. Exactly one of TelegramID and Username is set.
|
|
type PendingShareModel struct {
|
|
ID int64 `gorm:"primaryKey"`
|
|
ListID int64 `gorm:"not null;uniqueIndex:idx_pending_list_telegram,priority:1;uniqueIndex:idx_pending_list_username,priority:1"`
|
|
TelegramID *int64 `gorm:"uniqueIndex:idx_pending_list_telegram,priority:2"`
|
|
Username *string `gorm:"size:32;uniqueIndex:idx_pending_list_username,priority:2"`
|
|
Role string `gorm:"not null;size:16"`
|
|
CreatedByID int64 `gorm:"not null"`
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
func (PendingShareModel) TableName() string { return "pending_shares" }
|
|
|
|
type ItemModel struct {
|
|
ID int64 `gorm:"primaryKey"`
|
|
ListID int64 `gorm:"not null;index:idx_items_list_status_urgency_created,priority:1;index:idx_items_list_created,priority:1"`
|
|
Title string `gorm:"not null;size:200"`
|
|
Note string `gorm:"size:1000"`
|
|
Quantity float64 `gorm:"type:numeric(12,3);not null;default:1"`
|
|
Urgency string `gorm:"not null;size:16;default:normal;index:idx_items_list_status_urgency_created,priority:3"`
|
|
Status string `gorm:"not null;size:16;default:open;index:idx_items_list_status_urgency_created,priority:2"`
|
|
ReservedByID *int64 `gorm:"index"`
|
|
ReservedAt *time.Time
|
|
CompletedAt *time.Time
|
|
CreatedByID int64 `gorm:"not null;index"`
|
|
CreatedAt time.Time `gorm:"index:idx_items_list_created,priority:2"`
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
func (ItemModel) TableName() string { return "items" }
|
|
|
|
// BotAccessModel grants bot access. A username grant is pending until the
|
|
// matching user contacts the bot; TelegramID is then filled in.
|
|
type BotAccessModel struct {
|
|
ID int64 `gorm:"primaryKey"`
|
|
TelegramID *int64 `gorm:"uniqueIndex"`
|
|
Username *string `gorm:"uniqueIndex"`
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
func (BotAccessModel) TableName() string { return "bot_access" }
|