Skip to content

Commit

Permalink
Fix truncate() on sqlite3 dialect
Browse files Browse the repository at this point in the history
Relates to knex#2312: ids were not correctly reset anymore
  • Loading branch information
lehni committed Nov 23, 2017
1 parent aeec0a2 commit 23c6cee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/dialects/sqlite3/query/compiler.js
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
23 changes: 18 additions & 5 deletions test/integration/builder/additional.js
Expand Up @@ -97,16 +97,29 @@ 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(({ id }) => {
expect(id).to.equal(1);
});
});
}
});

});

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

0 comments on commit 23c6cee

Please sign in to comment.