Back when I was learning Ruby on Rails, I really liked the ease with which you could migrate your database, or even auto-generate migration scripts based the script name.
For example, say I want to create table and related class called Product, with fields and columns Description and Name. I just need to run a simple command and it creates the script for me. I then only to run db:migrate and I’m set.
rails generate model Product name:string description:text
Awesome, right? If only you could do something similar with .Net….
Guess what? You can now 🙂
MigrateDB.Net is a migration utility I wrote that will generate the desired ORM files based the schema information of your database, and that can generate Fluent Migrator database migration scripts in similar fashion as rails does.
How to use MigrateDB.Net:
MigrateDB.Net is fairly easy to use. You need to make sure that your user.config file is in the same directory as where you installed the tool.
Here’s sample user.config:
<config> <version>0</version> <connectionstring>ElConnectionString</connectionstring> <outputfolder>C:\Test</outputfolder> <namespaces classes="DomainClasses" mapping="DomainMappings" migration="SchemaMigrations"> </namespaces> </config>
There console can be used to:
- Generate ORM related files
- Generate the migration scripts based on the script name
- Generate empty migration scripts
To run the tool to generate ORM related files (domain objects and mappings), you can use the options:
This tells MigrateDB that you want it to read the schema information of a SqlServer database, and generate the files relevant to Fluent NHibernate.
To run the tool to generate migration scripts based on the script name, you can use the options:
-db:SqlServer -g -o:AddTableStore string:Address DateTime:CreatedOn
This tells MigrateDB that you want to create a script that will create a table called “Store” that will have “Address” and “CreatedOn” columns. The list of keywords that will trigger the creation of migration scripts is as follows:
- AddColumn e.g: AddColumnPriceToStoreItem double
This tells MigrateDb to create a migration script with an Up method to add a new column “Price” on the existing table “StoreItem”
- AddColumnsTo, e.g: AddColumnsToStoreItem double:price string:Name
This tells MigrateDb to create a migration script with an Up method to add 2 new columns (column Price of type double and column Name of type string) to the “StoreItem” table
- DropTable, e.g. DropTableStoreItem
This tells MigrateDB to create a migration script with an Up method to drop a table named “StoreItem”. While the other generated scripts will have both a Down (with logic to revert generated Up logic) and an Up method, this script will not have the Down method….for now.
- RemoveColumn, e.g. RemoveColumnPriceFromStoreItem
This tells MigrateDB to create an Up method to drop the column “Price” of the “StoreItem” table
- RenameColumn, e.g. RenameColumnPicToPriceOnStoreItem
This tells MigrateDb to create a migration script with logic to rename the “Pic” to “Price” on the “StoreItem” table
- RenameTable, e.g. RenameTableStoreIceToStoreItem
This tells MigrateDB to create an Up method to rename the table “StoreIce” to “StoreItem”.
If the “-g” option is used but the script name does not contain the expected keywords, an empty migration script will be generated.
To run the tool to generate empty migration script, you can use the options:
What to expect on the next release:
- “Trust” option: this will give the user the ability to “automagically” run the generated script and create the subsequent ORM files
- Generate migrations for managing foreign keys, i.e. Create, Delete, Edit
- Add primary key for the create table migration
- Add support for Oracle Databases
- Add support for generating HBM files (NHibernate’s xml mapping files)
- Generate Down method for DropTable* migration scripts
To check the code for yourself, you can clone it on github. Some code cleanup and documentation is still needed. Once that’s done, I will create the version 0.0.1 branch. Any code review is greatly appreciated.