Skip to content

Commit ff8ae82

Browse files
author
jghauser
committed
feat(sqlite-wrapper): make has_schema_changed more sophisticated
1 parent d41100a commit ff8ae82

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

lua/papis/sqlite-wrapper.lua

+38-2
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,48 @@ end
159159
local function has_schema_changed(new_schema, old_schema)
160160
local old_schema_okays = sqlite_utils.okeys(old_schema)
161161
local new_schema_okays = sqlite_utils.okeys(new_schema)
162-
-- local len = { new = #new_schema_len, old = #old_schema_len }
163162
if not vim.deep_equal(old_schema_okays, new_schema_okays) then
164163
return true
165164
else
166-
return false
165+
for _, key in pairs(new_schema_okays) do
166+
local normalised_value = {}
167+
if new_schema[key] == true then
168+
normalised_value = { type = "INTEGER", primary = true, required = true }
169+
elseif type(new_schema[key]) == "string" then
170+
local new_schema_type = new_schema[key]
171+
if new_schema_type ~= "luatable" then
172+
new_schema_type = string.upper(new_schema_type)
173+
end
174+
normalised_value.type = new_schema_type
175+
else
176+
for k, v in pairs(new_schema[key]) do
177+
if k == 1 then
178+
local new_schema_type = v
179+
if new_schema_type ~= "luatable" then
180+
new_schema_type = string.upper(new_schema_type)
181+
end
182+
normalised_value.type = new_schema_type
183+
elseif k == "primary" then
184+
normalised_value[k] = v
185+
elseif k == "required" then
186+
normalised_value[k] = v
187+
end
188+
end
189+
if not vim.tbl_get(new_schema[key], "primary") then
190+
normalised_value.primary = false
191+
end
192+
if not vim.tbl_get(new_schema[key], "required") then
193+
normalised_value.required = false
194+
end
195+
end
196+
for k, v in pairs(normalised_value) do
197+
if old_schema[key][k] ~= v then
198+
return true
199+
end
200+
end
201+
end
167202
end
203+
return false
168204
end
169205

170206
-- Schemas for all tables

0 commit comments

Comments
 (0)