using System; using System.Collections.Generic; using System.Data; using System.Linq; using FluentMigrator.Model; using FluentMigrator.Runner.Generators.Base; using FluentMigrator.Runner.Generators.SQLite; namespace Your.CustomFluentMigratorClasses { /// /// An almost 1:1 copy from the FM project /// internal class CustomSqliteColumn : ColumnBase { public CustomSqliteColumn() : base(new CustomSqliteTypeMap(), new SQLiteQuoter()) { } /// public override string Generate(IEnumerable columns, string tableName) { var colDefs = columns.ToList(); var foreignKeyColumns = colDefs.Where(x => x.IsForeignKey && x.ForeignKey != null); var foreignKeyClauses = foreignKeyColumns .Select(x => ", " + FormatForeignKey(x.ForeignKey, GenerateForeignKeyName)); // Append foreign key definitions after all column definitions and the primary key definition return base.Generate(colDefs, tableName) + string.Concat(foreignKeyClauses); } /// public override bool ShouldPrimaryKeysBeAddedSeparately(IEnumerable primaryKeyColumns) { // If there are no identity column then we can add as a separate constraint var pkColDefs = primaryKeyColumns.ToList(); return !pkColDefs.Any(x => x.IsIdentity) && pkColDefs.Any(x => x.IsPrimaryKey); } /// protected override string FormatIdentity(ColumnDefinition column) { // SQLite only supports the concept of Identity in combination with a single primary key // see: http://www.sqlite.org/syntaxdiagrams.html#column-constraint syntax details if (column.IsIdentity && !column.IsPrimaryKey && column.Type != DbType.Int32) { throw new ArgumentException("SQLite only supports identity on single integer, primary key coulmns"); } return string.Empty; } /// protected override string FormatPrimaryKey(ColumnDefinition column) { if (!column.IsPrimaryKey) { return string.Empty; } return column.IsIdentity ? "PRIMARY KEY AUTOINCREMENT" : string.Empty; } } }