Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve: add NULL handling in DB.lua #6054

Merged
merged 8 commits into from
Apr 20, 2022

Conversation

RollanzMushing
Copy link
Contributor

Brief overview of PR changes/additions

  • allows assigning NULL values through db methods
  • added checking for non-NULL values in columns that would be dropped by db:create and db:_migrate methods, in accordance with existing comments in DB.lua
  • added tests to DB_spec.lua to validate the changes

Motivation for adding to Mudlet

  • NULL is already partially supported through db:is_nil and db:is_not_nil methods. This expands on those capabilities
  • where a blank value such as "" is itself a possibility for a field, it is useful to indicate "unknown" with NULL
  • dropping columns without checking for presence of non-NULL values could cause unexpected loss of data

## Major
- introduced tables with `_isNull` key as a representation of NULL values
- `db:_migrate`:
  - simplified fetching columns where `table_info` is unavailable
  - added checks to prevent columns with non-NULL values from being dropped
  - added `force` parameter to bypass check if needed

## Minor changes
- removed some redundant conditional checks
- made `options` local instead of global
- condensed schema parsing (`pairs` is unaffected by clearing a key in middle of a loop)
- fixed error in `db:create()`
- added force parameter to `db:create()` and `db:_migrate()` to allow replicating previous behaviour concerning dropping columns
- changed `DB_spec.lua` to make use of `force` parameter
- fixed error handling NULL values in `db:_sql_convert()`
- added two tests to `DB_spec.lua` for column deletion
- added more extensive tests for null handling
@RollanzMushing RollanzMushing requested a review from a team as a code owner April 9, 2022 22:30
@add-deployment-links
Copy link

add-deployment-links bot commented Apr 9, 2022

Hey there! Thanks for helping Mudlet improve. 🌟

Test versions

You can directly test the changes here:

No need to install anything - just unzip and run.
Let us know if it works well, and if it doesn't, please give details.

@mudlet-machine-account mudlet-machine-account added this to the 4.16.0 milestone Apr 9, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Apr 9, 2022

Messages
✔️

PR type: Improvement

Generated by 🚫 dangerJS against 556cd09

@vadi2 vadi2 changed the title NULL handling in DB.lua Improve: add NULL handling in DB.lua Apr 10, 2022
src/mudlet-lua/lua/DB.lua Outdated Show resolved Hide resolved
src/mudlet-lua/tests/DB_spec.lua Show resolved Hide resolved
Copy link
Member

@demonnic demonnic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

honestly apart from a few linting issues I pointed out this is good. But we should take care of those. I'm one foot out the door or I would make the suggestions so you could just accept them in github

else
s = "datetime('" .. v._timestamp .. "', 'unixepoch')"
end
elseif t == "table" and v._isNull then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like the indentation got out of whack here somehow

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was fiddling with that part on Notepad++, where I didn't have tabs to spaces on. Converted to spaces now.

src/mudlet-lua/tests/DB_spec.lua Show resolved Hide resolved
@@ -105,7 +105,7 @@ describe("Tests DB.lua functions", function()
local results = db:fetch(mydb.enemies)
assert.is_true(#results == 1)
end)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty spaces

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing spaces in DB_spec.lua trimmed now.

@@ -453,7 +457,7 @@ function db:create(db_name, sheets)
-- field. The db package reserves any key that begins with an underscore to be special and syntax
-- for its own use.
for s_name, sht in pairs(sheets) do
options = {}
local options = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

local res = db:fetch(mydb.sheet)
test.blubb = nil -- we expect the blubb gets deleted
assert.are.equal(1, #res)
res[1]._row_id = nil --we get the row id back, which we don't need
assert.are.same(test, res[1])
end)


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the indents are also hit or miss in this section, some 2 space some 4. Typically we use a 2 space indent for the Lua stuff.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tabs now converted to two-spaces.

- converted tabs to two spaces
- Converted tabs to two spaces
- Trimmed trailing spaces
Co-authored-by: Vadim Peretokin <vperetokin@hey.com>
Copy link
Member

@vadi2 vadi2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this @RollanzMushing, and congrats on your first big Mudlet contribution!

@vadi2 vadi2 merged commit 092d751 into Mudlet:development Apr 20, 2022
@vadi2 vadi2 added the needs documentation This pull request changes things the players would use/see and thus needs an update in the manual label Apr 22, 2022
@vadi2
Copy link
Member

vadi2 commented Apr 22, 2022

@RollanzMushing could you update the documentation for your changes? Take the functions affected from https://wiki.mudlet.org/w/Manual:Database_Functions, copy them to https://wiki.mudlet.org/w/Area_51 and apply the necessary changes.

At release time, we will move them back out from area51.

@vadi2 vadi2 removed the needs documentation This pull request changes things the players would use/see and thus needs an update in the manual label Apr 27, 2022
@vadi2
Copy link
Member

vadi2 commented Apr 27, 2022

@RollanzMushing thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants