Skip to content

Commit bc49aeb

Browse files
author
Sandro Santilli
committed
Don't assume SELECT "updated_at" may write to the database.
Closes #99
1 parent bd92755 commit bc49aeb

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
1.5.1
22
-----
3+
* Improve cacheability of queries selecting "updated_at" fields (#99)
34

45
1.5.0
56
-----

app/controllers/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ app.get(global.settings.base_url+'/cachestatus', function(req, res) { handleCac
8282
//
8383
function queryMayWrite(sql) {
8484
var mayWrite = false;
85-
var pattern = RegExp("(alter|insert|update|delete|create|drop|truncate)", "i");
85+
var pattern = RegExp("\\b(alter|insert|update|delete|create|drop|truncate)\\b", "i");
8686
if ( pattern.test(sql) ) {
8787
mayWrite = true;
8888
}

test/acceptance/app.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,25 @@ test('GET /api/v1/sql with SQL parameter on DROP TABLE. should fail', function(d
391391
});
392392
});
393393

394+
// Check X-Cache-Channel when querying "updated_at" fields
395+
//
396+
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/99
397+
test('Field name is not confused with UPDATE operation', function(done){
398+
assert.response(app, {
399+
// view prepare_db.sh to see where to set api_key
400+
url: "/api/v1/sql?api_key=1234&"
401+
+ querystring.stringify({q:
402+
"SELECT min(updated_at) FROM private_table"
403+
}),
404+
headers: {host: 'vizzuality.localhost.lan:8080' },
405+
method: 'GET'
406+
},{}, function(res) {
407+
assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body);
408+
assert.equal(res.headers['x-cache-channel'], 'cartodb_test_user_1_db:private_table');
409+
done();
410+
});
411+
});
412+
394413
test('CREATE TABLE with GET and auth', function(done){
395414
assert.response(app, {
396415
url: "/api/v1/sql?" + querystring.stringify({

0 commit comments

Comments
 (0)