diff --git a/assets/.init.lua b/assets/.init.lua index 129dac1..1adb2db 100644 --- a/assets/.init.lua +++ b/assets/.init.lua @@ -1,5 +1,9 @@ -- LaunchBrowser() +local migrate = require "migrate" + +migrate.migrate() + fm = require "fullmoon" fm.setTemplate("hello", "Hello, {%& name %}") fm.setRoute("/hello/:name", function(r) diff --git a/assets/.lua/migrate.lua b/assets/.lua/migrate.lua new file mode 100644 index 0000000..d4985ba --- /dev/null +++ b/assets/.lua/migrate.lua @@ -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, +} \ No newline at end of file diff --git a/assets/migrations/01 create node.sql b/assets/migrations/01 create node.sql new file mode 100644 index 0000000..e69de29 diff --git a/assets/migrations/2 create node_type.sql b/assets/migrations/2 create node_type.sql new file mode 100644 index 0000000..e69de29