ActiveRecord 3.2 -> 4.1

Eec11c7d221770d15a4b16104f3cf07e?s=47 Kohei Suzuki
March 28, 2016
1.2k

ActiveRecord 3.2 -> 4.1

Eec11c7d221770d15a4b16104f3cf07e?s=128

Kohei Suzuki

March 28, 2016
Tweet

Transcript

  1. "DUJWF3FDPSE  

  2. ࣗݾ঺հ w ,PIFJ4V[VLJ !FBHMFUNU  w ΫοΫύουٕज़෦։ൃج൫άϧʔϓ w 3BJMTͷΞοϓάϨʔυΛ్த͔Β୲౰ w

    3BJMT΁ͷΞοϓάϨʔυ࣌ʹૺ۰ͨ͠໰୊ΫοΫύο υ։ൃऀϒϩάIUUQUFDIMJGFDPPLQBEDPNFOUSZ 
  3. Ξ΢τϥΠϯ w ϦϦʔε͢Δ·Ͱ w 453*$5@"--@5"#-&4 w 384QMJUUJOH w .POLFZ1BUDI w

    ϦϦʔεޙ 
  4. શମతͳํ਑ w ՄೳͳݶΓ3BJMTͱͷ྆ํͰಈ͘Α͏ʹ ͢Δ w ແཧͳ৔߹͸SBJMTϒϥϯνͰมߋΛՃ͍͑ͯ͘ w ՄೳͳݶΓSBJMTϒϥϯνͷมߋ͸গͳ͘͢Δ w ϩʔϧόοΫՄೳʹ͢Δ

    w ηογϣϯɺΩϟογϡɺʜʜ 
  5. λΠϜϥΠϯ w ࠷ॳ͸!B@NBUTVEB͞Μ͕࡞ۀ w ๻΋࡞ۀʹՃΘΔ w ςετ͕શ෦௨ΔΑ͏ʹͳΔ w εςʔδϯά΍ຊ൪Ͱಈ࡞֬ೝ w

    ෛՙςετ΍࢒ͬͨόά௵͠ w ॱ࣍ϦϦʔε 
  6. ϦϦʔε w DIBOHFEpMFTXJUI BEEJUJPOTBOE  EFMFUJPOT w Ұिؒͣͭॱ࣍ϦϦʔε w DPPLQBEDPN

    w ϞόΠϧΞϓϦ༻8FC"1* w ؅ཧը໘ɺόον
  7. 453*$5@"--@5"#-&4 

  8. .Z42-TRM@NPEF w .Z42-͕αϙʔτ͢Δ42-ߏจͱɺ.Z42-͕ ࣮ߦ͢ΔσʔλݕূʹӨڹ͢Δม਺ w σϑΥϧτͷTRM@NPEFͩͱɺҰ෦ͷόϦσʔ γϣϯΤϥʔ͕ΤϥʔͰ͸ͳ͘ܯࠂʹͳΔ 

  9. TRM@NPEF mysql> SHOW CREATE TABLE items \G *************************** 1. row

    *************************** Table: items Create Table: CREATE TABLE `items` ( `id` int(11) NOT NULL, `name` varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) 
  10. TRM@NPEF mysql> INSERT INTO items (name) VALUES ('foo'); Query OK,

    1 row affected, 1 warning (0.00 sec) mysql> SHOW WARNINGS; +---------+------+-----------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------+ | Warning | 1364 | Field 'id' doesn't have a default value | +---------+------+-----------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM items; +----+------+ | id | name | +----+------+ | 0 | foo | +----+------+ 1 row in set (0.00 sec) 
  11. TRM@NPEF mysql> INSERT INTO items (id, name) VALUES (1, 'I

    like very much'); Query OK, 1 row affected, 1 warning (0.00 sec) mysql> SHOW WARNINGS; +---------+------ +----------------------------------------------------------------------------+ | Level | Code | Message | +---------+------ +----------------------------------------------------------------------------+ | Warning | 1366 | Incorrect string value: '\xF0\x9F\x8D\x95 v...' for column 'name' at row 1 | +---------+------ +----------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM items; +----+---------+ | id | name | +----+---------+ | 1 | I like | +----+---------+ 1 row in set (0.00 sec) 
  12. 453*$5@"--@5"#-&4 w 3BJMT͔ΒউखʹSET @@SESSION.sql_mode = 'STRICT_ALL_TABLES'͕࣮ߦ͞ΕΔΑ͏ʹ • ಉ࣌ʹ strict: false

    ͱ͍͏Φϓγϣϯ΋௥Ճ͞ ΕͨͷͰɺͱΓ͋͑ͣ͜ΕΛ࢖͑͹3BJMTʹ্ ͛ΒΕΔ 
  13. 1BSUJBM*OTFSU w 3BJMT͔Βͷػೳ w */4&35͢Δͱ͖ʹɺσϑΥϧτ஋͔ΒมΘͬͨΧ ϥϜ͚ͩΛ*/4&35จʹೖΕΔ w IUUQTHJUIVCDPNSBJMTSBJMTDPNNJU F 

  14. 1BSUJBM*OTFSU w .Z42-TRM@NPEFͱ૊Έ߹Θ͞Δͱʜ  w 3BJMT w /6--͕ૠೖ͞ΕΑ͏ͱ͢ΔͷͰɺ/05/6-- ੍໿͕͋Ε͹Τϥʔ [1]

    pry(main)> Recipe.create (0.2ms) BEGIN SQL (0.4ms) INSERT INTO `recipes` (`title`, `user_id`) VALUES (NULL, NULL) (0.1ms) ROLLBACK ActiveRecord::StatementInvalid: Mysql2::Error: Column 'title' cannot be null: INSERT INTO `recipes` (`title`, `user_id`) VALUES (NULL, NULL) 
  15. 1BSUJBM*OTFSU w 3BJMT w ஋͕লུ͞ΕΔͷͰɺ/05/6--੍໿͕͋ͬͯ ΋উखʹσϑΥϧτ஋͕ૠೖ͞ΕΔ [1] pry(main)> Recipe.create (0.2ms)

    BEGIN SQL (0.3ms) INSERT INTO `recipes` VALUES () (0.3ms) COMMIT => #<Recipe id: 1, user_id: nil, title: nil> [2] pry(main)> Recipe.last Recipe Load (0.3ms) SELECT `recipes`.* FROM `recipes` ORDER BY `recipes`.`id` DESC LIMIT 1 => #<Recipe id: 1, user_id: 0, title: ""> 
  16. ΫοΫύουͰͷํ਑ w 3BJMTҠߦલʹ453*$5@"--@5"#-&4ʹ͢Δ w TRM@NPEF͸ηογϣϯຖʹઃఆͰ͖Δ w ঃʑʹ453*$5@"--@5"#-&4Λ༗ޮԽ 

  17. TUSJDUNPEF΁ͷҠߦ module SetStrictSqlMode def self.enable_strict_mode?; ...; end def self.included(base) if

    enable_strict_mode? base.class_eval do def configure_connection_with_sql_mode configure_connection_without_sql_mode execute("SET @@SESSION.sql_mode = 'STRICT_ALL_TABLES'", :skip_logging) end alias_method_chain :configure_connection, :sql_mode end end end end ActiveSupport.on_load(:active_record) do ActiveRecord::ConnectionAdapters::Mysql2Adapter.send(:include, SetStrictSqlMode) end 
  18. TUSJDUNPEF΁ͷҠߦ w ༗ޮԽͨ͠ঢ়ଶͰςετ͕௨ΔΑ͏ʹ͢Δ w UFTU EFWFMPQNFOU TUBHJOHͰ༗ޮԽ w ຊ൪ͷBQQαʔό୆Ͱ༗ޮԽ w

    ΤϥʔΛ؂ࢹͭͭ͠ɺগͣͭ͠ର৅αʔόΛ֦େ w िؒ΄Ͳ͔͚ͯɺશBQQαʔόͰ༗ޮԽ 
  19. 384QMJUUJOH 

  20. 384QMJUUJOH w BDUT@BT@SFBEPOMZBCMF MBTUVQEBUFɺ 3BJMT Λ౰࣌࢖͍ͬͯͨ w ౰વ3BJMTͰ΋ಈ͔ͳ͍ͷͰɺຐվ଄ࡁΈ w 3BJMTͰ΋Ͱ΋ಈ࡞͢Δ38TQMJUUJOHϥ

    ΠϒϥϦ͕ཉ͍͠ w IUUQTHJUIVCDPNFBHMFUNUTXJUDI@QPJOU 
  21. TXJUDI@QPJOU w Կ౓͔঺հͨ͜͠ͱ͋ΔͷͰলུ w IUUQFBHMFUNUIBUFCMPKQFOUSZ  w IUUQTTQFBLFSEFDLDPNFBHMFUNUGVTIV ECUPSBJMT 

  22. TXJUDI@QPJOU w "DUJWF3FDPSE΁ͷߋ৽࡞ۀதʹॻ͍ͨ w "DUJWF3FDPSEͷ಺෦ߏ଄ʹґଘͯ͠͸͍͚ͳ͍ͱ ͍͏ڧ͍ࢥ͍ w 3BJMT͔Β3BJMTCFUB·ͰͲͷόʔ δϣϯͰ΋ಈ͘ 

  23. .POLFZ1BUDI 

  24. .POLFZ1BUDI w "DUJWF3FDPSEͷ಺෦࣮૷͸ ͳ͔ͥ ඇৗʹมΘ Γ΍͍͢ w ৽چͷ࣮૷Λݟൺ΂ͳ͕Βஸೡʹ௚͍ͯ͘͠࡞ۀ 

  25. .POLFZ1BUDI w ϝιουͷγάωνϟ͕มΘͬͨɺΫϥε໊͕มΘͬ ͨɺ͸Θ͔Γ΍͍͢ w த్൒୺ʹ࣮૷Λίϐϖͭͭ͠׬શʹϝιουΛ্ ॻ͖͍ͯ͠Δέʔε͕Ұ൪͠ΜͲ͍ w Ͱ͖Δ͚ͩύονΛ֎͢ํ޲Ͱ͕Μ͹Δ

  26. .POLFZ1BUDI w ࠷ۙ͸ύονʹ৆ຯظݶΛઃఆ͢Δͷ͕ྲྀߦ

  27. ͦͷଞ w JNQMJDJUKPJOSFGFSFODF w JODMVEFT͚ͩͰ͸KPJO͞Εͳ͘ͳͬͨ w ͳΜ͔ؔ࿈ઌͰࢦఆ͍ͯ͠ΔTDPQF͕͖͍ͯͳ͍ w  ςʔϒϧ͘Β͍KPJO͢ΔͱͳΜ͔ΫΤϦ͕յ

    ΕΔ w TDPQFͷద༻ॱংͰͳ͔ͥมΘΔΫΤϦʜʜ
  28. ϦϦʔεޙ 

  29. Τϥʔ w ࣄલʹεςʔδϯάͰ৭ʑಈ࡞֬ೝͯ͠ɺຊ൪ͷҰ ෦ʹࠞͥͯΤϥʔ؂ࢹΛ͍ͯͯ͠΋ɺϦϦʔεޙʹ Τϥʔ͸ग़Δ w ࣗಈςετͰ΋खಈςετͰ΋ɺΧόʔ͖͠ΕΔن ໛Ͱ͸ͳ͍ w গͳ͘ͱ΋ΫοΫύου͸

  30. ࣮ࡍʹى͖ͨΤϥʔ w 453*$5@"--@5"#-&4ىҼ w varchar(255)ʹ௕͍จࣈྻΛૠೖ͠Α͏ͱͯ͠Τ ϥʔɺ౳ʑʜʜ w Τϥʔͱ͍ͬͯ΋ɺࠓ·Ͱͷόά͕ݟ͔ͭͬͨܗ w 453*$5@"--@5"#-&4͸ઌʹ༗ޮԽ͍ͯͨ͠ͷ

    ͰɺϦϦʔεͷॠؒʹ͜ΕΒͷΤϥʔ͕ൃੜͨ͠Θ ͚Ͱ͸ͳ͍ 
  31. εϩʔΫΤϦ w ςετɺεςʔδϯά౳Ͱ͸શ͘໰୊ແ͔͕ͬͨʜ w ຊ൪ͰϦϦʔεͯ͠͠͹Β͘ܦ͔ͬͯΒɺঃʑʹҰ ෦Ͱࠓ·ͰݟΒΕͳ͔ͬͨεϩʔΫΤϦ͕ൃੜ 

  32. 03%&3#:JE"4$ w 6TFSXIFSF  pSTUͷΑ͏ʹpSTUΛ࢖͏ͱɺউ खʹ03%&3#:JE"4$͕ͭ͘ w ͜Ε͕͚ͭͩ͘ͰҰ෦εϩʔΫΤϦԽ w ݟ͚ͭͨ΋ͷ͔ΒUBLF΍pOE@CZʹม͍͑ͯ͘

    ࡞ۀ 
  33. όον w 8FCΞϓϦͱҧͬͯɺόον͸࣮ߦස౓ʹΑͬͯ ͸मਖ਼ϛεͷൃݟ͕େ͖͘஗ΕΔ w ि࣍όονɺ݄࣍όον w "DUJWF3FDPSEΛۦ࢖ͯ͠ෳࡶͳΫΤϦΛ૊Έཱͯ ͍ͯΔέʔε΋͋Γɺमਖ਼͕೉͍͠ w

    ؅ཧը໘΋ࣅͨੑ࣭Λ࣋ͭ
  34. ·ͱΊ w "DUJWF3FDPSEͷͪΐͬͱͨ͠มߋͰ.Z42-ͷ ઃఆ·ͰมΘΔ w "DUJWF3FDPSEͷ಺෦࣮૷ʹґଘͯ͠͸͍͚ͳ͍ w ͋Δఔ౓ಈ࡞֬ೝΛͨ͠Βɺࢥ͍͖ͬͯϦϦʔε͠ ͯσόοά͢Δ͔͠ͳ͍