<?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>Julien Blin &#187; ActiveRecord</title>
	<atom:link href="http://blog.jblin.com/tag/activerecord/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jblin.com</link>
	<description>acts_as_architect( :.net, :rails )</description>
	<lastBuildDate>Tue, 02 Mar 2010 20:00:04 +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>NAnt task to drop/create sqlite database from ActiveRecord</title>
		<link>http://blog.jblin.com/2009/12/nant-task-to-dropcreate-sqlite-database-from-activerecord/</link>
		<comments>http://blog.jblin.com/2009/12/nant-task-to-dropcreate-sqlite-database-from-activerecord/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 20:00:04 +0000</pubDate>
		<dc:creator>Julien Blin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[nant]]></category>
		<category><![CDATA[SQLite]]></category>

		<guid isPermaLink="false">http://blog.jblin.com/?p=95</guid>
		<description><![CDATA[I really like to use ActiveRecord when building small apps or internal tools. It offers a lot of great features of his older brother NHibernate, without freightening everybody on your team.
I also really like the ability to generate the database schema based on the domain entities, so you don&#8217;t have to worry about SQL and [...]]]></description>
			<content:encoded><![CDATA[<p>I really like to use <a href="http://www.castleproject.org/activerecord/index.html">ActiveRecord</a> when building small apps or internal tools. It offers a lot of great features of his older brother <a href="http://nhforge.org">NHibernate</a>, without freightening everybody on your team.</p>
<p>I also really like the ability to <a href="http://www.castleproject.org/activerecord/documentation/trunk/usersguide/schemagen.html">generate the database schema based on the domain entities</a>, so you don&#8217;t have to worry about SQL and DDL.<br />
Combine that with <a href="http://www.sqlite.org/">SQLite</a>, and you can develop on your local machine without an installed database server (like Sql Server). Makes it really easy  to setup a new development machine (checkout the code, build and run &#8211; it should works).</p>
<p><span id="more-95"></span></p>
<p>Unfortunately, the only way to do it provided by the framework is on the application code. So you end with something like that in your initializing file:</p>
<pre class="brush: csharp;">
ActiveRecordStarter.Initialize(typeof(ADomainEntity).Assembly, ActiveRecordSectionHandler.Instance);
#if DEBUG
    // ActiveRecordStarter.CreateSchema();
#end
</pre>
<p>The problems with this approach are multiples, just to name a few:</p>
<ul>
<li>you could potentially erase all your production data if you forget to comment line 3 (thus the #if DEBUG statement &#8211; just in case)</li>
<li>it&#8217;s not very handy : each time you want to drop/create the schema, you have to uncomment the line, run the application, stop it, comment the line.</li>
</ul>
<p>That&#8217;s why I created a <a href="http://nant.sourceforge.net/">NAnt</a> target, allowing the previous code to never appear in the application. You just use the target on your local machine when you want to drop and create the schema.</p>
<p>A really cool feature of NAnt is the ability to use the <a href="http://nant.sourceforge.net/release/latest/help/tasks/script.html">script</a> task: it allows you to use your favorite .Net language to code an inline task. In that case we use the ability to dynamically load the Domain Model assembly to feed the ActiveRecordStarter with the correct information.</p>
<pre class="brush: xml;">
&lt;script language=&quot;C#&quot;&gt;
  &lt;references&gt;
	&lt;include name=&quot;System.Data.dll&quot; /&gt;
	&lt;include name=&quot;Dependencies\SQLite.NET-0.21\SQLite.NET.dll&quot; /&gt;
	&lt;include name=&quot;Dependencies\Castle-net-2.0-release-2007-9-20\Castle.Core.dll&quot;/&gt;
	&lt;include name=&quot;Dependencies\Castle-net-2.0-release-2007-9-20\Castle.ActiveRecord.dll&quot;/&gt;
  &lt;/references&gt;
  &lt;imports&gt;
	&lt;import namespace=&quot;System.IO&quot; /&gt;
	&lt;import namespace=&quot;System.Collections&quot; /&gt;
	&lt;import namespace=&quot;System.Reflection&quot; /&gt;
	&lt;import namespace=&quot;Castle.ActiveRecord&quot; /&gt;
	&lt;import namespace=&quot;Castle.ActiveRecord.Framework.Config&quot; /&gt;
  &lt;/imports&gt;
  &lt;code&gt;
	&lt;![CDATA[
		public static void ScriptMain(Project project) {
			string dbFile = project.Properties[&quot;db.sqlite.path&quot;];
			string connectionString = string.Format(&quot;Data Source={0};Version=3;New=True&quot;, dbFile);
			string modelAssemblyFilePath = Path.Combine(
                                project.BaseDirectory,
                                @&quot;PathToTheModelAssembly.dll&quot;
                            );

			// Create DB and Schema
			IDictionary props = new Hashtable();
			props.Add(&quot;hibernate.connection.driver_class&quot;, &quot;NHibernate.Driver.SQLiteDriver&quot;);
			props.Add(&quot;hibernate.dialect&quot;, &quot;NHibernate.Dialect.SQLiteDialect&quot;);
			props.Add(&quot;hibernate.connection.provider&quot;, &quot;NHibernate.Connection.DriverConnectionProvider&quot;);
			props.Add(&quot;hibernate.connection.connection_string&quot;, connectionString);

			InPlaceConfigurationSource config = new InPlaceConfigurationSource();
			config.Add(typeof(ActiveRecordBase), props);

			project.Log(Level.Info, &quot;Loading ActiveRecord configuration...&quot;);
			ActiveRecordStarter.Initialize(Assembly.LoadFrom(modelAssemblyFilePath), config);

			project.Log(Level.Info, &quot;Dumping schema...&quot;);
			ActiveRecordStarter.CreateSchema();
		}
	  ]]&gt;
  &lt;/code&gt;
&lt;/script&gt;
</pre>
<p>In a nutshell:</p>
<ul>
<li>lines 3-6: don&#8217;t forget to add the necessary dependencies, otherwise compilation will fail. I use <a href="http://sourceforge.net/projects/adodotnetsqlite/">SQLLite.Net</a> library to connect to the sqlite database</li>
<li>line 18: notice the ability to use a NAnt property to define the sqlite file name</li>
<li>line 19: the &#8220;New=true&#8221; parameter will tell the provider to create the needed database file (you may need to add a task before this one to drop an existing file)</li>
<li>line 20: we specify the .net assembly that contains the ActiveRecord domain</li>
<li>line 26-33: we configure ActiveRecord using code, not a config file (much easier when using NAnt)</li>
</ul>
<p>You can also define another target, this time to generate a .sql file containing the schema for Sql Server, for example:</p>
<pre class="brush: xml;">
&lt;script language=&quot;C#&quot;&gt;
  &lt;references&gt;
	&lt;include name=&quot;System.Data.dll&quot; /&gt;
	&lt;include name=&quot;Dependencies\Castle-net-2.0-release-2007-9-20\Castle.Core.dll&quot;/&gt;
	&lt;include name=&quot;Dependencies\Castle-net-2.0-release-2007-9-20\Castle.ActiveRecord.dll&quot;/&gt;
  &lt;/references&gt;
  &lt;imports&gt;
	&lt;import namespace=&quot;System.IO&quot; /&gt;
	&lt;import namespace=&quot;System.Collections&quot; /&gt;
	&lt;import namespace=&quot;System.Reflection&quot; /&gt;
	&lt;import namespace=&quot;Castle.ActiveRecord&quot; /&gt;
	&lt;import namespace=&quot;Castle.ActiveRecord.Framework.Config&quot; /&gt;
  &lt;/imports&gt;
  &lt;code&gt;
	&lt;![CDATA[
		public static void ScriptMain(Project project) {
			string schemaFile = project.Properties[&quot;schema.file&quot;];
			string modelAssemblyFilePath = Path.Combine(
                                project.BaseDirectory,
                                @&quot;PathToTheModelAssembly.dll&quot;
                            );

			// Create DB and Schema
			IDictionary props = new Hashtable();
			props.Add(&quot;hibernate.connection.driver_class&quot;, &quot;NHibernate.Driver.SqlClientDriver&quot;);
			props.Add(&quot;hibernate.dialect&quot;, &quot;NHibernate.Dialect.MsSql2005Dialect&quot;);
			props.Add(&quot;hibernate.connection.provider&quot;, &quot;NHibernate.Connection.DriverConnectionProvider&quot;);

			InPlaceConfigurationSource config = new InPlaceConfigurationSource();
			config.Add(typeof(ActiveRecordBase), props);

			project.Log(Level.Info, &quot;Loading ActiveRecord configuration...&quot;);
			ActiveRecordStarter.Initialize(Assembly.LoadFrom(modelAssemblyFilePath), config);

			project.Log(Level.Info, &quot;Creating file...&quot;);
			ActiveRecordStarter.GenerateCreationScripts(schemaFile);
		}
	  ]]&gt;
  &lt;/code&gt;
&lt;/script&gt;
</pre>
<p>I believe a similar approach could be used in conjunction with <a href="http://www.nhforge.org/doc/nh/en/index.html#toolsetguide-s1">NHibernate schema generator</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jblin.com/2009/12/nant-task-to-dropcreate-sqlite-database-from-activerecord/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
