{"id":47,"date":"2012-05-13T07:48:00","date_gmt":"2012-05-13T12:48:00","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/acts\/?p=47"},"modified":"2012-07-01T00:45:33","modified_gmt":"2012-07-01T04:45:33","slug":"abstracting-schemas-for-mysql-and-oracle-with-yii-migrations","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/acts\/2012\/05\/13\/abstracting-schemas-for-mysql-and-oracle-with-yii-migrations\/","title":{"rendered":"Abstracting schemas for MySQL and Oracle with Yii Migrations"},"content":{"rendered":"<p>Yii migrations are an interesting way to keep track of your database changes. \u00a0I was originally looking for a tool that would abstract the schema SQL &#8212; with something like the Yii ActiveRecord &#8212; so I wouldn&#8217;t have to write duplicate code for Oracle and MySQL. \u00a0I mean, that&#8217;s kind of the point of going with the PDO abstraction.<\/p>\n<p>So more than a few forum question\/answers led me to Yii migration:<\/p>\n<p><a href=\"http:\/\/www.yiiframework.com\/doc\/guide\/1.1\/en\/database.migration\">http:\/\/www.yiiframework.com\/doc\/guide\/1.1\/en\/database.migration<\/a><\/p>\n<p>I seriously have that page open 3 times in my tabs and I have read it top to bottom probably 20 times. \u00a0I keep going back to it over and over hoping it will have more information. \u00a0It&#8217;s just a little short on answers to all of my questions.<\/p>\n<p>As I said, all I wanted was a way to abstract the schema, so the original premise of &#8220;migrations&#8221; was a little different than what I was looking for, but it being the only non-custom option, I went forward with it. \u00a0The point of migrations is to make sure the state of your database matches the revision of your code. \u00a0So it&#8217;s sort of like its own hokey versioning system. \u00a0As such, it doesn&#8217;t function as simply as I might like it. \u00a0It&#8217;s not easy to focus on one migration if it&#8217;s not the last migration. \u00a0Because the point of migrations is that you&#8217;re moving forward and editing a past migration would be like editing revision X without it effecting all of the future revisions. \u00a0So it makes sense for what it is, it&#8217;s just awkward. \u00a0For one the file structure HAS to include the timestamp. \u00a0And it has to include that timestamp at the front of the filename, so autocompleting these migration filenames is a pain in the ass.<\/p>\n<p>One important thing they don&#8217;t talk about in the documentation is that the list of what&#8217;s been migrated is added automatically to your database via the &#8220;tbl_migrations&#8221; &#8212; and please remember the &#8220;s as it will insert it as lowercase, which could be annoying for Oracle users, though Yii seems to handle it &#8220;appropriately&#8221;.<\/p>\n<p>The only major hurdle here is dealing with autoincrement.  The way this was dealt with was I created an Autoincrement class that I plopped in the migrations directory:<br \/>\n<a href=\"https:\/\/github.com\/jazahn\/Quizmo\/blob\/master\/quizmo\/protected\/migrations\/Autoincrement.php\" title=\"Autoincrement\">protected\/migrations\/Autoincrement.php<\/a><\/p>\n<p>This meant that I could just create the tables like so:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nclass m120402_194059_Quizes extends CDbMigration\r\n{\r\n\tpublic function up()\r\n\t{\r\n\r\n\t\t$this-&gt;createTable(&#039;QUIZES&#039;, array(\r\n\t\t\t&#039;ID&#039; =&gt; &#039;pk&#039;,\r\n\t\t\t&#039;COLLECTION_ID&#039; =&gt; &#039;integer NOT NULL&#039;,\r\n\t\t\t&#039;TITLE&#039; =&gt; &#039;string NOT NULL&#039;,\r\n\t\t\t&#039;DESCRIPTION&#039; =&gt; &#039;string&#039;,\r\n\t\t\t&#039;VISIBILITY&#039; =&gt; &quot;integer NOT NULL&quot;,\r\n\t\t\t&#039;STATE&#039; =&gt; &#039;string&#039;,\r\n\t\t\t&#039;SHOW_FEEDBACK&#039; =&gt; &#039;integer&#039;,\r\n\t\t\t&#039;START_DATE&#039; =&gt; &#039;datetime&#039;,\r\n\t\t\t&#039;END_DATE&#039; =&gt; &#039;datetime&#039;,\r\n\t\t\t&#039;DATE_MODIFIED&#039; =&gt; &#039;datetime&#039;,\r\n\t\t\t&#039;DELETED&#039; =&gt; &quot;integer NOT NULL&quot;,\r\n\t\t));\t\r\n\r\n\t\tAutoincrement::up(&#039;QUIZES&#039;, Yii::app()-&gt;db-&gt;driverName);\r\n\r\n\r\n\t}\r\n\r\n\tpublic function down()\r\n\t{\r\n\r\n\t\tAutoincrement::down(&#039;QUIZES&#039;, Yii::app()-&gt;db-&gt;driverName);\r\n\r\n\t\t$this-&gt;dropTable(&#039;QUIZES&#039;);\r\n\r\n\t}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Yii migrations are an interesting way to keep track of your database changes. \u00a0I was originally looking for a tool that would abstract the schema SQL &#8212; with something like the Yii ActiveRecord &#8212; so I wouldn&#8217;t have to write duplicate code for Oracle and MySQL. \u00a0I mean, that&#8217;s kind of the point of going [&hellip;]<\/p>\n","protected":false},"author":4571,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[63924,1841,63941,63942,3163,63928,63937],"tags":[63965,63951,3159,4034,63964,63943],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-atg","category-databases","category-mysql-databases","category-oracle-databases","category-php","category-quizmo-atg","category-yii","tag-abstraction","tag-migration","tag-mysql","tag-oracle","tag-schema","tag-yii-2"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/users\/4571"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":4,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"predecessor-version":[{"id":230,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts\/47\/revisions\/230"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}