From 6eb7d78c5979864939f3fdf8ac2e7588f446925e Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Mon, 10 Jun 2024 17:40:10 -0600 Subject: [PATCH] Find and sort migration files --- assets/.init.lua | 4 +++ assets/.lua/migrate.lua | 35 ++++++++++++++++++++++++ assets/migrations/01 create node.sql | 0 assets/migrations/2 create node_type.sql | 0 4 files changed, 39 insertions(+) create mode 100644 assets/.lua/migrate.lua create mode 100644 assets/migrations/01 create node.sql create mode 100644 assets/migrations/2 create node_type.sql 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