gitlab.com/ignitionrobotics/web/ign-go@v1.0.0-rc4/database/gorm/gorm.go (about)

     1  package gorm
     2  
     3  import (
     4  	"errors"
     5  	"github.com/jinzhu/gorm"
     6  	"gitlab.com/ignitionrobotics/web/ign-go"
     7  	"log"
     8  )
     9  
    10  var (
    11  	// ErrEmptyDatabaseName is returned when a database configuration contains an empty database name
    12  	ErrEmptyDatabaseName = errors.New("db config contains empty database name")
    13  )
    14  
    15  // getDBConfigFromEnvVars reads environment variables to return a database connection configuration.
    16  // The environment variables used are:
    17  // * IGN_DB_ADDRESS Address of the DBMS.
    18  // * IGN_DB_USERNAME Username to connect to the DBMS with.
    19  // * IGN_DB_PASSWORD Password to connect to the DBMS with.
    20  // * IGN_DB_NAME Name of the database to connect to.
    21  // * IGN_DB_MAX_OPEN_CONNS - (Optional) You run the risk of getting a 'too many connections' error if this is not set.
    22  func getDBConfigFromEnvVars() (*ign.DatabaseConfig, error) {
    23  	// Get the db config
    24  	var dbConfig ign.DatabaseConfig
    25  	var err error
    26  	dbConfig, err = ign.NewDatabaseConfigFromEnvVars()
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	if len(dbConfig.Name) == 0 {
    31  		return nil, ErrEmptyDatabaseName
    32  	}
    33  
    34  	return &dbConfig, nil
    35  }
    36  
    37  // GetDBFromEnvVars reads environment variables to return a Gorm database connection.
    38  func GetDBFromEnvVars() (*gorm.DB, error) {
    39  	// Get the db config
    40  	dbConfig, err := getDBConfigFromEnvVars()
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	// Connect to the db
    46  	db, err := ign.InitDbWithCfg(dbConfig)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	return db, nil
    52  }
    53  
    54  // GetTestDBFromEnvVars reads environment variables to return a Gorm database connection to a test database.
    55  // The test database will have "_test" automatically appended to the target database name.
    56  func GetTestDBFromEnvVars() (*gorm.DB, error) {
    57  	// Get the db config
    58  	dbConfig, err := getDBConfigFromEnvVars()
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	// Add the test name suffix
    64  	dbConfig.Name += "_test"
    65  
    66  	// Connect to the db
    67  	db, err := ign.InitDbWithCfg(dbConfig)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	return db, nil
    73  }
    74  
    75  // DropModels drops database models.
    76  func DropModels(tx *gorm.DB, models ...interface{}) error {
    77  	if tx == nil {
    78  		return errors.New("attempted to migrate with an invalid tx")
    79  	}
    80  
    81  	log.Printf("DropModels: Dropping models: %v\n", models)
    82  	if err := tx.DropTableIfExists(models...).Error; err != nil {
    83  		log.Println("DropModels: Error while running DropTableIfExists, error:", err)
    84  		return err
    85  	}
    86  
    87  	return nil
    88  }
    89  
    90  // MigrateModels migrates database models.
    91  // If the model table already exists, it will be updated to reflect the model structure. The table will only have
    92  // columns added or updated but not dropped.
    93  func MigrateModels(tx *gorm.DB, models ...interface{}) error {
    94  	if tx == nil {
    95  		return errors.New("attempted to migrate with an invalid tx")
    96  	}
    97  
    98  	log.Printf("MigrateModels: Migrating tables: %v\n", models)
    99  	if err := tx.AutoMigrate(models...).Error; err != nil {
   100  		log.Println("MigrateModels: Error while running AutoMigrate, error:", err)
   101  		return err
   102  	}
   103  
   104  	return nil
   105  }
   106  
   107  // CleanAndMigrateModels drops existing target model tables and recreates them.
   108  func CleanAndMigrateModels(tx *gorm.DB, models ...interface{}) error {
   109  	if tx == nil {
   110  		return errors.New("attempted to clean database with an invalid tx")
   111  	}
   112  
   113  	if err := DropModels(tx, models...); err != nil {
   114  		log.Println("CleanAndMigrateModels: Error while running DropModels, error:", err)
   115  		return err
   116  	}
   117  
   118  	if err := MigrateModels(tx, models...); err != nil {
   119  		log.Println("CleanAndMigrateModels: Error while running MigrateModels, error:", err)
   120  		return err
   121  	}
   122  
   123  	return nil
   124  }