Beispiel #1
0
// Load executes all of the statements in the provided sql script against the
// test database, panicking if any fail.  The receiver is returned allowing for
// chain-style calling within your test functions.
func (db *DB) Load(sql string) *DB {
	conn := db.Open()
	defer conn.Close()

	tx, err := conn.Begin()
	if err != nil {
		err = errors.Wrap(err, "begin failed")
		panic(err)
	}

	defer tx.Rollback()

	for i, cmd := range sqlutils.AllStatements(sql) {
		_, err = tx.Exec(cmd)
		if err != nil {
			err = errors.Wrapf(err, "failed execing statement: %d", i)
			panic(err)
		}
	}

	err = tx.Commit()
	if err != nil {
		err = errors.Wrap(err, "commit failed")
		panic(err)
	}

	return db
}
Beispiel #2
0
// ExecAll runs all sql commands in `script` against `r` within a single
// transaction.
func (r *Repo) ExecAll(script string) error {
	err := r.Begin()
	if err != nil {
		return err
	}

	defer r.Rollback()

	for _, cmd := range sqlutils.AllStatements(script) {
		_, err = r.ExecRaw(cmd)
		if err != nil {
			return err
		}
	}

	return r.Commit()
}