<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>Ovalpixels Blog about Web-Building &#187; installed applications</title>
	<atom:link href="http://www.ovalpixels.com/blog/tag/installed-applications/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ovalpixels.com/blog</link>
	<description>Tips &#38; Tricks on Web Design and Development</description>
	<lastBuildDate>Fri, 28 Jan 2011 09:17:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CakePHP Migrations without PEAR</title>
		<link>http://www.ovalpixels.com/blog/2008/11/17/cakephp-migrations-without-pear/</link>
		<comments>http://www.ovalpixels.com/blog/2008/11/17/cakephp-migrations-without-pear/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 21:19:31 +0000</pubDate>
		<dc:creator>georgi</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Bakery Joel Moss]]></category>
		<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Chris Wanstrath]]></category>
		<category><![CDATA[installed applications]]></category>
		<category><![CDATA[Joel Moss]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.ovalpixels.com/blog/?p=67</guid>
		<description><![CDATA[Rails fans out there? It is no secret that CakePHP was born to implement the ideas of Ruby on Rails to the world of PHP &#8211; wonderful ideas, indeed, and I think we should pay tribute to the guys at 37signals. But what CakePHP still lacks, in my opinion, is pure and complete DB abstraction. [...]]]></description>
			<content:encoded><![CDATA[<h3>Rails fans out there?</h3>
<p>It is no secret that CakePHP was born to implement the ideas of Ruby on Rails to the world of PHP &#8211; wonderful ideas, indeed, and I think we should pay tribute to the guys at 37signals.</p>
<p>But what CakePHP still lacks, in my opinion, is pure and complete DB abstraction. It is true that most recent versions of Cake ( v.2 RC3 as of now ) have done a lot in that area &#8211; the framework is now strongly decoupled from MySQL, although I personally have never heard of study cases that use Oracle, for example.</p>
<p>To the point &#8211; in a multi-developer environment it is critical to have a versioning control system ( you know you should be using <a  href="http://git.or.cz" target="_blank">git</a>, right? ) and a database &#8216;versioning&#8217; system &#8211; that is where Rails is much more powerful than CakePHP &#8211; it has <strong>database migrations</strong>.<span id="more-67"></span></p>
<h3>Some migrations&#8217; background</h3>
<p>If you know what migrations are &#8211; skip that part and go directly to the next one. Otherwise &#8211; here is a simple explanation: they are related to the database like git ( or svn or cvs ) is related to the source. They simple provide a way to go back or forth to a previous or succeeding state ( or version ) of the database schema. The features of migrations are:</p>
<ul>
<li>They are DB agnostic &#8211; this means that they are not affected by the type of the DB server you&#8217;re using and help you in deploying your apps on different environments</li>
<li>They help you with your agile development &#8211; you can put info in and out without affecting the rest of the project</li>
<li>They provide you with a quick way to revert back to a previous state of the DB in case something went wrong</li>
<li>DB migrations can save your life if you work in a team</li>
</ul>
<p>To illustrate the all said, I have a simple example: You create a blog ( of course a blog &#8211; that&#8217;s probably the most often used example in the world ). Let&#8217;s split the development into three simple parts &#8211; development of blog posts CRUD; adding ACL; adding Comments to posts and.. that&#8217;s it &#8211; we want a simple example, after all.</p>
<p>So, first &#8211; blog posts Create, Read, Update, Delete. You begin by creating your DB schema. You need a simple posts&#8217; table with a few fields &#8211; id, title, body, created. Then you write your PHP code, of course ( out of scope here ).</p>
<p>But one beautiful day you decide more than one person should be able to add posts and modify their own posts only &#8211; that&#8217;s when you read about Access Control Lists. Now you need a few more tables &#8211; users, aros, acos, acl and you need to add a field to the posts&#8217; table ( user_id, for example ). Our small blog is getting just a bit more complex.</p>
<p>Finally, you want comments! But you have no time and ask your cousin for a favor ( he is a good dev, after all ). He adds another table for that. And then decides only registered users ( who cannot write new posts ) can add comments. And, of course, writes some more code. And inserts some more data in the db.</p>
<p>You ( and your cousin ) have went through 3 distinctive phases of agile development. You have, of course, used git ( oh, well, maybe svn or cvs ) to keep track of your code and make sure everything is ok and that you can revert back to a previous, stable version, if anything goes wrong. But then, one day &#8211; something really goes wrong. And you need to revert from state 3 to state 2 &#8211; i.e. remove the comments. But &#8230; your cousin is</p>
<ul>
<li> out of town</li>
<li>mad at you because you behaved badly on his last birthday party</li>
<li>has no idea what he did for you so many months ago</li>
</ul>
<p>No problem &#8211; you use your source versioning system and do the reversal. But.. something is still wrong &#8211; your database stays unchanged. What did you add from v2 to v3? Did you remove something? And you know nothing about Database Server X &#8211; can you write the SQLs to revert the schema?</p>
<p>That is where DB migrations come in and save your life. Well, maybe not that important with your blog but imagine 100+ development iterations and 10+ developers working simultaneously on your project and maintaining it on many different platforms &#8211; this can turn into a <em>small</em> nightmare.</p>
<h3>Joel Moss and his migrations</h3>
<p>In his <a  href="http://bakery.cakephp.org/articles/view/cake-db-migrations-v2-1">article</a> at the Bakery Joel Moss describes how to use his CakePHP migrations shell. Although that project was my inspiration and I highly respect his work, his approach has several drawbacks:</p>
<ul>
<li>it uses PEAR &#8211; I don&#8217;t like it &#8211; that&#8217;s why I use CakePHP. I do not find necessary to explain this &#8211; I guess it is highly subjective.</li>
<li>it is non-modular &#8211; you cannot use it to deploy applications &#8211; it is just a shell with no &#8216;core&#8217;</li>
<li>it cannot make a snapshot of your already existing schema &#8211; you haven&#8217;t used migrations yet? That&#8217;s something you will need.</li>
<li>it cannot merge your tables &#8211; that can be crucial when you already have different versions of the schema on different platforms and you just want them all to be standardized</li>
</ul>
<p>So, I proudly present you with</p>
<h3>My CakePHP Yaml Migrations and Fixtures</h3>
<p>The idea was born when <a  href="http://pagebakers.nl" target="_blank">Eelco</a> and I started working on PagebakeryCMS some time ago and was later further developed when we decided we needed a standardized CakePHP App Installer. You can get a <a  href="http://github.com/georgious/cakephp-yaml-migrations-and-fixtures" target="_blank">copy</a> of the project and follow it, too. What I personally find useful in it:</p>
<ul>
<li>no external libraries &#8211; only the SPYC class used to parse YAML files ( that is authored by Chris Wanstrath )</li>
<li>can easily complement your source versioning system &#8211; use it for team collaboration</li>
<li>can be used for DB abstraction installers &#8211; when deploying your applications on different platforms you don&#8217;t want to modify your SQLs &#8211; and the project is DB agnostic. The package also has fixtures ( well, the terms is not used correctly &#8211; this is for adding initial data to your database )</li>
<li>can be used to standardize already installed applications on different platforms</li>
<li>can help you make your apps DB agnostic by generating a YAML structure of your schema from where you can go on with the agnostic aproach</li>
</ul>
<h4>How it works</h4>
<p>Just put all files in your CakePHP vendors folder.</p>
<p>If you want to use the API only, simply include the classes in your code</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">App<span style="color: #339933;">::</span><span style="color: #004000;">Import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'vendor'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'migrations'</span> <span style="color: #009900;">&#41;</span></pre></div></div>

<p>and/or</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">App<span style="color: #339933;">::</span><span style="color: #004000;">Import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'vendor'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'fixtures'</span> <span style="color: #009900;">&#41;</span></pre></div></div>

<p>And then, for example</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$migrations</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Migrations<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$migrations</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>load<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'comments.yml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$migrations</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>up<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>For more usages &#8211; dig into the code a bit <img src='http://www.ovalpixels.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.ovalpixels.com/blog/2008/11/17/cakephp-migrations-without-pear/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

