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

The underground in ActiveRecord::ConnectionAdapters

The underground in ActiveRecord::ConnectionAdapters

沖縄Ruby会議02
http://ruby.okinawa/okrk02/

Koichi ITO

March 10, 2018
Tweet

More Decks by Koichi ITO

Other Decks in Programming

Transcript

  1. "DUJWF3FDPSE
    $POOFDUJPO"EBQUFST
    ͷԼͷੈ
    ք
    ླྀٿେֶ޻ֶ෦߸ؗ
    4BU

    ԭೄ3VCZձٞ
    5IFVOEFSHSPVOEJO"3$POOFDUJPO"EBQUFST
    %JWFUPUIFSBJMTSBJMTSFQPTJUPSZ
    ג
    Ӭ࿨γεςϜϚωδϝϯτ
    ҏ౻ߒҰ !LPJD

    View full-size slide

  2. w %JWFUPSBJMTSBJMTSFQP
    w "3$POOFDUJPO"EBQUFST
    w 0SBDMFFOIBODFEBEBQUFS
    w 0SBDMFXJUI3BJMT
    "HFOEB
    ˞#BTFEPO3BJMTSD

    View full-size slide

  3. Dive to rails/rails

    View full-size slide

  4. AR::ConectionAdapters

    View full-size slide

  5. w "3ͷϞσϧܦ༝Ͱͷݺͼग़͠ʹର͠
    ͯɺ3%#.4͝ͱʹཧʹ͔ͳͬͨ42-
    Λ૊Έཱͯͯฦ͢໾ׂ
    w 3%#.4ͷ࢓༷Λલఏ͢Δɻྫ͑͹
    0SBDMFͩͱۭจࣈͱ/6--Λ۠ผ͠
    ͳ͍ɺHἬ͸ࣝผࢠ͕จࣈҎԼ
    "3$POOFDUJPO"EBQUFST

    View full-size slide

  6. SBJMTSBJMTͷςετͰ஌Δ3%#.4ͷ
    ࢓༷ʹΑΔৼΔ෣͍ͷҧ͍
    DVSSFOU@BEBQUFS

    View full-size slide

  7. rails/rails#29370

    View full-size slide

  8. Ξμϓλͷࢦఆͱ
    3BJMTΛىಈ࣌ͷ
    ৼΔ෣͍ʹ͍ͭͯ

    View full-size slide

  9. w ͝ଘ஌EBUBCBTFZNMʹࢦఆ͢Δ
    BEBQUFSͱ͸Կͳͷ͔͍·Ұ౓
    $POOFDUJPO"EBQUFSͷબ୒
    development:
    adapter: mysql2
    development:
    adapter: postgresql
    development:
    adapter: sqlite3
    development:
    adapter: oracle_enhanced

    View full-size slide

  10. w EBUBCBTFZNMͰࢦఆͨ͠Ξμϓλ͕
    SBJMTDSBJMTTͰཁٻ͞ΕΔἬͷྲྀΕ
    "33BJMUJFJOJUJBMJ[FS
    "3$POOFDUJPO)BOEMJOHFTUBCMJTI@DPOOFDUJ
    PO
    "3$POOFDUJPO"EBQUFST$POOFDUJPO4QFDJpD
    BUJPOTQFD
    FTUBCMJTI@DPOOFDUJPO

    View full-size slide

  11. "33BJMUJFJOJUJBMJ[FS
    IUUQTHJUIVCDPNSBJMTSBJMTCMPCWSDBDUJWFSFDPSEMJCBDUJWF@SFDPSESBJMUJFSC-

    View full-size slide

  12. "3$POOFDUJPO)BOEMJOH

    FTUBCMJTI@DPOOFDUJPO
    IUUQTHJUIVCDPNSBJMTSBJMTCMPCWSDBDUJWFSFDPSEMJCBDUJWF@SFDPSEDPOOFDUJPO@IBOEMJOHSC-

    View full-size slide

  13. "3$POOFDUJPO"EBQUFST

    $POOFDUJPO4QFDJpDBUJPOTQFD
    IUUQTHJUIVCDPNSBJMTSBJMTCMPCWSDBDUJWFSFDPSEMJCBDUJWF@SFDPSEDPOOFDUJPO@BEBQUFSTDPOOFDUJPO@TQFDJpDBUJPOSC-

    View full-size slide

  14. w EBUBCBTFZNM΁ͷࢦఆBEBQUFS
    ΛݩʹΞμϓλΛSFRVJSF͢Δ
    "3$POOFDUJPO"EBQUFST

    $POOFDUJPO4QFDJpDBUJPOTQFD
    path_to_adapter =
    "active_record/connection_adapters/#{spec[:adapter]}_adapter"
    begin
    require path_to_adapter
    rescue LoadError => e
    (snip)
    end
    (snip)
    adapter_method = "#{spec[:adapter]}_connection"
    IUUQTHJUIVCDPNSBJMTSBJMTCMPCWSDBDUJWFSFDPSEMJCBDUJWF@SFDPSEDPOOFDUJPO@BEBQUFSTDPOOFDUJPO@TQFDJpDBUJPOSC--
    SFRVJSF

    View full-size slide

  15. SFRVJSF͞ΕΔ$PSFBEBQUFST

    View full-size slide

  16. w EBUBCBTFZNM΁ͷࢦఆBEBQUFS
    ΛݩʹίωΫγϣϯϝιου໊Λܾఆ
    "3$POOFDUJPO"EBQUFST

    $POOFDUJPO4QFDJpDBUJPOTQFD
    path_to_adapter =
    "active_record/connection_adapters/#{spec[:adapter]}_adapter"
    begin
    require path_to_adapter
    rescue LoadError => e
    (snip)
    end
    (snip)
    adapter_method = "#{spec[:adapter]}_connection"
    IUUQTHJUIVCDPNSBJMTSBJMTCMPCWSDBDUJWFSFDPSEMJCBDUJWF@SFDPSEDPOOFDUJPO@BEBQUFSTDPOOFDUJPO@TQFDJpDBUJPOSC--
    @DPOOFDUJPO

    View full-size slide

  17. IUUQTHJUIVCDPNSBJMTSBJMTCMPCWSDBDUJWFSFDPSEMJCBDUJWF@SFDPSEDPOOFDUJPO@BEBQUFSTNZTRM@BEBQUFSSC-
    @DPOOFDUJPO

    View full-size slide

  18. IUUQTHJUIVCDPNSBJMTSBJMTCMPCWSDBDUJWFSFDPSEMJCBDUJWF@SFDPSEDPOOFDUJPO@BEBQUFSTNZTRM@BEBQUFSSC-
    @DPOOFDUJPO
    %#$MJFOU

    View full-size slide

  19. w 3VCZΦϒδΣΫτܦ༝Ͱ.Z42-Λૢ࡞͢Δ
    ΫϥΠΞϯτͱͳΔυϥΠό(FN
    require "mysql2"
    client = Mysql2::Client.new(
    :host => "localhost", :username => "root"
    )
    results = client.query(
    "SELECT * FROM users WHERE group=‘githubbers'"
    )
    results.each do |row|
    puts row["id"] # row[“id”].is_a? Integer
    if row["dne"] # non-existant hash entry is nil
    puts row["dne"]
    end
    end
    .ZTRMHFN
    IUUQTHJUIVCDPNCSJBONBSJPNZTRMVTBHF

    View full-size slide

  20. "3ϞσϧͱυϥΠόͷதؒ૚
    $POOFDUJPO"EBQUFST
    "CTUSBDU"EBQUFS
    $POOFDUJPO
    "EBQUFST-BZFS
    "3.PEFM
    -BZFS
    %BUBCBTF
    $MJFOU-BZFS
    ˞ਤྫ͸.Z42-Ξμϓλ
    (FNpMFʹࢦఆ
    ͍ͯ͠Δͷ͸͜Ε
    $POOFDUJPO"EBQUFST
    "CTUSBDU.ZTRM"EBQUFS
    $POOFDUJPO"EBQUFST
    .ZTRM"EBQUFS
    HFN
    NZTRM
    .Z42-
    %BUBCBTF
    -BZFS
    "3

    View full-size slide

  21. "3ϞσϧͱυϥΠόͷதؒ૚
    $POOFDUJPO
    "EBQUFST-BZFS
    "3.PEFM
    -BZFS
    %BUBCBTF
    $MJFOU-BZFS
    ˞ਤྫ͸1PTUHSF42-Ξμϓλ
    (FNpMFʹࢦఆ
    ͍ͯ͠Δͷ͸͜Ε
    $POOFDUJPO"EBQUFST
    1PTUHSF42-"EBQUFS
    HFN
    QH
    1PTUHSF42-
    %BUBCBTF
    -BZFS
    $POOFDUJPO"EBQUFST
    "CTUSBDU"EBQUFS
    "3

    View full-size slide

  22. "3ϞσϧͱυϥΠόͷதؒ૚
    $POOFDUJPO
    "EBQUFST-BZFS
    "3.PEFM
    -BZFS
    %BUBCBTF
    $MJFOU-BZFS
    ˞ਤྫ͸42-JUFΞμϓλ
    (FNpMFʹࢦఆ
    ͍ͯ͠Δͷ͸͜Ε
    $POOFDUJPO"EBQUFST
    42-JUF"EBQUFS
    HFN
    TRMJUF
    42-JUF
    %BUBCBTF
    -BZFS
    $POOFDUJPO"EBQUFST
    "CTUSBDU"EBQUFS
    "3

    View full-size slide

  23. w EBUBCBTFZNMʹج͍ͮͨ
    $POOFDUJPO"EBQUFSTΛ࢖͏
    w $POOFDUJPO"EBQUFSͷઌͷ઀ଓ
    ͸σʔλϕʔευϥΠόHFNͱͳΔ
    w NZTRM QH TRMJUFͳͲ
    "3ͷσʔλϕʔε઀ଓͷߏ଄

    View full-size slide

  24. w SBJMTSBJMTϦϙδτϦͰϝϯςφ
    ϯε͞Ε͍ͯΔΞμϓλΛࢦ͢
    w ݱࡏ.Z42- NZTRM

    1PTUHSF42-42-JUFͷͭ
    w 3BJMTͷ$*ϚτϦοΫεʹೖͬͯΔ
    $PSFBEBQUFST

    View full-size slide

  25. w ֎෦(FNͱͯ͠ཁٻ͞ΕΔΞμϓλ
    w 0SBDMF
    w 42-4FSWFS
    w 'JSFCJSE
    w FUD FUD
    SEQBSUZBEBQUFST

    View full-size slide

  26. w 3BJMTຊମʹόϯυϧ͞Ε͍ͯΔίΞ
    ΞμϓλҎ֎΋͜͜ͰSFRVJSFͯ͠Δ
    "3$POOFDUJPO"EBQUFST

    $POOFDUJPO4QFDJpDBUJPOTQFD
    path_to_adapter =
    "active_record/connection_adapters/#{spec[:adapter]}_adapter"
    begin
    require path_to_adapter
    rescue LoadError => e
    (snip)
    end
    (snip)
    adapter_method = "#{spec[:adapter]}_connection"
    IUUQTHJUIVCDPNSBJMTSBJMTCMPCWSDBDUJWFSFDPSEMJCBDUJWF@SFDPSEDPOOFDUJPO@BEBQUFSTDPOOFDUJPO@TQFDJpDBUJPOSC--
    SFRVJSF
    ࠶ܝ

    View full-size slide

  27. Oracle enhanced adapter

    View full-size slide

  28. ೥Ҏ߱ͷ։ൃऀ

    View full-size slide

  29. w SBJMTSBJMTʹଘࡏ͍ͯͨ͠
    0SBDMF"EBQUFSͷ֦ு(FN
    w &EHF3BJMTʹ௥ਵ͍ͯ͠Δ
    w +%#$Λ࢖ͬͨ઀ଓΛαϙʔτͯ͠Δ
    w ͭ·Γ+3VCZαϙʔτΛ͍ͯ͠Δ
    0SBDMFFOIBODFEBEBQUFS

    View full-size slide

  30. w ੜ੒͢Δ$POOFDUJPOΛৼΓ෼͚͍ͯΔ
    0SBDMF&OIBODFE$POOFDUJPO
    IUUQTHJUIVCDPNSTJNPSBDMFFOIBODFECMPCWSDMJCBDUJWF@SFDPSEDPOOFDUJPO@BEBQUFSTPSBDMF@FOIBODFEDPOOFDUJPOSC

    View full-size slide

  31. w ৼΓ෼͚ͷλω͸36#:@&/(*/&
    03"$-&@&/)"/$&%@$0//&$5*0/
    IUUQTHJUIVCDPNSTJNPSBDMFFOIBODFECMPCWSDMJCBDUJWF@SFDPSEDPOOFDUJPO@BEBQUFSTPSBDMF@FOIBODFEDPOOFDUJPOSC

    View full-size slide

  32. 0$*$POOFDUJPO .3*

    $POOFDUJPO
    "EBQUFST-BZFS
    "3.PEFM
    -BZFS
    %BUBCBTF
    $MJFOU-BZFS
    ˞ਤྫ͸0SBDMFFOIBODFE
    0SBDMF&OIBODFE
    0$*$POOFDJUPO
    Ͱܨ͍͛ͯΔ
    $POOFDUJPO"EBQUFST
    0SBDMF&OIBODFE"EBQUFS
    0SBDMF
    %BUBCBTF
    -BZFS
    (FNpMFʹࢦఆ
    ͍ͯ͠Δͷ͸͜Ε
    HFN
    SVCZPDJ
    $POOFDUJPO"EBQUFST
    "CTUSBDU"EBQUFS
    "3

    View full-size slide

  33. +%#$$POOFDUJPO +3VCZ

    $POOFDUJPO
    "EBQUFST-BZFS
    "3.PEFM
    -BZFS
    %BUBCBTF
    $MJFOU-BZFS
    ˞ਤྫ͸0SBDMFFOIBODFE
    0SBDMF&OIBODFE
    +%#$$POOFDJUPO
    Ͱܨ͍͛ͯΔ
    $POOFDUJPO"EBQUFST
    0SBDMF&OIBODFE"EBQUFS
    0SBDMF
    %BUBCBTF
    -BZFS
    $-"441"5)ʹ
    +"3Λࢦఆ
    KBS
    PKECDKBS
    $POOFDUJPO"EBQUFST
    "CTUSBDU"EBQUFS
    "3

    View full-size slide

  34. w KSVCZBDUJWFSFDPSEKECDBEBQUFS
    Ͱ؅ཧ͞Ε͍ͯΔ
    <ิ଍>0SBDMFҎ֎ͷ+%#$αϙʔτ

    View full-size slide

  35. A development of
    Oracle enhanced adapter

    View full-size slide

  36. w */%&9ʹؔ͢Δ5"#-&41"$&Λࢦఆ
    Ͱ͖ΔΑ͏ʹରԠͨ͠<>
    w SBJMTECTDIFNBMPBEͷϚϧνΠϯ
    αʔτʹରԠͨ͠<>
    w 3VCZ0$*Λ3VCZͷ
    #JH%FDJNBMରԠ
    ࢥ͍ग़ͷύονબ

    View full-size slide

  37. w */%&9ʹؔ͢Δ5"#-&41"$&Λࢦఆ
    Ͱ͖ΔΑ͏ʹରԠͨ͠<>
    w SBJMTECTDIFNBMPBEͷϚϧνΠϯ
    αʔτʹରԠͨ͠<>
    w 3VCZ0$*Λ3VCZͷ
    #JH%FDJNBMରԠ
    ͸͡ΊͯͷΞμϓλύον

    View full-size slide

  38. rsim/oracle-enhanced#1028

    View full-size slide

  39. w 0SBDMFͰ͸5"#-&41"$&Ͱςʔϒϧ
    ΍ΠϯσοΫεͷอଘྖҬࢦఆ͕Ͱ͖Δ
    w 0SBDMFFOBIBODFEBEBQUFSͰ͸
    EFGBVMU@UBCMFTQBDFTͰࢦఆՄೳ
    w QSJNBSZLFZʹରͯ͠͏·͘ޮ͔ͳ
    ͔ͬͨͷͰ௚ͨ͠
    5"#-&41"$&ͷࢦఆ

    View full-size slide

  40. w ౰࣌ࣗ৴͕ͳ͘*TTVFͰ͓࢕͍
    खݩʹϞϯΩʔύον͸͋ͬͨ

    View full-size slide

  41. w 54@*/%&9Ͱͳ͘54@%"5"ʹೖͬͯͨ
    ໰୊΁ͷमਖ਼ύον
    0QFOB13

    View full-size slide

  42. "3.JHSBUJPOͷDSBFUF@UBCMF΁ͷ
    SEQBSUZΞμϓλ࣮૷΁ͷมߋ
    தུ
    ௥Ճ
    IUUQTHJUIVCDPNSTJNPSBDMFFOIBODFECMPCWSDMJCBDUJWF@SFDPSEDPOOFDUJPO@BEBQUFSTPSBDMF@FOIBODFETDIFNB@TUBUFNFOUTSC-

    View full-size slide

  43. w "-5&3*/%&93*-%5"#-&41"$&Λ
    ࣮ߦ͢ΔϝιουΛఆ͍ٛͯ͠Δ
    ADSFBUF@UBCMFA͔ΒͷϑοΫ

    View full-size slide

  44. w ϦϙδτϦΛԣஅͨ͠มߋ͕ඞཁͩͬͨ
    $*͕ͱ͓Βͳ͍

    View full-size slide

  45. w 64&34Ͱͳ͘4:45&.ʹอଘ͞Εͯͨ
    w %&'"6-55"#-&41"$&ͷมߋ
    w 0SBDMF޲͚ͷSBJMTEFWCPY΁ͷ13
    ZBIPOEBSBJMTEFWCPY

    View full-size slide

  46. w 3%#.4΁ͷΞμϓλ։ൃͳͷͰ
    3%#.4ʹԠͨ͡։ൃ؀ڥΛ੔උ͢Δ
    ඞཁ͕͋Δ
    w ZBIPOEBSBJMTEFWCPY͕
    0SBDMF޲͚ͷ։ൃ؀ڥͱͳΔ
    w $*ͱ߹Θͤͯ͜ͷ؀ڥΛ࢖͏ͷ͕ྑ͍
    ։ൃ؀ڥ΁ͷ஌ݟΛಘΔ

    View full-size slide

  47. Oracle with Rails 6.0

    View full-size slide

  48. w 3BJMTҎલ͸ϓϥΠϚϦΩʔ*%ͷ
    ࠾൪ʹ͍ͭͯ4&26&/$&Ͱ෷͍ग़͞
    Ε͍ͯΔ
    w 3BJMTҎ߱͸0SBDMFDͷ
    *EFOUJUZ$PMVNOΛ࢖͏ํ਑Ͱݕ౼
    w OFYUWBMͱ͍͏1P$ͷϦϒʔτ
    1P$*EFOUJUZ

    View full-size slide

  49. w 3BJMTҎલͷσϑΥϧτ஋͸
    EFGBVMU@TFRVFODF@TUBSU@WBMVF

    w 3BJMTҎޙͷσϑΥϧτ஋͸
    EFGBVMU@TFRVFODF@TUBSU@WBMVF
    ʹͳΓଞͷΞμϓλͱಉ͡ڍಈʹ
    $IBOHFAEFGBVMU@TFRVFODF@TUBSU@WBMVFA
    GSPNUP

    View full-size slide

  50. w SBJMTECTUSVDUVSFEVNQͷࡍ
    ʹσϑΥϧτͰ7*&8͕μϯϓ͞Ε
    ΔΑ͏ʹͳΔ
    &YUSBDUWJFXTUPEC
    TUSVDUVSFTRMCZEFGBVMU

    View full-size slide

  51. w 0SBDMF͸%"5"#"4&-*/,ͱ͍͏
    ଞͷ%#ͷσʔλΛҾ͘ػߏ͕͋Γ
    3BJMTܥ0SBDMFΞμϓλἬαϙʔτ
    w 3BJMTҎޙ͸3BJMTͷෳ਺%#ର
    Ԡʹ೚ͤΔ͜ͱʹͳΓػೳ࡟আ͢Δํ
    ਑ SBJMTSBJMT΋ࢀর

    %SPQEBUBCBTFMJOLTVQQPSU

    View full-size slide

  52. %SPQEBUBCBTFMJOLTVQQPSU

    View full-size slide

  53. w "3#BTFDPOOFDUJPOUBCMFTͷ
    ໭Γ஋ʹؚΊͯΔϚςϏϡʔΛ֎͠ςʔ
    ϒϧͷΈฦ͢Α͏ʹ͢Δ͔Ͳ͏͔ʁ
    w 1PTUHSF42-Ξμϓλʹ͓͚Δಉϝ
    ιου͕ϚςϏϡʔΛฦ͔͢Ͳ͏͔ʹ
    ΑΔ (JU)VCίϝϯτௐ΂

    &YDMVEFANBUFSJBMJ[FE@WJFXTA
    GSPNAUBCMFTA

    View full-size slide

  54. w 0SBDMFD3ҎԼͰ͸ΧϥϜ໊ͳ
    ͲࣝผࢠʹόΠτҎԼͷ੍ݶ͕͋Δ
    w SBJMTSBJMTͷ"3ͷҰ෦ͷςετ͕
    0SBDMFͰམͪΔ͍ͪཁҼͰ΋͋Δ
    w 0SBDMFD3Ҏ্͸όΠτ
    ·Ͱͷ֦ு͞ΕΔ͚ͲͲ͏͢Δʁ
    -POHJEFOUJpFSTTVQQPSU

    View full-size slide

  55. +3VCZ+%#$TVQQPSUT0SBDMFD
    PSIJHIFS1%# 1MVHHBCMFEBUBCBTF

    View full-size slide

  56. Oracle enhanced 6.0
    may require Oracle
    12c or higher

    View full-size slide

  57. w ਓྨͰ΋ݟ͍ͯΔਓ͕ݶΒΕ͍ͯΔͱ͍͏
    $POOFDUJPO"EBQUFSTͷ໾ׂͱߏ੒ͱ
    ϦϙδτϦͰͷ৔ॴʹ͍ͭͯ࿩͠·ͨ͠
    w "SFMपล͸೉͍͠ͷͰׂѪ͍ͯ͠·͕͢ɺ
    ͜ͷ͋ͨΓ৮ΕΔͱ͔ͳΓڧ͍ͱࢥ͍·͢
    w LBNJQP͞ΜͱZBIPOEB͞Μ͸͍͢͝ਓ
    ͓ΘΓʹ

    View full-size slide