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

ActiveRecord 3.2 -> 4.1

Kohei Suzuki
March 28, 2016
1.7k

ActiveRecord 3.2 -> 4.1

Kohei Suzuki

March 28, 2016
Tweet

Transcript

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

    3BJMT΁ͷΞοϓάϨʔυ࣌ʹૺ۰ͨ͠໰୊ΫοΫύο υ։ൃऀϒϩάIUUQUFDIMJGFDPPLQBEDPNFOUSZ 
  2. 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) 
  3. 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) 
  4. 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) 
  5. 453*$5@"--@5"#-&4 w 3BJMT͔ΒউखʹSET @@SESSION.sql_mode = 'STRICT_ALL_TABLES'͕࣮ߦ͞ΕΔΑ͏ʹ • ಉ࣌ʹ strict: false

    ͱ͍͏Φϓγϣϯ΋௥Ճ͞ ΕͨͷͰɺͱΓ͋͑ͣ͜ΕΛ࢖͑͹3BJMTʹ্ ͛ΒΕΔ 
  6. 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) 
  7. 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: ""> 
  8. 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 
  9. TUSJDUNPEF΁ͷҠߦ w ༗ޮԽͨ͠ঢ়ଶͰςετ͕௨ΔΑ͏ʹ͢Δ w UFTU EFWFMPQNFOU TUBHJOHͰ༗ޮԽ w ຊ൪ͷBQQαʔό୆Ͱ༗ޮԽ w

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