èªåçæãããDDLãsedã§èª¿æ´
macOS 10.12 Sierra, MariaDB 10.1, ERMaster 1.0.0.v20150619-0219
ERå³ãæ軽ã«æ¸ãã«ã¯Eclipseãã©ã°ã¤ã³ã®ERMasterãã¨ã£ã¦ã便å©ã
ããæ°å¹´ãæç¨ãã¦ããã
ã¨ããã§ãMariaDBã®ãã¼ãã«å®ç¾©ã¨ããã®ã¯ãä»ã®ä¸è¬çãªDBMSã¨æ¯ã¹ã¦ããªãç¹æ®ã§ãããç¹ã«ç§ããã¤ãæ°ã«ãããã¨ã2ã¤ããã1ã¤ã¯VARCHARåãããã©ã«ãã§ignore caseï¼å¤§æåå°æååºå¥ç¡ãï¼ãªã®ã¨ããã1ã¤ã¯TIMESTAMPåã2ã¤ä»¥ä¸ããæã«1ã¤ã®åã«ããDEFAULT CURRENT_TIMESTAMPãæå®ã§ããªããã¨ã ã
ç§ã®å ´åã¯ãvarcharåã¯å ¨ã¦BINARYã«ãã¦ãã¾ãããããè¯ããã¯ãã¦ãããsedã§å¤æããã«ã¯ä¾ãã°ä»¥ä¸ã®ããã«ããã
sed -E '/BINARY/!s/(varchar\([0-9]+\))/\1 BINARY/g' create_table.sql
ãã1ã¤ã®TIMESTAMPåé¡ã«ã¤ãã¦ã¯ããã¨ãã°ã以ä¸ã®ãããªè¦ä»¶ãä»®å®ããã
- å ¨ãã¼ãã«ã«çææ¥æã¨æ´æ°æ¥æãæããã
- çææ¥æã create_ts ã¨ãã
- æ´æ°æ¥æã update_ts ã¨ãã
- create_ts 㯠default current_timestamp ã«ããã
- update_ts 㯠default current_timestamp ã«ãã¦ããã¤ãon update current_timestamp ã«ããã
ããã¯MariaDBã®ä»æ§ä¸ã§ããªãã®ã§ãcreate_ts ã®æ¹ã¯ default 0 ã«ãããããã¦ãupdate_ts 㯠default current_timestamp ã«ãã¦ããã
ERMasterã§DDLãèªåçæããã¨ã
CREATE TABLE some_table ( -- create_ts timestamp DEFAULT 0 NOT NULL, update_ts timestamp DEFAULT current_timestamp NOT NULL );
ãããªãããã ã
create_ts ã«ã¤ãã¦ã¯ãINSERT æã« NULL ãæå®ãããã¨ã«ãã£ã¦ãç¾å¨æ¥æãç»é²ããããã«ããã((ããã§ãinsert into some_table(create_ts) values(current_timestamp);
ã¨ãã¦ãã¾ãã¨ãtimestamp(3) ã®ããã«ããªç§ã¾ã§ä¿æããã¦ããå ´åã«ãcreate_ts 㨠update_ts ã®å¤ãå¾®å¦ã«ããã))
insert into some_table(create_ts) values(null);
update_ts ã® ON UPDATEã«ã¤ãã¦ã¯ãsed ã§ã¤ã足ãã¦ãã¾ããã
sed -E '/ON UPDATE/!s/(update_ts timestamp DEFAULT current_timestamp NOT NULL)/\1 ON UPDATE current_timestamp/g' create_table.sql
ãã¼ã
ããã¾ãã¹ãã¼ããããªãæ°ããããã©ããã¾ãã1åãã¤æã§ç´ãããã¯é¥ãã«ãã·ã ããã¦ã
#!/usr/bin/env bash # Mac OS X (using BSD sed) # Script to adjust DDL made by ERMaster for MariaDB sed -i .bak1 -E '/ON UPDATE/!s/(update_ts timestamp DEFAULT current_timestamp NOT NULL)/\1 ON UPDATE current_timestamp/g' create_table.sql sed -i .bak2 -E '/BINARY/!s/(varchar\([0-9]+\))/\1 BINARY/g' create_table.sql rm create_table.sql.bak1 rm create_table.sql.bak2
ãã¤ã³ã1. BSD sed ã§ã¯ ignore case ã§ããªã
ãªã®ã§ãs/target/replace/gi
ã¨æ¸ãããã¦ãæ¸ããªãã
ãã¤ã³ã2. ã³ãã³ãã®å¼ç¨ç¬¦ã¯ããã«ã¯ã©ã¼ãã§ãªãã·ã³ã°ã«ã¯ã©ã¼ãã§
ãããªãã¨ã!s
ã® ! ããBASHã³ãã³ãã¨ãã¦æ©è½ãã¦ãã¾ã£ã¦ãã¾ãåããªãã
ãã¤ã³ã3. -i ã®å¾ãã«ããã¯ã¢ãããã¡ã¤ã«ã®ãµãã£ãã¯ã¹ãã¤ãã
åç´ã«ä¸æ¸ããããã ããªã®ã«ãããã¯ã¢ãããä½ãããã®ã§ãå¾ã§æ¶ãã
ãã¤ã³ã4. -E ãã¤ããã°æ£è¦è¡¨ç¾ã®ã¨ã¹ã±ã¼ãã¯åºæ¬ãããªã
\(
ã¨ã \+
ã¨ãã¯æåãã®ãã®ã表ãã
-e
ã®æã¨éã
ã¡ãã£ã¨ãããã¨ã ããã©ãããªãã ããããæããããããã£ããªãï¼ã³ããï¼