database-cleaner
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,7 @@ | ||
1.2.0 / 2016-11-15 | ||
================== | ||
* Support pooled MySQL connections (Rob Wells) | ||
* Fix: Do not drop VIEWS (Matteo Ferrando) | ||
1.1.0 / 2016-08-16 | ||
@@ -2,0 +8,0 @@ ================== |
@@ -10,2 +10,6 @@ module.exports = { | ||
sqlite: { | ||
skipTables: ['schema_migrations'] | ||
}, | ||
mongodb: { | ||
@@ -12,0 +16,0 @@ skipCollections: ['schema_migrations'] |
@@ -53,5 +53,8 @@ var DatabaseCleaner = module.exports = function(type, config) { | ||
var database = db.config.connectionConfig ? | ||
db.config.connectionConfig.database : db.config.database; | ||
var count = 0; | ||
var length = tables.length; | ||
var tableName = 'Tables_in_' + db.config.database; | ||
var tableName = 'Tables_in_' + database; | ||
var skippedTables = config.mysql.skipTables; | ||
@@ -95,3 +98,3 @@ var strategy = config.mysql.strategy || 'deletion'; | ||
db.query("SELECT table_name FROM information_schema.tables WHERE table_schema = '" + schema + "';", function(err, tables) { | ||
db.query("SELECT table_name FROM information_schema.tables WHERE table_schema = '" + schema + "' AND table_type = 'BASE TABLE';", function(err, tables) { | ||
if (err) return callback(err); | ||
@@ -159,2 +162,40 @@ | ||
cleaner['sqlite'] = function(db, callback) { | ||
var skippedTables = config.sqlite.skipTables; | ||
return db.all("SELECT name FROM sqlite_master WHERE type='table'", function(err, rows) { | ||
if (err) return callback(err); | ||
var tables = rows.map(function(row) { | ||
return row.name; | ||
}); | ||
var num_expected = 0; | ||
var num_returned = 0; | ||
var responded = false; | ||
function maybeRespond(err) { | ||
if (responded) { | ||
return; | ||
} | ||
num_returned++; | ||
if (err || num_returned === num_expected) { | ||
responded = true; | ||
callback(err); | ||
} | ||
} | ||
tables.forEach(function(table) { | ||
if (table === 'sqlite_sequence') return; | ||
if (skippedTables.indexOf(table) !== -1) return; | ||
num_expected++; | ||
db.run("DELETE FROM " + table, maybeRespond); | ||
}); | ||
if (!num_expected) return callback(); // when there is nothing to clear | ||
}); | ||
}; | ||
this.clean = function (db, callback) { | ||
@@ -161,0 +202,0 @@ cleaner[type](db, callback); |
@@ -5,3 +5,3 @@ { | ||
"keywords" : [ "database", "cleaner", "mongodb", "redis", "couchdb", "tests", "package.json", "elasticsearch" ], | ||
"version" : "1.1.0", | ||
"version" : "1.2.0", | ||
"author" : "Emerson Macedo <emerleite@gmail.com>", | ||
@@ -23,3 +23,4 @@ "repository" : { | ||
"should": "4.0.4", | ||
"elasticsearch": "8.2.0" | ||
"elasticsearch": "8.2.0", | ||
"sqlite3": "3.1.6" | ||
}, | ||
@@ -26,0 +27,0 @@ "main": "index", |
@@ -12,2 +12,7 @@ var should = require('should'), | ||
database: 'database_cleaner' | ||
}), | ||
pool = new mysql.createPool({ | ||
host: process.env.MYSQL_HOST || 'localhost', | ||
user: 'root', | ||
database: 'database_cleaner' | ||
}); | ||
@@ -129,2 +134,13 @@ | ||
}); | ||
describe('with pooled connection', function() { | ||
before(function(done) { | ||
databaseCleaner = new DatabaseCleaner('mysql'); | ||
done(); | ||
}); | ||
it('should not error with a pooled connection', function(done) { | ||
databaseCleaner.clean(pool, done); | ||
}); | ||
}); | ||
}); | ||
@@ -131,0 +147,0 @@ |
@@ -32,3 +32,3 @@ var should = require('should'), | ||
queryClient(client, 'CREATE TABLE test2 (id SERIAL, title VARCHAR(255) NOT NULL, PRIMARY KEY(id));', []), | ||
queryClient(client, 'CREATE TABLE \"Test3\" (id SERIAL, title VARCHAR(255) NOT NULL, PRIMARY KEY(id));', []), | ||
queryClient(client, 'CREATE TABLE "Test3" (id SERIAL, title VARCHAR(255) NOT NULL, PRIMARY KEY(id));', []), | ||
queryClient(client, 'CREATE TABLE schema_migrations (id SERIAL, version VARCHAR(255) NOT NULL, PRIMARY KEY(id));', []), | ||
@@ -43,3 +43,3 @@ queryClient(client, 'INSERT INTO other_schema.test1 (title) VALUES ($1);', ["foo"]), | ||
queryClient(client, 'INSERT INTO test2 (title) VALUES ($1);', ["bar"]), | ||
queryClient(client, 'INSERT INTO \"Test3\" (title) VALUES ($1);', ["foobar"]), | ||
queryClient(client, 'INSERT INTO "Test3" (title) VALUES ($1);', ["foobar"]), | ||
queryClient(client, 'INSERT INTO schema_migrations (version) VALUES ($1);', ["20150716190240"]), | ||
@@ -56,7 +56,7 @@ function(next) { release(); next(); }, | ||
async.parallel([ | ||
queryClient(client, "DROP SCHEMA other_schema CASCADE", []), | ||
queryClient(client, "DROP TABLE test1", []), | ||
queryClient(client, "DROP TABLE test2", []), | ||
queryClient(client, "DROP TABLE \"Test3\"", []), | ||
queryClient(client, "DROP TABLE schema_migrations", []), | ||
queryClient(client, 'DROP SCHEMA other_schema CASCADE', []), | ||
queryClient(client, 'DROP TABLE test1 CASCADE', []), | ||
queryClient(client, 'DROP TABLE test2 CASCADE', []), | ||
queryClient(client, 'DROP TABLE "Test3" CASCADE', []), | ||
queryClient(client, 'DROP TABLE schema_migrations CASCADE', []), | ||
function(next) { release(); next(); }, | ||
@@ -157,3 +157,2 @@ ], done); | ||
describe('truncation strategy', function() { | ||
@@ -192,2 +191,27 @@ before(function() { | ||
}); | ||
it('should not try to truncate views', function(done) { | ||
pg.connect(connectionString, function(err, client, release) { | ||
if (err) return done(err); | ||
queryClient(client, 'CREATE VIEW public.test1_test2_view AS SELECT t1.id as t1, t2.id as t2, t1.title as t1_title, t2.title as t2_title FROM test1 t1, test2 t2;', [], function(err) { | ||
if (err) return done(err); | ||
databaseCleaner.clean(client, function(err) { | ||
if (err) return done(err); | ||
client.query("SELECT * FROM test1_test2_view", function(err, result) { | ||
if (err) return done(err); | ||
queryClient(client, 'DROP VIEW test1_test2_view;', [], function(err) { | ||
if (err) return done(err); | ||
release(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -194,0 +218,0 @@ }); |
35982
16
754
11
9