github.com/jxgolibs/go-oauth2-server@v1.0.1/util/migrations/bootstrap.go (about)

     1  package migrations
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/RichardKnop/go-oauth2-server/log"
     7  	"github.com/jinzhu/gorm"
     8  )
     9  
    10  // Bootstrap creates "migrations" table
    11  // to keep track of already run database migrations
    12  func Bootstrap(db *gorm.DB) error {
    13  	migrationName := "bootstrap_migrations"
    14  
    15  	migration := new(Migration)
    16  	// Using Error instead of RecordNotFound because we want to check
    17  	// if the migrations table exists. This is different from later migrations
    18  	// where we query the already create migrations table.
    19  	exists := nil == db.Where("name = ?", migrationName).First(migration).Error
    20  
    21  	if exists {
    22  		log.INFO.Printf("Skipping %s migration", migrationName)
    23  		return nil
    24  	}
    25  
    26  	log.INFO.Printf("Running %s migration", migrationName)
    27  
    28  	// Create migrations table
    29  	if err := db.CreateTable(new(Migration)).Error; err != nil {
    30  		return fmt.Errorf("Error creating migrations table: %s", db.Error)
    31  	}
    32  
    33  	// Save a record to migrations table,
    34  	// so we don't rerun this migration again
    35  	migration.Name = migrationName
    36  	if err := db.Create(migration).Error; err != nil {
    37  		return fmt.Errorf("Error saving record to migrations table: %s", err)
    38  	}
    39  
    40  	return nil
    41  }