Skip to content

Commit 731bdac

Browse files
committed
Added generic migration exception.
Added DataRecordExtensions class which provides a .TryParse<T>("Column") method which can be used to make parsing the results of an ITransformationProvider.ExecuteQuery easier. Added quick readme to explain what this fork of Migrator.Net is about.
1 parent fa3d2b8 commit 731bdac

File tree

7 files changed

+125
-0
lines changed

7 files changed

+125
-0
lines changed

README.markdown

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
![DevDefined.OAuth logo][1]
2+
3+
Introduction
4+
------------
5+
6+
This project is a fork of "ye olde trusty" Migrator.Net - the original project can be found [here on google code][1], and has since [moved to github][2]
7+
8+
What's different in the fork
9+
----------------------------
10+
11+
In this fork the main changes are:
12+
13+
* Now targets .Net Framework 3.5 instead of 2.0
14+
* Support for reserved words
15+
* Support for guid types across all databases.
16+
* Utility classes for removing all tables etc. from a database (to support migration integration tests)
17+
* Warnings in Oracle when attempting to create column/table/key names that are over-length.
18+
19+
Downloads/Releases
20+
------------------
21+
22+
You can download releases from the [google code site][14].
23+
24+
[1]: http://code.google.com/p/migratordotnet/
25+
[2]: https://github.com/migratordotnet/Migrator.NET
26+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Data;
3+
4+
namespace Migrator.Framework
5+
{
6+
public static class DataRecordExtensions
7+
{
8+
public static T TryParse<T>(this IDataRecord record, string name)
9+
{
10+
return TryParse(record, name, () => default(T));
11+
}
12+
13+
public static T TryParse<T>(this IDataRecord record, string name, Func<T> defaultValue)
14+
{
15+
object value = record[name];
16+
17+
Type type = typeof (T);
18+
19+
if (value == null || Convert.IsDBNull(value)) return defaultValue();
20+
21+
if (type == typeof (DateTime?) || type == typeof (DateTime))
22+
{
23+
return (T) (object) (Convert.ToDateTime(value));
24+
}
25+
26+
if (type == typeof (Guid) || type == typeof (Guid?))
27+
{
28+
if (value is byte[]) return (T) (object) new Guid((byte[]) value);
29+
return (T) ((object) new Guid(value.ToString()));
30+
}
31+
32+
if (type == typeof (string))
33+
{
34+
return (T) ((object) value.ToString());
35+
}
36+
37+
if (type == typeof (Int32?) || type == typeof (Int32))
38+
{
39+
return (T) (object) Convert.ToInt32(value);
40+
}
41+
42+
if (type == typeof (Int64?) || type == typeof (Int64))
43+
{
44+
return (T) (object) Convert.ToInt64(value);
45+
}
46+
47+
if (type == typeof (bool) || type == typeof (bool?))
48+
{
49+
if (value is Int32 || value is Int64 || value is Int16 || value is UInt16 || value is UInt32 || value is UInt64)
50+
{
51+
long intValue = Convert.ToInt64(value);
52+
return (T) (object) (intValue != 0);
53+
}
54+
55+
if (value is string)
56+
{
57+
bool result;
58+
if (bool.TryParse((string) value, out result))
59+
{
60+
return (T) (object) result;
61+
}
62+
}
63+
64+
return (T) value;
65+
}
66+
67+
try
68+
{
69+
return (T) value;
70+
}
71+
catch (InvalidCastException ex)
72+
{
73+
throw new MigrationException(string.Format("Invalid cast exception of value: {0} of type: {1} to type: {2} (field name: {3})", value, value.GetType(), typeof (T), name), ex);
74+
}
75+
}
76+
}
77+
}

src/Migrator.Framework/Migrator.Framework-vs2010.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<Reference Include="System.Data" />
5959
</ItemGroup>
6060
<ItemGroup>
61+
<Compile Include="DataRecordExtensions.cs" />
6162
<Compile Include="Column.cs" />
6263
<Compile Include="ColumnProperty.cs" />
6364
<Compile Include="ForeignKeyConstraint.cs" />

src/Migrator/DuplicatedVersionException.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace Migrator
1818
/// <summary>
1919
/// Exception thrown when a migration number is not unique.
2020
/// </summary>
21+
[Serializable]
2122
public class DuplicatedVersionException : Exception
2223
{
2324
public DuplicatedVersionException(long version)

src/Migrator/IrreversibleMigrationException.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Migrator
1919
/// Exception thrown in a migration <c>Down()</c> method
2020
/// when changes can't be undone.
2121
/// </summary>
22+
[Serializable]
2223
public class IrreversibleMigrationException : Exception
2324
{
2425
public IrreversibleMigrationException() : base("Irreversible migration")

src/Migrator/MigrationException.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace Migrator
4+
{
5+
/// <summary>
6+
/// Exception thrown for a generic migration issue
7+
/// </summary>
8+
[Serializable]
9+
public class MigrationException : Exception
10+
{
11+
public MigrationException() { }
12+
public MigrationException( string message ) : base( message ) { }
13+
public MigrationException( string message, Exception inner ) : base( message, inner ) { }
14+
protected MigrationException(
15+
System.Runtime.Serialization.SerializationInfo info,
16+
System.Runtime.Serialization.StreamingContext context ) : base( info, context ) { }
17+
}
18+
}

src/Migrator/Migrator-vs2010.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Compile Include="IrreversibleMigrationException.cs" />
9090
<Compile Include="MigrateAnywhere.cs" />
9191
<Compile Include="MigrationComparer.cs" />
92+
<Compile Include="MigrationException.cs" />
9293
<Compile Include="MigrationLoader.cs" />
9394
<Compile Include="Migrator.cs" />
9495
<Compile Include="ProviderFactory.cs" />

0 commit comments

Comments
 (0)