Slide 1

Slide 1 text

3BJMTPO5XP%#T :VJDIJSP,BOFLP େߐށ3VCZձٞ

Slide 2

Slide 2 text

8IP*BN ,BOFLP:VJDIJSP !ZVJLOL

Slide 3

Slide 3 text

ZVJLOL (JU)VC TQJLFPMBG UXJUUFS EPDSBJMTSBJMTHVJEFTKQ QSZBDUJWF@EFDPSBUPS ΋ͱܦཧϚϯ

Slide 4

Slide 4 text

3BJMTΨΠυ຋༁νʔϜ

Slide 5

Slide 5 text

&EHF3BJMT

Slide 6

Slide 6 text

IUUQTTQFBLFSEFDLDPNB@NBUTVEBSBJMTLBJGBGBMTFIJNJUV

Slide 7

Slide 7 text

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

&EHF3BJMT $PNNJUFS

Slide 10

Slide 10 text

&EHF3BJMT $PNNJUFS

Slide 11

Slide 11 text

&EHF3BJMT $PNNJUFS ೔ຊਓͬΆ͍ਓ

Slide 12

Slide 12 text

IUUQZZBHJIBUFOBCMPHDPN

Slide 13

Slide 13 text

ࢲͱ"TBLVTBSC w େߐށ3VCZձٞࢀՃ w ॳࢀՃ͸z"TBLVTBSCճه೦͓ॕ͍ձz w ݄ʹճ͘Β͍ࢀՃ w 3BJMTͷ͜ͱͱ͔ɺ3VCZͷ͜ͱͱ͔ΛDPNNJUFS ͷਓʹ૬ஊ

Slide 14

Slide 14 text

3VCZʹݶΒͣɺ࿩ऀ͕ݱ ࡏڵຯͷ͋Δ໘ന͍಺༰

Slide 15

Slide 15 text

໘ന͍ͱ͸

Slide 16

Slide 16 text

3BJMTͱෳ਺%#

Slide 17

Slide 17 text

ෳ਺%#ͱ͸

Slide 18

Slide 18 text

ෳ਺%# w %#෼ׂʁ w 38TQMJUUJOH w γϟʔσΟϯάʁ IUUQTTQFBLFSEFDLDPNFBHMFUNUGVTIVECUPSBJMT

Slide 19

Slide 19 text

38TQMJUUJOH TXJUDI@QPJOU

Slide 20

Slide 20 text

γϟʔσΟϯά NJYFE@HBVHF

Slide 21

Slide 21 text

0DUPQVT ஌Βͳ͍ࢠͰ͢Ͷ

Slide 22

Slide 22 text

ΧδϡΞϧͳ%#෼ׂ w ࠓճ͸ෛՙ෼ࢄ͕໨తͰ͸ͳ͘ w ͋ΔαʔϏεͷಛఆͷ5BCMF 6TFST ΛଞͷαʔϏ εͰ΋࢖͍͍ͨ w 3BJMT.Z42-

Slide 23

Slide 23 text

w 6TFST w 3FMBUJPOTIJQT w .JDSPQPTUT w ʜ w 6TFST w 3FMBUJPOTIJQT w 'FFET w ʜ mysql> show databases; +-----------------------------+ | Database | +-----------------------------+ | service1_development | | service2_development | | service1_test | | service2_test | +-----------------------------+ 2 rows in set (0.00 sec) 4FSWJDF 4FSWJDF

Slide 24

Slide 24 text

IUUQTHJUIVCDPNSBJMTSBJMTUSFF WBDUJWFSFDPSEUFTUDBTFT NVMUJQMF@EC@UFTUSC

Slide 25

Slide 25 text

ʜ ݁ߏੲ͔Β͋Δ

Slide 26

Slide 26 text

