github.com/litesolutions/justifay-api@v1.0.0-2.0.20220707114139-46f28a909481/migrations/22052021010101_initial.go (about)

     1  package migrations
     2  
     3  import (
     4  	//	"github.com/go-pg/migrations"
     5  	//	"github.com/go-pg/pg/orm"
     6  	"context"
     7  	"fmt"
     8  
     9  	"github.com/uptrace/bun"
    10  
    11  	//"github.com/uptrace/bun/dialect/pgdialect"
    12  	//_ "github.com/jackc/pgx/v4/stdlib"
    13  
    14  	"github.com/litesolutions/justifay-api/model"
    15  )
    16  
    17  func init() {
    18  
    19  	// Drop and create tables.
    20  	models := []interface{}{
    21  		(*model.UserGroup)(nil),
    22  		(*model.StreetAddress)(nil),
    23  		(*model.Tag)(nil),
    24  		(*model.Role)(nil),
    25  		(*model.User)(nil),
    26  		(*model.Link)(nil),
    27  		//		(*model.UserGroupPrivacy)(nil),
    28  		(*model.GroupType)(nil),
    29  		//		(*model.UserGroupMember)(nil),
    30  		(*model.EmailToken)(nil),
    31  		(*model.Client)(nil),
    32  		(*model.Scope)(nil),
    33  		(*model.RefreshToken)(nil),
    34  		(*model.AuthorizationCode)(nil),
    35  		(*model.AccessToken)(nil),
    36  	}
    37  
    38  	Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
    39  		fmt.Print(" [up migration] ")
    40  
    41  		// if _, err := db.QueryContext(ctx, `CREATE EXTENSION IF NOT EXISTS "hstore"`); err != nil {
    42  		// 	return err
    43  		// }
    44  
    45  		// if _, err := db.QueryContext(ctx, `CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`); err != nil {
    46  		// 	return err
    47  		// }
    48  
    49  		for _, model := range models {
    50  			_, err := db.NewDropTable().Model(model).IfExists().Exec(ctx)
    51  			if err != nil {
    52  				panic(err)
    53  			}
    54  
    55  			_, err = db.NewCreateTable().Model(model).Exec(ctx)
    56  			if err != nil {
    57  				panic(err)
    58  			}
    59  		}
    60  
    61  		return nil
    62  	}, func(ctx context.Context, db *bun.DB) error {
    63  		fmt.Print(" [down migration] ")
    64  		for _, this_model := range models {
    65  			_, err := db.NewDropTable().Model(this_model).IfExists().Exec(ctx)
    66  			if err != nil {
    67  				panic(err)
    68  			}
    69  		}
    70  		return nil
    71  	})
    72  
    73  	// Migrations.MustRegister(func(db migrations.DB) error {
    74  	// if _, err := db.Exec( /* language=sql */ `CREATE EXTENSION IF NOT EXISTS "hstore"`); err != nil {
    75  	// 	return err
    76  	// }
    77  	//
    78  	// if _, err := db.Exec( /* language=sql */ `CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`); err != nil {
    79  	//   return err
    80  	// }
    81  	// if _, err := db.Exec(`CREATE TYPE github.com/resonatecoop/_status AS ENUM ('paid', 'free', 'both');`); err != nil {
    82  	// 	return err
    83  	// }
    84  
    85  	// if _, err := db.Exec(`CREATE TYPE play_type AS ENUM ('paid', 'free');`); err != nil {
    86  	// 	return err
    87  	// }
    88  
    89  	// if _, err := db.Exec(`CREATE TYPE github.com/resonatecoop/_group_type AS ENUM ('lp', 'ep', 'single', 'playlist');`); err != nil {
    90  	// 	return err
    91  	// }
    92  
    93  	// for _, model := range []interface{}{
    94  	// 	&model.StreetAddress{},
    95  	// 	&model.Tag{},
    96  	// 	&model.User{},
    97  	// 	&model.Link{},
    98  	// 	&model.UserGroupPrivacy{},
    99  	// 	&model.GroupTaxonomy{},
   100  	// 	&model.UserGroup{},
   101  	// 	// &model.github.com/resonatecoop/{},
   102  	// 	// &model.github.com/resonatecoop/Group{},
   103  	// 	&model.UserGroupMember{},
   104  	// 	// &model.Play{},
   105  	// } {
   106  	// 	err := orm.CreateTable(db.(orm.DB), model, &orm.CreateTableOptions{
   107  	// 		FKConstraints: true,
   108  	// 		IfNotExists:   true,
   109  	// 	})
   110  	// 	if err != nil {
   111  	// 		return err
   112  	// 	}
   113  	// }
   114  
   115  	// 	db.RegisterModel((*model.UserGroupMember)(nil))
   116  	// 	if _, err := db.Exec(`alter table user_group_members add foreign key (user_group_id) references user_groups(id)`); err != nil {
   117  	// 		return err
   118  	// 	}
   119  	// 	if _, err := db.Exec(`alter table user_group_members add foreign key (member_id) references user_groups(id)`); err != nil {
   120  	// 		return err
   121  	// 	}
   122  	// 	return nil
   123  	// }, func(db migrations.DB) error {
   124  	// 	if _, err := db.Exec(`DROP TYPE IF EXISTS play_type CASCADE;`); err != nil {
   125  	// 		return err
   126  	// 	}
   127  	// 	if _, err := db.Exec(`DROP TYPE IF EXISTS github.com/resonatecoop/_status CASCADE;`); err != nil {
   128  	// 		return err
   129  	// 	}
   130  	// 	if _, err := db.Exec(`DROP TYPE IF EXISTS github.com/resonatecoop/_group_type CASCADE;`); err != nil {
   131  	// 		return err
   132  	// 	}
   133  	// 	for _, model := range []interface{}{
   134  	// 		// &model.Play{},
   135  	// 		&model.Tag{},
   136  	// 		// &model.github.com/resonatecoop/Group{},
   137  	// 		// &model.github.com/resonatecoop/{},
   138  	// 		&model.GroupTaxonomy{},
   139  	// 		&model.UserGroupMember{},
   140  	// 		&model.StreetAddress{},
   141  	// 		&model.UserGroupPrivacy{},
   142  	// 		&model.UserGroup{},
   143  	// 		&model.User{},
   144  	// 		&model.Link{},
   145  	// 	} {
   146  	// 		err := orm.DropTable(db.(orm.DB), model, &orm.DropTableOptions{
   147  	// 			IfExists: true,
   148  	// 			Cascade:  true,
   149  	// 		})
   150  	// 		if err != nil {
   151  	// 			return err
   152  	// 		}
   153  	// 	}
   154  
   155  	// 	return nil
   156  	// })
   157  }