{"action":"created","comment":{"url":"https://api.github.com/repos/CartoDB/cartodb/pulls/comments/65056628","id":65056628,"diff_hunk":"@@ -79,30 +83,185 @@ def fix_oid(table_name)\n       end\n \n       def cartodbfy(table_name)\n+        schema_name = user.database_schema\n+        qualified_table_name = \"\\\"#{schema_name}\\\".#{table_name}\"\n+\n+        type = fix_the_geom_type!(schema_name, table_name)\n+        import_cleanup(schema_name, table_name)\n+\n+        user.transaction_with_timeout(statement_timeout: STATEMENT_TIMEOUT) do |user_conn|\n+          user_conn.run(%Q{\n+            SELECT cartodb.CDB_CartodbfyTable('#{schema_name}'::TEXT,'#{qualified_table_name}'::REGCLASS);\n+          })\n+        end\n+\n+        update_table_pg_stats(qualified_table_name)\n+        return type\n+      rescue => exception\n+        CartoDB::Logger.error(message: 'Error in sync cartodbfy',\n+                              exception: exception,\n+                              user: user,\n+                              table: table_name)\n+        raise exception\n+      end\n+\n+      def update_table_pg_stats(qualified_table_name)\n+        user.transaction_with_timeout(statement_timeout: STATEMENT_TIMEOUT) do |user_conn|\n+          user_conn.run(%Q{\n+            ANALYZE #{qualified_table_name};\n+          })\n+        end\n+      end\n+\n+      def setup_table(table_name, geo_type)\n         table = user.tables.where(name: table_name).first.service\n \n         table.force_schema = true\n \n         table.import_to_cartodb(table_name)\n         table.schema(reload: true)\n-\n-        table.send :set_the_geom_column!\n-        table.import_cleanup\n-\n-        table.send :cartodbfy\n-        table.schema(reload: true)\n         table.reload\n \n-        table.send :update_table_pg_stats\n+        # We send the detected geometry type to avoid manipulating geoms twice\n+        # set_the_geom_column! should just edit the metadata with the specified type\n+        table.send :set_the_geom_column!, geo_type\n         table.save\n       rescue => exception\n-        CartoDB::Logger.error(message: 'Error in sync cartodbfy',\n+        CartoDB::Logger.error(message: 'Error in setup cartodbfy',\n                               exception: exception,\n                               user: user,\n                               table: table_name)\n       ensure\n         fix_oid(table_name)\n-        update_cdb_tablemetadata(table_name)\n+      end\n+\n+      def fix_the_geom_type!(schema_name, table_name)\n+        qualified_table_name = \"\\\"#{schema_name}\\\".#{table_name}\"\n+\n+        # Partial duplication of Table#set_the_geom_column! to be able to\n+        # operate with non-registered tables.\n+        type = nil\n+        the_geom_data = user.in_database[%Q{\n+          SELECT a.attname, t.typname\n+          FROM pg_attribute a, pg_type t\n+          WHERE attrelid = '#{qualified_table_name}'::regclass\n+          AND attname = '#{THE_GEOM}'\n+          AND a.atttypid = t.oid\n+          LIMIT 1\n+        }].first\n+        return nil unless the_geom_data\n+\n+        if the_geom_data[:typname] != 'geometry'\n+          user.in_database.rename_column(qualified_table_name, THE_GEOM, :the_geom_str)\n+          return nil\n+        end\n+\n+        geom_type = user.in_database[%Q{","path":"app/models/synchronization/adapter.rb","position":134,"original_position":134,"commit_id":"23b8009b32cedf577304b5574b755885aecd52e1","original_commit_id":"23b8009b32cedf577304b5574b755885aecd52e1","user":{"login":"houndci-bot","id":6697940,"avatar_url":"https://avatars.githubusercontent.com/u/6697940?v=3","gravatar_id":"","url":"https://api.github.com/users/houndci-bot","html_url":"https://github.com/houndci-bot","followers_url":"https://api.github.com/users/houndci-bot/followers","following_url":"https://api.github.com/users/houndci-bot/following{/other_user}","gists_url":"https://api.github.com/users/houndci-bot/gists{/gist_id}","starred_url":"https://api.github.com/users/houndci-bot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/houndci-bot/subscriptions","organizations_url":"https://api.github.com/users/houndci-bot/orgs","repos_url":"https://api.github.com/users/houndci-bot/repos","events_url":"https://api.github.com/users/houndci-bot/events{/privacy}","received_events_url":"https://api.github.com/users/houndci-bot/received_events","type":"User","site_admin":false},"body":"Use `%` instead of `%Q`.<br>Use `%Q` only for strings that contain both single quotes and double quotes, or for dynamic strings that contain double quotes.","created_at":"2016-05-30T11:02:37Z","updated_at":"2016-05-30T11:02:37Z","html_url":"https://github.com/CartoDB/cartodb/pull/7543#discussion_r65056628","pull_request_url":"https://api.github.com/repos/CartoDB/cartodb/pulls/7543","_links":{"self":{"href":"https://api.github.com/repos/CartoDB/cartodb/pulls/comments/65056628"},"html":{"href":"https://github.com/CartoDB/cartodb/pull/7543#discussion_r65056628"},"pull_request":{"href":"https://api.github.com/repos/CartoDB/cartodb/pulls/7543"}}},"pull_request":{"url":"https://api.github.com/repos/CartoDB/cartodb/pulls/7543","id":71687112,"html_url":"https://github.com/CartoDB/cartodb/pull/7543","diff_url":"https://github.com/CartoDB/cartodb/pull/7543.diff","patch_url":"https://github.com/CartoDB/cartodb/pull/7543.patch","issue_url":"https://api.github.com/repos/CartoDB/cartodb/issues/7543","number":7543,"state":"open","locked":false,"title":"7171 CartoDBfy tables before switching in Sync Tables","user":{"login":"iriberri","id":1730320,"avatar_url":"https://avatars.githubusercontent.com/u/1730320?v=3","gravatar_id":"","url":"https://api.github.com/users/iriberri","html_url":"https://github.com/iriberri","followers_url":"https://api.github.com/users/iriberri/followers","following_url":"https://api.github.com/users/iriberri/following{/other_user}","gists_url":"https://api.github.com/users/iriberri/gists{/gist_id}","starred_url":"https://api.github.com/users/iriberri/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iriberri/subscriptions","organizations_url":"https://api.github.com/users/iriberri/orgs","repos_url":"https://api.github.com/users/iriberri/repos","events_url":"https://api.github.com/users/iriberri/events{/privacy}","received_events_url":"https://api.github.com/users/iriberri/received_events","type":"User","site_admin":false},"body":"Output of some tests: https://gist.github.com/iriberri/42a1a44a47f9ee5e428901dcd4a125d5\r\n\r\nFixes #7171 \r\n\r\nOther similar branches I created just for test purposes (see gist above):\r\n* `7171-cdbfy_in_syncs_b`\r\n* `cdbfy_master_with_sleep`\r\n\r\nWIP: The code is still in its first iterations\r\n\r\nPending: \r\n* [ ] test thoroughly for cartodbfycation exceptions and special geom types (multipoint, simple linestrings) to check postgres queries flow\r\n* [ ] get some numbers on ghost tables query times","created_at":"2016-05-27T09:46:19Z","updated_at":"2016-05-30T11:02:37Z","closed_at":null,"merged_at":null,"merge_commit_sha":"98afbe5ee099bd66c8445ea7efb505dd28ef73b0","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/CartoDB/cartodb/pulls/7543/commits","review_comments_url":"https://api.github.com/repos/CartoDB/cartodb/pulls/7543/comments","review_comment_url":"https://api.github.com/repos/CartoDB/cartodb/pulls/comments{/number}","comments_url":"https://api.github.com/repos/CartoDB/cartodb/issues/7543/comments","statuses_url":"https://api.github.com/repos/CartoDB/cartodb/statuses/23b8009b32cedf577304b5574b755885aecd52e1","head":{"label":"CartoDB:7171-cdbfy_in_syncs","ref":"7171-cdbfy_in_syncs","sha":"23b8009b32cedf577304b5574b755885aecd52e1","user":{"login":"CartoDB","id":1799254,"avatar_url":"https://avatars.githubusercontent.com/u/1799254?v=3","gravatar_id":"","url":"https://api.github.com/users/CartoDB","html_url":"https://github.com/CartoDB","followers_url":"https://api.github.com/users/CartoDB/followers","following_url":"https://api.github.com/users/CartoDB/following{/other_user}","gists_url":"https://api.github.com/users/CartoDB/gists{/gist_id}","starred_url":"https://api.github.com/users/CartoDB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CartoDB/subscriptions","organizations_url":"https://api.github.com/users/CartoDB/orgs","repos_url":"https://api.github.com/users/CartoDB/repos","events_url":"https://api.github.com/users/CartoDB/events{/privacy}","received_events_url":"https://api.github.com/users/CartoDB/received_events","type":"Organization","site_admin":false},"repo":{"id":4909355,"name":"cartodb","full_name":"CartoDB/cartodb","owner":{"login":"CartoDB","id":1799254,"avatar_url":"https://avatars.githubusercontent.com/u/1799254?v=3","gravatar_id":"","url":"https://api.github.com/users/CartoDB","html_url":"https://github.com/CartoDB","followers_url":"https://api.github.com/users/CartoDB/followers","following_url":"https://api.github.com/users/CartoDB/following{/other_user}","gists_url":"https://api.github.com/users/CartoDB/gists{/gist_id}","starred_url":"https://api.github.com/users/CartoDB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CartoDB/subscriptions","organizations_url":"https://api.github.com/users/CartoDB/orgs","repos_url":"https://api.github.com/users/CartoDB/repos","events_url":"https://api.github.com/users/CartoDB/events{/privacy}","received_events_url":"https://api.github.com/users/CartoDB/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/CartoDB/cartodb","description":"Location Intelligence & Data Visualization tool","fork":false,"url":"https://api.github.com/repos/CartoDB/cartodb","forks_url":"https://api.github.com/repos/CartoDB/cartodb/forks","keys_url":"https://api.github.com/repos/CartoDB/cartodb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/CartoDB/cartodb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/CartoDB/cartodb/teams","hooks_url":"https://api.github.com/repos/CartoDB/cartodb/hooks","issue_events_url":"https://api.github.com/repos/CartoDB/cartodb/issues/events{/number}","events_url":"https://api.github.com/repos/CartoDB/cartodb/events","assignees_url":"https://api.github.com/repos/CartoDB/cartodb/assignees{/user}","branches_url":"https://api.github.com/repos/CartoDB/cartodb/branches{/branch}","tags_url":"https://api.github.com/repos/CartoDB/cartodb/tags","blobs_url":"https://api.github.com/repos/CartoDB/cartodb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/CartoDB/cartodb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/CartoDB/cartodb/git/refs{/sha}","trees_url":"https://api.github.com/repos/CartoDB/cartodb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/CartoDB/cartodb/statuses/{sha}","languages_url":"https://api.github.com/repos/CartoDB/cartodb/languages","stargazers_url":"https://api.github.com/repos/CartoDB/cartodb/stargazers","contributors_url":"https://api.github.com/repos/CartoDB/cartodb/contributors","subscribers_url":"https://api.github.com/repos/CartoDB/cartodb/subscribers","subscription_url":"https://api.github.com/repos/CartoDB/cartodb/subscription","commits_url":"https://api.github.com/repos/CartoDB/cartodb/commits{/sha}","git_commits_url":"https://api.github.com/repos/CartoDB/cartodb/git/commits{/sha}","comments_url":"https://api.github.com/repos/CartoDB/cartodb/comments{/number}","issue_comment_url":"https://api.github.com/repos/CartoDB/cartodb/issues/comments{/number}","contents_url":"https://api.github.com/repos/CartoDB/cartodb/contents/{+path}","compare_url":"https://api.github.com/repos/CartoDB/cartodb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/CartoDB/cartodb/merges","archive_url":"https://api.github.com/repos/CartoDB/cartodb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/CartoDB/cartodb/downloads","issues_url":"https://api.github.com/repos/CartoDB/cartodb/issues{/number}","pulls_url":"https://api.github.com/repos/CartoDB/cartodb/pulls{/number}","milestones_url":"https://api.github.com/repos/CartoDB/cartodb/milestones{/number}","notifications_url":"https://api.github.com/repos/CartoDB/cartodb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/CartoDB/cartodb/labels{/name}","releases_url":"https://api.github.com/repos/CartoDB/cartodb/releases{/id}","deployments_url":"https://api.github.com/repos/CartoDB/cartodb/deployments","created_at":"2012-07-05T15:23:45Z","updated_at":"2016-05-28T17:06:28Z","pushed_at":"2016-05-30T11:02:29Z","git_url":"git://github.com/CartoDB/cartodb.git","ssh_url":"git@github.com:CartoDB/cartodb.git","clone_url":"https://github.com/CartoDB/cartodb.git","svn_url":"https://github.com/CartoDB/cartodb","homepage":"http://cartodb.com","size":387387,"stargazers_count":1142,"watchers_count":1142,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":289,"mirror_url":null,"open_issues_count":923,"forks":289,"open_issues":923,"watchers":1142,"default_branch":"master"}},"base":{"label":"CartoDB:master","ref":"master","sha":"8cbcf7a721c6a8ad233773116b8302cc482d2702","user":{"login":"CartoDB","id":1799254,"avatar_url":"https://avatars.githubusercontent.com/u/1799254?v=3","gravatar_id":"","url":"https://api.github.com/users/CartoDB","html_url":"https://github.com/CartoDB","followers_url":"https://api.github.com/users/CartoDB/followers","following_url":"https://api.github.com/users/CartoDB/following{/other_user}","gists_url":"https://api.github.com/users/CartoDB/gists{/gist_id}","starred_url":"https://api.github.com/users/CartoDB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CartoDB/subscriptions","organizations_url":"https://api.github.com/users/CartoDB/orgs","repos_url":"https://api.github.com/users/CartoDB/repos","events_url":"https://api.github.com/users/CartoDB/events{/privacy}","received_events_url":"https://api.github.com/users/CartoDB/received_events","type":"Organization","site_admin":false},"repo":{"id":4909355,"name":"cartodb","full_name":"CartoDB/cartodb","owner":{"login":"CartoDB","id":1799254,"avatar_url":"https://avatars.githubusercontent.com/u/1799254?v=3","gravatar_id":"","url":"https://api.github.com/users/CartoDB","html_url":"https://github.com/CartoDB","followers_url":"https://api.github.com/users/CartoDB/followers","following_url":"https://api.github.com/users/CartoDB/following{/other_user}","gists_url":"https://api.github.com/users/CartoDB/gists{/gist_id}","starred_url":"https://api.github.com/users/CartoDB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CartoDB/subscriptions","organizations_url":"https://api.github.com/users/CartoDB/orgs","repos_url":"https://api.github.com/users/CartoDB/repos","events_url":"https://api.github.com/users/CartoDB/events{/privacy}","received_events_url":"https://api.github.com/users/CartoDB/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/CartoDB/cartodb","description":"Location Intelligence & Data Visualization tool","fork":false,"url":"https://api.github.com/repos/CartoDB/cartodb","forks_url":"https://api.github.com/repos/CartoDB/cartodb/forks","keys_url":"https://api.github.com/repos/CartoDB/cartodb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/CartoDB/cartodb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/CartoDB/cartodb/teams","hooks_url":"https://api.github.com/repos/CartoDB/cartodb/hooks","issue_events_url":"https://api.github.com/repos/CartoDB/cartodb/issues/events{/number}","events_url":"https://api.github.com/repos/CartoDB/cartodb/events","assignees_url":"https://api.github.com/repos/CartoDB/cartodb/assignees{/user}","branches_url":"https://api.github.com/repos/CartoDB/cartodb/branches{/branch}","tags_url":"https://api.github.com/repos/CartoDB/cartodb/tags","blobs_url":"https://api.github.com/repos/CartoDB/cartodb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/CartoDB/cartodb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/CartoDB/cartodb/git/refs{/sha}","trees_url":"https://api.github.com/repos/CartoDB/cartodb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/CartoDB/cartodb/statuses/{sha}","languages_url":"https://api.github.com/repos/CartoDB/cartodb/languages","stargazers_url":"https://api.github.com/repos/CartoDB/cartodb/stargazers","contributors_url":"https://api.github.com/repos/CartoDB/cartodb/contributors","subscribers_url":"https://api.github.com/repos/CartoDB/cartodb/subscribers","subscription_url":"https://api.github.com/repos/CartoDB/cartodb/subscription","commits_url":"https://api.github.com/repos/CartoDB/cartodb/commits{/sha}","git_commits_url":"https://api.github.com/repos/CartoDB/cartodb/git/commits{/sha}","comments_url":"https://api.github.com/repos/CartoDB/cartodb/comments{/number}","issue_comment_url":"https://api.github.com/repos/CartoDB/cartodb/issues/comments{/number}","contents_url":"https://api.github.com/repos/CartoDB/cartodb/contents/{+path}","compare_url":"https://api.github.com/repos/CartoDB/cartodb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/CartoDB/cartodb/merges","archive_url":"https://api.github.com/repos/CartoDB/cartodb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/CartoDB/cartodb/downloads","issues_url":"https://api.github.com/repos/CartoDB/cartodb/issues{/number}","pulls_url":"https://api.github.com/repos/CartoDB/cartodb/pulls{/number}","milestones_url":"https://api.github.com/repos/CartoDB/cartodb/milestones{/number}","notifications_url":"https://api.github.com/repos/CartoDB/cartodb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/CartoDB/cartodb/labels{/name}","releases_url":"https://api.github.com/repos/CartoDB/cartodb/releases{/id}","deployments_url":"https://api.github.com/repos/CartoDB/cartodb/deployments","created_at":"2012-07-05T15:23:45Z","updated_at":"2016-05-28T17:06:28Z","pushed_at":"2016-05-30T11:02:29Z","git_url":"git://github.com/CartoDB/cartodb.git","ssh_url":"git@github.com:CartoDB/cartodb.git","clone_url":"https://github.com/CartoDB/cartodb.git","svn_url":"https://github.com/CartoDB/cartodb","homepage":"http://cartodb.com","size":387387,"stargazers_count":1142,"watchers_count":1142,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":289,"mirror_url":null,"open_issues_count":923,"forks":289,"open_issues":923,"watchers":1142,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/CartoDB/cartodb/pulls/7543"},"html":{"href":"https://github.com/CartoDB/cartodb/pull/7543"},"issue":{"href":"https://api.github.com/repos/CartoDB/cartodb/issues/7543"},"comments":{"href":"https://api.github.com/repos/CartoDB/cartodb/issues/7543/comments"},"review_comments":{"href":"https://api.github.com/repos/CartoDB/cartodb/pulls/7543/comments"},"review_comment":{"href":"https://api.github.com/repos/CartoDB/cartodb/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/CartoDB/cartodb/pulls/7543/commits"},"statuses":{"href":"https://api.github.com/repos/CartoDB/cartodb/statuses/23b8009b32cedf577304b5574b755885aecd52e1"}}}}
{"actor":{"display_login":"houndci-bot"}}