forked from fluentmigrator/fluentmigrator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIntialMigrationCode.tt
More file actions
171 lines (153 loc) · 4.19 KB
/
IntialMigrationCode.tt
File metadata and controls
171 lines (153 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<#
/*
T4 Template for importing the DB structure for the Fluent Migration. Adapted from PetaPoco/Subsonic T4 code.
Created by Guru Kathiresan - gururamnath -att- yahoo.com
-----------------------------------------------------------------------------------------
Features
=========
* Generate Intial Table structure for all support DB
* Support for Index code generation for SQLite DB
Todo
====
1) Ability to import other DB items like Triggers/Procedures/etc..
2) Abiity add the intial setup data
3) Genrated FKey code does not work for SQLite. FM problem ?
3) Generate FKey and Index code for Oracle/MSSQL/MSSQL Compact/MySQL/PGSQL
*/
#>
<#@ include file="FM.Core.ttinclude" #>
<#
// Settings
ConnectionStringName = ""; // Uses last connection string in config if not specified
Namespace = "Migrations";
ClassPrefix = "";
ClassSuffix = "";
IncludeViews = false;
// Read schema
var tables = LoadTables();
/*
// Tweak Schema
tables["tablename"].Ignore = true; // To ignore a table
tables["tablename"].ClassName = "newname"; // To change the class name of a table
tables["tablename"]["columnname"].Ignore = true; // To ignore a column
tables["tablename"]["columnname"].PropertyName="newname"; // To change the property name of a column
tables["tablename"]["columnname"].PropertyType="bool"; // To change the property type of a column
*/
// Generate output
#>
<#
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;
using System.Linq.Expressions;
using FluentMigrator;
namespace <#=Namespace #>
{
[Migration(<#=GetCurrentTimeStamp()#>)]
public class CreateInitialDB : Migration
{
public override void Up()
{
<#
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#>")<#}
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) #>)<#}
}
#>
;
<#
List<TableIndex> IndexList = tbl.Indices;
foreach(TableIndex indx in IndexList)
{
#>
Create.Index("<#=indx.Name#>").OnTable("<#=tbl.Name#>")<#if (indx.IsUnique){#>.Unique()<#}#>
<#
foreach(IndexColumn col in indx.IndexColumns)
{
#>
.OnColumn("<#=col.Name#>")
<#
}
#>
;
<#
}
#>
<#
}
#>
<#
bool commentAlreadyWritten = false;
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 (fkey.CascadeDelete)
{ #>
.OnDelete(Rule.Cascade)<#
}
if (fkey.CascadeUpdate)
{ #>
.OnUpdate(Rule.Cascade)<# }
#>;
<#
}
}
#>
}
public override void Down()
{
<#
foreach(Table tbl in from t in tables where !t.Ignore select t)
{
List<FKey> FKeyList = tbl.FKeys;
foreach(FKey fkey in FKeyList)
{
#>
Delete.ForeignKey().FromTable("<#=tbl.Name#>").ForeignColumn("<#=fkey.FromColumn#>").ToTable("<#=fkey.ToTable#>").PrimaryColumn("<#=fkey.ToColumn#>");
<#
}
}
#>
<#
foreach(Table tbl in from t in tables where !t.Ignore select t)
{
foreach(TableIndex idx in from i in tbl.Indices select i)
{
#>
Delete.Index("<#=idx.Name#>");
<#
}
}
#>
<#
var DeleteTableList = from t in tables where !t.Ignore select t;
if (DeleteTableList != null)
DeleteTableList = DeleteTableList.Reverse();
foreach(Table tbl in DeleteTableList)
{
#>
Delete.Table("<#=tbl.Name#>");
<#
}
#>
}
}
}