peachy/assets/.lua/migrate.lua

35 lines
882 B
Lua
Raw Normal View History

2024-06-10 23:40:10 +00:00
local re = require "re"
local path = require "path"
local filenameRegex = re.compile([=[^([0-9]+).*\.sql$]=])
local function migrate()
local migrations = {}
local seqnums = {}
local paths = GetZipPaths("/migrations/")
for _, p in ipairs(paths) do
-- basename -> sequence number
-- check that sequence number doesn't already exist
local basename = path.basename(p)
local _, seqnum = filenameRegex:search(basename)
seqnum = tonumber(seqnum)
if seqnum and not seqnums[seqnum] then
table.insert(seqnums, seqnum)
table.insert(migrations, {
filename = p,
seqnum = seqnum,
})
else
print(string.format("found weird migration name: %s", p))
end
end
table.sort(migrations, function(a, b) return a.seqnum < b.seqnum end)
for i,mig in ipairs(migrations) do
print(string.format("%d: %s", i, mig.filename))
end
end
return {
migrate = migrate,
}