Added CreateIndex to Migrator

This commit is contained in:
kay.one 2011-06-04 23:01:45 -07:00
parent a1653022ad
commit 76d029361b
3 changed files with 902 additions and 842 deletions

View File

@ -13,7 +13,7 @@ namespace Migrator.Framework
/// <summary>
/// Get this provider or a NoOp provider if you are not running in the context of 'provider'.
/// </summary>
ITransformationProvider this[string provider] { get;}
ITransformationProvider this[string provider] { get; }
/// <summary>
/// The list of Migrations currently applied to the database.
@ -194,6 +194,15 @@ namespace Migrator.Framework
/// <param name="constraint"></param>
void GenerateForeignKey(string foreignTable, string primaryTable, ForeignKeyConstraint constraint);
/// <summary>
/// Add an Index to a table
/// </summary>
/// <param name="name">The name of the index to add.</param>
/// <param name="table">The name of the table that will get the index.</param>
/// <param name="unique">If the index will be unique</param>
/// <param name="columns">The name of the column or columns that are in the index.</param>
void AddIndex(string name, string table, bool unique, params string[] columns);
/// <summary>
/// Add a primary key to a table
/// </summary>
@ -376,6 +385,13 @@ namespace Migrator.Framework
/// <param name="name">The name of the constraint to remove</param>
void RemoveConstraint(string table, string name);
/// <summary>
/// Remove an existing index
/// </summary>
/// <param name="table">The table that contains the index.</param>
/// <param name="name">The name of the index to remove</param>
void RemoveIndex(string table, string name);
/// <summary>
/// Remove an existing table
/// </summary>

View File

@ -1,3 +1,4 @@
using System;
using System.Data;
using Migrator.Framework;
using ForeignKeyConstraint=Migrator.Framework.ForeignKeyConstraint;
@ -54,6 +55,11 @@ namespace Migrator.Providers
// No Op
}
public void RemoveIndex(string table, string name)
{
// No Op
}
public void AddTable(string name, params Column[] columns)
{
// No Op
@ -129,6 +135,16 @@ namespace Migrator.Providers
// No Op
}
public void AddIndex(string name, string table, params string[] columns)
{
//No Op
}
public void AddIndex(string name, string table, bool unique, params string[] columns)
{
//No Op
}
public void AddPrimaryKey(string name, string table, params string[] columns)
{
// No Op

View File

@ -134,6 +134,11 @@ namespace Migrator.Providers
}
}
public void RemoveIndex(string table, string name)
{
throw new NotImplementedException();
}
public virtual void AddTable(string table, string engine, string columns)
{
table = _dialect.TableNameNeedsQuote ? _dialect.Quote(table) : table;
@ -409,6 +414,23 @@ namespace Migrator.Providers
AddColumn(table, column, type, size, property, null);
}
public void AddIndex(string name, string table, bool unique, params string[] columns)
{
try
{
var uniqueText = "";
if (unique) uniqueText = "UNIQUE";
var command = String.Format("CREATE {0} INDEX {1} ON {2} ({3})", uniqueText, name, table, String.Join(",", columns));
ExecuteNonQuery(command);
}
catch (Exception e)
{
Logger.Exception("Unable to add index", e);
}
}
/// <summary>
/// Append a primary key to a table.
/// </summary>
@ -562,7 +584,8 @@ namespace Migrator.Providers
Logger.Warn(ex.Message);
throw;
}
} }
}
}
private IDbCommand BuildCommand(string sql)
{
@ -595,7 +618,8 @@ namespace Migrator.Providers
Logger.Warn("query failed: {0}", cmd.CommandText);
throw;
}
} }
}
}
public object ExecuteScalar(string sql)
{
@ -611,7 +635,8 @@ namespace Migrator.Providers
Logger.Warn("Query failed: {0}", cmd.CommandText);
throw;
}
} }
}
}
public IDataReader Select(string what, string from)
{
@ -658,7 +683,7 @@ namespace Migrator.Providers
public virtual int Delete(string table)
{
return Delete(table, (string[])null, (string[]) null);
return Delete(table, (string[])null, (string[])null);
}
public virtual int Delete(string table, string[] columns, string[] values)
@ -743,12 +768,14 @@ namespace Migrator.Providers
{
get
{
if(_appliedMigrations == null)
if (_appliedMigrations == null)
{
_appliedMigrations = new List<long>();
CreateSchemaInfoTable();
using(IDataReader reader = Select("version","SchemaInfo")){
while(reader.Read()){
using (IDataReader reader = Select("version", "SchemaInfo"))
{
while (reader.Read())
{
_appliedMigrations.Add(Convert.ToInt64(reader.GetValue(0)));
}
}
@ -764,7 +791,7 @@ namespace Migrator.Providers
public void MigrationApplied(long version)
{
CreateSchemaInfoTable();
Insert("SchemaInfo",new string[]{"version"},new string[]{version.ToString()});
Insert("SchemaInfo", new string[] { "version" }, new string[] { version.ToString() });
_appliedMigrations.Add(version);
}
@ -816,13 +843,14 @@ namespace Migrator.Providers
public virtual string QuoteValues(string values)
{
return QuoteValues(new string[] {values})[0];
return QuoteValues(new string[] { values })[0];
}
public virtual string[] QuoteValues(string[] values)
{
return Array.ConvertAll<string, string>(values,
delegate(string val) {
delegate(string val)
{
if (null == val)
return "null";
else