Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions src/FluentMigrator.T4/FM.Core.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ public class FKey
{
public string ToTable;
public string FromColumn;
public string ToColumn;
public string ToColumn;
public bool CascadeDelete;
public bool CascadeUpdate;
}


Expand Down Expand Up @@ -737,8 +739,28 @@ class SqlServerSchemaReader : SchemaReader

List<FKey> LoadFKeys(string tblName)
{
var result=new List<FKey>();
return result;
using (var cmd = _factory.CreateCommand())
{
cmd.Connection = _connection;
cmd.CommandText = string.Format(FKEY_INFO_SQL);
cmd.Parameters.Add(new SqlParameter("@tblName", tblName));

var result = new List<FKey>();
using (var rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
var key = new FKey();
key.ToTable = (string)rdr["Table"];
key.ToColumn = (string)rdr["To"];
key.FromColumn = (string)rdr["From"];
key.CascadeDelete = (bool)rdr["CascadeDelete"];
key.CascadeUpdate = (bool)rdr["CascadeUpdate"];
result.Add(key);
}
}
return result;
}
}

string GetPK(string table){
Expand Down Expand Up @@ -847,7 +869,25 @@ class SqlServerSchemaReader : SchemaReader
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME=@tableName AND TABLE_SCHEMA=@schemaName
ORDER BY OrdinalPosition ASC";


const string FKEY_INFO_SQL = @"SELECT
columnsA.name AS [From] , tablesB.name AS [Table], columnsB.name AS [To]
, cast((case when (fk.delete_referential_action_desc = 'CASCADE') then 1 else 0 end) as bit) as [CascadeDelete]
, cast((case when (fk.update_referential_action_desc = 'CASCADE') then 1 else 0 end) as bit) as [CascadeUpdate]
FROM sys.foreign_key_columns fkc
JOIN sys.columns AS columnsA
ON fkc.parent_object_id = columnsA.object_id
AND fkc.parent_column_id = columnsA.column_id
JOIN sys.columns AS columnsB
ON fkc.referenced_object_id = columnsB.object_id
AND fkc.referenced_column_id = columnsB.column_id
JOIN sys.tables AS tablesA
ON fkc.parent_object_id = tablesA.object_id
JOIN sys.tables AS tablesB
ON fkc.referenced_object_id = tablesB.object_id
JOIN sys.foreign_keys fk
ON fkc.constraint_object_id = fk.object_id
WHERE tablesA.object_id = OBJECT_ID(@tblName)";
}

class SqlServerCeSchemaReader : SchemaReader
Expand Down
31 changes: 19 additions & 12 deletions src/FluentMigrator.T4/IntialMigrationCode.tt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ if (string.IsNullOrEmpty(Namespace)) Namespace=ConnectionStringName;
if (string.IsNullOrEmpty(Namespace)) Namespace="FluentMigration";
#>
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
Expand All @@ -68,15 +69,12 @@ foreach(Table tbl in from t in tables where !t.Ignore select t)
{
#>
//For <#=tbl.Name#>
Create.Table("<#=tbl.Name#>")<#if (string.IsNullOrEmpty(tbl.Schema) == false ){#>.InSchema("<#=tbl.Schema#>")<#}#>
<#
Create.Table("<#=tbl.Name#>")<#if (string.IsNullOrEmpty(tbl.Schema) == false ){#>.InSchema("<#=tbl.Schema#>")<#}
var ColumnList = from c in tbl.Columns where !c.Ignore select c;
foreach(Column col in ColumnList)
{
#>
.WithColumn("<#=col.Name#>").<#=GetMigrationTypeFunctionForType(col.PropertyType,col.Size,col.Precision)#><#if (col.IsPK){#>.PrimaryKey()<#}#><#if (col.IsAutoIncrement){#>.Identity()<#}#><#if (col.IsNullable){#>.Nullable()<#} else {#>.NotNullable()<#}#><#if (col.DefaultValue != null){#>.WithDefaultValue(<#= GetColumnDefaultValue(col) #>)<#}#>
<#
}
foreach(Column col in ColumnList)
{ #>
.WithColumn("<#=col.Name#>").<#=GetMigrationTypeFunctionForType(col.PropertyType,col.Size,col.Precision)#><#if (col.IsPK){#>.PrimaryKey()<#}#><#if (col.IsAutoIncrement){#>.Identity()<#}#><#if (col.IsNullable){#>.Nullable()<#} else {#>.NotNullable()<#}#><#if (col.DefaultValue != null){#>.WithDefaultValue(<#= GetColumnDefaultValue(col) #>)<#}
}
#>
;
<#
Expand Down Expand Up @@ -109,10 +107,19 @@ foreach(Table tbl in from t in tables where !t.Ignore select t)
List<FKey> FKeyList = tbl.FKeys;
foreach(FKey fkey in FKeyList)
{
#>
<#if(commentAlreadyWritten == false) { #>//Foreign Key List <# commentAlreadyWritten = true;} #>
Create.ForeignKey().FromTable("<#=tbl.Name#>").ForeignColumn("<#=fkey.FromColumn#>").ToTable("<#=fkey.ToTable#>").PrimaryColumn("<#=fkey.ToColumn#>")
;
if(commentAlreadyWritten == false) { #>
// Foreign Key List <# commentAlreadyWritten = true;} #>
Create.ForeignKey()
.FromTable("<#=tbl.Name#>").ForeignColumn("<#=fkey.FromColumn#>")
.ToTable("<#=fkey.ToTable#>").PrimaryColumn("<#=fkey.ToColumn#>")<#
if (fkey.CascadeDelete)
{ #>
.OnDelete(Rule.Cascade)<#
}
if (fkey.CascadeUpdate)
{ #>
.OnUpdate(Rule.Cascade)<# }
#>;
<#
}
}
Expand Down