<?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>Benjamin Atkin &#187; databases</title>
	<atom:link href="http://benatkin.com/tag/databases/feed/" rel="self" type="application/rss+xml" />
	<link>http://benatkin.com</link>
	<description></description>
	<lastBuildDate>Sun, 29 Jan 2012 03:51:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Rails 3.1 and Xeround MySQL on Heroku</title>
		<link>http://benatkin.com/2011/10/13/rails-3-1-and-xeround-mysql-on-heroku/</link>
		<comments>http://benatkin.com/2011/10/13/rails-3-1-and-xeround-mysql-on-heroku/#comments</comments>
		<pubDate>Fri, 14 Oct 2011 05:31:45 +0000</pubDate>
		<dc:creator>Ben Atkin</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[heroku]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://benatkin.com/?p=979</guid>
		<description><![CDATA[I&#8217;m developing a Rails site that uses MySQL and runs on Heroku. I&#8217;m using Xeround for the database, which has a free trial. Xeround is a database PaaS (Platform as a Service) that runs on several places, including Amazon Web &#8230; <a href="http://benatkin.com/2011/10/13/rails-3-1-and-xeround-mysql-on-heroku/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m developing a Rails site that uses MySQL and runs on Heroku. I&#8217;m using <a href="http://xeround.com/">Xeround</a> for the database, which has a free trial. Xeround is a database PaaS (Platform as a Service) that runs on several places, including Amazon Web Services in their U. S. East region, which is where Heroku runs. Running it in the same place where Heroku runs gives it a low latency.</p>
<p>To set up Xeround, sign up for an account and set up a database instance in the AWS U. S. East region. Go to the page for the database instance, and you will see the username and the database URLs.</p>
<p>Rails reads its database configuration from <code>config/database.yml</code>. Heroku overwrites <code>config/database.yml</code> with a file it generates from the <code>DATABASE_URL</code> configuration variable. The configuration variable uses the database driver name as the network scheme (e. g. <code>mysql://</code>). There is more than one mysql driver for Rails; the one suggested in the Rails Guide is <code>mysql2</code>.</p>
<p>To use mysql2 as the database driver with Xeround, use the following for the <code>DATABASE_URL</code> configuration variable:</p>
<div class="highlight-wrapper ">
<div class="tools">
<div class="wrap">
<a href="#" class="show-raw">raw</a><a href="#" class="show-colored">highlighted</a><a href="#" class="to-clipboard">copy</a><a href="#" class="print">print</a><a href="#" class="about">?</a>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
<pre class="raw"><code>heroku config:add DATABASE_URL='mysql2://{{username}}:{{password}}@{{hostname}}:{{port}}/{{dbname}}'</code></pre>
<div class="highlighted">
<table class="highlighttable">
<tr>
<td class="linenos">
<div class="linenodiv">
<pre class="nl">1</pre>
</div>
</td>
<td class="code">
<div class="highlight">
<pre><span class="x">heroku config:add DATABASE_URL='mysql2://</span><span class="cp">{{</span><span class="nv">username</span><span class="cp">}}</span><span class="x">:</span><span class="cp">{{</span><span class="nv">password</span><span class="cp">}}</span><span class="x">@</span><span class="cp">{{</span><span class="nv">hostname</span><span class="cp">}}</span><span class="x">:</span><span class="cp">{{</span><span class="nv">port</span><span class="cp">}}</span><span class="x">/</span><span class="cp">{{</span><span class="nv">dbname</span><span class="cp">}}</span><span class="x">'</span>
</pre>
</div>
</td>
</tr>
</table>
</div>
</div>
<p>The username, hostname, and port number can be seen on the DB Instance Manager page on Xeround. The hostname and port are under one of the three <em>DNS Names</em>; I picked the top one. The password is what was entered for the database password during signup. The database name is the name of the database to be added using <a href="http://www.phpmyadmin.net/home_page/index.php">PHPMyAdmin</a>.</p>
<p>The driver also needs to be included in the <code>Gemfile</code>. To include it just for production, use:</p>
<pre class="brush: ruby; title: ; notranslate">
group :production do
  gem 'mysql2'
end
</pre>
<p>After adding this, run <code>bundle install</code>, commit your changes, and push to Heroku.</p>
<p>The database still needs to be created. To create the database, click on one of the links under <em>DNS Names</em> on the DB Instance Manager. This brings up PHPMyAdmin. Enter the database password and log in. Then create the database, with the same name used for the <code>DATABASE_URL</code> configuration variable.</p>
<p>Now that the database has been created, the DATABASE_URL is set, and the Gemfile is updated and pushed, you should be able to migrate the database by running <code>heroku run rake db:migrate</code>.</p>
<p>I&#8217;m using the latest version of Rails (3.1.1) on Heroku&#8217;s cedar stack.</p>
<p>It will be interesting to see how well Xeround works for this project. There are other MySQL hosting options for EC2 if I need them, and migrating is fairly simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://benatkin.com/2011/10/13/rails-3-1-and-xeround-mysql-on-heroku/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Trying Out CouchDB In FireBug</title>
		<link>http://benatkin.com/2008/09/13/trying-out-couchdb-in-firebug/</link>
		<comments>http://benatkin.com/2008/09/13/trying-out-couchdb-in-firebug/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 09:05:22 +0000</pubDate>
		<dc:creator>Ben Atkin</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[couchdb]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[firebug]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.benatkin.com/weblog/?p=116</guid>
		<description><![CDATA[Since I love FireBug, and CouchDB has a JSON API, I attempted to combine the two. My goal was to find a way to use jQuery&#8216;s AJAX functions to create, read, and update CouchDB documents. Getting Started The first thing &#8230; <a href="http://benatkin.com/2008/09/13/trying-out-couchdb-in-firebug/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Since I love <a href="http://getfirebug.com/">FireBug</a>, and <a href="http://incubator.apache.org/couchdb/">CouchDB</a> has a JSON API, I attempted to combine the two. My goal was to find a way to use <a href="http://jquery.com/">jQuery</a>&#8216;s AJAX functions to create, read, and update CouchDB documents.</p>
<h2>Getting Started</h2>
<p>The first thing I tried was creating a merb project with a single page referencing jQuery, and accessing CouchDB from there. The browser&#8217;s security model got in my way, however. A page on one port on localhost can&#8217;t access another port with AJAX. I searched for ways to give a page special permission, but my search turned up empty and I figured that I&#8217;d rather not go against the browser anyway, if I could help it.</p>
<p>The next thing I tried was getting a page that references jQuery to be served by CouchDB&#8217;s web server. That way the page and jQuery&#8217;s JSON API would be on the same port and Firefox would be happy. I opened up CouchDB&#8217;s Futon Utility Client at http://localhost:5984/_utils/ and found that it already had the latest version of jQuery included! Problem solved.</p>
<p>I opened the FireBug console (which was already enabled on localhost from when I was trying to get it working with my Merb project) and started trying out jQuery&#8217;s AJAX functions on CouchDB.</p>
<p style="text-align: center;"><a href="http://www.benatkin.com/weblog/wp-content/uploads/2008/09/couch1.png"><img class="size-medium wp-image-122 aligncenter" title="couch1" src="http://www.benatkin.com/weblog/wp-content/uploads/2008/09/couch1.png" alt="" width="657" height="440"></a></p>
<p style="text-align: left;">The GET request returned successfully! However, FireBug doesn&#8217;t retain the response, so I have to click <strong>Load Response</strong> to get it.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-126" title="couch2" src="http://www.benatkin.com/weblog/wp-content/uploads/2008/09/couch2.png" alt="" width="367" height="123"></p>
<p style="text-align: left;">OK, there&#8217;s CouchDB&#8217;s welcome message! Expanding the GET request and clicking <strong>Load Response</strong> is tedious, though, so a better technique is needed. The first thing that comes to mind is doing a synchronous request. I find that get() doesn&#8217;t support this option, so I need to use ajax().</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-127" title="couch3" src="http://www.benatkin.com/weblog/wp-content/uploads/2008/09/couch3.png" alt="" width="417" height="82"></p>
<p style="text-align: left;">Aside from FireBug&#8217;s quoting <a href="http://failblog.org/">FAIL</a>, it works nicely. What I&#8217;d really like, though, is for FireBug to pretty-print the JSON. So I throw an eval() statement around it, and add the left side of an assignment statement because otherwise I would get an error (another way to get it working is to throw square brackets around the expression).</p>
<p style="text-align: center;"><img class="size-full wp-image-128 aligncenter" title="couch4" src="http://www.benatkin.com/weblog/wp-content/uploads/2008/09/couch4.png" alt="" width="497" height="134"></p>
<p style="text-align: left;">The output is quite a bit nicer, in my opinion.</p>
<h2 style="text-align: left;">Creating a Database</h2>
<p style="text-align: left;">To create a database I use the <a href="http://wiki.apache.org/couchdb/HttpDatabaseApi">HttpDatabaseApi</a> page on the <a href="http://incubator.apache.org/couchdb/">CouchDB wiki</a>. Creating a database requires use of a <strong>PUT</strong> request, which can be made with jQuery&#8217;s ajax() function if the browser supports it (and Firefox does).</p>
<p style="text-align: left;"><img class="aligncenter size-full wp-image-129" title="couch5" src="http://www.benatkin.com/weblog/wp-content/uploads/2008/09/couch5.png" alt="" width="500" height="59"></p>
<p style="text-align: left;">CouchDB returns a simple OK message to let me know it worked.</p>
<h2 style="text-align: left;">Creating a Document</h2>
<p style="text-align: left;">Now that I have a database, I can create a document using the HttpDocumentApi. I opt to take the cavalier approach of having the server generate an ID rather than supplying an ID of my own. To do this, I send a POST with the database&#8217;s URL for the address and the document&#8217;s contents for the POST data. To send the POST data, I call ajax() with the document&#8217;s contents (as a JSON expression) for the data option and the string &#8216;json&#8217; for the dataType option.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-135" title="couch7" src="http://www.benatkin.com/weblog/wp-content/uploads/2008/09/couch7.png" alt="" width="635" height="210"></p>
<p style="text-align: left;">It didn&#8217;t work. I expand the POST and look at the data sent to find out why.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-136" title="couch8" src="http://www.benatkin.com/weblog/wp-content/uploads/2008/09/couch8.png" alt="" width="635" height="210"></p>
<p style="text-align: left;">The ajax() function didn&#8217;t serialize the data! It&#8217;s not built into jQuery. It makes sense, given that one of jQuery&#8217;s features is its small footprint. I remember that when I viewed the source of the main page of CouchDB&#8217;s Futon Utility Client I saw a JSON include. It&#8217;s in <a href="http://localhost:5984/_utils/script/json2.js">http://localhost:5984/_utils/script/json2.js</a>. It&#8217;s not a jQuery plug-in, but its interface is fairly simple. To serialize data, you call JSON.stringify() with an object as the first parameter. I add this to my statement and try again.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-137" title="couch9" src="http://www.benatkin.com/weblog/wp-content/uploads/2008/09/couch9.png" alt="" width="634" height="209"></p>
<p style="text-align: left;">It worked! Or, at least, it didn&#8217;t error. It returned a globally unique ID (GUID) generated by the server. Now I use the ID to GET the document&#8217;s contents.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-138" title="couch10" src="http://www.benatkin.com/weblog/wp-content/uploads/2008/09/couch10.png" alt="" width="634" height="209"></p>
<p style="text-align: left;">Only a summary of the object is shown. If I click the object it expands to a full view.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-139" title="couch11" src="http://www.benatkin.com/weblog/wp-content/uploads/2008/09/couch11.png" alt="" width="633" height="208"></p>
<p style="text-align: left;">The data is there. The revision number is also present. One of CouchDB&#8217;s coolest features is built-in versioning.</p>
<p style="text-align: left;">After I realized that I was given only a summary of the object, I tried creating a document again and seeing if I missed any info that was supplied in the response to the POST request. It turns out that I did. The revision (_rev) was given. I&#8217;d like to have Firebug print out the whole response (and not a short summary), but I don&#8217;t see any way of doing so.</p>
<h2 style="text-align: left;">Updating a Document</h2>
<p style="text-align: left;">Documents can be updated by sending a PUT request with the document&#8217;s URL as the address and the new contents as the data. The new contents must include the revision number upon which the update is based. This is to prevent conflicts. If a revision number other than that of the latest revision is supplied, it means that another client updated it first.</p>
<p style="text-align: left;">I update the document by getting the latest copy, storing it in a variable, removing the ID (since it&#8217;s already in the URL), changing the radius, and sending a PUT request with the new database contents.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-140" title="couch12" src="http://www.benatkin.com/weblog/wp-content/uploads/2008/09/couch12.png" alt="" width="634" height="209"></p>
<p style="text-align: left;">The document is updated. If I expand the object I can see the new revision number.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-141" title="couch13" src="http://www.benatkin.com/weblog/wp-content/uploads/2008/09/couch13.png" alt="" width="633" height="208"></p>
<p style="text-align: left;">Pretty cool, huh?</p>
<h2 style="text-align: left;">Conclusion &amp; Next Steps</h2>
<p>Before I tried using CouchDB with Firebug, I tried using the Futon Utility Client. I felt that I learned more and better retained what I learned when I used Firebug. Firebug as it is right now has a couple of issues that will keep me from using it as an environment in which to try doing different things with CouchDB. The first issue is that the pretty-print doesn&#8217;t show the full object in the console view, and there&#8217;s no option in the UI to make it do so. The second issue is that I find the editor to be insufficient. When the editor is in vertical mode, there&#8217;s no way to go through the history of commands entered. When the editor is in horizontal mode, it can&#8217;t expand beyond one line. When I switch between tabs, the text in the editor is often lost.</p>
<p>Firebug is, however, being actively worked on, and will hopefully get some features that will make it easier and more fun to use to play with JavaScript API&#8217;s and web services.</p>
<p>I would like to get the custom JavaScript environment mentioned at the top of this post set up and working with CouchDB. One way to get it working might be to set up a proxy of sorts to CouchDB. I could have any requests starting with /couchdb be forwarded to the CouchDB port with the /couchdb part stripped off.</p>
<p>This was fun. I think the next thing I&#8217;ll do with CouchDB is try writing a simple wiki using CouchDB and Merb.</p>
<p style="text-align: left;">
<p style="text-align: center;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://benatkin.com/2008/09/13/trying-out-couchdb-in-firebug/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

