forked from jsteenb2/mess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hour_mysql_repository.go
198 lines (164 loc) · 4.91 KB
/
hour_mysql_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package adapters
import (
"context"
"database/sql"
"os"
"time"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/trainer/domain/hour"
"github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"go.uber.org/multierr"
)
type mysqlHour struct {
ID string `db:"id"`
Hour time.Time `db:"hour"`
Availability string `db:"availability"`
}
type MySQLHourRepository struct {
db *sqlx.DB
hourFactory hour.Factory
}
func NewMySQLHourRepository(db *sqlx.DB, hourFactory hour.Factory) *MySQLHourRepository {
if db == nil {
panic("missing db")
}
if hourFactory.IsZero() {
panic("missing hourFactory")
}
return &MySQLHourRepository{db: db, hourFactory: hourFactory}
}
// sqlContextGetter is an interface provided both by transaction and standard db connection
type sqlContextGetter interface {
GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
}
func (m MySQLHourRepository) GetHour(ctx context.Context, time time.Time) (*hour.Hour, error) {
return m.getOrCreateHour(ctx, m.db, time, false)
}
func (m MySQLHourRepository) getOrCreateHour(
ctx context.Context,
db sqlContextGetter,
hourTime time.Time,
forUpdate bool,
) (*hour.Hour, error) {
dbHour := mysqlHour{}
query := "SELECT * FROM `hours` WHERE `hour` = ?"
if forUpdate {
query += " FOR UPDATE"
}
err := db.GetContext(ctx, &dbHour, query, hourTime.UTC())
if errors.Is(err, sql.ErrNoRows) {
// in reality this date exists, even if it's not persisted
return m.hourFactory.NewNotAvailableHour(hourTime)
} else if err != nil {
return nil, errors.Wrap(err, "unable to get hour from db")
}
availability, err := hour.NewAvailabilityFromString(dbHour.Availability)
if err != nil {
return nil, err
}
domainHour, err := m.hourFactory.UnmarshalHourFromDatabase(dbHour.Hour.Local(), availability)
if err != nil {
return nil, err
}
return domainHour, nil
}
const mySQLDeadlockErrorCode = 1213
func (m MySQLHourRepository) UpdateHour(
ctx context.Context,
hourTime time.Time,
updateFn func(h *hour.Hour) (*hour.Hour, error),
) error {
for {
err := m.updateHour(ctx, hourTime, updateFn)
if val, ok := errors.Cause(err).(*mysql.MySQLError); ok && val.Number == mySQLDeadlockErrorCode {
continue
}
return err
}
}
func (m MySQLHourRepository) updateHour(
ctx context.Context,
hourTime time.Time,
updateFn func(h *hour.Hour) (*hour.Hour, error),
) (err error) {
tx, err := m.db.Beginx()
if err != nil {
return errors.Wrap(err, "unable to start transaction")
}
// Defer is executed on function just before exit.
// With defer, we are always sure that we will close our transaction properly.
defer func() {
// In `UpdateHour` we are using named return - `(err error)`.
// Thanks to that, that can check if function exits with error.
//
// Even if function exits without error, commit still can return error.
// In that case we can override nil to err `err = m.finish...`.
err = m.finishTransaction(err, tx)
}()
existingHour, err := m.getOrCreateHour(ctx, tx, hourTime, true)
if err != nil {
return err
}
updatedHour, err := updateFn(existingHour)
if err != nil {
return err
}
if err := m.upsertHour(tx, updatedHour); err != nil {
return err
}
return nil
}
// upsertHour updates hour if hour already exists in the database.
// If your doesn't exists, it's inserted.
func (m MySQLHourRepository) upsertHour(tx *sqlx.Tx, hourToUpdate *hour.Hour) error {
updatedDbHour := mysqlHour{
Hour: hourToUpdate.Time().UTC(),
Availability: hourToUpdate.Availability().String(),
}
_, err := tx.NamedExec(
`INSERT INTO
hours (hour, availability)
VALUES
(:hour, :availability)
ON DUPLICATE KEY UPDATE
availability = :availability`,
updatedDbHour,
)
if err != nil {
return errors.Wrap(err, "unable to upsert hour")
}
return nil
}
// finishTransaction rollbacks transaction if error is provided.
// If err is nil transaction is committed.
//
// If the rollback fails, we are using multierr library to add error about rollback failure.
// If the commit fails, commit error is returned.
func (m MySQLHourRepository) finishTransaction(err error, tx *sqlx.Tx) error {
if err != nil {
if rollbackErr := tx.Rollback(); rollbackErr != nil {
return multierr.Combine(err, rollbackErr)
}
return err
} else {
if commitErr := tx.Commit(); commitErr != nil {
return errors.Wrap(err, "failed to commit tx")
}
return nil
}
}
func NewMySQLConnection() (*sqlx.DB, error) {
config := mysql.NewConfig()
config.Net = "tcp"
config.Addr = os.Getenv("MYSQL_ADDR")
config.User = os.Getenv("MYSQL_USER")
config.Passwd = os.Getenv("MYSQL_PASSWORD")
config.DBName = os.Getenv("MYSQL_DATABASE")
config.ParseTime = true // with that parameter, we can use time.Time in mysqlHour.Hour
db, err := sqlx.Connect("mysql", config.FormatDSN())
if err != nil {
return nil, errors.Wrap(err, "cannot connect to MySQL")
}
return db, nil
}