Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Developer happiness with DB Charmer

Lance Gleason
February 28, 2014

Developer happiness with DB Charmer

Presentation by Lance Gleason from Polyglot Programming Inc at the Atlanta Ruby Meetup and Jozi.rb in Johannesburg South Africa.

http://www.polyglotprogramminginc.com
http://www.polyglotprogramminginc.com/charming-large-databases-with-octopuses/

Lance Gleason

February 28, 2014
Tweet

More Decks by Lance Gleason

Other Decks in Programming

Transcript

  1. ?

  2. ?

  3. SQL

  4. production:      blah:          adapter:  mysql

             username:  blah          host:  blah.local          database:  blah   !    foo:          adapter:  mysql          username:  foo          host:  foo.local          database:  foo  
  5. class  Foo  <  ActiveRecord::Model;  end   ! Foo.switch_connection_to(:blah)   Foo.switch_connection_to('foo')

      Foo.switch_connection_to(Bar)   Foo.switch_connection_to(Baz.connectio n)   Foo.switch_connection_to(nil)  
  6. class  MultiDbTest  <  ActiveRecord::Migration        db_magic  :connection  =>

     :second_db   !      def  self.up            create_table  :test_table,  :force  =>  true  do  |t|              t.string  :test_string                t.timestamps            end        end   !      def  self.down            drop_table  :test_table        end    end  
  7. class  MultiDbTest  <  ActiveRecord::Migration      def  self.up    

         on_db  :second_db  do              create_table  :test_table,  :force  =>  true  do   |t|                  t.string  :test_string                  t.timestamps              end          end      end   !    def  self.down          on_db  :second_db  {  drop_table  :test_table  }      end   end  
  8. class  MultiDbTest  <  ActiveRecord::Migration      db_magic  :connections  =>  [:second_db,

     :default]   !    def  self.up          create_table  :test_table,  :force  =>  true  do  |t|              t.string  :test_string              t.timestamps          end      end   !    def  self.down          drop_table  :test_table      end   end  
  9. class  Foo  <  ActiveRecord::Base      db_magic  :slave  =>  :slave01

      end   ! class  Bar  <  ActiveRecord::Base      db_magic  :slaves  =>   [  :slave01,  :slave02  ]   end  
  10. class  Foo  <  ActiveRecord::Base      db_magic  :connection  =>  :foo

      end   class  Bar  <  ActiveRecord::Base      db_magic  :connection  =>  :bar,  :slave   =>  :bar_slave   end  
  11. class  Foo  <  ActiveRecord::Model;  end   ! Foo.switch_connection_to(:blah)   Foo.switch_connection_to('foo')

      Foo.switch_connection_to(Bar)   Foo.switch_connection_to(Baz.connectio n)   Foo.switch_connection_to(nil)  
  12. octopus:      replicated:  true      production:    

         slave1:              adapter:  mysql              host:  localhost              database:  octopus_shard2          slave2:              adapter:  mysql              host:  localhost              database:  octopus_shard3          slave3:              adapter:  mysql              host:  localhost              database:  octopus_shard4          slave4:              adapter:  mysql              host:  localhost              database:  octopus_shard5  
  13. class  CreateUsersOnBothShards  <   ActiveRecord::Migration      using(:brazil,  :canada)  

    !    def  self.up          User.create!(:name  =>  "Both")      end   !    def  self.down          User.delete_all      end   end  
  14. class  ApplicationController  <   ActionController::Base      around_filter  :select_shard  

    !    def  select_shard(&block)          Octopus.using(:brazil,  &block)      end   end