diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index 4119655f..10b5324b 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -391,13 +391,15 @@ LayergroupController.prototype.getAffectedTables = function(user, dbName, layerg function getSQL(err, mapConfig) { assert.ifError(err); - var queries = mapConfig.getLayers() - .map(function(lyr) { - return lyr.options.sql; - }) - .filter(function(sql) { - return !!sql; - }); + var queries = []; + mapConfig.getLayers().map(function(layer) { + queries.push(layer.options.sql); + if (layer.options.affected_tables) { + layer.options.affected_tables.map(function(table) { + queries.push('SELECT * FROM ' + table + ' LIMIT 0'); + }); + } + }); return queries.length ? queries.join(';') : null; }, diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index 417cf266..f32ef06a 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -312,9 +312,15 @@ MapController.prototype.afterLayergroupCreate = function(req, res, mapconfig, la done(); }); - var sql = mapconfig.getLayers().map(function(layer) { - return layer.options.sql; - }).join(';'); + var sql = []; + mapconfig.getLayers().map(function(layer) { + sql.push(layer.options.sql); + if (layer.options.affected_tables) { + layer.options.affected_tables.map(function(table) { + sql.push('SELECT * FROM ' + table + ' LIMIT 0'); + }); + } + }); var dbName = req.params.dbname; var layergroupId = layergroup.layergroupid; @@ -325,7 +331,7 @@ MapController.prototype.afterLayergroupCreate = function(req, res, mapconfig, la }, function getAffectedTablesAndLastUpdatedTime(err, connection) { assert.ifError(err); - QueryTables.getAffectedTablesFromQuery(connection, sql, this); + QueryTables.getAffectedTablesFromQuery(connection, sql.join(';'), this); }, function handleAffectedTablesAndLastUpdatedTime(err, result) { if (req.profiler) { diff --git a/lib/cartodb/models/mapconfig/adapter/analysis-mapconfig-adapter.js b/lib/cartodb/models/mapconfig/adapter/analysis-mapconfig-adapter.js index 09467ea9..c6467488 100644 --- a/lib/cartodb/models/mapconfig/adapter/analysis-mapconfig-adapter.js +++ b/lib/cartodb/models/mapconfig/adapter/analysis-mapconfig-adapter.js @@ -332,7 +332,6 @@ function AnalysisError(message) { } function getAllAffectedTablesFromSourceNodes(node) { - var affectedTables = []; var affectedTables = node.getAllInputNodes(function (node) { return node.getType() === 'source'; }).reduce(function(list, node) { return list.concat(node.getAffectedTables()); },[]); diff --git a/test/acceptance/analysis/analysis-layers.js b/test/acceptance/analysis/analysis-layers.js index 3a54ea2c..a745fb97 100644 --- a/test/acceptance/analysis/analysis-layers.js +++ b/test/acceptance/analysis/analysis-layers.js @@ -146,36 +146,6 @@ describe('analysis-layers', function() { }); }); - it('should have empty affected tables if it has only "source" node', function(done) { - var useCase = useCases[0]; - - var testClient = new TestClient(useCase.mapConfig, 1234); - - testClient.getLayergroup(function(err, layergroupResult) { - assert.ok(!err, err); - - var affected_tables = layergroupResult.metadata.layers[0].meta.affected_tables; - assert.equal(affected_tables.length, 0); - - testClient.drain(done); - }); - }); - - it('should have empty affected tables if it has a node other than "source"', function(done) { - var useCase = useCases[1]; - - var testClient = new TestClient(useCase.mapConfig, 1234); - - testClient.getLayergroup(function(err, layergroupResult) { - assert.ok(!err, err); - - var affected_tables = layergroupResult.metadata.layers[0].meta.affected_tables; - assert.equal(affected_tables[0], 'public.populated_places_simple_reduced'); - - testClient.drain(done); - }); - }); - it('should NOT fail for non-authenticated requests when it is just source', function(done) { var useCase = useCases[0]; diff --git a/test/acceptance/templates.js b/test/acceptance/templates.js index 7201b7fb..74d34d82 100644 --- a/test/acceptance/templates.js +++ b/test/acceptance/templates.js @@ -1052,8 +1052,9 @@ describe('template_api', function() { 'Unexpected error for authorized instance: ' + res.statusCode + ' -- ' + res.body); assert.equal(res.headers['content-type'], "application/json; charset=utf-8"); var cc = res.headers['x-cache-channel']; + var expectedCC = 'test_windshaft_cartodb_user_1_db:public.test_table_private_1'; assert.ok(cc); - assert.ok(cc.match, /ciao/, cc); + assert.equal(cc, expectedCC); // hack simulating restart... server.layergroupAffectedTablesCache.cache.reset(); // need to clean channel cache var get_request = { @@ -1072,8 +1073,9 @@ describe('template_api', function() { 'Unexpected error for authorized instance: ' + res.statusCode + ' -- ' + res.body); assert.equal(res.headers['content-type'], "application/json; charset=utf-8"); var cc = res.headers['x-cache-channel']; + var expectedCC = 'test_windshaft_cartodb_user_1_db:public.test_table_private_1'; assert.ok(cc, "Missing X-Cache-Channel on fetch-after-restart"); - assert.ok(cc.match, /ciao/, cc); + assert.equal(cc, expectedCC); return null; }, function deleteTemplate(err) diff --git a/test/acceptance/x_cache_channel.js b/test/acceptance/x_cache_channel.js index cd12e6ce..298c0ef0 100644 --- a/test/acceptance/x_cache_channel.js +++ b/test/acceptance/x_cache_channel.js @@ -28,6 +28,7 @@ describe('get requests x-cache-channel', function() { var mapConfigs = [ { "description": "header should be present", + "x_cache_channel": "test_windshaft_cartodb_user_1_db:public.test_table", "data": { version: '1.4.0', @@ -63,6 +64,9 @@ describe('get requests x-cache-channel', function() { }, { "description": "header should be present and be composed with source table name", + "x_cache_channel": "test_windshaft_cartodb_user_1_db:" + + "public.analysis_2f13a3dbd7_9eb239903a1afd8a69130d1ece0fc8b38de8592d" + + ",public.test_table", "data": { version: '1.5.0', @@ -181,8 +185,9 @@ describe('get requests x-cache-channel', function() { mapConfigs.forEach(function(mapConfigData) { describe(mapConfigData.description, function() { var mapConfig = mapConfigData.data; + var expectedCacheChannel = mapConfigData.x_cache_channel; it('/api/v1/map Map instantiation', function(done) { - var testFn = validateXCacheChannel(done, 'test_windshaft_cartodb_user_1_db:public.test_table'); + var testFn = validateXCacheChannel(done, expectedCacheChannel); withLayergroupId(mapConfig, function(err, layergroupId, res) { testFn(res); }); @@ -192,8 +197,8 @@ describe('get requests x-cache-channel', function() { withLayergroupId(mapConfig, function(err, layergroupId) { assert.response( server, - getRequest('/api/v1/map/' + layergroupId + '/0/0/0@2x.png'), - validateXCacheChannel(done, 'test_windshaft_cartodb_user_1_db:public.test_table') + getRequest('/api/v1/map/' + layergroupId + '/0/0/0@2x.png', true), + validateXCacheChannel(done, expectedCacheChannel) ); }); }); @@ -202,8 +207,8 @@ describe('get requests x-cache-channel', function() { withLayergroupId(mapConfig, function(err, layergroupId) { assert.response( server, - getRequest('/api/v1/map/' + layergroupId + '/0/0/0.png'), - validateXCacheChannel(done, 'test_windshaft_cartodb_user_1_db:public.test_table') + getRequest('/api/v1/map/' + layergroupId + '/0/0/0.png', true), + validateXCacheChannel(done, expectedCacheChannel) ); }); }); @@ -212,8 +217,8 @@ describe('get requests x-cache-channel', function() { withLayergroupId(mapConfig, function(err, layergroupId) { assert.response( server, - getRequest('/api/v1/map/' + layergroupId + '/0/0/0/0.png'), - validateXCacheChannel(done, 'test_windshaft_cartodb_user_1_db:public.test_table') + getRequest('/api/v1/map/' + layergroupId + '/0/0/0/0.png', true), + validateXCacheChannel(done, expectedCacheChannel) ); }); }); @@ -222,8 +227,8 @@ describe('get requests x-cache-channel', function() { withLayergroupId(mapConfig, function(err, layergroupId) { assert.response( server, - getRequest('/api/v1/map/' + layergroupId + '/0/attributes/1'), - validateXCacheChannel(done, 'test_windshaft_cartodb_user_1_db:public.test_table') + getRequest('/api/v1/map/' + layergroupId + '/0/attributes/1', true), + validateXCacheChannel(done, expectedCacheChannel) ); }); }); @@ -232,8 +237,8 @@ describe('get requests x-cache-channel', function() { withLayergroupId(mapConfig, function(err, layergroupId) { assert.response( server, - getRequest('/api/v1/map/static/center/' + layergroupId + '/0/0/0/400/300.png'), - validateXCacheChannel(done, 'test_windshaft_cartodb_user_1_db:public.test_table') + getRequest('/api/v1/map/static/center/' + layergroupId + '/0/0/0/400/300.png', true), + validateXCacheChannel(done, expectedCacheChannel) ); }); }); @@ -242,8 +247,8 @@ describe('get requests x-cache-channel', function() { withLayergroupId(mapConfig, function(err, layergroupId) { assert.response( server, - getRequest('/api/v1/map/static/bbox/' + layergroupId + '/-45,-45,45,45/400/300.png'), - validateXCacheChannel(done, 'test_windshaft_cartodb_user_1_db:public.test_table') + getRequest('/api/v1/map/static/bbox/' + layergroupId + '/-45,-45,45,45/400/300.png', true), + validateXCacheChannel(done, expectedCacheChannel) ); }); });