@@ -10,11 +10,11 @@ PulsarLib.SQL.Migrations._migrator = include("sv_migrator.lua")
1010
1111--- Creates a new Migrator
1212--- @param name string | table The name of the migrator.
13- --- @param up function | number ? The function to run when migrating up .
14- --- @param down function The function to run when migrating down .
13+ --- @param sort number The sort order of the migrator .
14+ --- @param up function The function to run when migrating up .
1515--- @return MIGRATOR
16- function PulsarLib .SQL .Migrations :Migrator (name , up , down )
17- return self ._migrator :New (name , up , down )
16+ function PulsarLib .SQL .Migrations :Migrator (name , sort , up )
17+ return self ._migrator :New (name , sort , up )
1818end
1919
2020--- Loads the ran migrations from the database.
@@ -43,17 +43,35 @@ function PulsarLib.SQL.Migrations:LoadStored(callback)
4343 return
4444 end
4545
46- local dt = include (self .filePath .. " /sv_" .. id .. " .lua" )
47- if isstring (dt ) then
48- self .Stored [id ] = self :Migrator (id , tonumber (sort ), function (slf , done )
49- return PulsarLib .SQL :RawQuery (dt , done , function (err )
46+ local migrationData = include (self .filePath .. " /sv_" .. id .. " .lua" )
47+ if isstring (migrationData ) then
48+ self .Stored [id ] = self :Migrator (id , tonumber (sort ) or 0 , function (slf , done )
49+ return PulsarLib .SQL :RawQuery (migrationData , function ()
50+ done ()
51+ end , function (err )
5052 PulsarLib .Logging :Fatal (" Failed to run migration " .. id .. " : " .. err )
5153 end )
5254 end )
53- elseif isfunction (dt ) then
54- self .Stored [id ] = self :Migrator (id , tonumber (sort ), dt )
55- elseif istable (dt ) then
56- self .Stored [id ] = self :Migrator (id , sort , dt .up )
55+ elseif isfunction (migrationData ) then
56+ self .Stored [id ] = self :Migrator (id , tonumber (sort ) or 0 , function (slf , done )
57+ local success = migrationData ()
58+ if success then
59+ done ()
60+ return
61+ end
62+
63+ PulsarLib .Logging :Fatal (" Failed to run migration " .. id .. " . Unable to fetch error message." )
64+ end )
65+ elseif istable (migrationData ) and migrationData .up then
66+ self .Stored [id ] = self :Migrator (id , sort , function (slf , done )
67+ local success = migrationData .up ()
68+ if success then
69+ done ()
70+ return
71+ end
72+
73+ PulsarLib .Logging :Fatal (" Failed to run migration " .. id .. " . Unable to fetch error message." )
74+ end )
5775 else
5876 self .Logging :Warning (" Invalid Migration Format: " .. migration )
5977 end
0 commit comments