<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Martinicity: Tag rake</title>
    <link>http://martinicity.net/articles_controller.rb/tag?tag=rake</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Mike Blake</description>
    <item>
      <title>Testing a Non-Rails Application Using Rails</title>
      <description>&lt;p&gt;Rails developers working on enterprise software projects are suprised to discover the lack of automated tests in many mature web applications. As frustrating as this can be, A lack of automated tests is also a tremendous opportunity to &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Learn your non-rails applications underlying database structure.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Demonstrate to a devlopment team the power of The Rails Framework.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Encourage automated testing.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Rails may be the quickest path to automate some basic test of your non-rails applications data model. These steps will get you all set up to write your automated tests in Ruby.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="#connect"&gt;Connect to Your Enterprise Database&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#conventions"&gt;Set DB Conventions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#safety"&gt;Safety Net&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#schema"&gt;Duplicate the Schema&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#extract"&gt;Extract Development Data&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4 id='connect'&gt;I. Connect to Your Enterprise Database from Rails.&lt;/h4&gt;

&lt;p&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Download and install any os driver, ruby gems, and rails adapters needed to connect to your database:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://wiki.rubyonrails.com/rails/pages/DatabaseDrivers"&gt;Supported Rails Databases&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;&lt;a href="http://wiki.rubyonrails.com/rails/pages/MySQL"&gt;MySQL&lt;/a&gt;
    &lt;br&gt;&lt;a href="http://www-128.ibm.com/developerworks/db2/library/techarticle/dm-0606dumbill/?ca=dgr-lnxw01DB24RubyonRails"&gt;DB2&lt;/a&gt;
    &lt;br&gt;&lt;a href="http://www.oracle.com/technology/pub/articles/saternos-ror-faq.html"&gt;Oracle&lt;/a&gt;
    &lt;br&gt;&lt;a href="http://wiki.rubyonrails.org/rails/pages/HowToSetupSybaseAdapterOnRails"&gt;Sybase&lt;/a&gt;
    &lt;br&gt;&lt;a href="http://wiki.rubyonrails.org/rails/pages/HowtoConnectToMicrosoftSQLServer"&gt;SQLServer&lt;/a&gt;
    &lt;br&gt;&lt;a href="http://wiki.rubyonrails.org/rails/pages/PostgreSQL"&gt;PostgreSQL&lt;/a&gt;
    &lt;br&gt;&lt;a href="http://wiki.rubyonrails.org/rails/pages/Firebird"&gt;Firebird&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id='conventions'&gt;II. Set Your Existing Database Conventions &lt;/h4&gt;

&lt;p&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.pragmaticprogrammer.com/titles/fr_rr/"&gt;Rails Recipes&lt;/a&gt;
, Recipie #16 can walk you through this very quickly.&lt;/p&gt;

&lt;p&gt;Identify any conventions used by your legacy database.  Application wide conventions can be set in the &lt;code&gt;config/environment.rb&lt;/code&gt; file.
They are specified by calling the appropriate class methods available on &lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html"&gt;ActiveRecord::Base&lt;/a&gt;. The methods you need to call depend on how your database is configured:&lt;/p&gt;

&lt;pre&gt;
ActiveRecord::Base.table_name_prefix 'myapp_'
ActiveRecord::Base.table_name_suffix = '_def'
ActiveRecord::Base.sequence_name = 'dev_company'
ActiveRecord::Base.pluralize_table_names = false 
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore # or :table_name
&lt;/pre&gt;

&lt;h4 id='safety'&gt;III. Safety Net&lt;/h4&gt;

&lt;p&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Load the &lt;a href="http://agilewebdevelopment.com/plugins/safety_net"&gt;SafetyNet Plugin&lt;/a&gt; into your rails app to prevent from destroying your development database.&lt;/p&gt;

&lt;h4 id='schema'&gt;IV. Duplicate Your Database Schema&lt;/h4&gt;

&lt;p&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;OK , with your dev and test development databases set correctly, let&amp;#8217;s try duplicting the schema.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rake db:test:clone&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If this works right off the bat, you&amp;#8217;re one of the lucky ones. Skip to &lt;a href="#extract"&gt;Extracting Development Data&lt;/a&gt;
&lt;p&gt;&lt;/p&gt;

&lt;p&gt;If you recieved errors from the clone command, do steps A through C .&lt;/p&gt;

&lt;p&gt;A. You can now correct any errors you may have had by manually modifying schema.rb .  See &lt;a href="/articles/2007/03/29/rails-on-oracle"&gt;Oracle Errors&lt;/a&gt; for some problems I had with the Oracle Database. Since schema.rb is generated, it&amp;#8217;s a good idea to rename it when you modify it manually.&lt;/p&gt;

&lt;p&gt;B. The clone command above may have started loading data; you need may to purge  the test db.
&lt;code&gt;
rake db:test:purge
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;C. Tell rake to load your new schema file by passing the new name, relative to RAILS_ROOT in the SCHEMA environment variable:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;
rake db:schema:load RAILS_ENV=test SCHEMA=db/oracle_schema.rb
&lt;/code&gt; &lt;/p&gt;

