-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigration.lua
More file actions
323 lines (275 loc) · 9.07 KB
/
migration.lua
File metadata and controls
323 lines (275 loc) · 9.07 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
local migration = {}
local sql = require("sql")
local time = require("time")
local migration_core = require("core")
local repository = require("repository")
local function execute_migration(migration_item, options)
if not migration_item or not options or not options.db or not options.db_type then
return {
status = "error",
error = "Invalid migration or options"
}
end
local db = options.db
local db_type = options.db_type
local direction = options.direction or "up"
local migration_id
if options.id then
migration_id = options.id
else
return {
status = "error",
error = "Migration ID is required",
}
end
local impl = migration_item.database_implementations[db_type]
if not impl then
return {
status = "skipped",
description = migration_item.description,
reason = "No implementation for database type: " .. tostring(db_type),
name = migration_item.description
}
end
if direction == "up" and not impl.up then
return {
status = "error",
description = migration_item.description,
error = "Missing 'up' implementation for " .. tostring(db_type),
name = migration_item.description
}
elseif direction == "down" and not impl.down then
return {
status = "error",
description = migration_item.description,
error = "Missing 'down' implementation for " .. tostring(db_type),
name = migration_item.description
}
end
if direction == "up" then
local is_applied, check_err = repository.is_applied(db, migration_id)
if check_err then
return {
status = "error",
description = migration_item.description,
error = "Failed to check migration status: " .. tostring(check_err),
name = migration_item.description
}
end
if is_applied and not options.force then
return {
status = "skipped",
description = migration_item.description,
reason = "Migration already applied",
name = migration_item.description
}
end
end
local tx, tx_err = db:begin()
if tx_err then
return {
status = "error",
description = migration_item.description,
error = "Failed to start transaction: " .. tostring(tx_err),
name = migration_item.description
}
end
local start_time = time.now()
local success, err
if direction == "up" then
success, err = cpcall(impl.up, tx)
else
success, err = cpcall(impl.down, tx)
if success then
local remove_ok, remove_err = repository.remove_migration(tx, migration_id)
if not remove_ok then
tx:rollback()
return {
status = "error",
description = migration_item.description,
error = "Failed to remove migration record: " .. tostring(remove_err),
name = migration_item.description
}
end
end
end
if not success then
tx:rollback()
return {
status = "error",
description = migration_item.description,
error = tostring(err),
name = migration_item.description
}
end
if direction == "up" then
local record_ok, record_err = repository.record_migration(
tx,
migration_id,
migration_item.description
)
if not record_ok then
tx:rollback()
return {
status = "error",
description = migration_item.description,
error = "Failed to record migration: " .. tostring(record_err),
name = migration_item.description
}
end
end
if direction == "up" and impl.after then
local after_success, after_err = cpcall(impl.after, tx)
if not after_success then
tx:rollback()
return {
status = "error",
description = migration_item.description,
error = "After hook failed: " .. tostring(after_err),
name = migration_item.description
}
end
end
local commit_success, commit_err = tx:commit()
if not commit_success then
return {
status = "error",
description = migration_item.description,
error = "Failed to commit transaction: " .. tostring(commit_err),
name = migration_item.description
}
end
local end_time = time.now()
local duration = end_time:sub(start_time)
local status
if direction == "up" then
status = "applied"
else
status = "reverted"
end
return {
status = status,
description = migration_item.description,
duration = duration:milliseconds() / 1000,
name = migration_item.description
}
end
function migration.run(fn, options)
options = options or {}
if not options.database_id and not options.db then
return {
status = "error",
error = "Database ID or connection is required"
}
end
options.direction = options.direction or "up"
if options.direction ~= "up" and options.direction ~= "down" then
return {
status = "error",
error = "Invalid direction: must be 'up' or 'down'"
}
end
local db, db_err
local need_release = false
if options.db then
db = options.db
else
db, db_err = sql.get(options.database_id)
if db_err then
return {
status = "error",
error = "Failed to connect to database: " .. tostring(db_err)
}
end
need_release = true
end
local init_ok, init_err = repository.init_tracking_table(db)
if not init_ok then
if need_release then db:release() end
return {
status = "error",
error = "Failed to initialize migration tracking table: " .. tostring(init_err)
}
end
local db_type, type_err = db:type()
if type_err then
if need_release then db:release() end
return {
status = "error",
error = "Failed to determine database type: " .. tostring(type_err)
}
end
local success, implementations_or_err = cpcall(migration_core.define, fn)
if not success then
if need_release then db:release() end
return {
status = "error",
error = "Failed to define migration: " .. tostring(implementations_or_err)
}
end
local implementations = implementations_or_err
local results = {
migrations = {},
total = #implementations,
applied = 0,
skipped = 0,
skipped_reasons = {},
failed = 0,
db_type = db_type
}
local start_time = time.now()
for _, m in ipairs(implementations) do
if m.database_implementations[db_type] then
local result = execute_migration(m, {
db = db,
db_type = db_type,
direction = options.direction,
force = options.force,
id = options.id,
})
table.insert(results.migrations, result)
if result.status == "applied" or result.status == "reverted" then
results.applied = results.applied + 1
elseif result.status == "skipped" then
results.skipped = results.skipped + 1
local skipped_info = {
name = result.name,
reason = result.reason
}
table.insert(results.skipped_reasons, skipped_info)
elseif result.status == "error" then
results.failed = results.failed + 1
if not options.force then
results.status = "error"
results.error = tostring(result.error)
break
end
end
else
results.skipped = results.skipped + 1
local skipped_info = {
name = m.description,
reason = "No implementation for database type: " .. tostring(db_type)
}
table.insert(results.skipped_reasons, skipped_info)
end
end
local end_time = time.now()
results.duration = end_time:sub(start_time):milliseconds() / 1000
if not results.status then
results.status = results.failed > 0 and "failed" or "complete"
end
if need_release then
db:release()
end
return results
end
function migration.define(fn)
if not fn or type(fn) ~= "function" then
error("Migration definition must be a function")
end
return function(options)
return migration.run(fn, options)
end
end
return migration