github.com/friesencr/pop/v6@v6.1.6/migration_info.go (about)

     1  package pop
     2  
     3  import "fmt"
     4  
     5  // Migration handles the data for a given database migration
     6  type Migration struct {
     7  	// Path to the migration (./migrations/123_create_widgets.up.sql)
     8  	Path string
     9  	// Version of the migration (123)
    10  	Version string
    11  	// Name of the migration (create_widgets)
    12  	Name string
    13  	// Direction of the migration (up)
    14  	Direction string
    15  	// Type of migration (sql)
    16  	Type string
    17  	// DB type (all|postgres|mysql...)
    18  	DBType string
    19  	// Runner function to run/execute the migration
    20  	Runner func(Migration, *Connection) error
    21  	// No transaction
    22  	NoTransaction bool
    23  }
    24  
    25  // Run the migration. Returns an error if there is
    26  // no mf.Runner defined.
    27  func (mf Migration) Run(c *Connection) error {
    28  	if mf.Runner == nil {
    29  		return fmt.Errorf("no runner defined for %s", mf.Path)
    30  	}
    31  	return mf.Runner(mf, c)
    32  }
    33  
    34  // Migrations is a collection of Migration
    35  type Migrations []Migration
    36  
    37  func (mfs Migrations) Len() int {
    38  	return len(mfs)
    39  }
    40  
    41  func (mfs Migrations) Swap(i, j int) {
    42  	mfs[i], mfs[j] = mfs[j], mfs[i]
    43  }
    44  
    45  func (mfs *Migrations) Filter(f func(mf Migration) bool) {
    46  	vsf := make(Migrations, 0)
    47  	for _, v := range *mfs {
    48  		if f(v) {
    49  			vsf = append(vsf, v)
    50  		}
    51  	}
    52  	*mfs = vsf
    53  }
    54  
    55  type (
    56  	UpMigrations struct {
    57  		Migrations
    58  	}
    59  	DownMigrations struct {
    60  		Migrations
    61  	}
    62  )
    63  
    64  func (mfs UpMigrations) Less(i, j int) bool {
    65  	if mfs.Migrations[i].Version == mfs.Migrations[j].Version {
    66  		// force "all" to the back
    67  		return mfs.Migrations[i].DBType != "all"
    68  	}
    69  	return mfs.Migrations[i].Version < mfs.Migrations[j].Version
    70  }
    71  
    72  func (mfs DownMigrations) Less(i, j int) bool {
    73  	if mfs.Migrations[i].Version == mfs.Migrations[j].Version {
    74  		// force "all" to the back
    75  		return mfs.Migrations[i].DBType != "all"
    76  	}
    77  	return mfs.Migrations[i].Version > mfs.Migrations[j].Version
    78  }