-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.lua
More file actions
635 lines (533 loc) · 18.2 KB
/
runner.lua
File metadata and controls
635 lines (533 loc) · 18.2 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
local sql = require("sql")
local time = require("time")
local funcs = require("funcs")
local repository = require("repository")
local registry_finder = require("registry")
local runner = {}
local function create_error(message)
return {
status = "error",
error = tostring(message)
}
end
local function get_description(migration)
if migration.meta and migration.meta.description and migration.meta.description ~= "" then
return migration.meta.description
end
if migration.comment and migration.comment ~= "" then
return migration.comment
end
return ""
end
local Runner = {}
Runner.__index = Runner
function runner.setup(database_id)
if not database_id then
error("Database ID is required for migration runner setup")
end
local self = setmetatable({}, Runner)
self.database_id = database_id
return self
end
function Runner:find_migrations(options)
options = options or {}
local db, err = sql.get(self.database_id)
if err then
return nil, "Failed to connect to database: " .. tostring(err)
end
local db_type, type_err = db:type()
if type_err then
db:release()
return nil, "Failed to determine database type: " .. tostring(type_err)
end
local init_ok, init_err = repository.init_tracking_table(db)
if not init_ok then
db:release()
return nil, "Failed to initialize migration tracking table: " .. tostring(init_err)
end
local applied_migrations, applied_err = repository.get_migrations(db)
if applied_err then
db:release()
return nil, "Failed to get applied migrations: " .. tostring(applied_err)
end
local applied_map = {}
for _, m in ipairs(applied_migrations or {}) do
applied_map[m.id] = m
end
local find_options = {
target_db = self.database_id,
tags = options.tags
}
local migrations, find_err = registry_finder.find(find_options)
if find_err then
db:release()
return nil, "Failed to find migrations: " .. tostring(find_err)
end
db:release()
local applied = {}
local pending = {}
for _, migration in ipairs(migrations) do
local migration_id = migration.id
if applied_map[migration_id] then
migration.applied = true
migration.applied_at = applied_map[migration_id].applied_at
table.insert(applied, migration)
else
migration.applied = false
migration.applied_at = nil
table.insert(pending, migration)
end
end
table.sort(applied, function(a, b)
return (a.applied_at or "") < (b.applied_at or "")
end)
table.sort(pending, function(a, b)
local a_time = a.meta and a.meta.timestamp or ""
local b_time = b.meta and b.meta.timestamp or ""
return a_time < b_time
end)
local sorted = {}
for _, m in ipairs(applied) do
table.insert(sorted, m)
end
for _, m in ipairs(pending) do
table.insert(sorted, m)
end
return sorted
end
function Runner:get_next_migration(options)
local migrations, err = self:find_migrations(options)
if err then
return nil, err
end
if not migrations or #migrations == 0 then
return nil, "No migrations found"
end
for _, migration in ipairs(migrations) do
if not migration.applied then
return migration
end
end
return nil, "All migrations have been applied"
end
local function execute_migration(migration_id, options)
local executor = funcs.new()
local result, exec_err = executor:call(migration_id, options)
if exec_err then
return {
status = "error",
error = "Failed to execute migration: " .. tostring(exec_err)
}
end
if result.migrations and #result.migrations > 0 then
return result.migrations[1]
end
return result
end
function Runner:run(options)
options = options or {}
local migrations, find_err = self:find_migrations(options)
if find_err then
return create_error(find_err)
end
if not migrations or #migrations == 0 then
return {
status = "complete",
message = "No migrations found",
migrations_found = 0,
migrations_applied = 0,
migrations_skipped = 0,
migrations_failed = 0
}
end
local results = {
status = "running",
migrations_found = #migrations,
migrations_applied = 0,
migrations_skipped = 0,
migrations_failed = 0,
migrations = {},
skipped_details = {}
}
local start_time = time.now()
for _, migration in ipairs(migrations) do
if migration.applied then
results.migrations_skipped = results.migrations_skipped + 1
local skip_details = {
id = migration.id,
name = get_description(migration),
reason = "Already applied",
skip_type = "already_applied"
}
table.insert(results.skipped_details, skip_details)
table.insert(results.migrations, {
id = migration.id,
status = "skipped",
skip_type = "already_applied",
reason = "Already applied",
applied_at = migration.applied_at,
description = get_description(migration)
})
goto continue
end
local migration_options = {
database_id = self.database_id,
direction = "up",
id = migration.id
}
local result = execute_migration(migration.id, migration_options)
if result and result.status == "error" then
results.migrations_failed = results.migrations_failed + 1
table.insert(results.migrations, {
id = migration.id,
status = "error",
error = result.error,
description = get_description(migration)
})
results.status = "error"
results.error = result.error
break
elseif result and result.status == "applied" then
results.migrations_applied = results.migrations_applied + 1
table.insert(results.migrations, {
id = migration.id,
status = "applied",
description = get_description(migration),
duration = result.duration
})
else
results.migrations_skipped = results.migrations_skipped + 1
local reason = result and result.reason or "Unknown"
if result and result.skipped_reasons and #result.skipped_reasons > 0 then
reason = result.skipped_reasons[1].reason
end
local skip_details = {
id = migration.id,
name = get_description(migration),
reason = reason,
skip_type = "other"
}
table.insert(results.skipped_details, skip_details)
table.insert(results.migrations, {
id = migration.id,
status = "skipped",
skip_type = "other",
reason = reason,
description = get_description(migration)
})
end
::continue::
end
local end_time = time.now()
results.duration = end_time:sub(start_time):milliseconds() / 1000
if results.status ~= "error" then
results.status = "complete"
end
return results
end
function Runner:run_next(options)
options = options or {}
local migrations, err = self:find_migrations(options)
if err then
return {
status = "complete",
message = err,
migrations_found = 0,
migrations_applied = 0,
migrations_skipped = 0,
migrations_failed = 0
}
end
if not migrations or #migrations == 0 then
return {
status = "complete",
message = "No migrations found",
migrations_found = 0,
migrations_applied = 0,
migrations_skipped = 0,
migrations_failed = 0
}
end
local allowed_ids = options.allowed_ids or {}
local target_migration = nil
local skipped_migrations = {}
for _, migration in ipairs(migrations) do
if not migration.applied then
if #allowed_ids > 0 then
local is_allowed = false
for _, allowed_id in ipairs(allowed_ids) do
if migration.id == allowed_id then
is_allowed = true
break
end
end
if is_allowed then
target_migration = migration
break
else
table.insert(skipped_migrations, {
id = migration.id,
name = get_description(migration),
reason = "Not in allowed IDs list",
skip_type = "other"
})
end
else
target_migration = migration
break
end
end
end
if not target_migration then
local message = #skipped_migrations > 0
and "No migrations in allowed list found"
or "All migrations have been applied"
return {
status = "complete",
message = message,
migrations_found = #skipped_migrations,
migrations_applied = 0,
migrations_skipped = #skipped_migrations,
migrations_failed = 0,
migrations = {},
skipped_details = skipped_migrations
}
end
local results = {
status = "running",
migrations_found = 1 + #skipped_migrations,
migrations_applied = 0,
migrations_skipped = #skipped_migrations,
migrations_failed = 0,
migrations = {},
skipped_details = skipped_migrations
}
local start_time = time.now()
local migration_options = {
database_id = self.database_id,
direction = "up",
id = target_migration.id
}
local result = execute_migration(target_migration.id, migration_options)
if result and result.status == "error" then
results.migrations_failed = 1
table.insert(results.migrations, {
id = target_migration.id,
status = "error",
error = result.error,
description = get_description(target_migration)
})
results.status = "error"
results.error = result.error
elseif result and result.status == "applied" then
results.migrations_applied = 1
table.insert(results.migrations, {
id = target_migration.id,
status = "applied",
description = get_description(target_migration),
duration = result.duration
})
else
results.migrations_skipped = results.migrations_skipped + 1
local reason = result and result.reason or "Unknown"
if result and result.skipped_reasons and #result.skipped_reasons > 0 then
reason = result.skipped_reasons[1].reason
end
local skip_details = {
id = target_migration.id,
name = get_description(target_migration),
reason = reason,
skip_type = "other"
}
table.insert(results.skipped_details, skip_details)
table.insert(results.migrations, {
id = target_migration.id,
status = "skipped",
skip_type = "other",
reason = reason,
description = get_description(target_migration)
})
end
local end_time = time.now()
results.duration = end_time:sub(start_time):milliseconds() / 1000
if results.status ~= "error" then
results.status = "complete"
end
return results
end
function Runner:rollback(options)
options = options or {}
local db, err = sql.get(self.database_id)
if err then
return create_error("Failed to connect to database: " .. tostring(err))
end
local init_ok, init_err = repository.init_tracking_table(db)
if not init_ok then
db:release()
return create_error("Failed to initialize migration tracking table: " .. tostring(init_err))
end
local applied_migrations, query_err = repository.get_migrations(db)
if query_err then
db:release()
return create_error("Failed to get applied migrations: " .. tostring(query_err))
end
db:release()
if not applied_migrations or #applied_migrations == 0 then
return {
status = "complete",
message = "No migrations to roll back",
migrations_found = 0,
migrations_reverted = 0,
migrations_skipped = 0,
migrations_failed = 0
}
end
for i, migration in ipairs(applied_migrations) do
local registry_entry = registry_finder.get(migration.id)
if registry_entry then
applied_migrations[i].registry_entry = registry_entry
end
end
table.sort(applied_migrations, function(a, b)
return (a.applied_at or "") > (b.applied_at or "")
end)
local allowed_ids = options.allowed_ids or {}
if #allowed_ids > 0 then
local filtered = {}
for _, migration in ipairs(applied_migrations) do
for _, allowed_id in ipairs(allowed_ids) do
if migration.id == allowed_id then
table.insert(filtered, migration)
break
end
end
end
if #filtered == 0 then
return {
status = "complete",
message = "No migrations in allowed list found in applied migrations",
migrations_found = 0,
migrations_reverted = 0,
migrations_skipped = 0,
migrations_failed = 0
}
end
applied_migrations = filtered
end
local count = options.count or 1
if count > #applied_migrations then
count = #applied_migrations
end
local to_rollback = {}
for i = 1, count do
table.insert(to_rollback, applied_migrations[i])
end
local results = {
status = "running",
migrations_found = #to_rollback,
migrations_reverted = 0,
migrations_skipped = 0,
migrations_failed = 0,
migrations = {},
skipped_details = {}
}
local start_time = time.now()
for _, migration in ipairs(to_rollback) do
local migration_options = {
database_id = self.database_id,
direction = "down",
id = migration.id
}
local result = execute_migration(migration.id, migration_options)
if result and result.status == "error" then
results.migrations_failed = results.migrations_failed + 1
table.insert(results.migrations, {
id = migration.id,
status = "error",
error = result.error,
description = migration.description or ""
})
results.status = "error"
results.error = result.error
break
elseif result and result.status == "reverted" then
results.migrations_reverted = results.migrations_reverted + 1
table.insert(results.migrations, {
id = migration.id,
status = "reverted",
description = migration.description or "",
duration = result.duration
})
else
results.migrations_skipped = results.migrations_skipped + 1
local reason = result and result.reason or "Unknown"
if result and result.skipped_reasons and #result.skipped_reasons > 0 then
reason = result.skipped_reasons[1].reason
end
local skip_details = {
id = migration.id,
name = migration.description or "",
reason = reason,
skip_type = "other"
}
table.insert(results.skipped_details, skip_details)
table.insert(results.migrations, {
id = migration.id,
status = "skipped",
skip_type = "other",
reason = reason,
description = migration.description or ""
})
end
end
local end_time = time.now()
results.duration = end_time:sub(start_time):milliseconds() / 1000
if results.status ~= "error" then
results.status = "complete"
end
return results
end
function Runner:status(options)
options = options or {}
local migrations, find_err = self:find_migrations(options)
if find_err then
return create_error(find_err)
end
local status_report = {
database_id = self.database_id,
db_type = nil,
total_migrations = #migrations,
applied_migrations = 0,
pending_migrations = 0,
migrations = {}
}
local db, err = sql.get(self.database_id)
if err then
return create_error("Failed to connect to database: " .. tostring(err))
end
local db_type, type_err = db:type()
if type_err then
db:release()
return create_error("Failed to determine database type: " .. tostring(type_err))
end
status_report.db_type = db_type
db:release()
for _, migration in ipairs(migrations) do
local migration_status = {
id = migration.id,
description = get_description(migration),
timestamp = migration.meta and migration.meta.timestamp or "",
tags = migration.meta and migration.meta.tags or {},
status = migration.applied and "applied" or "pending",
applied_at = migration.applied_at
}
if migration.applied then
status_report.applied_migrations = status_report.applied_migrations + 1
else
status_report.pending_migrations = status_report.pending_migrations + 1
end
table.insert(status_report.migrations, migration_status)
end
return status_report
end
return runner