Socket
Socket
Sign inDemoInstall

database-cleaner

Package Overview
Dependencies
0
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.1.0

5

Changelog.md

@@ -0,1 +1,6 @@

1.1.0 / 2016-08-16
==================
* Support for specifying postgres schema (Ian Zabel)
1.0.0 / 2016-06-25

@@ -2,0 +7,0 @@ ==================

9

lib/database-cleaner.js

@@ -91,3 +91,6 @@ var DatabaseCleaner = module.exports = function(type, config) {

cleaner['postgresql'] = function(db, callback) {
db.query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';", function(err, tables) {
var schema = config.postgresql.schema || 'public';
var schemaPrefix = '"' + schema + '".';
db.query("SELECT table_name FROM information_schema.tables WHERE table_schema = '" + schema + "';", function(err, tables) {
if (err) return callback(err);

@@ -111,3 +114,3 @@

if (skippedTables.indexOf(table['table_name']) === -1) {
db.query("DELETE FROM " + "\"" + table['table_name'] + "\"", function() {
db.query("DELETE FROM " + schemaPrefix + "\"" + table['table_name'] + "\"", function() {
count++;

@@ -131,3 +134,3 @@

}).map(function(table) {
return '"' + table['table_name'] + '"';
return schemaPrefix + '"' + table['table_name'] + '"';
}).join(', ');

@@ -134,0 +137,0 @@

@@ -5,3 +5,3 @@ {

"keywords" : [ "database", "cleaner", "mongodb", "redis", "couchdb", "tests", "package.json", "elasticsearch" ],
"version" : "1.0.0",
"version" : "1.1.0",
"author" : "Emerson Macedo <emerleite@gmail.com>",

@@ -8,0 +8,0 @@ "repository" : {

@@ -27,2 +27,5 @@ var should = require('should'),

async.series([
queryClient(client, 'CREATE SCHEMA other_schema', []),
queryClient(client, 'CREATE TABLE other_schema.test1 (id SERIAL, title VARCHAR(255) NOT NULL, PRIMARY KEY(id));', []),
queryClient(client, 'CREATE TABLE other_schema.test2 (id SERIAL, title VARCHAR(255) NOT NULL, PRIMARY KEY(id));', []),
queryClient(client, 'CREATE TABLE test1 (id SERIAL, title VARCHAR(255) NOT NULL, PRIMARY KEY(id));', []),

@@ -32,2 +35,6 @@ queryClient(client, 'CREATE TABLE test2 (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));', []),
queryClient(client, 'INSERT INTO other_schema.test1 (title) VALUES ($1);', ["foo"]),
queryClient(client, 'INSERT INTO other_schema.test1 (title) VALUES ($1);', ["bar"]),
queryClient(client, 'INSERT INTO other_schema.test2 (title) VALUES ($1);', ["foo"]),
queryClient(client, 'INSERT INTO other_schema.test2 (title) VALUES ($1);', ["bar"]),
queryClient(client, 'INSERT INTO test1 (title) VALUES ($1);', ["foo"]),

@@ -49,2 +56,3 @@ queryClient(client, 'INSERT INTO test1 (title) VALUES ($1);', ["bar"]),

async.parallel([
queryClient(client, "DROP SCHEMA other_schema CASCADE", []),
queryClient(client, "DROP TABLE test1", []),

@@ -59,3 +67,3 @@ queryClient(client, "DROP TABLE test2", []),

it('should delete all non skippedTable records', function(done) {
it('should delete all non skippedTable records in default schema', function(done) {
pg.connect(connectionString, function(err, client, release) {

@@ -66,7 +74,11 @@ if (err) return done(err);

async.parallel([
queryClient(client, "SELECT * FROM other_schema.test1", []),
queryClient(client, "SELECT * FROM other_schema.test2", []),
queryClient(client, "SELECT * FROM test1", []),
queryClient(client, "SELECT * FROM test2", [])
], function(err, results) {
results[0].rows.length.should.equal(0);
results[1].rows.length.should.equal(0);
results[0].rows.length.should.equal(2);
results[1].rows.length.should.equal(2);
results[2].rows.length.should.equal(0);
results[3].rows.length.should.equal(0);

@@ -80,2 +92,33 @@ release();

describe('specifying a different schema', function() {
before(function() {
var config = { postgresql: { strategy: 'truncation', skipTables: [], schema: 'other_schema' } };
databaseCleaner = new DatabaseCleaner('postgresql', config);
});
it('should delete all non skippedTable records in specified schema', function(done) {
pg.connect(connectionString, function(err, client, release) {
if (err) return done(err);
databaseCleaner.clean(client, function() {
async.parallel([
queryClient(client, "SELECT * FROM other_schema.test1", []),
queryClient(client, "SELECT * FROM other_schema.test2", []),
queryClient(client, "SELECT * FROM test1", []),
queryClient(client, "SELECT * FROM test2", [])
], function(err, results) {
results[0].rows.length.should.equal(0);
results[1].rows.length.should.equal(0);
results[2].rows.length.should.equal(2);
results[3].rows.length.should.equal(2);
release();
done();
});
});
});
});
});
it('should delete all records when table name is capitalized', function(done) {

@@ -133,2 +176,4 @@ pg.connect(connectionString, function(err, client, release) {

queryClient(client, "SELECT * FROM test2", []),
queryClient(client, "SELECT * FROM other_schema.test2", []),
queryClient(client, "SELECT * FROM other_schema.test2", []),
queryClient(client, "SELECT last_value FROM test1_id_seq", []),

@@ -139,4 +184,6 @@ queryClient(client, "SELECT last_value FROM test2_id_seq", [])

results[1].rows.length.should.equal(0);
results[2].rows[0].last_value.should.equal('1');
results[3].rows[0].last_value.should.equal('1');
results[2].rows.length.should.equal(2);
results[3].rows.length.should.equal(2);
results[4].rows[0].last_value.should.equal('1');
results[5].rows[0].last_value.should.equal('1');

@@ -143,0 +190,0 @@ release();

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc