<?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>Brain Matters &#187; database</title>
	<atom:link href="http://blog.agoragames.com/tag/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.agoragames.com</link>
	<description></description>
	<lastBuildDate>Thu, 29 Jul 2010 19:13:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting started with data_fabric</title>
		<link>http://blog.agoragames.com/2010/01/29/getting-started-with-data_fabric/</link>
		<comments>http://blog.agoragames.com/2010/01/29/getting-started-with-data_fabric/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 15:46:01 +0000</pubDate>
		<dc:creator>David Czarnecki</dc:creator>
				<category><![CDATA[Bending Rails]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[active_record]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[data_fabric]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[sharding]]></category>

		<guid isPermaLink="false">http://blog.agoragames.com/?p=447</guid>
		<description><![CDATA[The data_fabric gem &#8220;provides flexible database connection switching for ActiveRecord&#8221;. If you&#8217;re not concerned with database sharding, you might want to skip this blog post. Or not. Either way, I&#8217;m not going to be offended.
I have a requirement that certain data in an application that I&#8217;m developing will probably have to be sharded because, if [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://github.com/mperham/data_fabric">data_fabric</a> gem &#8220;provides flexible database connection switching for ActiveRecord&#8221;. If you&#8217;re not concerned with database sharding, you might want to skip this blog post. Or not. Either way, I&#8217;m not going to be offended.</p>
<p>I have a requirement that certain data in an application that I&#8217;m developing will probably have to be sharded because, if you&#8217;ll excuse my English, there will a &#8220;shit ton&#8221; of data. This only affects one model out of the few models I have in the application. I don&#8217;t have a requirement that the data will be replicated (which is another feature supported in data_fabric), so I&#8217;m not going into that here. In any event, here is a rundown of how I got started developing and testing with data_fabric.</p>
<p>- Configure the data_fabric gem in your config/environment.rb file.</p>
<pre class="brush: ruby">

config.gem &#039;data_fabric&#039;
</pre>
<p>- In your model(s), decide on which column or how the data is going to be shared.</p>
<pre class="brush: ruby">

data_fabric :replicated =&gt; false, :shard_by =&gt; :initial_code
</pre>
<p>In this case, inital_code is a method that looks at a piece of the model&#8217;s data and gives me the correct shard.</p>
<p>- Setup the database shards in your config/database.yml file. I actually setup only one shard for development and testing environments to make things easier. I&#8217;m just including the one for the test environment here. You can read on the data_fabric site about the naming convention for sharded database connections.</p>
<pre class="brush: sql">

test:
adapter: mysql
encoding: utf8
reconnect: false
database: myapp_test
pool: 5
username: root
password:

# This is the database shard
initial_code_testenv_test:
adapter: mysql
encoding: utf8
reconnect: false
database: myapp_test_testenv
pool: 5
username: root
password:
</pre>
<p>- In config/initializers/my_app_model.rb, I actually stub out the initial_code method to return a single value for the development and test environments. This is merely convenience so I don&#8217;t have to include every single database shard for development and testing.</p>
<pre class="brush: ruby">

require &#039;mocha&#039;

if &#039;development&#039;.eql?(RAILS_ENV)
PromotionCode.stubs(:initial_code).returns(&#039;devenv&#039;)
end

if &#039;test&#039;.eql?(RAILS_ENV)
PromotionCode.stubs(:initial_code).returns(&#039;testenv&#039;)
end
</pre>
<p>- I copied part of the Rakefile from the data_fabric gem to actually be able to migrate the database for the sharded database connections. This was definitely missing from the data_fabric README.</p>
<pre class="brush: ruby">

require &#039;fileutils&#039;
include FileUtils::Verbose

namespace :db do
task :migrate do
require &#039;erb&#039;
require &#039;logger&#039;
require &#039;active_record&#039;
reference = YAML::load(ERB.new(IO.read(&quot;config/database.yml&quot;)).result)
env = RAILS_ENV = ENV[&#039;RAILS_ENV&#039;] || &#039;development&#039;
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger.level = Logger::WARN
ActiveRecord::Base.configurations = reference.dup
old_config = reference[env]
reference.each_key do |name|
next unless name.include? env
next if name.include? &#039;slave&#039; # Replicated databases should not be touched directly

puts &quot;Migrating #{name}&quot;
ActiveRecord::Base.clear_active_connections!
ActiveRecord::Base.configurations[env] = reference[name]
ActiveRecord::Base.establish_connection RAILS_ENV
ActiveRecord::Migration.verbose = ENV[&quot;VERBOSE&quot;] ? ENV[&quot;VERBOSE&quot;] == &quot;true&quot; : true
ActiveRecord::Migrator.migrate(&quot;db/migrate/&quot;, ENV[&quot;VERSION&quot;] ? ENV[&quot;VERSION&quot;].to_i : nil)
end
end
end
</pre>
<p>- In my test classes that use the sharded model, I have setup and teardown methods that activate and deactivate the shard.</p>
<pre class="brush: ruby">

def setup
DataFabric.activate_shard(:initial_code =&gt; &#039;testenv&#039;)
end

def teardown
MyAppModel.delete_all
DataFabric.deactivate_shard(:initial_code =&gt; &#039;testenv&#039;)
end
</pre>
<p>I did find that I needed to delete all the objects in the database for the sharded model. I&#8217;m still digging into why that&#8217;s the case. My ActiveRecord_fu isn&#8217;t that strong I guess.</p>
<p>All in all, sharding is relatively easy with data_fabric. Pimping, however, &#8220;ain&#8217;t easy.&#8221; But that&#8217;s for another blog post I guess.</p>




	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fblog.agoragames.com%2F2010%2F01%2F29%2Fgetting-started-with-data_fabric%2F&amp;title=Getting%20started%20with%20data_fabric" title="Reddit"><img src="http://blog.agoragames.com/wp-content/plugins/var/www/waxer-blog/shared/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fblog.agoragames.com%2F2010%2F01%2F29%2Fgetting-started-with-data_fabric%2F&amp;title=Getting%20started%20with%20data_fabric&amp;bodytext=The%20data_fabric%20gem%20%22provides%20flexible%20database%20connection%20switching%20for%20ActiveRecord%22.%20If%20you%27re%20not%20concerned%20with%20database%20sharding%2C%20you%20might%20want%20to%20skip%20this%20blog%20post.%20Or%20not.%20Either%20way%2C%20I%27m%20not%20going%20to%20be%20offended.%0D%0A%0D%0AI%20have%20a%20requirement%20t" title="Digg"><img src="http://blog.agoragames.com/wp-content/plugins/var/www/waxer-blog/shared/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fblog.agoragames.com%2F2010%2F01%2F29%2Fgetting-started-with-data_fabric%2F&amp;title=Getting%20started%20with%20data_fabric&amp;notes=The%20data_fabric%20gem%20%22provides%20flexible%20database%20connection%20switching%20for%20ActiveRecord%22.%20If%20you%27re%20not%20concerned%20with%20database%20sharding%2C%20you%20might%20want%20to%20skip%20this%20blog%20post.%20Or%20not.%20Either%20way%2C%20I%27m%20not%20going%20to%20be%20offended.%0D%0A%0D%0AI%20have%20a%20requirement%20t" title="del.icio.us"><img src="http://blog.agoragames.com/wp-content/plugins/var/www/waxer-blog/shared/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fblog.agoragames.com%2F2010%2F01%2F29%2Fgetting-started-with-data_fabric%2F" title="Technorati"><img src="http://blog.agoragames.com/wp-content/plugins/var/www/waxer-blog/shared/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://blog.agoragames.com/2010/01/29/getting-started-with-data_fabric/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->