&lt;h4 id='extract'&gt;V. Extracting Development Data&lt;/h4&gt;

&lt;p&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Copy the code from 
Rails Recipe #42: &lt;a href="http://media.pragprog.com/titles/fr_rr/code/CreateFixturesFromLiveData/lib/tasks/extract_fixtures.rake"&gt;extract_fixtures.rake&lt;/a&gt; to your RAILS_ROOT/lib/tasks directory.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;&lt;strong&gt;TIP: If your develoment database has lots of data, Modify the SQL Select in this code to limit the amount of data you copy using the SQL limit statement:&lt;/strong&gt;&lt;/p&gt;
    
    &lt;p&gt;&lt;code&gt;sql  = "SELECT * FROM %s limit = 100"&lt;/code&gt;&lt;/p&gt;
    
    &lt;p&gt;Or for a proprietary database, the equivalent command:&lt;/p&gt;
    
    &lt;p&gt;&lt;code&gt;sql  = "SELECT * FROM %s WHERE ROWNUM&amp;lt;=100"&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then run 
&lt;code&gt;
 rake extract_fixtures
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;p&gt;&lt;/p&gt;

&lt;h4&gt;You&amp;#8217;ve made your testbed so you can lie in it.&lt;/h4&gt;

&lt;p&gt;You now have and exact copy of your development database, and a collection of sample data in Yaml format, and are ready to begin writing some Unit Tests. Stay tuned for some examples. Rail on!
&lt;span&gt;&lt;/p&gt;

&lt;script type="text/javascript"&gt;
digg_url = 'http://martinicity.net/articles/2007/03/31/testing-a-non-rails-application-using-rails';
digg_title = 'Testing a Non-Rails Application Using Rails';&lt;/script&gt;

&lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt;

&lt;p&gt;&lt;/span&gt;&lt;/p&gt;</description>
      <pubDate>Sat, 31 Mar 2007 13:11:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:ae1917e2-4a47-4fa2-be71-d57109445b46</guid>
      <author>Mike Blake</author>
      <link>http://martinicity.net/articles/2007/03/31/testing-a-non-rails-application-using-rails</link>
      <category>Create</category>
      <category>rails</category>
      <category>ruby</category>
      <category>unit tests</category>
      <category>database</category>
      <category>yml</category>
      <category>rake</category>
    </item>
    <item>
      <title>Rails SafetyNet</title>
      <description>&lt;h2&gt;&lt;b&gt;Curent Mood:&lt;/b&gt; Bored with Deleting the Production Database&lt;/h2&gt;

&lt;p&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;I thought I was the only clown working on Rails projects and ignoring the &lt;a href="http://dev.rubyonrails.org/svn/rails/applications/plugins/config/database.example.yml"&gt;warning in config/database.yml&lt;/a&gt; and using rake to wipe out  perfectly good databases.  But I&amp;#8217;ve seen it happen to others now, and enough&amp;#8217;s enough.  So I created the &lt;h3&gt;SafetyNet Plugin&lt;/h3&gt;.&lt;/p&gt;

&lt;p&gt;You can install Safety Net in your app, by running&lt;/p&gt;

&lt;p&gt;&lt;code&gt;
ruby script/plugin install svn://rubyforge.org/var/svn/apptrain/trunk/vendor/plugins/safety_net
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That&amp;#8217;s it. Now, If the &lt;b&gt;test&lt;/b&gt; database points to the same database as &lt;b&gt;development&lt;/b&gt; or &lt;b&gt;production&lt;/b&gt; running rake will display the following message:&lt;/p&gt;

&lt;p&gt;&lt;img src="/images/safety_net.png" alt="safety net"/&gt;&lt;/p&gt;

&lt;pre&gt;
rake aborted! 



The name of your test database matches production or development.
&lt;/pre&gt;

&lt;h3&gt;How it Works&lt;/h3&gt;

&lt;p&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Purging the poor innocent database is prevented in two ways.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;By adding a prerequisite check to the rake script that normaly performs this task.&lt;/li&gt;
&lt;li&gt;By modifying the fixtures method on ActiveRecord to avoid the same fate when running individual tests with the ruby command. &lt;/li&gt;
&lt;/ol&gt;</description>
      <pubDate>Wed, 07 Feb 2007 21:14:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:7d6f994d-0845-41c7-a8ea-09a9157c94db</guid>
      <author>Mike Blake</author>
      <link>http://martinicity.net/articles/2007/02/07/safety-net</link>
      <category>Create</category>
      <category>safetynet</category>
      <category>safetynet</category>
      <category>apptrain</category>
      <category>rails</category>
      <category>plugin</category>
      <category>rake</category>
      <category>rails test database</category>
    </item>
  </channel>
</rss>
