From f85da858a6d0a9bcba8eb719b5fc0023a635565b Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Tue, 27 Sep 2016 13:51:23 +0530 Subject: [PATCH 1/2] fix(lucid): use primary key instead of id model instance operations should use primary key value instead of hardcoded id Closes #51 --- src/Lucid/Model/Mixins/Persistance.js | 6 +-- test/unit/fixtures/model.js | 10 ++++ test/unit/lucid.spec.js | 72 +++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/src/Lucid/Model/Mixins/Persistance.js b/src/Lucid/Model/Mixins/Persistance.js index 562d4947..7fe7a011 100644 --- a/src/Lucid/Model/Mixins/Persistance.js +++ b/src/Lucid/Model/Mixins/Persistance.js @@ -70,7 +70,7 @@ Peristance.update = function * () { if (this.transaction) { query.transacting(this.transaction) } - const affected = yield query.where('id', this.$primaryKeyValue).updateAttributes(dirtyValues) + const affected = yield query.where(this.constructor.primaryKey, this.$primaryKeyValue).updateAttributes(dirtyValues) if (affected > 0) { _.merge(this.attributes, dirtyValues) this.original = _.clone(this.attributes) @@ -92,7 +92,7 @@ Peristance.update = function * () { */ Peristance.delete = function * () { const deleteHandler = function * () { - const query = this.constructor.query().where('id', this.$primaryKeyValue) + const query = this.constructor.query().where(this.constructor.primaryKey, this.$primaryKeyValue) if (this.transaction) { query.transacting(this.transaction) } @@ -117,7 +117,7 @@ Peristance.delete = function * () { */ Peristance.restore = function * () { const restoreHandler = function * () { - const query = this.constructor.query().where('id', this.$primaryKeyValue) + const query = this.constructor.query().where(this.constructor.primaryKey, this.$primaryKeyValue) if (this.transaction) { query.transacting(this.transaction) } diff --git a/test/unit/fixtures/model.js b/test/unit/fixtures/model.js index 029c411e..f9913989 100644 --- a/test/unit/fixtures/model.js +++ b/test/unit/fixtures/model.js @@ -14,6 +14,14 @@ module.exports = { table.timestamps() table.timestamp('deleted_at').nullable() }), + knex.schema.createTable('zombies', function (table) { + table.increments('zombie_id') + table.string('username') + table.string('firstname') + table.string('lastname') + table.timestamps() + table.timestamp('deleted_at').nullable() + }), knex.schema.createTable('accounts', function (table) { table.increments() table.string('account_name') @@ -48,6 +56,7 @@ module.exports = { down: function (knex) { const dropTables = [ knex.schema.dropTable('users'), + knex.schema.dropTable('zombies'), knex.schema.dropTable('accounts'), knex.schema.dropTable('profiles'), knex.schema.dropTable('cars'), @@ -59,6 +68,7 @@ module.exports = { truncate: function (knex) { const truncateTables = [ knex.table('users').truncate(), + knex.table('zombies').truncate(), knex.table('accounts').truncate(), knex.table('profiles').truncate(), knex.table('cars').truncate(), diff --git a/test/unit/lucid.spec.js b/test/unit/lucid.spec.js index db334641..71a03951 100644 --- a/test/unit/lucid.spec.js +++ b/test/unit/lucid.spec.js @@ -44,6 +44,7 @@ describe('Lucid', function () { afterEach(function * () { yield Database.table('users').truncate() + yield Database.table('zombies').truncate() }) context('Model', function () { @@ -1795,6 +1796,77 @@ describe('Lucid', function () { const reFetchUser = yield User.findBy('username', 'foo') expect(reFetchUser.lastname).to.equal('foo') }) + + it('should perform the update query on the model primary key instead of id', function * () { + let updateQuery = null + class Zombie extends Model { + static get primaryKey () { + return 'zombie_id' + } + static boot () { + super.boot() + this.onQuery((query) => { + updateQuery = query + }) + } + } + + Zombie.bootIfNotBooted() + const zombie = yield Zombie.create({username: 'foo', lastname: 'bar'}) + expect(zombie.zombie_id).to.equal(1) + zombie.lastname = 'baz' + yield zombie.save() + expect(queryHelpers.formatQuery(updateQuery.sql)).to.equal(queryHelpers.formatQuery('update "zombies" set "lastname" = ?, "updated_at" = ? where "zombie_id" = ?')) + yield Zombie.findByOrFail('lastname', 'baz') + }) + + it('should perform the delete query on the model primary key instead of id', function * () { + let deleteQuery = null + class Zombie extends Model { + static get primaryKey () { + return 'zombie_id' + } + static boot () { + super.boot() + this.onQuery((query) => { + deleteQuery = query + }) + } + } + + Zombie.bootIfNotBooted() + const zombie = yield Zombie.create({username: 'foo', lastname: 'bar'}) + expect(zombie.zombie_id).to.equal(1) + yield zombie.delete() + expect(queryHelpers.formatQuery(deleteQuery.sql)).to.equal(queryHelpers.formatQuery('delete from "zombies" where "zombie_id" = ?')) + }) + + it('should perform the restore query on the model primary key instead of id', function * () { + let restoreQuery = null + class Zombie extends Model { + static get primaryKey () { + return 'zombie_id' + } + + static get deleteTimestamp () { + return 'deleted_at' + } + + static boot () { + super.boot() + this.onQuery((query) => { + restoreQuery = query + }) + } + } + + Zombie.bootIfNotBooted() + const zombie = yield Zombie.create({username: 'foo', lastname: 'bar'}) + expect(zombie.zombie_id).to.equal(1) + yield zombie.delete() + yield zombie.restore() + expect(queryHelpers.formatQuery(restoreQuery.sql)).to.equal(queryHelpers.formatQuery('update "zombies" set "deleted_at" = ?, "updated_at" = ? where "zombie_id" = ?')) + }) }) context('Model Transactions', function () { From 918b58470c8a936253dcd2df6c20f5f8e5dcace6 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Tue, 27 Sep 2016 14:02:38 +0530 Subject: [PATCH 2/2] chore(release): release 3.0.6 --- CHANGELOG.md | 10 ++++++++++ package.json | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 087f1ed3..33635764 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ + +## [3.0.6](https://github.com/adonisjs/adonis-lucid/compare/v3.0.5...v3.0.6) (2016-09-27) + + +### Bug Fixes + +* **lucid:** use primary key instead of id ([f85da85](https://github.com/adonisjs/adonis-lucid/commit/f85da85)), closes [#51](https://github.com/adonisjs/adonis-lucid/issues/51) + + + ## [3.0.5](https://github.com/adonisjs/adonis-lucid/compare/v3.0.4...v3.0.5) (2016-09-26) diff --git a/package.json b/package.json index 8ab0f893..cfa4951b 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "directories": { "test": "test" }, - "version": "3.0.5", + "version": "3.0.6", "scripts": { "lint": "standard src/**/*.js src/**/**/*.js src/**/**/**/*.js lib/*.js test/**/*.js providers/*.js", "test:all": "DB=sqlite3 npm run test && DB=mysql npm run test && DB=pg npm run test",