addining gzip support to copyfrom
This commit is contained in:
parent
f749798ca5
commit
791967877c
@ -9,6 +9,7 @@ const { initializeProfilerMiddleware } = require('../middlewares/profiler');
|
||||
const rateLimitsMiddleware = require('../middlewares/rate-limit');
|
||||
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimitsMiddleware;
|
||||
|
||||
const zlib = require('zlib');
|
||||
const PSQL = require('cartodb-psql');
|
||||
const copyTo = require('pg-copy-streams').to;
|
||||
const copyFrom = require('pg-copy-streams').from;
|
||||
@ -81,7 +82,7 @@ CopyController.prototype.handleCopyTo = function (req, res, next) {
|
||||
res.on('error', next);
|
||||
pgstream.on('error', next);
|
||||
pgstream.on('end', next);
|
||||
|
||||
|
||||
pgstream.pipe(res);
|
||||
});
|
||||
} catch (err) {
|
||||
@ -92,7 +93,7 @@ CopyController.prototype.handleCopyTo = function (req, res, next) {
|
||||
|
||||
CopyController.prototype.responseCopyTo = function (req, res) {
|
||||
let { filename } = req.query;
|
||||
|
||||
|
||||
if (!filename) {
|
||||
filename = 'carto-sql-copyto.dmp';
|
||||
}
|
||||
@ -105,7 +106,7 @@ CopyController.prototype.responseCopyTo = function (req, res) {
|
||||
CopyController.prototype.handleCopyFrom = function (req, res, next) {
|
||||
const { sql } = req.query;
|
||||
|
||||
if (!sql) {
|
||||
if (!sql) {
|
||||
return next(new Error("Parameter 'sql' is missing, must be in URL or first field in POST"));
|
||||
}
|
||||
|
||||
@ -113,8 +114,6 @@ CopyController.prototype.handleCopyFrom = function (req, res, next) {
|
||||
if (!sql.toUpperCase().startsWith("COPY ")) {
|
||||
return next(new Error("SQL must start with COPY"));
|
||||
}
|
||||
|
||||
req.setEncoding('utf8');
|
||||
|
||||
try {
|
||||
const start_time = Date.now();
|
||||
@ -139,7 +138,11 @@ CopyController.prototype.handleCopyFrom = function (req, res, next) {
|
||||
return next();
|
||||
});
|
||||
|
||||
req.pipe(pgstream);
|
||||
if (req.get('content-encoding') === 'gzip') {
|
||||
req.pipe(zlib.createGunzip()).pipe(pgstream);
|
||||
} else {
|
||||
req.pipe(pgstream);
|
||||
}
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
|
@ -98,4 +98,25 @@ describe('copy-endpoints', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('should work with copyfrom and gzip', function(done){
|
||||
assert.response(server, {
|
||||
url: "/api/v1/sql/copyfrom?" + querystring.stringify({
|
||||
sql: "COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)",
|
||||
gzip: true
|
||||
}),
|
||||
data: fs.createReadStream(__dirname + '/../support/csv/copy_test_table.csv.gz'),
|
||||
headers: {
|
||||
host: 'vizzuality.cartodb.com',
|
||||
'content-encoding': 'gzip'
|
||||
},
|
||||
method: 'POST'
|
||||
},{}, function(err, res) {
|
||||
assert.ifError(err);
|
||||
const response = JSON.parse(res.body);
|
||||
assert.equal(!!response.time, true);
|
||||
assert.strictEqual(response.total_rows, 6);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
BIN
test/support/csv/copy_test_table.csv.gz
Normal file
BIN
test/support/csv/copy_test_table.csv.gz
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user