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

fix increment/decrement method float bug #1069

Closed
wants to merge 2 commits into from

Conversation

olalonde
Copy link

The increment/decrement method would convert float numbers
to integers instead of leaving them as is.

Fixes #868

The increment/decrement method would convert float numbers
to integers instead of leaving them as is.
@elhigu
Copy link
Member

elhigu commented Nov 23, 2015

Implementation looks good, but it seems to be missing test case for it. test/integration/builder/updates.js could be good place to put the test.

@olalonde
Copy link
Author

Sorry can't manage to run integration tests. Maybe merge this and open another issue?

@avimar
Copy link

avimar commented Nov 26, 2015

I was pulling my hair out for a bit trying to figure out why decrement didn't work, so I dug into the source code and realized it did parseInt...
So I had to do this silly workaround:
.update('mycol', knex.raw('mycol-'+ variable))

@rhys-vdw
Copy link
Member

@olalonde Just write the test and push to the PR, Travis will run it for you. If it fails you can just fix it and push again. (Or set up the test environment - a bit more involved.)

Also I agree with @chrisbroome that parseFloat would be preferable to new Number.

@rhys-vdw
Copy link
Member

Thinking on this more - I'm actually not in favor of this change. These methods are meant to be for use with 'counter' style columns. The default of 1 for the increment/decrement size makes sense only in the context of integers.

When dealing with floating point numbers it would make more sense to have methods add and subtract that will be no-ops if passed null. But then if you're going to add add and subtract why not add multiply and divide? And what if you want to use a different column (eg. colA = colB + 5)? The permutations grow near endlessly. Currently this is exactly what knex.raw is for:

knex('table').update({ column: knex.raw('?? + ?', column, 1.5) }).then(result =>

If you want to support writing arbitrary statements like this without using raw I think we'd need something like this:

const { column, value } = knex.statement;
knex('table').update({
  thing: column('thing').plus(1.5), // "thing" = "thing" + 1.5
  other: column('other').times(column('thing')), // "other" = "other" * "thing"
  thingy: value(3).plus(1.5) // "thingy" = 3 + 1.5
).then(result =>

@olalonde
Copy link
Author

olalonde commented Dec 3, 2015

That's a lot of bike-shedding over a removed parseInt call. People who want to use increment/decrement with integers still can. People who want to increment/decrement floats will now be able to. Simple as that. Can we just merge this and open another issue for the unrelated stuff?

@rhys-vdw
Copy link
Member

rhys-vdw commented Dec 4, 2015

People who want to increment/decrement floats will now be able to.

Fair enough @olalonde.

I was waiting for the change to parseFloat, which you've just added. There are some conflicts so it'll have to rebased and merge manually. I've gotta run now but I'll set a reminder to do it (if nobody beats me to it).

@olalonde
Copy link
Author

olalonde commented Dec 4, 2015

Thanks.

@rhys-vdw
Copy link
Member

Hey @olalonde, did you end up writing a test for this?

@elhigu
Copy link
Member

elhigu commented Mar 15, 2016

Closing abandoned pull request, I'll update the issue mentioning that here is almost ready pull request if someone feels like finishing this.

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

Successfully merging this pull request may close these issues.

5 participants