{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/24965","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/24965/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/24965/comments","events_url":"https://api.github.com/repos/rails/rails/issues/24965/events","html_url":"https://github.com/rails/rails/pull/24965","id":154156215,"number":24965,"title":"Change some establish_connection logic","user":{"login":"arthurnn","id":833383,"avatar_url":"https://avatars.githubusercontent.com/u/833383?v=3","gravatar_id":"","url":"https://api.github.com/users/arthurnn","html_url":"https://github.com/arthurnn","followers_url":"https://api.github.com/users/arthurnn/followers","following_url":"https://api.github.com/users/arthurnn/following{/other_user}","gists_url":"https://api.github.com/users/arthurnn/gists{/gist_id}","starred_url":"https://api.github.com/users/arthurnn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arthurnn/subscriptions","organizations_url":"https://api.github.com/users/arthurnn/orgs","repos_url":"https://api.github.com/users/arthurnn/repos","events_url":"https://api.github.com/users/arthurnn/events{/privacy}","received_events_url":"https://api.github.com/users/arthurnn/received_events","type":"User","site_admin":true},"labels":[{"url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2016-05-11T04:09:41Z","updated_at":"2016-05-18T02:35:21Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/rails/rails/pulls/24965","html_url":"https://github.com/rails/rails/pull/24965","diff_url":"https://github.com/rails/rails/pull/24965.diff","patch_url":"https://github.com/rails/rails/pull/24965.patch"},"body":"### Summary\r\nMove `establish_connection` to connection handler.\r\nThis give us room to deprecate `establish_connection` on the AR::Base to use the one from the connection handler instead.\r\n\r\n### Details\r\n\r\nAs you know, now the connection handler is not coupled with the model anymore, so establish connection should not be a method in the model.\r\n\r\nWith this API, you would do something like:\r\n```\r\nActiveRecord::Base.connection_handler.establish_connection({database: 'foo'}, name: \"readonly\")\r\nclass User < ActiveRecord::Base\r\n  self.connection_specification_name = \"readonly\"\r\nend\r\n\r\nActiveRecord::Base.connection_handler.establish_connection(:some_key_from_databaseyml, name: \"readonly\")\r\n\r\nActiveRecord::Base.connection_handler.establish_connection(\"URL://\", name: \"readonly\")\r\n```\r\n\r\nThis is backwards compatible as far as I can tell, so it is safe to be backported to 5.0.0\r\n\r\n### Next steps (all still up to discussion)\r\n- Make AR internals use `ActiveRecord::Base.connection_handler.establish_connection` API\r\n- Deprecate `AR::Base.establish_connection`\r\n- Make ConnectionSpecification only work with a hash or a string.\r\n - Hash: when passing a full config\r\n - String: using a database url\r\n - Symbol: we wont need this anymore, this is used to point to a config from the databases.yml file (see next item)\r\n- Change database.yml file to the following:\r\n```\r\nproduction:\r\n  primary: \r\n    database: foo\r\n  secondary:\r\n    database: foo_readonly\r\n  shard:\r\n    database: shard1\r\n```\r\n\r\nLike that, the railties AR initializer, can iterate over the ENV key, and create pools for each of the configs using the right name.\r\n\r\ncc @jeremy @tenderlove @rafaelfranca @matthewd \r\nalso @jrafanie as we discussed that today."},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/219909836","html_url":"https://github.com/rails/rails/pull/24965#issuecomment-219909836","issue_url":"https://api.github.com/repos/rails/rails/issues/24965","id":219909836,"user":{"login":"arthurnn","id":833383,"avatar_url":"https://avatars.githubusercontent.com/u/833383?v=3","gravatar_id":"","url":"https://api.github.com/users/arthurnn","html_url":"https://github.com/arthurnn","followers_url":"https://api.github.com/users/arthurnn/followers","following_url":"https://api.github.com/users/arthurnn/following{/other_user}","gists_url":"https://api.github.com/users/arthurnn/gists{/gist_id}","starred_url":"https://api.github.com/users/arthurnn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arthurnn/subscriptions","organizations_url":"https://api.github.com/users/arthurnn/orgs","repos_url":"https://api.github.com/users/arthurnn/repos","events_url":"https://api.github.com/users/arthurnn/events{/privacy}","received_events_url":"https://api.github.com/users/arthurnn/received_events","type":"User","site_admin":true},"created_at":"2016-05-18T02:35:21Z","updated_at":"2016-05-18T02:35:21Z","body":"@kaspth thanks a lot for reviewing this. i havent much time to work on it. I will this week."}}