Find and sort migration files

This commit is contained in:
Brandon Dyck 2024-06-10 17:40:10 -06:00
parent 3e15c4b047
commit 6eb7d78c59
4 changed files with 39 additions and 0 deletions

View File

@ -1,5 +1,9 @@
-- LaunchBrowser() -- LaunchBrowser()
local migrate = require "migrate"
migrate.migrate()
fm = require "fullmoon" fm = require "fullmoon"
fm.setTemplate("hello", "Hello, {%& name %}") fm.setTemplate("hello", "Hello, {%& name %}")
fm.setRoute("/hello/:name", function(r) fm.setRoute("/hello/:name", function(r)

35
assets/.lua/migrate.lua Normal file
View File

@ -0,0 +1,35 @@
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,
}

View File

View File