<?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/"
	>

<channel>
	<title>LolocoJr &#187; ruby php migrations mysql postgresql</title>
	<atom:link href="http://www.railsguru.com/articles/tag/ruby-php-migrations-mysql-postgresql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.railsguru.com</link>
	<description>Andy Lo-A-Foe&#039;s blog</description>
	<lastBuildDate>Fri, 09 Jul 2010 12:08:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Rewriting a (large) PHP application in Rails, part 1</title>
		<link>http://www.railsguru.com/articles/2006/12/13/rewriting-a-large-php-application-in-rails-part-1/</link>
		<comments>http://www.railsguru.com/articles/2006/12/13/rewriting-a-large-php-application-in-rails-part-1/#comments</comments>
		<pubDate>Wed, 13 Dec 2006 02:27:00 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[Home]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby php migrations mysql postgresql]]></category>

		<guid isPermaLink="false">http://www.railsguru.com/2007/04/16/rewriting-a-large-php-application-in-rails-part-1</guid>
		<description><![CDATA[<p>
In recent weeks I was busy converting a fairly large PHP application to Rails. The existing PHP application is about 65.500 lines of intermingled PHP and HTML/CSS code. Yep, a classic PHP application without any database abstraction layer, no templating, no MVC. This is why I dubbed it &#8220;large&#8221;, but replacing that with &#8220;crappy&#8221; would [...]]]></description>
			<content:encoded><![CDATA[<p>
In recent weeks I was busy converting a fairly large <span class="caps">PHP</span> application to Rails. The existing <span class="caps">PHP</span> application is about 65.500 lines of intermingled <span class="caps">PHP</span> and <span class="caps">HTML</span>/CSS code. Yep, a classic <span class="caps">PHP</span> application without any database abstraction layer, no templating, no <span class="caps">MVC</span>. This is why I dubbed it &#8220;large&#8221;, but replacing that with &#8220;crappy&#8221; would be fine too <img src='http://www.railsguru.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</p>
<p>
I divided the project in a couple of deliverables:</p>
<ol>
<li>Data model analysis, redesign, data migration</li>
<li><span class="caps">HTML</span> conversion to layout/partials</li>
<li>Business logic analysis and conversion to Ruby</li>
<li>Integration with external web application</li>
<li>Testing</li>
<li>Production deployment</li>
</ol>
<p>In typical agile/xp/scrum/ad-hoc/you-name-it fashion, all but the last deliverable were not actually delivered in full until production day.</p>
<p>
Since I wanted to get something visible as soon as possible and with semi-live data the first thing I did was analyze the current data model. The model consisted of 46 tables. 8 tables were discarded right away (code/data rot). After further analysis the table count was reduced to 23. Next up was converting and migrating the current data set to the new data model. The <span class="caps">PHP</span> site was using Mysql 4.23 while the Rails version would be using PostgreSQL 8.1. This was <span class="caps">BTW</span> also the first Rails project where I religiously used migrations and <strong>migrations absolutely rock!</strong>. A total of 53 migration scripts were generated during the course of development. Back to conversion; Instead of writing <span class="caps">CSV</span>/YAML exporter/importer scripts I used the following mechanism to import/convert legacy data objects:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt>14<tt>
</tt>15<tt>
</tt>16<tt>
</tt>17<tt>
</tt>18<tt>
</tt>19<tt>
</tt><strong>20</strong><tt>
</tt>21<tt>
</tt>22<tt>
</tt>23<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="c"># Read in Rails config</span><tt>
</tt>config = <span class="co">Rails</span>::<span class="co">Configuration</span>.new<tt>
</tt><tt>
</tt><span class="c"># Set up the class for the table/objects we want to convert</span><tt>
</tt><span class="c"># Of course the legacy database does not follow Rails conventions</span><tt>
</tt><span class="c"># so we always use table_name to fix this</span><tt>
</tt><span class="r">class</span> <span class="cl">OldVenue</span> &lt; <span class="co">ActiveRecord</span>::<span class="co">Base</span><tt>
</tt>  <span class="r">def</span> <span class="pc">self</span>.table_name<tt>
</tt>    <span class="s"><span class="dl">&quot;</span><span class="k">adressen</span><span class="dl">&quot;</span></span><tt>
</tt>  <span class="r">end</span><tt>
</tt><span class="r">end</span><tt>
</tt><tt>
</tt><span class="c"># Set up a connection to the legacy MySQL database</span><tt>
</tt><span class="co">ActiveRecord</span>::<span class="co">Base</span>.establish_connection config.database_configuration[<span class="s"><span class="dl">&quot;</span><span class="k">old_production</span><span class="dl">&quot;</span></span>]<tt>
</tt><tt>
</tt><span class="c"># Save the connection</span><tt>
</tt>mysql_connection = <span class="co">OldVenue</span>.connection<tt>
</tt><tt>
</tt><span class="c"># Establish a connection to our new shiny PostgreSQL database</span><tt>
</tt><span class="co">ActiveRecord</span>::<span class="co">Base</span>.establish_connection config.database_configuration[<span class="s"><span class="dl">&quot;</span><span class="k">production</span><span class="dl">&quot;</span></span>]<tt>
</tt><tt>
</tt><span class="c"># But restore the mysql connection for the legacy ActiveRecord class</span><tt>
</tt><span class="co">OldVenue</span>.connection = mysql_connection</pre>
</td>
</tr>
</table>
<p>The above code allows the conversion script to access both MySQL and PostgreSQL databases simultaneously.This means I can do something like:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">old_venues = <span class="co">OldVenue</span>.find(<span class="sy">:all</span>)<tt>
</tt><tt>
</tt><span class="r">for</span> o <span class="r">in</span> old_venues <span class="r">do</span><tt>
</tt>  n = <span class="co">Venue</span>.new<tt>
</tt>  n.id = o.id<tt>
</tt>  n.name = o.naam<tt>
</tt>  n.zip = o.postcode<tt>
</tt>  <span class="c"># More field assignments</span><tt>
</tt>  n.save<tt>
</tt><span class="r">end</span></pre>
</td>
</tr>
</table>
<p>..Instant data migration from MySQL to PostgreSQL without messy <span class="caps">CSV</span>/YAML export/import!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsguru.com/articles/2006/12/13/rewriting-a-large-php-application-in-rails-part-1/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>
