Last active
October 6, 2016 02:21
-
-
Save faloi/e378bcd7ace6a2d10bdc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using FluentMigrator.Model; | |
using FluentMigrator.SchemaDump.SchemaWriters; | |
namespace SampleApplication.SchemaDump | |
{ | |
public class RCDumpWriter : SchemaWriterBase | |
{ | |
private readonly string nameSpace; | |
private readonly string migrationName; | |
public RCDumpWriter(string nameSpace, string migrationName) | |
{ | |
this.nameSpace = nameSpace; | |
this.migrationName = migrationName; | |
} | |
public override void WriteToStream(ICollection<TableDefinition> tables, StreamWriter output) | |
{ | |
//start writing a migration file | |
output.WriteLine("using FluentMigrator;"); | |
output.WriteLine(String.Empty); | |
output.WriteLine("namespace " + nameSpace); | |
output.WriteLine("{"); | |
output.WriteLine("\t[Migration(0)]"); | |
output.WriteLine("\tpublic class " + migrationName + " : Migration"); | |
output.WriteLine("\t{"); | |
output.WriteLine("\t\tpublic override void Up()"); | |
output.WriteLine("\t\t{"); | |
Separator(output, "Tables"); | |
foreach (var table in tables) | |
{ | |
WriteTable(table, output); | |
} | |
Separator(output, "Foreign keys"); | |
foreach (var table in tables) | |
{ | |
foreach (var foreignKey in table.ForeignKeys) | |
{ | |
WriteForeignKey(foreignKey, output); | |
} | |
} | |
output.WriteLine("\t\t}"); //end method | |
output.WriteLine("\t\tpublic override void Down()"); | |
output.WriteLine("\t\t{"); | |
foreach (var table in tables) | |
{ | |
WriteDeleteTable(table, output); | |
} | |
output.WriteLine("\t\t}"); //end method | |
output.WriteLine("\t}"); //end class | |
output.WriteLine(String.Empty); | |
output.WriteLine("}"); //end namespace | |
} | |
private static void Separator(StreamWriter output, string title) | |
{ | |
output.WriteLine("\t\t\t//" + title); | |
} | |
protected void WriteTable(TableDefinition table, StreamWriter output) | |
{ | |
output.WriteLine("\t\t\tCreate.Table(\"" + table.Name + "\")"); | |
foreach (var column in table.Columns) | |
{ | |
WriteColumn(column, output, column==table.Columns.Last()); | |
} | |
output.WriteLine(); | |
} | |
protected void WriteForeignKey(ForeignKeyDefinition foreignKey, StreamWriter output) | |
{ | |
output.WriteLine(); | |
output.WriteLine( | |
new StringBuilder() | |
.AppendFormat("\t\t\tCreate.ForeignKey(\"{0}\")", foreignKey.Name) | |
.AppendLine() | |
.AppendFormat("\t\t\t\t.FromTable(\"{0}\").ForeignColumns({1})", foreignKey.PrimaryTable, JoinColumns(foreignKey.ForeignColumns)) | |
.AppendLine() | |
.AppendFormat("\t\t\t\t.ToTable(\"{0}\").PrimaryColumns({1});", foreignKey.ForeignTable, JoinColumns(foreignKey.PrimaryColumns)) | |
); | |
} | |
private string JoinColumns(IEnumerable<string> columns) | |
{ | |
var value = columns.Aggregate("", (acum, elem) => acum + "\"" + elem + "\", " ); | |
return value.Substring(0, value.Length - 2); | |
} | |
protected void WriteDeleteTable(TableDefinition table, StreamWriter output) | |
{ | |
output.WriteLine("\t\t\tDelete.Table(\"" + table.Name + "\");"); | |
} | |
protected void WriteColumn(ColumnDefinition column, StreamWriter output, bool isLastColumn) | |
{ | |
string columnSyntax = ".WithColumn(\"" + column.Name + "\")"; | |
switch (column.Type) | |
{ | |
case DbType.Boolean: | |
columnSyntax += ".AsBoolean()"; | |
break; | |
case DbType.Byte: | |
columnSyntax += ".AsByte()"; | |
break; | |
case DbType.Int16: | |
columnSyntax += ".AsInt16()"; | |
break; | |
case DbType.Int32: | |
columnSyntax += ".AsInt32()"; | |
break; | |
case DbType.Int64: | |
columnSyntax += ".AsInt64()"; | |
break; | |
case DbType.Double: | |
columnSyntax += ".AsDouble()"; | |
break; | |
case DbType.Date: | |
columnSyntax += ".AsDate()"; | |
break; | |
case DbType.DateTime: | |
columnSyntax += ".AsDateTime()"; | |
break; | |
default: | |
columnSyntax += ".AsString()"; | |
break; | |
} | |
if (column.IsIdentity) | |
columnSyntax += ".Identity()"; | |
if (column.IsPrimaryKey) | |
columnSyntax += ".PrimaryKey()"; | |
else | |
{ | |
columnSyntax += column.IsNullable.Value ? ".Nullable()" : ".NotNullable()"; | |
if (column.IsIndexed) columnSyntax += ".Indexed()"; | |
} | |
if (isLastColumn) columnSyntax += ";"; | |
output.WriteLine("\t\t\t\t" + columnSyntax); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment