github.com/dhax/go-base@v0.0.0-20231004214136-8be7e5c1972b/database/migrate/2_bootstrap_users.go (about)

     1  package migrate
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/go-pg/migrations"
     7  )
     8  
     9  const bootstrapAdminAccount = `
    10  INSERT INTO accounts (id, email, name, active, roles)
    11  VALUES (DEFAULT, 'admin@boot.io', 'Admin Boot', true, '{admin}')
    12  `
    13  
    14  const bootstrapUserAccount = `
    15  INSERT INTO accounts (id, email, name, active)
    16  VALUES (DEFAULT, 'user@boot.io', 'User Boot', true)
    17  `
    18  
    19  func init() {
    20  	up := []string{
    21  		bootstrapAdminAccount,
    22  		bootstrapUserAccount,
    23  	}
    24  
    25  	down := []string{
    26  		`TRUNCATE accounts CASCADE`,
    27  	}
    28  
    29  	migrations.Register(func(db migrations.DB) error {
    30  		fmt.Println("add bootstrap accounts")
    31  		for _, q := range up {
    32  			_, err := db.Exec(q)
    33  			if err != nil {
    34  				return err
    35  			}
    36  		}
    37  		return nil
    38  	}, func(db migrations.DB) error {
    39  		fmt.Println("truncate accounts cascading")
    40  		for _, q := range down {
    41  			_, err := db.Exec(q)
    42  			if err != nil {
    43  				return err
    44  			}
    45  		}
    46  		return nil
    47  	})
    48  }