Skip to content

Commit 554358b

Browse files
committed
fix: Migrations not being correctly ran/created
1 parent d630719 commit 554358b

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

lua/pulsar_lib/core/sql/sv_migrations.lua

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
1818
end
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

lua/pulsar_lib/core/sql/sv_migrator.lua

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,13 @@ end
5252
--- @param name string|table The name of the migrator.
5353
--- @param sort number The sort order of the migrator.
5454
--- @param up function The function to run when migrating up.
55-
--- @param down function The function to run when migrating down.
5655
--- @return MIGRATOR
57-
function MIGRATOR:New(name, sort, up, down)
56+
function MIGRATOR:New(name, sort, up)
5857
if istable(name) then
5958
if name.up then
6059
up = name.up
6160
end
6261

63-
if name.down then
64-
down = name.down
65-
end
66-
6762
if name.name then
6863
name = name.name
6964
end
@@ -79,9 +74,6 @@ function MIGRATOR:New(name, sort, up, down)
7974
if isfunction(up) then
8075
dt.Up = up
8176
end
82-
if isfunction(down) then
83-
dt.Down = down
84-
end
8577

8678
return setmetatable(dt, MIGRATOR)
8779
end

0 commit comments

Comments
 (0)