{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/26120","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/26120/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/26120/comments","events_url":"https://api.github.com/repos/rails/rails/issues/26120/events","html_url":"https://github.com/rails/rails/issues/26120","id":170660261,"number":26120,"title":"Proposal: Increasing efficiency of current_version method in active_record/migration","user":{"login":"kyatul","id":3594425,"avatar_url":"https://avatars.githubusercontent.com/u/3594425?v=3","gravatar_id":"","url":"https://api.github.com/users/kyatul","html_url":"https://github.com/kyatul","followers_url":"https://api.github.com/users/kyatul/followers","following_url":"https://api.github.com/users/kyatul/following{/other_user}","gists_url":"https://api.github.com/users/kyatul/gists{/gist_id}","starred_url":"https://api.github.com/users/kyatul/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kyatul/subscriptions","organizations_url":"https://api.github.com/users/kyatul/orgs","repos_url":"https://api.github.com/users/kyatul/repos","events_url":"https://api.github.com/users/kyatul/events{/privacy}","received_events_url":"https://api.github.com/users/kyatul/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-08-11T14:31:49Z","updated_at":"2016-08-11T17:54:35Z","closed_at":"2016-08-11T15:09:21Z","body":"In [**activerecord/lib/active_record/migration.rb**](https://github.com/rails/rails/blob/master/activerecord/lib/active_record/migration.rb), for class **Migrator** we have two methods **current_version** and **get_all_version**.\r\n\r\nCurrently **current_version** calls **get_all_version** and selects maximum value from returned array.\r\n**get_all_version** fetches all the records, and then it loops through it gathering version number, and then sorting it.\r\n\r\n**CURRENT CODE**\r\n`\r\n      def get_all_versions(connection = Base.connection)        \r\n        \r\n       ActiveSupport::Deprecation.silence do\r\n          if connection.table_exists?(schema_migrations_table_name)\r\n            SchemaMigration.all.map { |x| x.version.to_i }.sort  \r\n          else\r\n            []\r\n          end        \r\n        end      \r\n      end\r\n\r\n      def current_version(connection = Base.connection)\r\n        get_all_versions(connection).max || 0\r\n      end`\r\n\r\nWe can get rid of all this overhead, if we can simply query DB to fetch the maximum version number in **current_version** method.\r\n\r\n**PROPOSED CODE**\r\n`\r\n    def current_version(connection = Base.connection)\r\n      ActiveSupport::Deprecation.silence do\r\n        if connection.table_exists?(schema_migrations_table_name)\r\n            SchemaMigration.maximum(:version).to_i\r\n        end\r\n      end\r\n   end`\r\n\r\nAlthough current code has cleaner one line approach, but we can look for bigger picture regarding efficiency.\r\nAny suggestions on this are highly appreciated"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/239238768","html_url":"https://github.com/rails/rails/issues/26120#issuecomment-239238768","issue_url":"https://api.github.com/repos/rails/rails/issues/26120","id":239238768,"user":{"login":"kaspth","id":350807,"avatar_url":"https://avatars.githubusercontent.com/u/350807?v=3","gravatar_id":"","url":"https://api.github.com/users/kaspth","html_url":"https://github.com/kaspth","followers_url":"https://api.github.com/users/kaspth/followers","following_url":"https://api.github.com/users/kaspth/following{/other_user}","gists_url":"https://api.github.com/users/kaspth/gists{/gist_id}","starred_url":"https://api.github.com/users/kaspth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kaspth/subscriptions","organizations_url":"https://api.github.com/users/kaspth/orgs","repos_url":"https://api.github.com/users/kaspth/repos","events_url":"https://api.github.com/users/kaspth/events{/privacy}","received_events_url":"https://api.github.com/users/kaspth/received_events","type":"User","site_admin":false},"created_at":"2016-08-11T17:54:35Z","updated_at":"2016-08-11T17:54:35Z","body":"Sorry, I meant to say `current_version`. I don't think there's any need for the benchmark because the proposed change doesn't seem worth it from a maintainability standpoint. I can't see us switching to the proposed change even if it was 10x faster, it's just not a hot spot :)\r\n\r\nUnless you can prove it gets called more often than I think and show me that your proposed change has a marked difference on say `db:migrate` :D"}}
{"actor":{"display_login":"kaspth"}}