// 1. Setup connection of database func (s *ChangeLogSuite) SetUpSuite(c *C) { flag.Parse() if *driverName == "" { c.Log("No database assigned, some tests would be skipped") return } c.Logf("Connect to database. Driver Name: [%v]. DSN: [%v]", *driverName, *dsn) var err error if dbConfig, err = patchsql.NewDatabaseConfig(*driverName, *dsn); err != nil { c.Fatalf("Connect database error: %v", err) } }
// Executes the patches in change log func ExecutePatches(changeLogConfig *ChangeLogConfig) (err error) { /** * Loads configuration of patches */ var loadedPatches []PatchConfig if loadedPatches, err = LoadChangeLogFromFile(changeLogConfig.ChangeLog); err != nil { return } // :~) /** * Connect to database */ var dbConfig *patchsql.DatabaseConfig if dbConfig, err = patchsql.NewDatabaseConfig( changeLogConfig.DriverName, changeLogConfig.Dsn, ); err != nil { return } defer dbConfig.Close() // :~) /** * Checking the schema for change log */ if err = dbConfig.Execute(checkChangeLogSchema); err != nil { return } // :~) /** * Iterates each patch and applies it to database */ var numberOfApplied = 0 for _, p := range loadedPatches { /** * Checks if the patch has been applied */ if patchApplied, _ := hasPatchApplied(dbConfig, &p); patchApplied { continue } // :~) log.Printf("Applying patch: [%v](%v)...", p.Id, p.Filename) var scripts []string /** * Loads scripts from file */ if scripts, err = p.loadScripts(changeLogConfig.PatchFileBase, changeLogConfig.Delimiter); err != nil { return fmt.Errorf("Load script file[%v/%v] error: %v", changeLogConfig.PatchFileBase, p.Filename, err) } // :~) /** * Applies patch to database */ if err = applyPatch(dbConfig, &p, scripts); err != nil { var patchErr = fmt.Errorf("Patch [%v](%v) has error: %v", p.Id, p.Filename, err) log.Println(patchErr) return patchErr } // :~) numberOfApplied++ log.Printf("Applying patch success. [%v](%v).", p.Id, p.Filename) } // :~) log.Printf("Number of applied patches: %v", numberOfApplied) return }