v2

package
v0.0.0-...-dcafc8e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CreateUser = gormigrate.Migration{
	ID: "2024-09-18:create-users",
	Migrate: func(tx *gorm.DB) error {
		if err := tx.AutoMigrate(&User{}); err != nil {
			return err
		}

		if err := tx.Migrator().AddColumn(&accountWithNullableUserID{}, "user_id"); err != nil {
			return err
		}

		var accounts []accountWithNullableUserID
		if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Find(&accounts).Error; err != nil {
			return err
		}

		if len(accounts) != 0 {
			user := User{
				Name: "default",
			}
			if err := tx.Create(&user).Error; err != nil {
				return err
			}

			if err := tx.Model(&account{}).Where("1 = 1").Update("user_id", user.ID).Error; err != nil {
				return err
			}
		}

		return tx.Migrator().AlterColumn(&account{}, "user_id")
	},
	Rollback: func(tx *gorm.DB) error {
		if err := tx.Migrator().DropColumn(&account{}, "user_id"); err != nil {
			return err
		}

		return tx.Migrator().DropTable(&User{})
	},
}

CreateUser defines the migration, which creates the users.

Functions

This section is empty.

Types

type User

type User struct {
	ID        string `gorm:"type:uuid;primary_key;default:uuid_generate_v4()"`
	CreatedAt time.Time
	UpdatedAt time.Time
	Disabled  bool   `gorm:"not null;default:false"`
	Name      string `gorm:"type:varchar(255);not null"`
	Nickname  string `gorm:"type:varchar(255)"`

	Accounts []account
}

User represents the database model for a user

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL