<?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>JazzRecord Blog &#187; sqlite</title>
	<atom:link href="http://blog.jazzrecord.org/tag/sqlite/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jazzrecord.org</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 09 Dec 2009 21:25:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>New Features in JazzRecord 0.7: Migrations and isNew()</title>
		<link>http://blog.jazzrecord.org/2009/02/new-features-in-jazzrecord-07-migrations-and-isnew/</link>
		<comments>http://blog.jazzrecord.org/2009/02/new-features-in-jazzrecord-07-migrations-and-isnew/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 07:01:10 +0000</pubDate>
		<dc:creator>thynctank</dc:creator>
				<category><![CDATA[Library]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[behind-the-scenes]]></category>
		<category><![CDATA[contribution]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[localization]]></category>
		<category><![CDATA[migrations]]></category>
		<category><![CDATA[sqlite]]></category>

		<guid isPermaLink="false">http://blog.jazzrecord.org/?p=20</guid>
		<description><![CDATA[First off, thanks to Khou Suylong of Mango Team for once again providing me with a bug find which led to a fix!
We’ve been busy once more, putting together JazzRecord 0.7, which incorporates manual migrations with all of the major features from Rails’ ActiveRecord migrations. While there are no official updated docs yet, here’s a brief list of changes:
ID must now [...]]]></description>
			<content:encoded><![CDATA[<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;">First off, <strong>thanks to Khou Suylong of Mango Team</strong> for once again providing me with a bug find which led to a fix!</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;">We’ve been busy once more, putting together <strong><strong>JazzRecord</strong> <strong>0.7</strong></strong>, which incorporates <em>manual <strong>migrations</strong></em> with all of the major <strong>features</strong> from Rails’ ActiveRecord <strong>migrations</strong>. While there are no official updated docs yet, here’s a brief list of changes:</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;">ID must now explicitly be listed as a column for a model. If using automigrations, you must list it in the model definition. The explicit requirement for ID column may change going forward, but once we hit Version 1 (another month, perhaps) we will stand firm on the API. <em>If using manual <strong>migrations</strong>, you may now leave out columns from the model definition entirely and rely on <strong>migrations</strong> setting the model’s column knowledge for you.</em></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;">What this means is, <em>no matter what changes you make to a db schema using <strong>migrations</strong>, the model is always aware of the current state of the schema</em> and will allow you to create, destroy, and modify properties of records with no problem. The only thing to watch out for is validations if you have any, because they can cause problems if there are requirements which cannot be satisfied given a specific updated schema.</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;">The following schema operations are available as of now:</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;"><strong>JazzRecord</strong>.createTable(tableName, cols) &#8211; this creates a table using a columns object, identical the the (now optional) columns object from a model declaration.</p>
<ul style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; font-family: 'Lucida Grande', Verdana, sans-serif; padding: 0px;">
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 15px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;"><code style="font-family: 'Andale Mono', Monaco, 'Courier New', Courier, monospace; background-color: #666666; color: #ffffff; padding: 0px; margin: 0px;"><strong>JazzRecord</strong>.dropTable(tableName)</code> &#8211; removes a table from the DB.</li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 15px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;"><code style="font-family: 'Andale Mono', Monaco, 'Courier New', Courier, monospace; background-color: #666666; color: #ffffff; padding: 0px; margin: 0px;"><strong>JazzRecord</strong>.renameTable(tableName)</code> &#8211; renames a table</li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 15px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;"><code style="font-family: 'Andale Mono', Monaco, 'Courier New', Courier, monospace; background-color: #666666; color: #ffffff; padding: 0px; margin: 0px;"><strong>JazzRecord</strong>.addColumn(tableName, colName, colType)</code> &#8211; adds a column of given type to a table</li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 15px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;"><code style="font-family: 'Andale Mono', Monaco, 'Courier New', Courier, monospace; background-color: #666666; color: #ffffff; padding: 0px; margin: 0px;"><strong>JazzRecord</strong>.removeColumn(tableName, colName)</code> &#8211; removes a column from a table</li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 15px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;"><code style="font-family: 'Andale Mono', Monaco, 'Courier New', Courier, monospace; background-color: #666666; color: #ffffff; padding: 0px; margin: 0px;"><strong>JazzRecord</strong>.renameColumn(tableName, oldName, newName)</code> &#8211; renames a given column in a table</li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 5px; margin-left: 15px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;"><code style="font-family: 'Andale Mono', Monaco, 'Courier New', Courier, monospace; background-color: #666666; color: #ffffff; padding: 0px; margin: 0px;"><strong>JazzRecord</strong>.changeColumn(tableName, colName, colType)</code> &#8211; changes the type of a column. This can result in data loss, as determined by how the DB converts data types</li>
</ul>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;">The last three functions were the trickiest, because <strong>SQLite does not provide inherent capabilities for modifying columns once they exist in a table</strong>. In order to reproduce these<strong>features</strong>, then, we have to go about slurping up all data in a table, dropping the existing table, creating a <strong>new</strong> table with the <strong>new</strong> schema, and spitting the same data back into the database, with data types changed or columns removed or changed in type. To keep this (hopefully) relatively safe, we wrap the destructive DROP operation in a transaction.</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;">Did anyone even know <strong>JazzRecord</strong> had a <code style="font-family: 'Andale Mono', Monaco, 'Courier New', Courier, monospace; background-color: #666666; color: #ffffff; padding: 0px; margin: 0px;"><strong>JazzRecord</strong>.runTransaction(func, bind)</code> method? I’m not sure! Calling this and passing in a function full of operations will ensure that if something goes wrong and you or some function you call throws an exception, all will revert to its pre-transaction state. Pretty cool, huh?</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;">Making use of the schema operations is as simple as calling them anywhere, though typically you’d organize them into <strong>migrations</strong>. <strong>Migrations</strong> are now a bit streamlined, as well.</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;">To automigrate, simply call <code style="font-family: 'Andale Mono', Monaco, 'Courier New', Courier, monospace; background-color: #666666; color: #ffffff; padding: 0px; margin: 0px;"><strong>JazzRecord</strong>.migrate()</code> with no options, or to refresh the DB to an empty state, pass in an object with refresh property set to true:<code style="font-family: 'Andale Mono', Monaco, 'Courier New', Courier, monospace; background-color: #666666; color: #ffffff; padding: 0px; margin: 0px;"><strong>JazzRecord</strong>.migrate({refresh: true})</code>. To optionally load fixture data, assign a fixtures object (same as the old object format) to <em><strong>JazzRecord</strong>.fixtures</em>, and if it exists it will be loaded. To run <strong>migrations</strong>, simply assign to <em><strong>JazzRecord</strong>.<strong>migrations</strong></em> an object literal with numerically-named properties (starting with the number 1) each containing an object literal which has an up function and a down function. These functions should be the reverse of one another, and for an explanation of proper migration usage consult any Rails tutorial or wait for the documentation. Here’s a simple sample migration which creates a couple tables and adds a column to one of them over a few schema versions:</p>
<pre style="font-family: 'Lucida Grande', Verdana, sans-serif; padding: 0px; margin: 0px;"><code style="font-family: 'Andale Mono', Monaco, 'Courier New', Courier, monospace; background-color: #666666; color: #ffffff; display: block; padding: 15px; margin: 0px;">  <strong>JazzRecord</strong>.<strong>migrations</strong> = {
    1: {
      up: function() {
        <strong>JazzRecord</strong>.createTable("military_dudes", {
          id: "number",
          name: "text",
          title: "text",
          base_id: "number"
        });

        <strong>JazzRecord</strong>.createTable("bases", {
          id: "number",
          location_name: "text"
        });
      },
      down: function() {
        <strong>JazzRecord</strong>.dropTable("bases");
        <strong>JazzRecord</strong>.dropTable("military_dudes");
      }
    },

    2: {
      up: function() {
        <strong>JazzRecord</strong>.renameColumn("military_dudes", "title", "rank");
        <strong>JazzRecord</strong>.addColumn("bases", "state", "text");
      },
      down: function() {
        <strong>JazzRecord</strong>.removeColumn("bases", "state");
        <strong>JazzRecord</strong>.renameColumn("military_dudes", "rank", "title");
      }
    }
  };
</code></pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;">After setting up a <strong>migrations</strong> object like this, you can call <code style="font-family: 'Andale Mono', Monaco, 'Courier New', Courier, monospace; background-color: #666666; color: #ffffff; padding: 0px; margin: 0px;"><strong>JazzRecord</strong>.migrate()</code> standalone (to migrate to the latest version), or you can pass in a version number to reach the specified migration number. To reset to pre-migration status, call <code style="font-family: 'Andale Mono', Monaco, 'Courier New', Courier, monospace; background-color: #666666; color: #ffffff; padding: 0px; margin: 0px;"><strong>JazzRecord</strong>.migrate(0)</code>. In this way, you can evolve an application’s schema over time and can almost effortlessly jump between schema versions to diagnose problems or revert when you’ve introduced more problems than you’ve fixed! You can pretty much use <strong>migrations</strong> exactly as in Rails: changing schema and inserting/removing/updating data for existing, already-deployed applications. Soon we will add additional tools for updating multiple columns and multiple rows simultaneously, which will further add to the usefulness of <strong>migrations</strong>.</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;">The smaller, but still useful, function introduced in <strong>JazzRecord</strong> <strong>0.7</strong> is Record’s <code style="font-family: 'Andale Mono', Monaco, 'Courier New', Courier, monospace; background-color: #666666; color: #ffffff; padding: 0px; margin: 0px;"><strong>isNew</strong>()</code> function. This function, when called, will return true if a record has <em>not</em> been saved yet, or false if it <em>has</em>.</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;">As a previous blog post stated, we are extremely interested in having our documentation translated into other languages, so please contact me at <a style="font-family: 'Lucida Grande', Verdana, sans-serif; color: #336699; font-weight: bold; text-decoration: none; padding: 0px; margin: 0px;" href="mailto:thynctank@thynctank.com">thynctank@thynctank.com</a> if you can help with that.</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; font-family: 'Lucida Grande', Verdana, sans-serif; line-height: 17px; padding: 0px;">Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jazzrecord.org/2009/02/new-features-in-jazzrecord-07-migrations-and-isnew/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
