Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema_dumper #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion src/Migrator/Framework/ITransformationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ IDataReader SelectComplex(IDbCommand cmd, string table, string[] columns, string
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
string Encode(Guid guid);
object Encode(Guid guid);

/// <summary>
/// Change the target database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ void GuardAgainstMaximumColumnNameLengthForOracle(string name, Column[] columns)
}
}

public override string Encode(Guid guid)
public override object Encode(Guid guid)
{
byte[] bytes = guid.ToByteArray();
var hex = new StringBuilder(bytes.Length * 2);
Expand Down
2 changes: 1 addition & 1 deletion src/Migrator/Providers/NoOpTransformationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ public string QuoteTableNameIfRequired(string name)
throw new NotImplementedException();
}

public string Encode(Guid guid)
public object Encode(Guid guid)
{
return guid.ToString();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Migrator/Providers/TransformationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1537,9 +1537,9 @@ public virtual string QuoteTableNameIfRequired(string name)
return name;
}

public virtual string Encode(Guid guid)
public virtual object Encode(Guid guid)
{
return guid.ToString();
return guid;
}

public virtual string[] QuoteColumnNamesIfRequired(params string[] columnNames)
Expand Down
78 changes: 78 additions & 0 deletions src/Migrator/Tools/CreateNonclusteredDump.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Migrator.Framework;
using System;

using System.IO;
using System.Linq;
using Index = Migrator.Framework.Index;

namespace Migrator.Tools
{
[Migration(62)]
internal class CreateNonclusteredDump : Migration
{
public override void Up()
{

var tables = Database.GetTables();
var tab = tables.Where(o => o.ToUpper().StartsWith("WMS") || o.ToUpper().StartsWith("aaCOMMON") || o.ToUpper().StartsWith("aaVISU") || o.ToUpper().StartsWith("aaMFCV2")).ToList();
string variable = "";
string removeFKString = "";
string removePKString = "";
string removeIndexString = "";
string addPKString = "";
string addFKString = "";
foreach (var table in tab)
{
foreach (var fk in Database.GetForeignKeyConstraints(table))
{
removeFKString += $"Database.RemoveForeignKey(\"{table}\", \"{fk.Name}\");";
var fkcols = fk.Columns;
var pkcols = fk.PkColumns;
var arr = pkcols.ToArray();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = $"\"{arr[i]}\"";
}
addFKString += $"Database.AddForeignKey(\"{fk.Name}\", \"{fk.Table}\", {doArr(fkcols)}, \"{fk.PkTable}\", {doArr(pkcols)});";
}
Index[] inds = Database.GetIndexes(table);
var pK = inds.FirstOrDefault(o => o.PrimaryKey && o.KeyColumns.Length == 1);
if (pK == null)
continue;
var pkCol = Database.GetColumnByName(table, pK.KeyColumns[0]);
if (pkCol.Type != System.Data.DbType.Guid)
continue;

string var = $"pK_{table}String";
variable += $"String {var}={String.Format("getVariableName(\"{0}\");", table)}";
removePKString += $"Database.RemovePrimaryKey(\"{table}\");";
removeIndexString += $"Database.RemoveIndex(\"{table}\",{String.Format("{0}", var)});";
addPKString += $"Database.AddPrimaryKeyNonClustered({String.Format("{0}", var)}, \"{table}\", new string[] { String.Format("{{\"{0}\"}}", pK.KeyColumns[0]) });";
}
File.WriteAllText(@"c:\mlog\wms.txt", variable + "" + removeFKString + "" + removePKString + "" + removeIndexString + "" + addPKString + "" + addFKString);
}
private string getVariableName(string table)
{
Index[] inds = Database.GetIndexes(table);
var pK = inds.FirstOrDefault(o => o.PrimaryKey && o.KeyColumns.Length == 1);
if (pK == null)
return $"PK_{table}_N";
var pkCol = Database.GetColumnByName(table, pK.KeyColumns[0]);
if (pkCol.Type != System.Data.DbType.Guid)
return $"PK_{table}_N";
return pK.Name;
}
private string doArr(string[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
arr[i] = $"\"{arr[i]}\"";
}
return $"new string[]{String.Format("{{{0}}}", string.Join(",", arr))}";
}

public override void Down()
{
}
}
}
Loading