BDUJWFSFDPSEUFTUDBTFT NVMUJQMF@EC@UFTUSC w Ұݟɺ-(5.ʹΈ͑Δ͡Όͳ͍Ͱ͔͢ def test_associations c1 = Course.find(1) assert_equal 2, c1.entrants.count e1 = Entrant.find(1) assert_equal e1.course.id, c1.id c2 = Course.find(2) assert_equal 1, c2.entrants.count e3 = Entrant.find(3) assert_equal e3.course.id, c2.id end

Slide 27

Slide 27 text

ෳࡶͳ"TTPDJBUJPO w 5FBDIFST w $PMMFHF5FBDIFST w $PMMFHFT %BUBCBTF %BUBCBTF $PMMFHF5FBDIFS $PMMFHF 5FBDIFS / /

Slide 28

Slide 28 text

ෳࡶͳ"TTPDJBUJPO # OK SELECT `teachers`.* FROM `teachers` INNER JOIN `college_teachers` ON `teachers`.`id` = `college_teachers`.`teacher_id` WHERE `college_teachers`.`college_id` = 1 # NG SELECT `colleges`.* FROM `colleges` INNER JOIN `college_teachers` ON `colleges`.`id` = `college_teachers`.`college_id` WHERE `college_teachers`.`teacher_id` = 1 # OK college.teachers # NG teacher.colleges

Slide 29

Slide 29 text

ෳࡶͳ"TTPDJBUJPO # OK SELECT `teachers`.* FROM `teachers` INNER JOIN `college_teachers` ON `teachers`.`id` = `college_teachers`.`teacher_id` WHERE `college_teachers`.`college_id` = 1 # NG SELECT `colleges`.* FROM `colleges` INNER JOIN `college_teachers` ON `colleges`.`id` = `college_teachers`.`college_id` WHERE `college_teachers`.`teacher_id` = 1 # OK college.teachers # NG teacher.colleges

Slide 30

Slide 30 text

DPMMFHFUFBDIFST $PMMFHF5FBDIFS $PMMFHF 5FBDIFS / / +0*/Մೳ

Slide 31

Slide 31 text

UFBDIFSDPMMFHFT $PMMFHF5FBDIFS $PMMFHF 5FBDIFS / / +0*/ෆՄೳ

Slide 32

Slide 32 text

EBUBCBTF໊Λ ΫΤϦʹؚΊΔ # BEFORE SELECT `colleges`.* FROM `colleges` INNER JOIN `college_teachers` ON `colleges`.`id` = `college_teachers`.`college_id` WHERE `college_teachers`.`teacher_id` = 1 # AFTER SELECT `database2`.`colleges`.* FROM `database2`.`colleges` INNER JOIN `database1`.`college_teachers` ON `database1`.`colleges`.`id` = `database1`.`college_teachers`.`college_id` WHERE `database1`.`college_teachers`.`teacher_id` = 1

Slide 33

Slide 33 text

"SFMΛ֦ுͩ

Slide 34

Slide 34 text

--- a/lib/arel/table.rb +++ b/lib/arel/table.rb @@ -6,7 +6,7 @@ module Arel @engine = nil class << self; attr_accessor :engine; end - attr_accessor :name, :engine, :aliases, :table_alias + attr_accessor :name, :engine, :aliases, :table_alias, :database_name @@ -18,6 +18,7 @@ module Arel @aliases = [] @table_alias = nil @primary_key = nil + @database_name = nil --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -612,6 +612,7 @@ module Arel end def visit_Arel_Table o, collector + collector << "#{quote_table_name o.database_name}." if o.database_name if o.table_alias collector << "#{quote_table_name o.name} #{quote_table_name o.table_alias}" else

Slide 35

Slide 35 text

--- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -235,7 +235,9 @@ module ActiveRecord # scope :published_and_commented, -> { published.and(self.arel_table[:comments_count].gt(0)) } # end def arel_table # :nodoc: - @arel_table ||= Arel::Table.new(table_name, arel_engine) + @arel_table ||= Arel::Table.new(table_name, arel_engine).tap {|a| + a.database_name = connection.instance_variable_get(:@config)[:database] if connection.instance_variable_get(:@config) + } end --- a/Gemfile +++ b/Gemfile @@ -124,3 +125,5 @@ end # A gem necessary for ActiveRecord tests with IBM DB gem 'ibm_db' if ENV['IBM_DB'] + +gem 'arel', github: 'yui-knk/arel', branch: 'feature/database_name'

Slide 36

Slide 36 text

3BJMTͷςετ͕͚͜Δ ActiveRecord::ConnectionAdapters::Mysql2SchemaTest#test_schema: ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.`posts` ORDER BY `activerecord_unittest`.`posts`.`id` ASC LIMIT 1’ at line 1: SELECT `activerecord_unittest`.`posts`.* FROM `activerecord_unittest`.`activerecord_unittest`.`posts` ORDER BY `activerecord_unittest`.`posts`.`id` ASC LIMIT 1 ... ruby/rails/activerecord/test/cases/adapters/mysql2/schema_test.rb:23:in `test_schema'

Slide 37

Slide 37 text

UFTUDBTFTBEBQUFST NZTRMTDIFNB@UFTUSC def setup @connection = ActiveRecord::Base.connection db = Post.connection_pool.spec.config[:database] table = Post.table_name @db_name = db @omgpost = Class.new(ActiveRecord::Base) do self.table_name = "#{db}.#{table}" def self.name; 'Post'; end end end def test_schema assert @omgpost.first end

Slide 38

Slide 38 text

"3UBCMF@OBNF class College < ActiveRecord::Base self.table_name = "database2.colleges" end class CollegeTeacher < ActiveRecord::Base self.table_name = "database1.college_teachers" end class Teacher < ActiveRecord::Base self.table_name = "database1.teachers" end

Slide 39

Slide 39 text

1SPCMFNT w (FN ""4. ͷΫΤϦ w .JHSBUJPO w 3BJMTଆͷαϙʔτ

Slide 40

Slide 40 text

""4. class User < ActiveRecord::Base include AASM aasm column: :status do state :sleeping #... end end User.sleeping # => "SELECT `database1`.`users`.* FROM `database1`.`users` WHERE `database1`.`users.status` = 'sleeping'"

Slide 41

Slide 41 text

""4. conditions = {"#{@klass.table_name}.#{@klass.aasm(@name).attribute_name}" => name.to_s} if ActiveRecord::VERSION::MAJOR >= 3 @klass.class_eval do scope name, lambda { where(conditions) } end else @klass.class_eval do named_scope name, :conditions => conditions end end

Slide 42

Slide 42 text

3BJMT ͓޷ΈͰ͸ͳ͍༷ࢠ

Slide 43

Slide 43 text

.JHSBUJPO w ։ൃ EFWFMPQ ͱςετ UFTU Ͱ͸6TFSTςʔϒϧͷ .JHSBUJPOϑΝΠϧ͕΄͍͠ w SJEHFQPMFHFNͰEVNQͯࠩ͠෼ΛͱΔ w 6TFST w 3FMBUJPOTIJQT w .JDSPQPTUT w ʜ w 6TFST w 3FMBUJPOTIJQT w 'FFET w ʜ 4FSWJDF 3FQPTJUPSZ 4FSWJDF 3FQPTJUPSZ

Slide 44

Slide 44 text

.JHSBUJPO # In Repository1 bundle exec ridgepole -c config/database.yml --export --split --output schemas/ Schemafile # In Repository2 bundle exec ridgepole -c config/database.yml --export --split --output schemas/ Schemafile # In Repository2 bundle exec ridgepole -c config/database.yml --diff schemas/users.schema ../ repository1/schemas/users.schema #add_column("users", "name", :string, {:after=>"id", :limit=>255}) # ALTER TABLE `users` ADD `name` varchar(255) AFTER `id`

Slide 45

Slide 45 text

αϙʔτͱ͸

Slide 46

Slide 46 text

3BJMTͷαϙʔτ

Slide 47

Slide 47 text

3BJMTͷαϙʔτ յΕͨ

Slide 48

Slide 48 text

3BJMTͷαϙʔτ ઌिؾ͕͍ͭͨ

Slide 49

Slide 49 text

3BJMTͷαϙʔτ ܧଓ৹ٞத

Slide 50

Slide 50 text

݁ߏલ͔Βͩͬͨɻɻɻ

Slide 51

Slide 51 text

·ͱΊ w 5FTUΛΑΜͰΈΔ w NBTUFSϒϥϯνΛ͞ΘͬͯΈΔ w 3BJMΛෑ͍ͯΈΔ φΠενϟϨϯδ

Slide 52

Slide 52 text

·ͱΊ

Slide 53

Slide 53 text

5IBOLZPV IUUQTXXXqJDLSDPNQIPUPTUBNCBLP