Skip to content

Commit

Permalink
Implemented the FixedMySQLGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
pysco68 committed Sep 20, 2015
1 parent b49f1d3 commit b59e801
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/FixedMySQLGenerator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pysco68.MySQL.FixedMigrationsGenerator", "Pysco68.MySQL.FixedMigrationsGenerator\Pysco68.MySQL.FixedMigrationsGenerator.csproj", "{56BF5065-87E4-4EA9-948A-50EA29CE24FD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{56BF5065-87E4-4EA9-948A-50EA29CE24FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{56BF5065-87E4-4EA9-948A-50EA29CE24FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{56BF5065-87E4-4EA9-948A-50EA29CE24FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{56BF5065-87E4-4EA9-948A-50EA29CE24FD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
23 changes: 23 additions & 0 deletions src/Pysco68.MySQL.FixedMigrationsGenerator/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v12.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
</provider></providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data></configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
namespace Pysco68.MySQL.FixedMigrationsGenerator
{
using System.Data.Entity.Migrations.Model;
using System.Data.Entity.Migrations.Sql;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// Extended MySqlMigrationSqlGenerator to fix GUID as index name issue
/// </summary>
public class FixedMySqlMigrationSqlGenerator : MySql.Data.Entity.MySqlMigrationSqlGenerator
{
private static MD5 md5 = MD5.Create();

/// <summary>
/// Make sure keys don't get too long...
/// </summary>
/// <param name="fkName"></param>
/// <returns></returns>
private string EscapeForeignKeyName(string fkName)
{
if (fkName.Length > 64)
{
StringBuilder sb = new StringBuilder();

var part1 = fkName.Substring(0, 51);
sb.Append(part1);
sb.Append("_");

var part2 = fkName.Substring(51);
byte[] buf = System.Text.Encoding.UTF8.GetBytes(part2);
byte[] hash = md5.ComputeHash(buf, 0, buf.Length);

for (int i = 0; i < 6; i++)
sb.Append(hash[i].ToString("x2"));

return sb.ToString();
}

return fkName;
}

private string TrimSchemaPrefix(string name)
{
// I have to do that on my own...
if (name.StartsWith("dbo."))
return name.Substring(4);

return name;
}

protected override MigrationStatement Generate(DropForeignKeyOperation op)
{
op.Name = TrimSchemaPrefix(op.Name);
op.Name = EscapeForeignKeyName(op.Name);
op.PrincipalTable = TrimSchemaPrefix(op.PrincipalTable)
op.DependentTable = TrimSchemaPrefix(op.DependentTable);
return base.Generate(op);
}

protected override MigrationStatement Generate(AddForeignKeyOperation op)
{
op.Name = TrimSchemaPrefix(op.Name);
op.Name = EscapeForeignKeyName(op.Name);
return base.Generate(op);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Pysco68.MySQL.FixedMigrationsGenerator")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Pysco68.MySQL.FixedMigrationsGenerator")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("56bf5065-87e4-4ea9-948a-50ea29ce24fd")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{56BF5065-87E4-4EA9-948A-50EA29CE24FD}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Pysco68.MySQL.FixedMigrationsGenerator</RootNamespace>
<AssemblyName>Pysco68.MySQL.FixedMigrationsGenerator</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.0.0\lib\net40\EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.0.0\lib\net40\EntityFramework.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MySql.Data, Version=6.9.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<HintPath>..\packages\MySql.Data.6.9.7\lib\net40\MySql.Data.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MySql.Data.Entity.EF6, Version=6.9.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<HintPath>..\packages\MySql.Data.Entity.6.9.7\lib\net40\MySql.Data.Entity.EF6.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FixedMySqlMigrationSqlGenerator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
6 changes: 6 additions & 0 deletions src/Pysco68.MySQL.FixedMigrationsGenerator/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.0.0" targetFramework="net4" />
<package id="MySql.Data" version="6.9.7" targetFramework="net4" />
<package id="MySql.Data.Entity" version="6.9.7" targetFramework="net4" />
</packages>

0 comments on commit b59e801

Please sign in to comment.