Skip to content

Commit

Permalink
feat(sqlite-wrapper): make has_schema_changed more sophisticated
Browse files Browse the repository at this point in the history
  • Loading branch information
jghauser committed Jul 29, 2024
1 parent 2d81e6a commit e6b1214
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions lua/papis/sqlite-wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,48 @@ end
local function has_schema_changed(new_schema, old_schema)
local old_schema_okays = sqlite_utils.okeys(old_schema)
local new_schema_okays = sqlite_utils.okeys(new_schema)
-- local len = { new = #new_schema_len, old = #old_schema_len }
if not vim.deep_equal(old_schema_okays, new_schema_okays) then
return true
else
return false
for _, key in pairs(new_schema_okays) do
local normalised_value = {}
if new_schema[key] == true then
normalised_value = { type = "INTEGER", primary = true, required = true }
elseif type(new_schema[key]) == "string" then
local new_schema_type = new_schema[key]
if new_schema_type ~= "luatable" then
new_schema_type = string.upper(new_schema_type)
end
normalised_value.type = new_schema_type
else
for k, v in pairs(new_schema[key]) do
if k == 1 then
local new_schema_type = v
if new_schema_type ~= "luatable" then
new_schema_type = string.upper(new_schema_type)
end
normalised_value.type = new_schema_type
elseif k == "primary" then
normalised_value[k] = v
elseif k == "required" then
normalised_value[k] = v
end
end
if not vim.tbl_get(new_schema[key], "primary") then
normalised_value.primary = false
end
if not vim.tbl_get(new_schema[key], "required") then
normalised_value.required = false
end
end
for k, v in pairs(normalised_value) do
if old_schema[key][k] ~= v then
return true
end
end
end
end
return false
end

-- Schemas for all tables
Expand Down

0 comments on commit e6b1214

Please sign in to comment.