Skip to content

Commit

Permalink
Fix truncate() on sqlite3 dialect (#2348)
Browse files Browse the repository at this point in the history
Relates to #2312: ids were not correctly reset anymore
  • Loading branch information
lehni authored and elhigu committed Nov 24, 2017
1 parent aeec0a2 commit b5ba51a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/dialects/sqlite3/query/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ assign(QueryCompiler_SQLite3.prototype, {

// Compile a truncate table statement into SQL.
truncate() {
const table = this.tableName
const { table } = this.single
return {
sql: `delete from ${table}`,
sql: `delete from ${this.tableName}`,
output() {
return this.query({
sql: `delete from sqlite_sequence where name = ${table}`
sql: `delete from sqlite_sequence where name = '${table}'`
}).catch(noop)
}
}
Expand Down
24 changes: 19 additions & 5 deletions test/integration/builder/additional.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,30 @@ module.exports = function(knex) {
tester('oracle', "truncate table \"test_table_two\"");
tester('mssql', 'truncate table [test_table_two]');
})
.then(function() {

.then(() => {
return knex('test_table_two')
.select('*')
.then(function(resp) {
.then(resp => {
expect(resp).to.have.length(0);
});

})
.then(() => {
// Insert new data after truncate and make sure ids restart at 1.
// This doesn't currently work on oracle, where the created sequence
// needs to be manually reset.
if (knex.client.dialect !== 'oracle') {
return knex('test_table_two').insert({ status: 1 })
.then(res => {
return knex('test_table_two')
.select('id')
.first()
.then(res => {
expect(res).to.be.an('object')
expect(res.id).to.equal(1);
});
});
}
});

});

it('should allow raw queries directly with `knex.raw`', function() {
Expand Down

0 comments on commit b5ba51a

Please sign in to comment.