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

     1  package migrate
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/go-pg/migrations"
     7  )
     8  
     9  const profileTable = `
    10  CREATE TABLE profiles (
    11  id serial NOT NULL,
    12  updated_at timestamp with time zone NOT NULL DEFAULT current_timestamp,
    13  account_id int NOT NULL REFERENCES accounts(id),
    14  theme text NOT NULL DEFAULT 'default',
    15  PRIMARY KEY (id)
    16  )`
    17  
    18  const bootstrapAccountProfiles = `
    19  INSERT INTO profiles(account_id) VALUES(1);
    20  INSERT INTO profiles(account_id) VALUES(2);
    21  `
    22  
    23  func init() {
    24  	up := []string{
    25  		profileTable,
    26  		bootstrapAccountProfiles,
    27  	}
    28  
    29  	down := []string{
    30  		`DROP TABLE profiles`,
    31  	}
    32  
    33  	migrations.Register(func(db migrations.DB) error {
    34  		fmt.Println("create profile table")
    35  		for _, q := range up {
    36  			_, err := db.Exec(q)
    37  			if err != nil {
    38  				return err
    39  			}
    40  		}
    41  		return nil
    42  	}, func(db migrations.DB) error {
    43  		fmt.Println("drop profile table")
    44  		for _, q := range down {
    45  			_, err := db.Exec(q)
    46  			if err != nil {
    47  				return err
    48  			}
    49  		}
    50  		return nil
    51  	})
    52  }