Skip to content

Commit

Permalink
Changed DbMap and createTables() to be able to support Transactions i…
Browse files Browse the repository at this point in the history
…n addtion to Connections. This is for Goose migration support.

commit 1e2e414
Author: Matt Culbreth <[email protected]>
Date:   Fri Apr 11 09:20:37 2014 -0400

    fixed else statement

commit 4fc09f6
Author: Matt Culbreth <[email protected]>
Date:   Fri Apr 11 09:18:40 2014 -0400

    changed dropTableImpl to use the transaction if it's there

commit 25cbcab
Author: Matt Culbreth <[email protected]>
Date:   Fri Apr 11 12:56:10 2014 +0000

    * added Tx to DbMap
    * added logic in createTables() to switch between Tx and DbMap for the Exec
  • Loading branch information
Matt Culbreth committed May 21, 2014
1 parent f296a21 commit 96ea999
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions gorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ type DbMap struct {
// Db handle to use with this map
Db *sql.DB

// Transaction handle to use with this map
Tx *sql.Tx

// Dialect implementation to use with this map
Dialect Dialect

Expand Down Expand Up @@ -811,7 +814,14 @@ func (m *DbMap) createTables(ifNotExists bool) error {
s.WriteString(") ")
s.WriteString(m.Dialect.CreateTableSuffix())
s.WriteString(";")
_, err = m.Exec(s.String())

// use the transaction if it's there. otherwise, use the db connection.
if m.Tx != nil {
_, err = m.Tx.Exec(s.String())
} else {
_, err = m.Exec(s.String())
}

if err != nil {
break
}
Expand Down Expand Up @@ -873,7 +883,14 @@ func (m *DbMap) dropTableImpl(table *TableMap, addIfExists bool) (err error) {
if addIfExists {
ifExists = " if exists"
}
_, err = m.Exec(fmt.Sprintf("drop table%s %s;", ifExists, m.Dialect.QuotedTableForQuery(table.SchemaName, table.TableName)))

// use the transaction if it's there. otherwise, use the db connection.
if m.Tx != nil {
_, err = m.Tx.Exec(fmt.Sprintf("drop table%s %s;", ifExists, m.Dialect.QuotedTableForQuery(table.SchemaName, table.TableName)))
} else {
_, err = m.Exec(fmt.Sprintf("drop table%s %s;", ifExists, m.Dialect.QuotedTableForQuery(table.SchemaName, table.TableName)))
}

return err
}

Expand Down

0 comments on commit 96ea999

Please sign in to comment.