go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/apputil/migrations.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package apputil
     9  
    10  import (
    11  	"go.charczuk.com/sdk/db/dbgen"
    12  	"go.charczuk.com/sdk/db/migration"
    13  )
    14  
    15  // MigrationSuite returns a fully formed migration suite you can use to
    16  // apply the `MigrationGroups` directly.
    17  func MigrationSuite(opts ...migration.SuiteOption) *migration.Suite {
    18  	return migration.New(
    19  		append(opts,
    20  			migration.OptGroups(
    21  				MigrationGroups()...,
    22  			),
    23  		)...,
    24  	)
    25  }
    26  
    27  // MigrationGroups are the included migrations for the tables
    28  // this package requires. You can use this array
    29  // directly or indirectly through `Migrations`.
    30  func MigrationGroups() []*migration.Group {
    31  	return []*migration.Group{
    32  		extensions(),
    33  		users(),
    34  		sessions(),
    35  	}
    36  }
    37  
    38  func extensions() *migration.Group {
    39  	return migration.NewGroupWithAction(
    40  		migration.NewStep(migration.Always(), migration.Statements(`CREATE EXTENSION IF NOT EXISTS "pgcrypto"`)),
    41  	)
    42  }
    43  
    44  func users() *migration.Group {
    45  	return migration.NewGroupWithAction(
    46  		dbgen.TableFrom(User{},
    47  			dbgen.UniqueKey(User{}, "email"),
    48  		),
    49  	)
    50  }
    51  
    52  func sessions() *migration.Group {
    53  	return migration.NewGroupWithAction(
    54  		dbgen.TableFrom(Session{},
    55  			dbgen.ForeignKey(Session{}, "user_id", User{}, "id"),
    56  		),
    57  	)
    58  }