Advertisement
Guest User

MySqlMigrationSqlGenerator

a guest
Feb 9th, 2015
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. protected override MigrationStatement Generate(AddForeignKeyOperation op)
  2.         {
  3.             StringBuilder sb = new StringBuilder();
  4.             string fkName = string.Format(
  5.                 CultureInfo.InvariantCulture,
  6.                     "FK_{0}_{1}_{2}",
  7.                     TrimSchemaPrefix(op.DependentTable),
  8.                     TrimSchemaPrefix(op.PrincipalTable),
  9.                     op.DependentColumns.Join(separator: "_"));
  10.  
  11.             if (fkName.Length > 64)
  12.             {
  13.                 MD5 md5 = System.Security.Cryptography.MD5.Create();
  14.                 fkName = "FK_" + md5.CalculateMD5Hash(string.Format(
  15.                     CultureInfo.InvariantCulture,
  16.                     "{0}_{1}_{2}",
  17.                     TrimSchemaPrefix(op.DependentTable),
  18.                     TrimSchemaPrefix(op.PrincipalTable),
  19.                     op.DependentColumns.Join(separator: "_")));
  20.             }
  21.             sb.Append("alter table `" + TrimSchemaPrefix(op.DependentTable) + "` add constraint `" + TrimSchemaPrefix(fkName) + "` " +
  22.                        " foreign key ");
  23.  
  24.             sb.Append("(" + string.Join(",", op.DependentColumns.Select(c => "`" + c + "`")) + ") ");
  25.             sb.Append("references `" + TrimSchemaPrefix(op.PrincipalTable) + "` ( " + string.Join(",", op.PrincipalColumns.Select(c => "`" + c + "`")) + ") ");
  26.  
  27.             if (op.CascadeDelete)
  28.             {
  29.                 sb.Append(" on update cascade on delete cascade ");
  30.             }
  31.             return new MigrationStatement { Sql = sb.ToString() };
  32.         }
  33.  
  34.         protected override MigrationStatement Generate(DropForeignKeyOperation op)
  35.         {
  36.             StringBuilder sb = new StringBuilder();
  37.  
  38.             string fkName = string.Format(
  39.                 CultureInfo.InvariantCulture,
  40.                     "FK_{0}_{1}_{2}",
  41.                     TrimSchemaPrefix(op.DependentTable),
  42.                     TrimSchemaPrefix(op.PrincipalTable),
  43.                     op.DependentColumns.Join(separator: "_"));
  44.  
  45.             if (fkName.Length > 64)
  46.             {
  47.                 MD5 md5 = System.Security.Cryptography.MD5.Create();
  48.                 fkName = "FK_" + md5.CalculateMD5Hash(string.Format(
  49.                     CultureInfo.InvariantCulture,
  50.                     "{0}_{1}_{2}",
  51.                     TrimSchemaPrefix(op.DependentTable),
  52.                     TrimSchemaPrefix(op.PrincipalTable),
  53.                     op.DependentColumns.Join(separator: "_")));
  54.             }
  55.  
  56.             sb = sb.AppendFormat("alter table `{0}` drop foreign key `{1}`", op.DependentTable, fkName);
  57.             return new MigrationStatement { Sql = sb.ToString() };
  58.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement