ç§ã®è¬æŒã§ã¯ã移è¡ã管çããããã®å®çžŸã®ããããŒã«ã§ããAlembicã䜿çšããçµéšãå ±æããŸããã Alembicãéžæããçç±ãããã䜿çšããŠç§»è¡ãæºåããæ¹æ³ãããããïŒèªåãŸãã¯æåã§ïŒå®è¡ããæ¹æ³ãäžå¯éçãªå€æŽã®åé¡ã解決ããæ¹æ³ã移è¡ããã¹ãããçç±ããã¹ãã§æããã«ã§ããåé¡ãããã³ããããå®è£ ããæ¹æ³-ç§ã¯ããããã¹ãŠã®è³ªåã«çããããšããŸãããåæã«ãAlembicã§ã®ç§»è¡äœæ¥ãç°¡åã§æ¥œãããã®ã«ããããã€ãã®ã©ã€ãããã¯ãå ±æããŸããã
ã¬ããŒãã®æ¥ä»¥éãGitHubã®ã³ãŒãããããã«æŽæ°ãããããã«å€ãã®äŸããããŸããã¹ã©ã€ãã«è¡šç€ºãããŠãããšããã«ã³ãŒãã確èªãããå Žåã¯ããã®æç¹ããã®ã³ããããžã®ãªã³ã¯ã次ã«ç€ºããŸãã
- ããã«ã¡ã¯ïŒç§ã®ååã¯ã¢ã¬ã¯ãµã³ããŒã§ãããšããã£ã«ã§åããŠããŸããä»æ¥ã¯ãç§ãã¡ãã©ã®ããã«ç§»è¡ãšå ±åããã©ã®ããã«ç§»è¡ãšå ±åã§ããããã話ãããããšæããŸããããããããã¯ããªããããç°¡åã«æ®ããã®ã«åœ¹ç«ã€ã§ãããã
移è¡ãšã¯äœã§ããïŒ
å§ããåã«ãäžè¬çãªç§»è¡ã«ã€ããŠèª¬æãã䟡å€ããããŸããããšãã°ãã¢ããªã±ãŒã·ã§ã³ãããããããæ©èœããããã«ããã€ãã®ã¿ãã¬ãããäœæãããããã«ã¢ã¯ã»ã¹ããŸãã次ã«ãäœããå€æŽãããæ°ããããŒãžã§ã³ãããŒã«ã¢ãŠãããŸããæåã®ãã¬ãŒããå€æŽããã2çªç®ã®ãã¬ãŒãã¯å€æŽãããŠãããã3çªç®ã®ãã¬ãŒãã¯ä»¥åã¯ãããŸããã§ãããã衚瀺ãããŸããã
次ã«ãã¢ããªã±ãŒã·ã§ã³ã®æ°ããããŒãžã§ã³ã衚瀺ãããäžéšã®ãã¬ãŒããåé€ãããæ®ãã®ãã¬ãŒãã«ã¯äœãèµ·ãããŸãããããã¯äœã§ããïŒããã移è¡ã§è¡šçŸã§ããç¶æ ãšèšããŸããããç¶æ ããå¥ã®ç¶æ ã«ç§»è¡ãããšããããã¯ã¢ããã°ã¬ãŒãã§ãããæ»ããããšãã¯ããŠã³ã°ã¬ãŒãã§ãã
移è¡ãšã¯äœã§ããïŒ
äžæ¹ã§ã¯ãããã¯ããŒã¿ããŒã¹ã®ç¶æ ãå€æŽããã³ãŒãã§ããäžæ¹ãããã¯ç§ãã¡ãéå§ããããã»ã¹ã§ãã
移è¡ã«ã¯ã©ã®ãããªããããã£ãå¿ èŠã§ããïŒã¢ããªã±ãŒã·ã§ã³ã®ããŒãžã§ã³ã§åãæ¿ããç¶æ ãã¢ãããã¯ã§ããããšãéèŠã§ããããšãã°ã2ã€ã®ããŒãã«ãå¿ èŠãªã®ã«ã1ã€ãã衚瀺ãããªãå Žåãããã¯æ¬çªç°å¢ã§ããŸãè¯ãçµæããããããªãå¯èœæ§ããããŸãã
å€æŽãããŒã«ããã¯ã§ããããšãéèŠã§ããæ°ããããŒãžã§ã³ãããŒã«ã¢ãŠãããå Žåãããã¯é¢éžãããããŒã«ããã¯ã§ãããéåžžããã¹ãŠãã²ã©ãçµäºããŸãã
ãŸããããŒãžã§ã³ãé çªã«äžŠã¹ãŠãããŒã«ããæ¹æ³ãé£éãããããšãéèŠã§ãã
ããŒã«
ãããã®ç§»è¡ãã©ã®ããã«å®è£ ã§ããŸããïŒ
é ã«æµ®ãã¶æåã®ã¢ã€ãã¢ïŒããŠã移è¡ã¯SQLã§ããã¯ãšãªã䜿çšããŠSQLãã¡ã€ã«ãäœæããŠã¿ãŸããããç§ãã¡ã®ç掻ã楜ã«ããããšãã§ããããã€ãã®ã¢ãžã¥ãŒã«ããããŸãã
å éšã§äœãèµ·ãã£ãŠããããèŠããšã確ãã«ããã€ãã®èŠæ±ããããŸããããã¯CREATETABLEãALTERããã®ä»äœã§ãããŸããŸããã downgrade_v1.sqlãã¡ã€ã«ã§ã¯ããã¹ãŠãã£ã³ã»ã«ããŸãã
ãªãããªãã¯ããããã¹ãã§ã¯ãªãã®ã§ããïŒäž»ã«ããªããããªãã®æã§ãããããå¿ èŠãããããã§ãã beginãèšè¿°ããŠãããå€æŽãã³ãããããããšãå¿ããªãã§ãã ãããã³ãŒããæžããšãã¯ããã¹ãŠã®äŸåé¢ä¿ãšäœãã©ã®é åºã§è¡ãããèŠããŠããå¿ èŠããããŸããããã¯ããªãæ¥åžžçã§ãå°é£ã§æéã®ãããä»äºã§ãã
誀ã£ãŠééã£ããã¡ã€ã«ãèµ·åããããšã«å¯Ÿããä¿è·ã¯ãããŸããããã¹ãŠã®ãã¡ã€ã«ãæåã§å®è¡ããå¿ èŠããããŸãã 15åã®ç§»è¡ãããå Žåãããã¯ç°¡åã§ã¯ãããŸãããããã€ãã®psqlã15ååŒã³åºãå¿ èŠããããŸãããããã»ã©ã¯ãŒã«ã§ã¯ãããŸããã
æãéèŠãªããšã¯ãããŒã¿ããŒã¹ãã©ã®ãããªç¶æ ã«ããããããããªãããšã§ããããŠã³ããŒããããã¡ã€ã«ãšããŠã³ããŒãããªãã£ããã¡ã€ã«ãã©ããã«ïŒçŽã«ãã©ããã«ïŒæžãçããå¿ èŠããããŸããããŸãè¯ããªãã§ãããyoyo-migrations
ã¢ãžã¥ãŒã«ããããŸããæãäžè¬çãªããŒã¿ããŒã¹ããµããŒãããçââã®ã¯ãšãªã䜿çšããŸãã 圌ãç§ãã¡ã«æäŸãããã®ãèŠããšã次ã®ããã«ãªããŸããåãSQLã衚瀺ãããŸãã yoyoã©ã€ãã©ãªãã€ã³ããŒãããPythonã³ãŒããå³åŽã«ãã§ã«ãããŸãã
ãããã£ãŠãæ£ç¢ºã«èªåçã«ç§»è¡ãéå§ã§ããŸããã€ãŸããæ°ãã移è¡ãäœæããŠãã§ãŒã³ã«è¿œå ããã³ãã³ãããããããã§SQLã³ãŒããèšè¿°ã§ããŸããã³ãã³ãã䜿çšããŠã1ã€ä»¥äžã®ç§»è¡ãé©çšããããããŒã«ããã¯ãããã§ããŸããããã¯ãã§ã«äžæ©åé²ããŠããŸãã
ããã«ãããŒã¿ããŒã¹ã§å®è¡ãããªã¯ãšã¹ããèµ·åãããã¡ã€ã«ãäœããçºçããå Žåã«ããŒã«ããã¯ããå¿ èŠãããå ŽæãçŽã«æžãå¿ èŠããªããªããŸããããçš®ã®çµ¶å¯Ÿç¢ºå®ãªä¿è·ããããŸããããŒã¿ããŒã¹ã®ä»ã®2ã€ã®ç¶æ éã®ç§»è¡ã®ããã«ãä»ã®ç®çã§èšèšããã移è¡ãéå§ã§ããªããªããŸããéåžžã«å€§ããªãã©ã¹ïŒããã¯ãåå¥ã®ãã©ã³ã¶ã¯ã·ã§ã³ã§å移è¡ãå®è¡ããŸããããã¯ãŸããã®ãããªä¿èšŒãäžããŸãã
æ¬ ç¹ã¯æããã§ããããªãã¯ãŸã çã®SQLãæã£ãŠããŸããããšãã°ãPythonã§ããžãã¯ãåºå€§ãªå€§èŠæš¡ãªããŒã¿ãçæããŠããå ŽåãSQLãããªããããããã䜿çšããããšã¯ã§ããŸããã
ãŸããèªååã§ããªãæ¥åžžæ¥åããããããããŸããããŒãã«éã®ãã¹ãŠã®é¢ä¿ïŒã©ããã«æžã蟌ãããšãã§ãããã®ãšããŸã äžå¯èœãªãã®ïŒã远跡ããå¿ èŠããããŸããäžè¬ã«ãéåžžã«æãããªæ¬ ç¹ããããŸãã
泚æãæã䟡å€ããããä»æ¥ã®è©±å šäœãåãäžããããŠãããã1ã€ã®ã¢ãžã¥ãŒã«ã¯ãAlembicã§ãã
ããã¯yoyoãšåããã®ããããŠãã£ãšãããããããŸãã移è¡ãç£èŠããŠäœæããæ¹æ³ãç¥ã£ãŠããã ãã§ãªããéåžžã«è€éãªããžãã¹ããžãã¯ãèšè¿°ããããããŒã¿çæå šäœãPythonã®ä»»æã®æ©èœãæ¥ç¶ãããããããšãã§ããŸããå¿ èŠã«å¿ããŠãããŒã¿ããã«ããŠå éšã§åŠçããŸããããããªãå Žåã¯ãããå¿ èŠã¯ãããŸããã
ã»ãšãã©ã®å Žåã圌ã¯èªåçã«ã³ãŒããæžãããšãã§ããŸãããã¡ãããåžžã«ã§ã¯ãããŸããããæã§ããããæžãå¿ èŠããã£ãåŸã¯ãè¯ããã©ã¹ã®ããã«èãããŸãã
圌ã¯ããããã®ã¯ãŒã«ãªãã®ãæã£ãŠããŸããããšãã°ãSQLiteã¯ALTERTABLEãå®å šã«ã¯ãµããŒãããŠããŸããããŸããAlembicã«ã¯ããããæ°è¡ã§ç°¡åã«ãã€ãã¹ã§ããæ©èœããããèããããšãããããŸããã
åã®ã¹ã©ã€ãã«ã¯ãDjango-migrationsã¢ãžã¥ãŒã«ããããŸãããããã¯ã移è¡ã«éåžžã«é©ããã¢ãžã¥ãŒã«ã§ããããŸãããã®åçã¯ãæ©èœçã«ã¯Alembicã«å¹æµããŸããå¯äžã®éãã¯ããã¬ãŒã ã¯ãŒã¯åºæã§ãããAlembicã¯ããã§ã¯ãªãããšã§ãã
SQLAlchemy
Alembicã¯SQLAlchemyã«åºã¥ããŠãããããSQLAlchemyãå°ãå®è¡ããŠããããäœã§ããããèŠããã調ã¹ããããããšããå§ãããŸãã
ãããŸã§ãçã®ã¯ãšãªã«ã€ããŠèŠãŠããŸãããçã®ã¯ãšãªã¯æªããããŸãããããã¯ãšãŠãè¯ãããšã§ããè² è·ã®é«ãã¢ããªã±ãŒã·ã§ã³ãããå ŽåãããããŸãã«å¿ èŠãªãã®ãããããŸãããäžéšã®ãªããžã§ã¯ããããçš®ã®ã¯ãšãªã«å€æããæéãç¡é§ã«ããå¿ èŠã¯ãããŸããã
è¿œå ã®ã©ã€ãã©ãªã¯å¿ èŠãããŸãããããªãã¯ãã ãã©ã€ããŒãåãã ãã§ãããã ãã§ããããã¯æ©èœããŸããããããããšãã°ãè€éãªã¯ãšãªãäœæããå Žåãããã¯ããã»ã©ç°¡åã§ã¯ãããŸããããŸããå®æ°ãååŸããŠèµ·åãã倧ããªè€æ°è¡ã®ã³ãŒããäœæããããšãã§ããŸãããããããã®ãããªèŠæ±ã10ã20ããå Žåããã§ã«èªãã®ã¯éåžžã«å°é£ã§ãããããããšãããããåå©çšããããšã¯ã§ããŸãããããªãã¯ããããã®ããã¹ããšããã¡ãããæååãfæååãªã©ãæäœããããã®é¢æ°ãæã£ãŠããŸãããããã¯ãã§ã«ããŸãè¯ãèãããŸããããããã¯èªã¿ã«ããã§ãã
ããšãã°ãã¯ãšãªãè€éãªæ§é ãå¿ èŠãªã¯ã©ã¹ãããå Žåãã€ã³ãã³ãã¯éåžžã«èŠçã§ããçã®ç§»è¡ãè¡ãããå Žåãäœãã䜿çšããŠããå ŽæãèŠã€ããå¯äžã®æ¹æ³ã¯ãgrepã䜿çšããããšã§ãããŸããåçã¯ãšãªçšã®åçããŒã«ããããŸããã
ããšãã°ãéåžžã«ç°¡åãªã¿ã¹ã¯ã§ãããšã³ãã£ãã£ãããã1ã€ã®ãã¬ãŒãã«15ã®ãã£ãŒã«ãããããŸãã PATCHãªã¯ãšã¹ããè¡ãããããšãŠãã·ã³ãã«ã«æããŸããçã®ã¯ãšãªã§ãããæžããŠã¿ãŠãã ãããèŠãç®ã¯ããŸãè¯ããªãããã«ãªã¯ãšã¹ããæ¿èªãããå¯èœæ§ã¯äœãã§ãã
ããã«ä»£ãããã®ããããŸã-ã¯ãšãªãã«ããŒãã¯ãšãªãPythonã®ãªããžã§ã¯ããšããŠè¡šãããšãã§ããããã確ãã«æ¬ ç¹ããããŸãã
䟿å®äžããªã¯ãšã¹ãã®çææéãšã¡ã¢ãªã®äž¡æ¹ãæ¯æãå¿ èŠããããŸãããããããã©ã¹ããããŸãã倧èŠæš¡ã§è€éãªã¢ããªã±ãŒã·ã§ã³ãäœæããå Žåã¯ãæœè±¡åãå¿ èŠã§ããã¯ãšãªãã«ããŒã¯ããããã®æœè±¡åãæäŸã§ããŸãããããã®ã¯ãšãªã¯å解ã§ããŸãããããã©ã®ããã«è¡ããããã«ã€ããŠã¯ãå°ãåŸã§èª¬æããŸãããããã¯ãããžãã¹ããžãã¯ã«é¢é£ä»ãããããã¬ã³ããªåãšãã§ã«åŒã°ããŠããé¢æ°ã§åå©çšãæ¡åŒµããŸãã¯ã©ããããããšãã§ããŸãã
åçã¯ãšãªã®äœæã¯éåžžã«ç°¡åã§ããäœããå€æŽããå¿ èŠãããå Žåã¯ã移è¡ãèšè¿°ããŠãã ãããã³ãŒãã®çµ±èšåæã§ååã§ãããšãŠã䟿å©ã§ãã
ãšã«ããSQLAlchemyã¯ãªãã§ããïŒãªãç«ã¡å¯ã䟡å€ãããã®ã§ããïŒ
ããã¯ã移è¡ã ãã§ãªããäžè¬çãªåé¡ã§ãã Alembicãããå ŽåãSQLAlchemyã¯åæãã©ã€ããŒã ãã§æ©èœããããã§ã¯ãªããããã¹ã¿ãã¯å šäœãäžåºŠã«äœ¿çšããã®ãçã«ããªã£ãŠããŸããã€ãŸããDjangoã¯éåžžã«åªããããŒã«ã§ãããAlchemyã¯ãããšãã°asyncpgãaiopgã§äœ¿çšã§ããŸãã Selivanovãèšã£ãããã«ãAsyncpgã䜿çšãããšã1ç§ããã100äžè¡ãèªã¿åãããšãã§ããŸããããŒã¿ããŒã¹ããèªã¿åã£ãŠãPythonã«è»¢éããŸãããã¡ãããSQLAlchemyã䜿çšãããšãå°ãå°ãªããªãããªãŒããŒããããçºçããŸãããšã«ããã
SQLAlchemyã«ã¯ãæäœæ¹æ³ãç¥ã£ãŠããä¿¡ããããªãã»ã©ã®æ°ã®ãã©ã€ããŒããããŸãã OracleãšPostgreSQLãããããã¹ãŠã®å¥œã¿ãšè²ã«å¯Ÿå¿ããŠããŸããããã«ããããã¯ãã§ã«ç®±ããåºãããŠãããå¥ã®ãã®ãå¿ èŠãªå Žåã¯ãæè¿èª¿ã¹ããšãããElasticsearchããããŸãã確ãã«ãèªãããã ãã§ãããããããŸããïŒ -SQLAlchemyã®Elasticsearchã
éåžžã«åªããããã¥ã¡ã³ãã倧èŠæš¡ãªã³ãã¥ããã£ããããŸããããããã®ã©ã€ãã©ãªããããŸãããããŠéèŠãªããšã«ãããã¯ããªãã«ãã¬ãŒã ã¯ãŒã¯ãšã©ã€ãã©ãªãæ瀺ããŸãããããŸãè¡ãå¿ èŠã®ããçãã¿ã¹ã¯ãå®è¡ããŠããå Žåãããã¯ããŒã«ã«ãªãå¯èœæ§ããããŸãã
ããã§ãããã¯äœã§æ§æãããŠããŸããïŒ
ä»æ¥äžç·ã«åãäž»èŠãªå®äœãããã«æã£ãŠããŸããããããã¯ããŒãã«ã§ãããªã¯ãšã¹ããäœæããã«ã¯ãAlchemyã«ãããäœã§ããããšç§ãã¡ãäœãæ±ã£ãŠããããäŒããå¿ èŠããããŸãã次ã¯MetaDataã¬ãžã¹ããªã§ãããšã³ãžã³ã¯ãããŒã¿ããŒã¹ã«æ¥ç¶ããDialectãä»ããŠããŒã¿ããŒã¹ãšéä¿¡ãããã®ã§ãã
ãããäœã§ãããã詳ããèŠãŠã¿ãŸãããã
MetaDataã¯äžçš®ã®ãªããžã§ã¯ãã§ãããã³ã³ããã§ãããããŒãã«ãã€ã³ããã¯ã¹ãããã³äžè¬ã«ãææããŠãããã¹ãŠã®ãšã³ãã£ãã£ãè¿œå ããŸããããã¯ãäžæ¹ã§ã¯ãèšè¿°ããã³ãŒãã«åºã¥ããŠããŒã¿ããŒã¹ãã©ã®ããã«è¡šç€ºããããåæ ãããªããžã§ã¯ãã§ããäžæ¹ãMetaDataã¯ããŒã¿ããŒã¹ã«ã¢ã¯ã»ã¹ããŠãå®éã«ååšãããã®ã®ã¹ãããã·ã§ãããååŸãããã®ãªããžã§ã¯ãã¢ãã«èªäœãæ§ç¯ã§ããŸãã
ãŸããMetaDataãªããžã§ã¯ãã«ã¯éåžžã«èå³æ·±ãæ©èœã1ã€ãããŸããããã«ãããã€ã³ããã¯ã¹ãšå¶çŽã®ããã©ã«ãã®åœåãã³ãã¬ãŒããå®çŸ©ã§ããŸãã PostgreSQLãMySQLãMariaDBãªã©ããã¹ãŠã®ããŒã¿ããŒã¹ã«ã¯ã€ã³ããã¯ã¹ã®åŒã³åºãæ¹æ³ã«é¢ããç¬èªã®ããžã§ã³ãããããã移è¡ãäœæããå Žåãããã¯éåžžã«éèŠã§ãã
äžéšã®éçºè ã¯ãç¬èªã®ããžã§ã³ãæã£ãŠããŸãããŸããSQLAlchemyã䜿çšãããšãæšæºãäžåºŠã ãèšå®ããŠããã®åäœããã¹ãŠç¢ºèªã§ããŸãã SQLiteãšPostgreSQLã®äž¡æ¹ã§åäœããå¿ èŠã®ãããããžã§ã¯ããéçºããå¿ èŠããããŸããããšãŠã䟿å©ã§ããã
次ã®ããã«ãªããŸããSQLAlchemyããMetaDataãªããžã§ã¯ããã€ã³ããŒãããäœæãããšãã«ãnaming_conventionãã©ã¡ãŒã¿ãŒã䜿çšããŠãã³ãã¬ãŒããæå®ããŸãããã®ãã©ã¡ãŒã¿ãŒã®ããŒã¯ãã€ã³ããã¯ã¹ãšå¶çŽã®ã¿ã€ããæå®ããŸããix-éåžžã®ã€ã³ããã¯ã¹ãuq-äžæã®ã€ã³ããã¯ã¹ãfk-å€éšããŒãpk-ãã©ã€ããªããŒã
Naming_conventionãã©ã¡ãŒã¿ãŒã®å€ã§ã¯ãã€ã³ããã¯ã¹ã¿ã€ã/å¶çŽïŒix / uq / fkãªã©ïŒãšããŒãã«åã§æ§æããããã¿ãŒã³ãäžç·ã§åºåã£ãŠæå®ã§ããŸããäžéšã®ãã³ãã¬ãŒãã§ã¯ããã¹ãŠã®åãäžèŠ§è¡šç€ºããããšãã§ããŸããããšãã°ããã©ã€ããªããŒã«å¯ŸããŠãããè¡ãå¿ èŠã¯ãªããããŒãã«åãæå®ããã ãã§æžã¿ãŸãã
æ°ãããããžã§ã¯ãã®äœæãéå§ãããšãã¯ãååä»ããã³ãã¬ãŒããäžåºŠè¿œå ãããšå¿ããŠããŸããŸãããã以éããã¹ãŠã®ç§»è¡ã¯åãååã®ã€ã³ããã¯ã¹ãšå¶çŽã§çæãããŠããŸãã
ããã¯å¥ã®çç±ã§éèŠã§ãããªããžã§ã¯ãã¢ãã«ã§ãã®ã€ã³ããã¯ã¹ãäžèŠã«ãªã£ããšå€æããŠåé€ãããšãAlembicã¯ãããäœãšåŒã°ããããèªèãã移è¡ãæ£ããçæããŸããããã¯ããã¹ãŠãæ£åžžã«æ©èœãããšããä¿¡é Œæ§ã®äžå®ã®ä¿èšŒã§ãã
å¿ ãééãããã1ã€ã®éåžžã«éèŠãªãšã³ãã£ãã£ã¯ãããŒãã«ã§ããããã¯ãããŒãã«ã«å«ãŸããå 容ã説æãããªããžã§ã¯ãã§ãã
ããŒãã«ã«ã¯ååãšããŒã¿ã¿ã€ãã®åããããMetaDataã¯èª¬æãããã¹ãŠã®ã¬ãžã¹ããªã§ãããããå¿ ç¶çã«MetaDataã¬ãžã¹ããªãåç §ããŸãããŸããããŒã¿ã¿ã€ãã®åããããŸãã
ç§ãã¡ã説æããããšã®ãããã§ãSQLAlchemyã¯ä»ã§ã¯å€ãã®ããšãç¥ãããšãã§ããŸããããã§å€éšããŒãæå®ããå Žåã§ãã圌女ã¯ããŒãã«ãäºãã«ã©ã®ããã«æ¥ç¶ãããŠããããç¥ã£ãŠããŸãããããŠã圌女ã¯äœããããå¿ èŠãããé åºãç¥ã£ãŠããã§ãããã
SQLAlchemyã«ã¯EngineããããŸããéèŠïŒãªã¯ãšã¹ãã«ã€ããŠè¿°ã¹ãããšã¯åå¥ã«äœ¿çšã§ãããšã³ãžã³ã¯åå¥ã«äœ¿çšã§ããŸãããããŠãããªãã¯ãã¹ãŠãäžç·ã«äœ¿ãããšãã§ããŸãã誰ãçŠããŸãããã€ãŸããEngineã¯ãµãŒããŒã«çŽæ¥æ¥ç¶ããæ¹æ³ãç¥ã£ãŠããããŸã£ããåãã€ã³ã¿ãŒãã§ã€ã¹ãæäŸããŸãããããããã¡ãããããŸããŸãªãã©ã€ããŒãDBAPIã«æºæ ããããšããŸããæšå¥šãè¡ããPythonã®PEPããããŸããããããEngineã¯ãã¹ãŠã®ããŒã¿ããŒã¹ã«å¯ŸããŠãŸã£ããåãã€ã³ã¿ãŒãã§ãŒã¹ãæäŸããéåžžã«äŸ¿å©ã§ãã
æåŸã®äž»èŠãªãã€ã«ã¹ããŒã³ã¯Dialectã§ããããã¯ããšã³ãžã³ãããŸããŸãªããŒã¿ããŒã¹ãšéä¿¡ããæ¹æ³ã§ããããã«ã¯ãããŸããŸãªèšèªãããŸããŸãªäººã ãããŸããŸãªæ¹èšããããŸãã
ãããäœã®ããã«ããã®ãèŠãŠã¿ãŸãããã
ããã¯éåžžã®ã€ã³ãµãŒããã©ã®ããã«èŠãããã§ãã IDãšé»åã¡ãŒã«ãã£ãŒã«ãããã£ããåã«èª¬æããããŒãã«ã«æ°ããè¡ãè¿œå ããå Žåã¯ãããã§é»åã¡ãŒã«ãæå®ããæ¿å ¥ãå®è¡ããŠãæ¿å ¥ãããã¹ãŠã®ãã®ãããã«è¿ããŸãã
ããããã®è¡ãè¿œå ãããå Žåã¯ã©ããªããŸããïŒåé¡ãªãã
ããã§å£è¿°ã®ãªã¹ãã転éããã ãã§ããããã€ãã®è¶ ã·ã³ãã«ãªãã³ã®å®ç§ãªã³ãŒãã®ããã«èŠããŸããããŒã¿ãå ¥ããããçš®ã®æ€èšŒãšJSONã¹ããŒãã«åæ Œããããã ãã§ããŒã¿ããŒã¹ã«å ¥ããŸãããè¶ ç°¡åã
äžéšã®ã¯ãšãªã¯éåžžã«è€éã§ãããªã¯ãšã¹ããå°å·ããŠè¡šç€ºã§ããå Žåãããã°ãã³ã³ãã€ã«ããå¿ èŠãããå ŽåããããŸããããã¯é£ããããšã§ã¯ãããŸããã Alchemyã䜿çšãããšãããããã¹ãŠãå®è¡ã§ããŸãããã®å Žåããªã¯ãšã¹ããã³ã³ãã€ã«ããŸãããå®éã«ããŒã¿ããŒã¹ã«äœãéä¿¡ããããã確èªã§ããŸãã
ããŒã¿èŠæ±ã¯éåžžã«åçŽã«èŠããŸããæåéã2è¡ã§ã1è¡ã§æžãããšãã§ããŸãã
ããšãã°ã15ãã£ãŒã«ãã®PATCHãªã¯ãšã¹ããäœæããæ¹æ³ã«ã€ããŠã®è³ªåã«æ»ããŸããããããã§ã¯ããã£ãŒã«ãã®ååããã®ããŒãããã³å€ã®ã¿ãæžã蟌ãå¿ èŠããããŸããå¿ èŠãªã®ã¯ããã ãã§ãããã¡ã€ã«ãæååã®äœæãããŸã£ããäœããããŸããã䟿å©ããã§ããã
ãããããç§ãä»äºã§æ¯æ¥äœ¿çšããŠããæãéèŠãªAlchemyæ©èœã¯ãã¯ãšãªã®å解ãšæ¡åŒµã§ãã
PostgreSQLã§ã€ã³ã¿ãŒãã§ã€ã¹ãäœæããŠãããšãããšãã¢ããªã±ãŒã·ã§ã³ã¯äœããã®æ¹æ³ã§äººãæ¿èªãããã®äººãCRUDãå®è¡ã§ããããã«ããå¿ èŠããããŸããããŠãå解ããããšã¯ããŸããããŸããã
ããŒã¿ã®ããŒãžã§ã³ç®¡çãããŸããŸãªæœè±¡åã䜿çšããéåžžã«è€éãªã¢ããªã±ãŒã·ã§ã³ãäœæããå Žåãçæããã¯ãšãªã¯èšå€§ãªæ°ã®ãµãã¯ãšãªã§æ§æãããå¯èœæ§ããããŸãããµãã¯ãšãªã¯ãµãã¯ãšãªãšçµåãããŸããããŸããŸãªã¿ã¹ã¯ããããŸãããŸããã¯ãšãªã®å解ãéåžžã«åœ¹ç«ã€å ŽåããããŸããããã«ãããããžãã¯ãšã³ãŒãã®èšèšãå€§å¹ ã«åé¢ã§ããŸãã
ãªããã®ããã«æ©èœããã®ã§ããïŒããšãã°ãusers_table.selectïŒïŒã¡ãœãããåŒã³åºããšããªããžã§ã¯ããè¿ãããŸããwhereïŒïŒãªã©ãçµæã®ãªããžã§ã¯ãã«å¯ŸããŠä»ã®ã¡ãœãããåŒã³åºããšãå®å šã«æ°ãããªããžã§ã¯ããè¿ãããŸãããã¹ãŠã®ã¯ãšãªãªããžã§ã¯ãã¯äžå€ã§ãããããã£ãŠã奜ããªãã®ã®äžã«æ§ç¯ããããšãã§ããŸãã
alembicããã®ç§»è¡
ããã§SQLAlchemyãåŠçããã€ãã«Alembicãã€ã°ã¬ãŒã·ã§ã³ãäœæã§ããããã«ãªããŸããã
Alembicã®äœ¿çšãéå§ããããšã¯ãç¹ã«åã«è¿°ã¹ãããã«ããŒãã«ããã§ã«èª¬æããMetaDataãªããžã§ã¯ããæå®ããŠããå Žåã¯ããŸã£ããé£ãããããŸããã alembicãpipinstallããalembic initalembicãšåŒã³ãŸãã alembic-ã¢ãžã¥ãŒã«ã®ååãããã¯ã³ãã³ãã©ã€ã³ã§ããããããããŸãã initã¯ã³ãã³ãã§ããæåŸã®åŒæ°ã¯ããããé 眮ãããã©ã«ããŒã§ãã
ãã®ã³ãã³ããåŒã³åºããšãããã€ãã®ãã¡ã€ã«ãäœæãããŸããããã«ã€ããŠè©³ããèŠãŠãããŸãã
alembic.iniã«ã¯äžè¬çãªæ§æããããŸãã script_locationã¯ããŸãã«ããªããè¡ãããå Žæã§ãã次ã«ãçæãã移è¡ã®ååã®ãã³ãã¬ãŒããšãããŒã¿ããŒã¹ã«æ¥ç¶ããããã®æ å ±ããããŸãã
æ°ãã移è¡çšã®ãã³ãã¬ãŒãããããŸãã ãæ°ãã移è¡ãå¿ èŠã§ãããšèšããšãAlembicã¯ç¹å®ã®ãã³ãã¬ãŒãã«åŸã£ãŠãããäœæããŸããããããã¹ãŠãã«ã¹ã¿ãã€ãºã§ããŸããéåžžã«ç°¡åã§ãããã®ãã¡ã€ã«ã«ã¢ã¯ã»ã¹ããŠãå¿ èŠãªãã®ãç·šéããŸããããã§æå®ã§ãããã¹ãŠã®å€æ°ã¯ãããã¥ã¡ã³ãã«ãããŸããããã¯æåã®éšåã§ããäžéšã«ããçš®ã®è§£èª¬ãããã®ã§ãããã§äœãèµ·ãã£ãŠããã®ããèŠãã®ã«äŸ¿å©ã§ãã次ã«ããã¹ãŠã®ç§»è¡ã«å«ãŸããã¹ãå€æ°ã®ã»ããããããŸã-ãªããžã§ã³ãdown_revisionãä»æ¥ã¯åœŒããšäžç·ã«åããŸããããã«-è¿œå ã®ã¡ã¿æ å ±ã
æãéèŠãªæ¹æ³ã¯ãã¢ããã°ã¬ãŒããšããŠã³ã°ã¬ãŒãã§ãã Alembicã¯ãããã§MetaDataãªããžã§ã¯ããã¹ããŒãã®èª¬æãšããŒã¿ããŒã¹ã«ãããã®ãšã®éã«èŠã€ããéãã眮ãæããŸãã
env.pyã¯ãAlembicã§æãèå³æ·±ããã¡ã€ã«ã§ããã³ãã³ãå®è¡ã®é²è¡ç¶æ³ãå¶åŸ¡ããèªåã§ã«ã¹ã¿ãã€ãºã§ããããã«ããŸãã MetaDataãªããžã§ã¯ããæ¥ç¶ããã®ã¯ãã®ãã¡ã€ã«ã§ããåã«è¿°ã¹ãããã«ãMetaDataãªããžã§ã¯ãã¯ãããŒã¿ããŒã¹å ã®ãã¹ãŠã®ãšã³ãã£ãã£ã®ã¬ãžã¹ããªã§ãã
ããã§ãã®MetaDataãªããžã§ã¯ããæ¥ç¶ããŠããŸãããããŠãã®æãããAlembicã¯ãããã«ç§ã®ã¢ãã«ãããã«ç§ã®ãã¬ãŒããããããšãç解ããŠããŸãã圌ã¯èªåãäœãæ±ã£ãŠããããç解ããŠããŸãã次ã«ãAlembicã«ã¯ããªãã©ã€ã³ãŸãã¯ãªã³ã©ã€ã³ã§AlembicãåŒã³åºãã³ãŒãããããŸããããã«ã€ããŠãæ€èšããŸãã
ããã¯ãŸãã«ããããžã§ã¯ãã§MetaDataãæ¥ç¶ããå¿ èŠãããè¡ã§ããäœããã¯ã£ããããŠããªããŠãå¿é ããªãã§ãã ãããç§ã¯ãã¹ãŠããããžã§ã¯ãã«éããŠGitHubã«æçš¿ããŸãããããªãã¯ãããè€è£œããŠèŠãããšãã§ããããããã¹ãŠæããããšãã§ããŸãã
ãªã³ã©ã€ã³ã¢ãŒããšã¯äœã§ããïŒãªã³ã©ã€ã³ã¢ãŒãã§ã¯ãAlembicã¯alembic.iniãã¡ã€ã«ã®sqlalchemy.urlãã©ã¡ãŒã¿ãŒã§æå®ãããããŒã¿ããŒã¹ã«æ¥ç¶ãã移è¡ã®å®è¡ãéå§ããŸãã
ãªããã®ã³ãŒããèŠãŠããã®ã§ããïŒ Alembicã¯éåžžã«æè»ã«ã«ã¹ã¿ãã€ãºã§ããŸãã
ããŸããŸãªããŒã¿ããŒã¹ã¹ããŒãã«ååšããå¿ èŠãããã¢ããªã±ãŒã·ã§ã³ããããšããŸããããšãã°ãäžåºŠã«å€æ°ã®ã¢ããªã±ãŒã·ã§ã³ã€ã³ã¹ã¿ã³ã¹ãå®è¡ãããããããç¬èªã®ã¹ããŒã ã§åäœããããã«ããããšããŸããããã¯äŸ¿å©ã§å¿ èŠãªå ŽåããããŸãã
è²»çšã¯äžåããããŸããã context.begin_transactionïŒïŒã¡ãœãããåŒã³åºããåŸãã³ãã³ããSET search_path = SCHEMAããèšè¿°ã§ããŸããããã«ãããPostgreSQLã«å¥ã®ããã©ã«ãã¹ããŒãã䜿çšããããã«æ瀺ãããŸãããããŠããããã¹ãŠã§ããä»åŸãã¢ããªã±ãŒã·ã§ã³ã¯å®å šã«ç°ãªãã¹ããŒã ã§åäœãã移è¡ã¯å¥ã®ã¹ããŒã ã«ç§»è¡ããŸããããã¯1è¡ã®è³ªåã§ãã
ãªãã©ã€ã³ã¢ãŒãããããŸããããã§ã¯ãAlembicã¯ãšã³ãžã³ã䜿çšããªãããšã«æ³šæããŠãã ãããããã§åœŒã«ãªã³ã¯ãæž¡ãã ãã§ãããã¡ããããšã³ãžã³ã転éããããšãã§ããŸãããã©ãã«ãæ¥ç¶ãããŸãããçã®ã¯ãšãªãçæããã ãã§ãã©ããã§å®è¡ã§ããŸãã
ã€ãŸããAlembicãšããã€ãã®MetaDataãšããŒãã«ããããŸãããããŠãæçµçã«èªåã§ç§»è¡ãçæããããšããŸãããã®ã³ãã³ããå®è¡ãããšãåºæ¬çã«ã¯ããã ãã§ãã Alembicã¯ããŒã¿ããŒã¹ã«ã¢ã¯ã»ã¹ããããã«äœããããã確èªããŸãã圌ã®ç¹å¥ãªã©ãã«ãalembic_versionsãã¯ãããŸããïŒããã¯ã移è¡ããã®ããŒã¿ããŒã¹ã§ãã§ã«å±éãããŠããããšã瀺ããŠããŸããïŒããã«ã©ã®ããŒãã«ãååšããããããããŸããããŒã¿ããŒã¹ã«å¿ èŠãªããŒã¿ã衚瀺ãããŸããããããã¹ãŠãåæãããã®ãã³ãã¬ãŒãã«åºã¥ããŠæ°ãããã¡ã€ã«ãçæãããšã移è¡ãè¡ãããŸãããã¡ãããAlembicã¯å¿ ãããå¿ èŠãªãã®ãçæãããšã¯éããªãããã移è¡ã§çæããããã®ãå¿ ã確èªããå¿ èŠããããŸããããããã»ãšãã©ã®å Žåãããã¯æ©èœããŸãã
äœãçæããŸãããïŒãŠãŒã¶ãŒã®ãµã€ã³ããããŸããã移è¡ãçæãããšãã«ãæåã®ã¡ãã»ãŒãžã瀺ããŸããã移è¡ã«ã¯ã以åã«alembic.iniã§æå®ãããä»ã®ãã³ãã¬ãŒãã䜿çšããŠinitial.pyãšããååãä»ããããŸãã
ãã®ç§»è¡ã®IDã«é¢ããæ å ±ããããŸãã down_revision = None-ããã¯æåã®ç§»è¡ã§ãã
次ã®ã¹ã©ã€ãã¯æãéèŠãªéšåã§ãïŒã¢ããã°ã¬ãŒããšããŠã³ã°ã¬ãŒãã
ã¢ããã°ã¬ãŒãã§ã¯ããã¬ãŒããäœæãããŠããããšãããããŸããããŠã³ã°ã¬ãŒãã§ã¯ããã®èšå·ã¯åé€ãããŸãã Alembicã¯ãããã©ã«ãã§ããã®ãããªã³ã¡ã³ããå ·äœçã«è¿œå ããŠãããã«ç§»åããŠç·šéããå°ãªããšããããã®ã³ã¡ã³ããåé€ããŸãã念ã®ããã移è¡ã確èªãããã¹ãŠãã客æ§ã«é©ããŠããããšã確èªããŸãããããã¯1ã€ã®ããŒã ã®åé¡ã§ãããã§ã«ç§»è¡ããããŸãã
ãã®åŸããã®ç§»è¡ãé©çšããå¯èœæ§ããããŸããç°¡åãªããšã§ã¯ãããŸãããããªãã¯ãã èšãå¿ èŠããããŸãïŒã¢ã¬ã ããã¯ã¢ããã°ã¬ãŒããããã圌ã¯çµ¶å¯Ÿã«ãã¹ãŠãé©çšããŸãã
é ãšèšããšãææ°ã®ç§»è¡ã«æŽæ°ããããšããŸããç¹å®ã®ç§»è¡ã«ååãä»ãããšãããã«æŽæ°ãããŸãã
ããšãã°ãæ°ãå€ãã£ãå Žåã«åããŠãããŠã³ã°ã¬ãŒãã³ãã³ãããããŸããããã¯ãã¹ãŠãã©ã³ã¶ã¯ã·ã§ã³ã§è¡ãããéåžžã«ç°¡åã«æ©èœããŸãã
ã€ãŸãã移è¡ããããããããå®è¡ããæ¹æ³ãç¥ã£ãŠããŸããã¢ããªã±ãŒã·ã§ã³ããããããšãã°ã次ã®è³ªåãããŠããå ŽåïŒCIãããããã¹ããå®è¡ãããŠããŸããããšãã°ã移è¡ãèªåçã«å®è¡ãããã©ããããããããŸããããã¶ãããªãã®æã§ãããããæ¹ãè¯ãã§ããïŒ
ããã«ã¯ããŸããŸãªèŠç¹ããããŸãããããããã«ãŒã«ãå®ã䟡å€ããããŸããç°¡åã«ã¢ã¯ã»ã¹ã§ãããããŒã¿ããŒã¹ãåããè»ã«ä¹ãããšãã§ããªãå Žåã¯ããã¡ãããèªåçã«è¡ãæ¹ãããã§ãããã
ã¢ã¯ã»ã¹ã§ããå Žåã¯ãã¯ã©ãŠãã§æ©èœãããµãŒãã¹ãäœæãããã€ãæã£ãŠããã©ãããããããããã«ã¢ã¯ã»ã¹ã§ããŸããããããã°ãèªåã§ãããå®è¡ã§ããããããã詳现ã«å¶åŸ¡ã§ããŸãã
äžè¬ã«ããããèªåçã«è¡ãããã®ããŒã«ã¯ãããããããŸããããšãã°ãåãKubernetesã§ããããå®è¡ã§ãããããã®ã³ãã³ããå®è¡ã§ããinitã³ã³ãããŒããããŸãããããè¡ãã«ã¯ãèµ·åã³ãã³ããDockerã«çŽæ¥è¿œå ã§ããŸãã
èæ ®ããå¿ èŠãããã®ã¯ã移è¡ãèªåçã«é©çšããå Žåãããšãã°ãããŒã«ããã¯ããããã§ããªãå Žåã«ã©ããªãããèããå¿ èŠããããšããããšã§ããããšãã°ã500ã®ã¬ãã€ãã®ããŒã¿ãã¬ãŒãããããŸãããããªãã¯èããŸããïŒããŠããã®ããŒã¿ã¯ãã¯ãããžãã¹ããžãã¯ã«ã¯å¿ èŠãããŸããããããããããèœãšãããšãã§ããŸãã圌ãã¯ãããåãããããèœãšããŸããããŸãã¯ãããŒã¿ã®æ倱ã«äŒŽã£ãŠå€æŽãããåã®ã¿ã€ããå€æŽããŸãããããšãã°ãé·ãåããããŸããããçããªããŸããããŸãã¯äœãããªããªã£ãŠããŸãããŸãã¯ãåãåé€ããŸãããå¿ èŠãªå Žåã§ãããŒã«ããã¯ããããšã¯ã§ããŸããã
ãã€ãŠç§ã¯ãªã³ãã¬ãã¹åãã®è£œåãäœæããŸãããããã¯ããã·ã³äžã§çŽæ¥äººã ã®ããã«exeãã¡ã€ã«ã«ãã£ãŠã€ã³ã¹ããŒã«ãããŸããç解ãããïŒã¯ããããªãã¯ç§»è¡ãæžããŸãããããã¯æ¬çªç°å¢ã«å ¥ããŸããã人ã ã¯ãã§ã«ãããã€ã³ã¹ããŒã«ããŸãããä»åŸ5幎éã§ãSLAã«åŸã£ãŠæ©èœããå¯èœæ§ããããäœããå€æŽãããå Žåã¯ãäœããæ¹åãããå¯èœæ§ããããŸãããã®æç¹ã§ãäžå¯éçãªå€æŽã«å¯ŸåŠããæ¹æ³ã«ã€ããŠèããŸãã
ããã«ããã±ããç§åŠã¯ãããŸãããã¢ã€ãã¢ã¯ããããã®åã®äœ¿çšãããŒãã«ã®äœ¿çšãã§ããã ãé¿ããããšãã§ãããšããããšã§ãããããã«é£çµ¡ããã®ããããªãããããšãã°ãORMã§ç¹å¥ãªãã³ã¬ãŒã¿ã䜿çšããŠãã£ãŒã«ãã«ããŒã¯ãä»ããããšãã§ããŸãã圌ã¯ãã°ã§ããªãããã®ãã£ãŒã«ãã«è§Šããããªãããã ã£ããšèšãã§ãããããããããªãã¯ãŸã 圌ã«èšåããŠããŸããããã¯ãã°ã«ã¿ã¹ã¯ãäœæãããã€ãåé€ããã ãã§ãã
ã©ã¡ãããšããã°ãããŒã«ããã¯ããæéããããŸãããããŠããã¹ãŠãããŸãããã°ãããã¯ãã°ã®åŸåã§ãã®ã¿ã¹ã¯ãèœã¡çããŠå®è¡ããŸããå®éã«ãã¹ãŠãåé€ããå¥ã®ç§»è¡ãè¡ããŸãã
ããã§æãéèŠãªè³ªåã§ãã移è¡ããã¹ãããçç±ãšæ¹æ³ã¯ïŒ
ç§ãå°ãã人ã®ã»ãšãã©ã¯ãããããŸããããããããããè¡ãæ¹ãè¯ãã§ããããã¯ãçã¿ãè¡ãæ±ã§æžãããã«ãŒã«ã§ããæ¬çªç°å¢ã§ç§»è¡ã䜿çšããããšã¯åžžã«å±éºã§ããããªãã¯ãããã©ã®ããã«çµãããã決ããŠç¥ããŸããã CIãæ§æããŠããå Žåãå®å šã«æ£åžžã«åäœããæ¬çªç°å¢ã§ã®éåžžã«åªãã移è¡ã§ããããžã£ãŒã¯ããå¯èœæ§ããããŸãã
å®éã®ãšããã移è¡ããã¹ããããšãã«ãããšãã°ãã¹ããŒãžãŸãã¯æ¬çªç°å¢ã®äžéšãããŠã³ããŒãããããšãã§ããŸãããããã¯ã·ã§ã³ã¯å€§èŠæš¡ã«ãªãå¯èœæ§ãããããã¹ãããã®ä»ã®ã¿ã¹ã¯ã®ããã«å®å šã«ããŠã³ããŒãããããšã¯ã§ããŸãããéçºæ ç¹ã¯ãååãšããŠãå®éã«ã¯çç£æ ç¹ã§ã¯ãããŸããã圌ãã¯äœå¹Žã«ãããã£ãŠèç©ã§ãããã®ã®å€ããæã£ãŠããŸããã
ããã¯ãäœãã移è¡ãããšãã«ããŒã¿ãç ŽæããããããŒã¿ã®äžè²«æ§ã倱ãããå€ããœãããŠã§ã¢ã§ããå¯èœæ§ããããŸãã誰ããå€éšããŒãè¿œå ããã®ãå¿ããå Žåãããã¯æé»ã®äŸåé¢ä¿ã§ããå¯èœæ§ããããŸãã圌ã¯ãããã€ãªãã£ãŠãããšæããŸãããäŸãã°åœŒã®ååã¯ããã«ã€ããŠç¥ããŸããããã£ãŒã«ããå¶ç¶ã«åŒã°ããæ¥ç¶ãããŠãããã©ããã¯ãŸã£ããããããŸããã
ãããã誰ããå ¥ã£ãŠãããçš®ã®ã€ã³ããã¯ã¹ãçŽæ¥æ¬çªç°å¢ã«è¿œå ããããšã«ããŸããããä»ã¯é ããªããŸãããéãåäœãå§ãããã©ããªãã§ããããïŒãèªåŒµããŠãããããããŸããããå®éã«ã¯ããŒã¿ããŒã¹å ã§äœããå€æŽããããšããããŸãã
ãã¡ãããã¹ããŒãã®ç§»è¡ã§ã¯ãããŒã«ã«ãšã©ãŒããããŸããæ£çŽãªãšãããç§ã¯ããã«ééããŠããŸãããéåžžãæåã®3ã€ã®åé¡ããããŸããããããŠãããããããŒã¿ãã©ã®ããã«è»¢éãããã¹ããã«ã€ããŠã®ä»®å®ã«ãããããå€ãã®ãšã©ãŒã
éåžžã«å€§ããªãªããžã§ã¯ãã¢ãã«ãããå Žåããã¹ãŠã念é ã«çœ®ãããšã¯å°é£ã§ããåžžã«ææ°ã®ããã¥ã¡ã³ããäœæããããšã¯å°é£ã§ããææ°ã®ããã¥ã¡ã³ãã¯ã³ãŒãã§ãããããžãã¹ããžãã¯ãå®å šã«èšè¿°ãããŠãããšã¯éããŸãããã€ãŸããäœãã©ã®ããã«æ©èœããããã誰ãäœã念é ã«çœ®ããŠãããã§ãã
äœã確èªã§ããŸããïŒå°ãªããšã移è¡ãå§ãŸããšããäºå®ãããã¯ãã§ã«çŽ æŽãããã§ãããããŠãã³ãŒãã«æããªã¿ã€ããã¹ããªãããšãæå¹ãªdowngradeïŒïŒã¡ãœãããããããšãdowngradeïŒïŒã¡ãœãããSQLAlchemyã«ãã£ãŠäœæããããã¹ãŠã®ããŒã¿ã¿ã€ããåé€ããããšã確èªã§ããŸãã
SQLAlchemyã¯å€ãã®çŽ æŽãããããšãããŸããããšãã°ãããŒãã«ãèšè¿°ããŠEnumåã¿ã€ããæå®ãããšãSQLAlchemyã¯PostgreSQLã§ãã®åæåã®ããŒã¿ã¿ã€ããèªåçã«äœæããŸãããã ããdowngradeïŒïŒã¡ãœããã§ãã®ããŒã¿ã¿ã€ããåé€ããã³ãŒãã¯èªåçã«çæãããŸããã
ãããèŠããŠç¢ºèªããå¿ èŠããããŸãã移è¡ãããŒã«ããã¯ããŠåé©çšããå ŽåãupgradeïŒïŒã¡ãœããã§æ¢åã®ããŒã¿ã¿ã€ããäœæããããšãããšãäŸå€ãã¹ããŒãããŸãããããŠæãéèŠãªããšã¯ã移è¡ã«ãã£ãŠããŒã¿ãå€æŽãããå Žåãã¢ããã°ã¬ãŒãæã«ããŒã¿ãæ£ããå€æŽãããããšã確èªããå¿ èŠããããŸãããŸããããŠã³ã°ã¬ãŒãæã«å¯äœçšãªãã«æ£ããããŒã«ããã¯ããããšã確èªããããšãéåžžã«éèŠã§ãã
ãã¹ãèªäœã«é²ãåã«ããã¹ããäœæããããã®æåã®æºåæ¹æ³ãèŠãŠã¿ãŸããããç§ã¯ããã«å¯Ÿããå€ãã®ã¢ãããŒããèŠãŠããŸãããäžéšã®äººã ã¯ãããŒã¹ããã¬ãŒããäœæããããããã¹ãŠã¯ãªãŒã³ã¢ãããããã£ã¯ã¹ãã£ãäœæããããçš®ã®èªåé©çšãã£ã¯ã¹ãã£ã䜿çšããŸãããããã100ïŒ ä¿è·ããå®å šã«éé¢ãããã¹ããŒã¹ã§ãã¹ããå®è¡ããçæ³çãªæ¹æ³ã¯ãå¥ã®ããŒã¿ããŒã¹ãäœæããããšã§ãã
ããŒã¿ããŒã¹ãäœæããã³åé€ã§ããçŽ æŽãããsqlalchemy_utilsã¢ãžã¥ãŒã«ããããŸãã PostgreSQLã§ã¯ã圌ã¯æ¬¡ã®ããšã確èªããŸããã¯ã©ã€ã¢ã³ãã®1ã€ãã¹ãªãŒãç¶æ ã«ãªããåæããªãã£ãå Žåãã誰ããããŒã¿ããŒã¹ã䜿çšããŠããŸããããŒã¿ããŒã¹ã䜿çšããŠäœãã§ããŸãããåé€ã§ããŸããããšãããšã©ãŒã§ã¯ã©ãã·ã¥ããããšã¯ãããŸããã代ããã«ã圌ã¯èª°ããããã«æ¥ç¶ããããå·éã«ç¢ºèªãããããã®ã¯ã©ã€ã¢ã³ããåæããå·éã«ããŒã¹ãåé€ããŸãã
ããŒã¿ããŒã¹ãæ§ç¯ããåãã¹ãã«ç§»è¡ãé©çšããããšã¯ãå¿ ãããè¿ éãªããã»ã¹ã§ã¯ãããŸãããããã¯æ¬¡ã®ããã«è§£æ±ºã§ããŸããPostgreSQLã¯ãã³ãã¬ãŒãããã®æ°ããããŒã¿ããŒã¹ã®äœæããµããŒãããŠãããããããŒã¿ããŒã¹ã®æºåã2ã€ã®ãã£ã¯ã¹ãã£ã«åå²ã§ããŸãã
æåã®ãã£ã¯ã¹ãã£ã¯1åå®è¡ããããã¹ãŠã®ãã¹ãïŒscope = sessionïŒãå®è¡ãããããŒã¿ããŒã¹ãäœæãããããã«ç§»è¡ãé©çšãããŸãã 2çªç®ã®ãã£ã¯ã¹ãã£ïŒscope = functionïŒã¯ãæåã®ãã£ã¯ã¹ãã£ã®ããŒã¹ã«åºã¥ããŠãåãã¹ãã®ããŒã¹ãçŽæ¥äœæããŸãã
ãã³ãã¬ãŒãããããŒã¿ããŒã¹ãäœæããã®ã¯éåžžã«é«éã§ãåãã¹ãã«ç§»è¡ãé©çšããæéãç¯çŽã§ããŸãã
äžæçã«ããŒã¿ããŒã¹ãäœæããæ¹æ³ã«ã€ããŠè©±ããŠããã ããªãããã®ãããªãã£ã¯ã¹ãã£ãæžãããšãã§ããŸããäœãèµ·ããŠãïŒã©ã³ãã ãªååãçæããŸãã念ã®ãããpytestã®æåŸã«è¿œå ããŸããããã«ãããPosticoãä»ããŠlocalhostã«ç§»åãããšãã«ããã¹ãã«ãã£ãŠäœæããããã®ãšäœæãããªãã£ããã®ãç解ã§ããŸãã
次ã«ããã®äººã瀺ããããŒã¿ããŒã¹ãžã®æ¥ç¶ã«é¢ããæ å ±ãå«ããªã³ã¯ãããæ°ããããŒã¿ããŒã¹ãæã€æ°ãããªã³ã¯ãçæããŸããäœæããŠãã¹ãã«éä¿¡ããŸãã人ããã®ããŒã¿ããŒã¹ãæäœããåŸããããåé€ããŸãã
ãã®ããŒã¿ããŒã¹ã«æ¥ç¶ããããã«ãšã³ãžã³ãæºåããããšãã§ããŸããã€ãŸãããã®ãã£ã¯ã¹ãã£ã§ã¯ãäŸåé¢ä¿ãšããŠäœ¿çšãããŠãã以åã®ãã£ã¯ã¹ãã£ãåç §ããŸãããšã³ãžã³ãäœæããŠãã¹ãã«éä¿¡ããŸãã
ã§ã¯ãã©ã®ãããªãã¹ããæžãããšãã§ããŸããïŒæåã®ãã¹ãã¯ãç§ã®ååã®çŽ æŽãããçºæã§ããç»å ŽããŠããã移è¡ã®åé¡ãå¿ããŠããŸã£ãããã§ãã
ããã¯éåžžã«ç°¡åãªãã¹ãã§ãããããžã§ã¯ãã«äžåºŠè¿œå ããŸããããã¯GitHubã®ãããžã§ã¯ãã«ãããŸã..ãããªãã¯ãããããªãã«ãã©ãã°ããŠãããããåé¡ã®çŽ80ããŒã»ã³ããè¿œå ããŠå¿ããããšãã§ããŸãã
ããã¯éåžžã«åçŽãªããšãããŸãïŒããã¯ãã¹ãŠã®ç§»è¡ã®ãªã¹ããååŸãããããã«å¯ŸããŠå埩ãéå§ããŸããã¢ããã°ã¬ãŒããããŠã³ã°ã¬ãŒããã¢ããã°ã¬ãŒããåŒã³åºããŸãã
ããšãã°ã5ã€ã®ç§»è¡ããããŸãããããã©ã®ããã«æ©èœãããèŠãŠã¿ãŸãããããããæåã®ç§»è¡ã§ããç§ãã¡ã¯ãããå®çŸããŸãããæåã®ç§»è¡ãããŒã«ããã¯ããå床å®è¡ããŸããããã§äœãèµ·ãã£ãã®ã§ããïŒå®éãããã§ã¯ãããšãã°2åã¯ããŒãã«ãäœæã§ããªãã£ããããããŠã³ã°ã¬ãŒãïŒïŒã¡ãœãããæ£ããå®è£ ããŠããããšãããããŸããã
ãã人ãããã€ãã®ããŒã¿ã¿ã€ããäœæããå Žåãã¿ã€ããã¹ããªããäžè¬çã«ã¯å°ãªããšãäœããã®åœ¢ã§æ©èœãããããããããåé€ããããšãããããŸãã
ãã®åŸããã¹ããé²ã¿ãŸãã圌ã¯2åç®ã®ç§»è¡ãè¡ããããã«å®è¡ãã1ã¹ãããããŒã«ããã¯ããåã³åé²ããŸãããããŠãããã¯ç§»è¡ã®åæ°ã ãçºçããŸãã
ãã®ãã¹ãã®ç®çã¯ãããŒã¿æ§é ãå€æŽããéã®åºæ¬çãªãšã©ãŒãåé¡ãèŠã€ããããšã§ãã
é段ã¯ç©ºã®ããŒã¹ããå§ãŸããéåžžã¯éåžžã«é«éã§ããã€ãŸãããã®ãã¹ãã¯ããŒã¿æ§é ã«é¢ãããã®ã§ããããã¯ã移è¡æã«ããŒã¿ãå€æŽããããšã§ã¯ãããŸãããããããå šäœãšããŠãããã¯ããªãã®åœãéåžžã«ããæãããšãã§ããŸãã
è¿ éãªä¿®æ£ãå¿ èŠãªå Žåã¯ããããããã§ãããã®ã«ãŒã«ã¯ã§ããååãšããŠããããžã§ã¯ãã«æ¿å ¥ãããšãç°¡åã«ãªããŸãã
ãã®ãã¹ãã¯æ¬¡ã®ããã«ãªããŸãããã¹ãŠã®ãªããžã§ã³ãååŸããAlembicæ§æãçæããŸãããããåã«èŠããã®ãalembic.iniãã¡ã€ã«ã§ãããããget_alembic_configé¢æ°ã§ããããŒã¹ãžã®ãã¹ãæå®ããããããã®ãã¡ã€ã«ãèªã¿åããäžæããŒã¹ãè¿œå ããŸãããã®åŸãAlembicã³ãã³ãã䜿çšã§ããŸãã
以åã«å®è¡ãããã³ãã³ãïŒalembic upgrade headïŒãå®å šã«ã€ã³ããŒãã§ããŸããæ®å¿µãªããããã®ã¹ã©ã€ãã¯ãã¹ãŠã®ã€ã³ããŒãã«é©åããããã§ã¯ãããŸããããç§ã®èšèãä¿¡ããŠãã ãããããã¯ãalembic.comã®ã€ã³ããŒãã¢ããã°ã¬ãŒãããã®ãã®ã§ããããã§èšå®ã翻蚳ããã©ãã§ã¢ããã°ã¬ãŒããè¡ãããæ瀺ããŸãã次ã«ããããŠã³ã°ã¬ãŒãããšèšããŸãã
ããŠã³ã°ã¬ãŒãã§ã¯ã移è¡ã¯down_revisionãã€ãŸãåã®ãªããžã§ã³ããŸãã¯ã-1ãã«ããŒã«ããã¯ãããŸãã
ã-1ãã¯ãçŸåšã®ç§»è¡ãããŒã«ããã¯ããããã«Alembicã«æ瀺ããå¥ã®æ¹æ³ã§ããæåã®ç§»è¡ãéå§ããããšãã«éåžžã«é¢é£æ§ãããããã®down_revisionã¯Noneã§ãããAlembicAPIã§ã¯downgradeã³ãã³ãã«Noneãæž¡ãããšã¯ã§ããŸããã
ãã®åŸãupgradeã³ãã³ããå床å®è¡ãããŸãã
次ã«ãããŒã¿ã䜿çšããŠç§»è¡ããã¹ãããæ¹æ³ã«ã€ããŠèª¬æããŸãã
ããŒã¿ã®ç§»è¡ã¯ãéåžžã¯éåžžã«åçŽã«èŠãããããªãã®ã§ãããæãçããã®ã§ããããããŒãã«ããããŒã¿ãéžæãæ¿å ¥ãååŸããããããããã«ç°ãªã圢åŒã§å¥ã®ããŒãã«ã«è»¢éããããã«èŠããã§ããã-ãã£ãšç°¡åãªããšã¯äœã§ããããïŒ
ãã®ãã¹ãã«ã€ããŠã¯ãåã®ãã¹ããšã¯ç°ãªããéçºã«éåžžã«è²»çšãããããšèšãããŠããŸãã倧èŠæš¡ãªç§»è¡ãè¡ã£ããšãããã¹ãŠã®äžå€æ¡ä»¶ã確èªããã®ã«6æéãããããšããããŸãããããã¹ãŠã説æããŠãåé¡ãããŸãããããããç§ããã§ã«ãããã®ç§»è¡ãè¡ã£ãŠãããšããç§ã¯èœã¡çããŠããŸããã
ãã®ãã¹ãã¯ã©ã®ããã«æ©èœããŸããïŒã¢ã€ãã¢ã¯ãçŸåšãã¹ãããããã®ãŸã§ã®ãã¹ãŠã®ç§»è¡ãé©çšããããšã§ããå€æŽãããããŒã¿ã®ã»ãããããŒã¿ããŒã¹ã«æ¿å ¥ããŸããæé»çã«å€æŽãããå¯èœæ§ã®ããè¿œå ã®ããŒã¿ãæ¿å ¥ããããšãèããããšãã§ããŸãã次ã«ãã¢ããã°ã¬ãŒãããŸããããŒã¿ãæ£ããå€æŽãããããšã確èªããããŠã³ã°ã¬ãŒããå®è¡ããŠãããŒã¿ãæ£ããå€æŽãããããšã確èªããŸãã
ã³ãŒãã¯æ¬¡ã®ããã«ãªããŸããã€ãŸãããªããžã§ã³ã«ãããã©ã¡ãŒã¿åãããããã©ã¡ãŒã¿ã®ã»ããããããŸããããã§ãšã³ãžã³ãåãå ¥ãããã¹ããéå§ãã移è¡ãåãå ¥ããŸãã
次ã«ããã¹ããããrev_headã§ãããããŠã3ã€ã®ã³ãŒã«ããã¯ããããã¯ãã©ããã§å®çŸ©ããã³ãŒã«ããã¯ã§ãããäœããè¡ãããåŸã«åŒã³åºãããŸããããã§äœãèµ·ãã£ãŠããã®ãã確èªã§ããŸãã
äŸã¯ã©ãã«ãããŸããïŒ
ç§ã¯ããããã¹ãŠGitHubã®äŸã«è©°ã蟌ã¿ãŸãããããã«ã¯å®éã«ã¯å€ãã®ã³ãŒãã¯ãããŸããããã¹ã©ã€ãã«è¿œå ããã®ã¯éåžžã«å°é£ã§ããç§ã¯æãåºæ¬çãªããšã«èããããšããŸããã GitHubã«ã¢ã¯ã»ã¹ããŠããããžã§ã¯ãèªäœã§ã©ã®ããã«æ©èœãããã確èªã§ããŸãããããæãç°¡åãªæ¹æ³ã§ãã
ä»ã«æ³šæãæã䟡å€ããããã®ã¯äœã§ããïŒèµ·åæã«ãAlembicã¯èµ·åããããã©ã«ããŒã§alembic.iniæ§æãã¡ã€ã«ãæ¢ããŸãããã¡ãããç°å¢å€æ°ALEMBIC_CONFIGã䜿çšããŠãã¹ãæå®ã§ããŸãããããã¯å¿ ããã䟿å©ã§ãããããããšã¯éããŸããã
å¥ã®åé¡ïŒããŒã¿ããŒã¹ã«æ¥ç¶ããããã®æ å ±ã¯alembic.iniã§æå®ãããŠããŸãããå€ãã®å Žåãè€æ°ã®ããŒã¿ããŒã¹ãé çªã«æäœããæ©èœãå¿ èŠã§ããããšãã°ã移è¡ãã¹ããŒãžã«å±éããŠãããæ¬çªç°å¢ã«å±éããŸããäžè¬ã«ãSQLALCHEMY_URLç°å¢å€æ°ã§æ¥ç¶æ å ±ãæå®ã§ããŸãããããã¯ãœãããŠã§ã¢ã®ãšã³ããŠãŒã¶ãŒã«ã¯ããŸãæçœã§ã¯ãããŸããã
ãŸãããšã³ããŠãŒã¶ãŒããalembicããããã$ project $ -dbããŠãŒãã£ãªãã£ã䜿çšããæ¹ãã¯ããã«çŽæçã§ãã
ãããžã§ã¯ãã®äŸãèŠããšãã¯ãstaff-dbãŠãŒãã£ãªãã£ãèŠãŠãã ãããããã¯Alembicã®èãã©ãããŒã§ãããAlembicãã«ã¹ã¿ãã€ãºãããã1ã€ã®æ¹æ³ã§ããããã©ã«ãã§ã¯ããããžã§ã¯ãå ã®alembic.iniãã¡ã€ã«ããã®å Žæã«é¢é£ããŠæ€çŽ¢ããŸãããŠãŒã¶ãŒã圌女ãšåŒãã§ãããã©ã«ããŒããã圌女èªèº«ãæ§æãã¡ã€ã«ãèŠã€ããŸãããŸããstaff-dbã¯åŒæ°--db-urlãè¿œå ããŸããããã䜿çšããŠãããŒã¿ããŒã¹ã«æ¥ç¶ããããã®æ å ±ãæå®ã§ããŸãããããŠãéèŠãªããšã«ãäžè¬çã«åãå ¥ããããŠãã--helpãªãã·ã§ã³ãæž¡ããŠããã確èªããŠãã ãããçµå±ã®ãšããããŠãŒãã£ãªãã£ã®ååã¯çŽæçã§ãã
ãã¹ãŠã®å®è¡å¯èœãªãããžã§ã¯ãã³ãã³ãã¯ããstaffãã¢ãžã¥ãŒã«ã®ååã§å§ãŸããŸããRESTAPIãå®è¡ããstaff-apiãšãåºæ¬ç¶æ ã管çããstaff-dbã§ãããã®ãã¿ãŒã³ãç解ãããšãã¯ã©ã€ã¢ã³ãã¯ããã°ã©ã ã®ååãæžã蟌ã¿ããã«ããŒã ãå¿ããå Žåã§ããTABããŒãæŒãããšã§å©çšå¯èœãªãã¹ãŠã®ãŠãŒãã£ãªãã£ã衚瀺ã§ããããã«ãªããŸããç§ã¯ãã¹ãŠãæã£ãŠããŸããããããšãã