github.com/pjdufour-truss/pop@v4.11.2-0.20190705085848-4c90b0ff4d5a+incompatible/connection.go (about) 1 package pop 2 3 import ( 4 "sync/atomic" 5 "time" 6 7 "github.com/gobuffalo/pop/internal/defaults" 8 "github.com/gobuffalo/pop/internal/randx" 9 "github.com/jmoiron/sqlx" 10 "github.com/pkg/errors" 11 ) 12 13 // Connections contains all available connections 14 var Connections = map[string]*Connection{} 15 16 // Connection represents all necessary details to talk with a datastore 17 type Connection struct { 18 ID string 19 Store store 20 Dialect dialect 21 Elapsed int64 22 TX *Tx 23 eager bool 24 eagerFields []string 25 } 26 27 func (c *Connection) String() string { 28 return c.URL() 29 } 30 31 // URL returns the datasource connection string 32 func (c *Connection) URL() string { 33 return c.Dialect.URL() 34 } 35 36 // MigrationURL returns the datasource connection string used for running the migrations 37 func (c *Connection) MigrationURL() string { 38 return c.Dialect.MigrationURL() 39 } 40 41 // MigrationTableName returns the name of the table to track migrations 42 func (c *Connection) MigrationTableName() string { 43 return c.Dialect.Details().MigrationTableName() 44 } 45 46 // NewConnection creates a new connection, and sets it's `Dialect` 47 // appropriately based on the `ConnectionDetails` passed into it. 48 func NewConnection(deets *ConnectionDetails) (*Connection, error) { 49 err := deets.Finalize() 50 if err != nil { 51 return nil, err 52 } 53 c := &Connection{ 54 ID: randx.String(30), 55 } 56 57 if nc, ok := newConnection[deets.Dialect]; ok { 58 c.Dialect, err = nc(deets) 59 if err != nil { 60 return c, errors.Wrap(err, "could not create new connection") 61 } 62 return c, nil 63 } 64 return nil, errors.Errorf("could not found connection creator for %v", deets.Dialect) 65 } 66 67 // Connect takes the name of a connection, default is "development", and will 68 // return that connection from the available `Connections`. If a connection with 69 // that name can not be found an error will be returned. If a connection is 70 // found, and it has yet to open a connection with its underlying datastore, 71 // a connection to that store will be opened. 72 func Connect(e string) (*Connection, error) { 73 if len(Connections) == 0 { 74 err := LoadConfigFile() 75 if err != nil { 76 return nil, err 77 } 78 } 79 e = defaults.String(e, "development") 80 c := Connections[e] 81 if c == nil { 82 return c, errors.Errorf("could not find connection named %s", e) 83 } 84 err := c.Open() 85 return c, errors.Wrapf(err, "couldn't open connection for %s", e) 86 } 87 88 // Open creates a new datasource connection 89 func (c *Connection) Open() error { 90 if c.Store != nil { 91 return nil 92 } 93 if c.Dialect == nil { 94 return errors.New("invalid connection instance") 95 } 96 details := c.Dialect.Details() 97 driver := details.Dialect 98 if details.Driver != "" { 99 driver = details.Driver 100 } 101 102 db, err := sqlx.Open(driver, c.Dialect.URL()) 103 if err != nil { 104 return errors.Wrap(err, "could not open database connection") 105 } 106 db.SetMaxOpenConns(details.Pool) 107 if details.IdlePool != 0 { 108 db.SetMaxIdleConns(details.IdlePool) 109 } 110 c.Store = &dB{db} 111 112 if d, ok := c.Dialect.(afterOpenable); ok { 113 err = d.AfterOpen(c) 114 if err != nil { 115 c.Store = nil 116 } 117 } 118 return errors.Wrap(err, "could not open database connection") 119 } 120 121 // Close destroys an active datasource connection 122 func (c *Connection) Close() error { 123 return errors.Wrap(c.Store.Close(), "couldn't close connection") 124 } 125 126 // Transaction will start a new transaction on the connection. If the inner function 127 // returns an error then the transaction will be rolled back, otherwise the transaction 128 // will automatically commit at the end. 129 func (c *Connection) Transaction(fn func(tx *Connection) error) error { 130 return c.Dialect.Lock(func() error { 131 var dberr error 132 cn, err := c.NewTransaction() 133 if err != nil { 134 return err 135 } 136 err = fn(cn) 137 if err != nil { 138 dberr = cn.TX.Rollback() 139 } else { 140 dberr = cn.TX.Commit() 141 } 142 if err != nil { 143 return err 144 } 145 return errors.Wrap(dberr, "error committing or rolling back transaction") 146 }) 147 148 } 149 150 // Rollback will open a new transaction and automatically rollback that transaction 151 // when the inner function returns, regardless. This can be useful for tests, etc... 152 func (c *Connection) Rollback(fn func(tx *Connection)) error { 153 cn, err := c.NewTransaction() 154 if err != nil { 155 return err 156 } 157 fn(cn) 158 return cn.TX.Rollback() 159 } 160 161 // NewTransaction starts a new transaction on the connection 162 func (c *Connection) NewTransaction() (*Connection, error) { 163 var cn *Connection 164 if c.TX == nil { 165 tx, err := c.Store.Transaction() 166 if err != nil { 167 return cn, errors.Wrap(err, "couldn't start a new transaction") 168 } 169 cn = &Connection{ 170 ID: randx.String(30), 171 Store: tx, 172 Dialect: c.Dialect, 173 TX: tx, 174 } 175 } else { 176 cn = c 177 } 178 return cn, nil 179 } 180 181 func (c *Connection) copy() *Connection { 182 return &Connection{ 183 ID: randx.String(30), 184 Store: c.Store, 185 Dialect: c.Dialect, 186 TX: c.TX, 187 } 188 } 189 190 // Q creates a new "empty" query for the current connection. 191 func (c *Connection) Q() *Query { 192 return Q(c) 193 } 194 195 // disableEager disables eager mode for current connection. 196 func (c *Connection) disableEager() { 197 c.eager = false 198 c.eagerFields = []string{} 199 } 200 201 // TruncateAll truncates all data from the datasource 202 func (c *Connection) TruncateAll() error { 203 return c.Dialect.TruncateAll(c) 204 } 205 206 func (c *Connection) timeFunc(name string, fn func() error) error { 207 start := time.Now() 208 err := fn() 209 atomic.AddInt64(&c.Elapsed, int64(time.Since(start))) 210 if err != nil { 211 return err 212 } 213 return nil 214 }