<?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>SmartLogic Solutions Blog &#187; MySQL</title>
	<atom:link href="http://blog.smartlogicsolutions.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.smartlogicsolutions.com</link>
	<description>News and updates from the people at SmartLogic Solutions</description>
	<lastBuildDate>Tue, 30 Nov 2010 21:39:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Setting Up Ubuntu 9.10 for Ruby On Rails Development</title>
		<link>http://blog.smartlogicsolutions.com/2010/02/01/setting-up-ubuntu-9-10-for-ruby-on-rails-development/</link>
		<comments>http://blog.smartlogicsolutions.com/2010/02/01/setting-up-ubuntu-9-10-for-ruby-on-rails-development/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 21:09:31 +0000</pubDate>
		<dc:creator>Adam Bachman</dc:creator>
				<category><![CDATA[Adam Bachman]]></category>
		<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Passenger]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby Enterprise Edition]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[environment]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://blog.smartlogicsolutions.com/?p=860</guid>
		<description><![CDATA[This is a document I put together at the beginning of 2010 while building yet another Ubuntu VM, digging through our internal documentation to try and find out what I needed. We've got the answers, and generally Ruby, Rails, and Ubuntu are pretty good about telling you how to install tools if you don't have them yet.

But the answers are spread out and distributed randomly. Plus, I can only see "The program '______' is currently not installed. You can install it by typing: sudo apt-get install ______" so many times before I lose interest and put off the task.

So, to prevent future headaches and help all of us out, I put together a how-to that can take a new Rails system from zero to code in a few minutes (depending on network speed) instead of half a day.]]></description>
			<content:encoded><![CDATA[<p>This is a document I put together at the beginning of 2010 while building yet another Ubuntu VM, digging through our internal documentation to try and find out what I needed. We&#8217;ve got the answers, and generally Ruby, Rails, and Ubuntu are pretty good about telling you how to install tools if you don&#8217;t have them yet.</p>
<p>But the answers are spread out and distributed randomly. Plus, I can only see &#8220;The program &#8216;______&#8217; is currently not installed. You can install it by typing: sudo apt-get install ______&#8221; so many times before I lose interest and put off the task.</p>
<p><span id="more-860"></span></p>
<p>So, to prevent future headaches and help all of us out, I put together a how-to that can take a new Rails system from zero to code in a few minutes (depending on network speed) instead of half a day. With no further ado,</p>
<h2>Setting up an Ubuntu box for Rails Development (including virtual machines)</h2>
<h4>Update everything</h4>
<pre>$ sudo aptitude update &amp;&amp; sudo aptitude dist-upgrade</pre>
<h4>Install necessary packages</h4>
<pre>$ sudo aptitude install build-essential vim \
  vim-runtime git-core subversion libsqlite3-dev</pre>
<p>We have projects in git (the newer ones) and svn, so we have to be able to switch fluidly between the two.</p>
<p>And I&#8217;m not trying to start a flame war, but Vim is the first choice for Rails development. You don&#8217;t have to take my word for it; <a href="http://weblog.jamisbuck.org/2008/10/10/coming-home-to-vim">Jamis Buck</a>, <a href="http://github.com/tpope">Tim Pope</a>, and <a href="http://www.adamlowe.me/2009/12/vim-destroys-all-other-rails-editors.html">Adam Lowe</a> all say the same. I recommend the NERDTree, FuzzyFinderTextMate, BufExplorer, and rails.vim plugins if nothing else.</p>
<h4>Install ruby</h4>
<pre>$ sudo aptitude install ruby1.8 ruby rdoc ruby1.8-dev \
  libpgsql-ruby1.8 libmysql-ruby1.8 irb libopenssl-ruby</pre>
<h4>Get rubygems from source. <a href="http://docs.rubygems.org/">http://docs.rubygems.org/</a></h4>
<pre>$ wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz</pre>
<pre>$ tar xzvf rubygems-1.3.5.tgz</pre>
<pre>$ cd rubygems-1.3.5</pre>
<pre>$ sudo ruby setup.rb</pre>
<pre>$ sudo ln -s /usr/bin/gem1.8 /usr/bin/gem</pre>
<pre>$ sudo gem update --system</pre>
<h4>Update ~/.gemrc (at least the :sources: chunk)</h4>
<pre>---
gem: --no-ri --no-rdoc
:benchmark: false
:verbose: true
:backtrace: false
:update_sources: true
:sources:
- http://gemcutter.org
- http://gems.rubyforge.org/
- http://gems.github.com</pre>
<h4>Install and setup Passenger (optional)</h4>
<pre>$ sudo apt-get install apache2-prefork-dev</pre>
<pre>$ sudo gem install passenger</pre>
<pre>$ sudo passenger-install-apache2-module</pre>
<p>and follow the instructions passenger gives you. Most folks in the office run some form of an Apache-Passenger stack for development purposes. I can explain how that setup works in a future post.</p>
<h4>Install geminstaller</h4>
<pre>$ sudo gem install geminstaller</pre>
<p>Almost all of our legacy projects use <a href="http://geminstaller.rubyforge.org/">geminstaller</a> to manage gem dependencies. In the future, new projects will be moving to <a href="http://github.com/wycats/bundler">Bundler</a>, but legacy projects matter and upgrading subsystems is not always justified.</p>
<p>&#8220;If it ain&#8217;t broke, don&#8217;t fix it&#8221; and the corollary: &#8220;because you probably won&#8217;t be able to bill it to the client&#8221; both apply.</p>
<h4>Install common packages (optional, may be required by projects)</h4>
<pre>$ sudo aptitude install imagemagick</pre>
<pre>$ sudo gem install utility_belt open_gem redgreen</pre>
<p>The first (imagemagick) is used by the Paperclip gem to manage files attached to Rails models. The second are tools that make Rails development more manageable on the command line. I highly recommend them. Besides, I&#8217;ve been sneaking redgreen in as a test dependency on our projects, so if you&#8217;re a developer here you&#8217;ll have to install it anyways.</p>
<h4>Create an ssh key (follow instructions)</h4>
<pre>$ ssh-keygen</pre>
<h3>EXAMPLE PROJECT</h3>
<p>All the stuff above won&#8217;t let you test-code-deploy without hassles, so here&#8217;s a sample run through of what the last mile of project-specific setup looks like.</p>
<h4>Get the project. SVN, Git, whatever. Go to it.</h4>
<pre>$ svn co rails-project</pre>
<pre>$ cd rails-project</pre>
<h4>Run geminstaller</h4>
<pre>$ sudo geminstaller                                 # normal</pre>
<pre>$ sudo geminstaller -c config/test/geminstaller.yml # test env</pre>
<h4>Install necessary aptitude packages</h4>
<pre>$ sudo aptitude install texlive-latex-base</pre>
<p>Don&#8217;t ask.</p>
<h4>Create the test database</h4>
<p>Mysql:</p>
<pre>$ rake RAILS_ENV=test db:create</pre>
<p>Postgres (using your username or DB username in place of &#8220;username&#8221;):</p>
<pre>$ sudo -u postgres psql</pre>
<pre>postgres=# CREATE USER username SUPERUSER;</pre>
<pre>postgres=# CREATE DATABASE username OWNER username;</pre>
<p>If you get an error regarding PGconn.quote_ident (Rails 2.3.x on Ubuntu 8.04) patch <code>activerecord-2.3.4/lib/active_record/connection_adapters/postgresql_adapter.rb</code> and add the following:</p>
<pre>def PGconn.quote_ident(name)
  %("#{name}")
end</pre>
<p>immediately after the &#8220;class PGresult&#8221; definition at the top of the file. Once that&#8217;s all done, the following:</p>
<pre>$ rake RAILS_ENV=test db:create</pre>
<pre>$ rake RAILS_ENV=test db:schema:load</pre>
<pre>$ rake RAILS_ENV=test test</pre>
<p>should work.</p>
<h3>go have fun, write code, blow our minds</h3>
<p>So that&#8217;s how we roll. Awkward requirements will still crop up as we pick up new projects (or very old projects) so you&#8217;ll always need a bit of apt/gem fu to get you through the day. Leave questions here or drop me a line at <a href="mailto:adam@smartlogicsolutions.com">adam@smartlogicsolutions.com</a> or <a href="http://twitter.com/abachman">http://twitter.com/abachman</a>.</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.smartlogicsolutions.com/2008/07/07/i-cant-upgrade-rubygems-from-111-to-120-on-ubuntu/" rel="bookmark" class="crp_title">I can&#8217;t upgrade RubyGems from 1.1.1 to 1.2.0 on Ubuntu</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/04/26/integrity-ci-on-passenger-222-with-ruby-enterprise-edition-on-ubuntu-804/" rel="bookmark" class="crp_title">Integrity CI on Passenger 2.2.2 with Ruby Enterprise Edition on Ubuntu 8.04</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/01/14/aptinstaller-032-released/" rel="bookmark" class="crp_title">Aptinstaller 0.3.2 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/08/04/introducing-environmentalize-an-intuitive-environment-focused-config-structure-for-your-rails-applications/" rel="bookmark" class="crp_title">Introducing environmentalist: an intuitive, environment-focused config structure for your rails applications</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/06/06/deploying-rails-apps-with-capistrano-without-root-or-sudo-privileges/" rel="bookmark" class="crp_title">Deploying Rails Apps with Capistrano without root or sudo Privileges</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.smartlogicsolutions.com/2010/02/01/setting-up-ubuntu-9-10-for-ruby-on-rails-development/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Rails 2.1 broke my mysql foreign keys!</title>
		<link>http://blog.smartlogicsolutions.com/2008/06/24/rails-21-broke-my-mysql-foreign-keys/</link>
		<comments>http://blog.smartlogicsolutions.com/2008/06/24/rails-21-broke-my-mysql-foreign-keys/#comments</comments>
		<pubDate>Tue, 24 Jun 2008 21:03:36 +0000</pubDate>
		<dc:creator>John Trupiano</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[John Trupiano]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.smartlogicsolutions.com/2008/06/24/rails-21-broke-my-mysql-foreign-keys/</guid>
		<description><![CDATA[Rails 2.1 introduced in the MySQL Adapter &#8220;smart integer columns.&#8221; The idea was to use the :limit option to determine whether a smallint, int, or bigint should be used. This is something that the Postgres adapter had already previously implemented. The relevant code in activerecord/lib/active_record/connection_adapters/mysql_adapter.rb is: # Maps logical Rails types to MySQL-specific data types. [...]]]></description>
			<content:encoded><![CDATA[<p>Rails 2.1 introduced in the MySQL Adapter &#8220;smart integer columns.&#8221;  The idea was to use the :limit option to determine whether a smallint, int, or bigint should be used.  This is something that the Postgres adapter had already previously implemented.  The relevant code in activerecord/lib/active_record/connection_adapters/mysql_adapter.rb is:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># Maps logical Rails types to MySQL-specific data types.</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> type_to_sql<span style="color:#006600; font-weight:bold;">&#40;</span>type, limit = <span style="color:#0000FF; font-weight:bold;">nil</span>, precision = <span style="color:#0000FF; font-weight:bold;">nil</span>, scale = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">super</span> <span style="color:#9966CC; font-weight:bold;">unless</span> type.<span style="color:#9900CC;">to_s</span> == <span style="color:#996600;">'integer'</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">case</span> limit
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">0</span>..<span style="color:#006666;">3</span>
      <span style="color:#996600;">&quot;smallint(#{limit})&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">4</span>..<span style="color:#006666;">8</span>
      <span style="color:#996600;">&quot;int(#{limit})&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">9</span>..<span style="color:#006666;">20</span>
      <span style="color:#996600;">&quot;bigint(#{limit})&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#996600;">'int(11)'</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Mirko Froehlich <a href="http://rails.lighthouseapp.com/projects/8994/tickets/55-remove-default-integer-limit-from-mysqladapter-native_database_types">suggests monkey patching this function</a>.  Timothy Jones <a href="http://timothynjones.wordpress.com/2008/06/10/change-to-activerecord-mysql-adapter-breaks-tests/">blogged about it</a>.</p>
<p>To monkey-patch this, just drop a file (fix_mysql_adapter.rb) into your initializers/ directory, as such:</p>
<p><span id="more-53"></span></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ActiveRecord
  <span style="color:#9966CC; font-weight:bold;">module</span> ConnectionAdapters
    <span style="color:#9966CC; font-weight:bold;">class</span> MysqlAdapter <span style="color:#006600; font-weight:bold;">&lt;</span> AbstractAdapter
      <span style="color:#008000; font-style:italic;"># Maps logical Rails types to MySQL-specific data types.</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> type_to_sql<span style="color:#006600; font-weight:bold;">&#40;</span>type, limit = <span style="color:#0000FF; font-weight:bold;">nil</span>, precision = <span style="color:#0000FF; font-weight:bold;">nil</span>, scale = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">super</span> <span style="color:#9966CC; font-weight:bold;">unless</span> type.<span style="color:#9900CC;">to_s</span> == <span style="color:#996600;">'integer'</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">case</span> limit
        <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">0</span>..<span style="color:#006666;">3</span>
          <span style="color:#996600;">&quot;smallint(#{limit})&quot;</span>
        <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">4</span>..<span style="color:#006666;">11</span>
          <span style="color:#996600;">&quot;int(#{limit})&quot;</span>
        <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">12</span>..<span style="color:#006666;">20</span>
          <span style="color:#996600;">&quot;bigint(#{limit})&quot;</span>
        <span style="color:#9966CC; font-weight:bold;">else</span>
          <span style="color:#996600;">'int(11)'</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>All we&#8217;ve done is change how the mysql adapter interprets the limit attribute.  In my opinion, this isn&#8217;t a particularly great solution, as it&#8217;s more appropriate for non-foreign key integers to behave as the rails team suggested.  This really only affects us when we&#8217;re dealing with foreign keys, because mysql enforces our column types to match.</p>
<p><strong>The Real Problem</strong><br />
So, if the problem isn&#8217;t here, where is it?  The problem actually lies in the schema dumper.  Notice in the previous code samples the else clause in the case statement.  If limit is nil (or outside of 0..20), then this falls back to int(11).  Curiously, when I have a migration such as the following, the schema dumper adds a :limit => 11, even though I didn&#8217;t specify it!</p>
<pre>
$> rails test -d mysql
$> cd test
$> ruby script/generate model user name:string
$> ruby script/generate model game name:string user_id:integer
$> rake db:create db:migrate
</pre>
<p>First, let&#8217;s check our game migration to verify that it doesn&#8217;t specify :limit => 11.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> CreateGames <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:games</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
      t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:name</span>
      t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:user_id</span>
&nbsp;
      t.<span style="color:#9900CC;">timestamps</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:games</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>So, there&#8217;s no limit specified.  But let&#8217;s take a look at schema.rb.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Schema</span>.<span style="color:#9900CC;">define</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">20080624161220</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
  create_table <span style="color:#996600;">&quot;games&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:force</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
    t.<span style="color:#CC0066; font-weight:bold;">string</span>   <span style="color:#996600;">&quot;name&quot;</span>
    t.<span style="color:#CC0066; font-weight:bold;">integer</span>  <span style="color:#996600;">&quot;user_id&quot;</span>,    <span style="color:#ff3333; font-weight:bold;">:limit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">11</span>
    t.<span style="color:#9900CC;">datetime</span> <span style="color:#996600;">&quot;created_at&quot;</span>
    t.<span style="color:#9900CC;">datetime</span> <span style="color:#996600;">&quot;updated_at&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  create_table <span style="color:#996600;">&quot;users&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:force</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
    t.<span style="color:#CC0066; font-weight:bold;">string</span>   <span style="color:#996600;">&quot;name&quot;</span>
    t.<span style="color:#9900CC;">datetime</span> <span style="color:#996600;">&quot;created_at&quot;</span>
    t.<span style="color:#9900CC;">datetime</span> <span style="color:#996600;">&quot;updated_at&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Ah ha!  Where&#8217;d that :limit => 11 come from?  The schema generator!!!  This is our culprit.  We need to isolate the schema generation code, and ensure that isn&#8217;t forcing :limit => 11 onto our integers that don&#8217;t explicitly set a limit.</p>
<p>As I started down this track, I hooked up with Rob Sterner from <a href="http://itasoftware.com">ITA Software</a> on IRC.  He filed the <a href="http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/463">original ticket</a>.  By the time I got in touch with him, he pretty much had the problem solved.</p>
<p>First, let&#8217;s fully illustrate the problem.  When you run migrations, naked integer statements</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:myvalue</span></pre></div></div>

<p>are inserted into the database with :limit => 11 (this is handled by the extract_limit() function in the adapters).  SchemaDumper.dump is called after your migrations are finished, and creates schema.rb by inspecting the database (not your migrations &#8212; which makes sense).  However, when extracting the information from the database, the SchemaDumper picks up this limit of 11, which causes it to write this out in your schema.rb file:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:myvalue</span>, <span style="color:#ff3333; font-weight:bold;">:limit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">11</span></pre></div></div>

<p>See a problem?  Yup, where in our migration we did not explicitly set a limit, the SchemaDumper did!</p>
<p><strong>The Solution</strong><br />
So what&#8217;s the solution?  Well, what we need to do is alter SchemaDumper to identify int(11), and special case the output (so that :limit => 11 is not appended).  At first, this seemed very hacky to me.  What if I want a bigint(11) and not int(11)?  Well, turns out this isn&#8217;t a valid concern.  Why?  If we really wanted bigint(11), our migration would look different:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:myvalue</span>, <span style="color:#ff3333; font-weight:bold;">:limit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">11</span></pre></div></div>

<p>So, when migrations are run, this field would be placed into the database as a bigint(11) column.  And, when SchemaDumper encounters it, it will also see it as bigint(11),  As long as our special case discriminates between bigint(11) and int(11), then we&#8217;re in the clear!  The fact is, in rails 2.1, there&#8217;s no way to get an int(11) column in mysql unless you&#8217;ve left off the :limit in your migration.</p>
<p><strong>Where&#8217;s the patch?</strong><br />
Funny (read: agonizing) story&#8230;..we were testing all of this against 2.1.0.  When we checked out edge rails, all of this code had changed!!!  In fact, looks like Jeremy Kemper (bitsweat) already fixed it.</p>
<p>For the sake of completeness, I&#8217;m including the patch that Rob threw together that does the trick for 2.1.0.  This patch applied to /activerecord-2.1.0/lib/active_record/connection_adapters/mysql_adapter.rb does the trick.</p>
<pre>
127c127,129
<           else
---
>           elsif sql_type == 'int(11)'
>             nil # special case for :integer columns w/ no explicit :limit set in their migration
>           else
</pre>
<p><strong>The Lesson</strong><br />
Check out edge rails to make sure that the problem is still unresolved before you spend all day on something!!!</p>
<p>Also, be aware that edge has changed the meaning of :limit.  See <a href="http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/463">the ticket</a> for Jeremy&#8217;s explanation.  Up through Rails 2.1, the :limit attribute for integers dictated the display width.  In other words, it did not specify the storage size, but rather the amount of space mysql would use to display it when returning query results.  Moving forward, it will be used to indicate the number of bytes to use for storage.  Rails will now effectively use tinyint (1), smallint (2), mediumint (3), int (4) and bigint (8).  So, prepare yourselves accordingly.</p>
<p>Special thanks to Rob Sterner for spending lots of time today working through this with me.</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.smartlogicsolutions.com/2008/06/13/ruby-on-rails-polymorphic-association-benchmarks/" rel="bookmark" class="crp_title">Ruby on Rails Polymorphic Association Benchmarks</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/10/10/making-willpaginate-and-rails-to_xml-play-nice-with-activeresource/" rel="bookmark" class="crp_title">Making WillPaginate and Rails to_xml play nice with ActiveResource</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/10/28/faking-a-will-paginate-collection-on-an-active-resource-model/" rel="bookmark" class="crp_title">Faking a Will Paginate Collection on an Active Resource model</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/06/05/sexier-migrations-in-rails-21/" rel="bookmark" class="crp_title">Sexier Migrations in Rails 2.1</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/07/11/problems-with-restful_authentication-plugin-and-internet-explorer-cookies/" rel="bookmark" class="crp_title">Problems with restful_authentication Plugin and Internet Explorer Cookies</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.smartlogicsolutions.com/2008/06/24/rails-21-broke-my-mysql-foreign-keys/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

