示例#1
0
        public override string[] GenerateSql(MigrationHistoryBuilder migrationsAffected, IGraphVisitor graph)
        {
            string createTable;
            string head = $"CREATE TABLE [{DatabaseName}].[{SchemaName}].[{TableName}]";
            var    optimizableMigrations = _definitions
                                           .SelectMany(m => graph.PullPrerequisitesForward(m, this, CanOptimize))
                                           .ToImmutableList();

            if (optimizableMigrations.Any())
            {
                var definitions = optimizableMigrations
                                  .OfType <TableDefinitionMigration>()
                                  .Select(d => d.GenerateDefinitionSql());
                createTable = $"{head}({string.Join(",", definitions)})";
            }
            else
            {
                createTable = head;
            }

            string[] sql =
            {
                createTable
            };
            migrationsAffected.AppendAll(optimizableMigrations);

            return(sql);
        }
示例#2
0
        public SchemaSpecification UseSchema(string schemaName)
        {
            var migration = new UseSchemaMigration(_databaseName, schemaName, Prerequisites);

            MigrationHistoryBuilder.Append(migration);
            migration.AddToParent();
            return(new SchemaSpecification(migration, MigrationHistoryBuilder));
        }
示例#3
0
        public CustomSqlSpecification Execute(string up, string down = null)
        {
            var migration = new CustomSqlMigration(_databaseName, up, down, Prerequisites);

            MigrationHistoryBuilder.Append(migration);
            migration.AddToParent();
            return(new CustomSqlSpecification(migration, MigrationHistoryBuilder));
        }
        public TableSpecification CreateTable(string tableName)
        {
            var migration = new CreateTableMigration(_migration, tableName, Prerequisites);

            MigrationHistoryBuilder.Append(migration);
            migration.AddToParent();
            return(new TableSpecification(migration, MigrationHistoryBuilder));
        }
示例#5
0
        public override string[] GenerateRollbackSql(MigrationHistoryBuilder migrationsAffected, IGraphVisitor graph)
        {
            string[] sql =
            {
                $"ALTER TABLE [{DatabaseName}].[{SchemaName}].[{TableName}]\r\n    DROP CONSTRAINT [PK_{TableName}]"
            };

            return(sql);
        }
        public override string[] GenerateRollbackSql(MigrationHistoryBuilder migrationsAffected, IGraphVisitor graph)
        {
            string[] sql =
            {
                $@"ALTER TABLE [{DatabaseName}].[{SchemaName}].[{TableName}]
    DROP COLUMN [{ColumnName}]"
            };

            return(sql);
        }
示例#7
0
        public override string[] GenerateRollbackSql(MigrationHistoryBuilder migrationsAffected, IGraphVisitor graph)
        {
            string[] sql =
            {
                $"DROP TABLE [{DatabaseName}].[{SchemaName}].[{TableName}]"
            };
            migrationsAffected.AppendAll(_definitions);

            return(sql);
        }
示例#8
0
        public override string[] GenerateSql(MigrationHistoryBuilder migrationsAffected, IGraphVisitor graph)
        {
            string columnNames = string.Join(", ", _columns.Select(c => $"[{c.ColumnName}]").ToArray());

            string[] sql =
            {
                $"ALTER TABLE [{DatabaseName}].[{SchemaName}].[{TableName}]\r\n    ADD CONSTRAINT [PK_{TableName}] PRIMARY KEY CLUSTERED ({columnNames})"
            };

            return(sql);
        }
示例#9
0
        public IndexSpecification CreateIndex(params ColumnSpecification[] columns)
        {
            var childMigration = new CreateIndexMigration(
                _migration,
                columns.Select(c => c.Migration),
                Prerequisites);

            MigrationHistoryBuilder.Append(childMigration);
            childMigration.AddToParent();
            return(new IndexSpecification(childMigration, MigrationHistoryBuilder));
        }
示例#10
0
        public override string[] GenerateRollbackSql(MigrationHistoryBuilder migrationsAffected, IGraphVisitor graph)
        {
            string indexTail = string.Join("_", Columns.Select(c => $"{c.ColumnName}").ToArray());

            string[] sql =
            {
                $"ALTER TABLE [{DatabaseName}].[{SchemaName}].[{TableName}]\r\n    DROP CONSTRAINT [FK_{TableName}_{indexTail}]"
            };

            return(sql);
        }
示例#11
0
        public override string[] GenerateSql(MigrationHistoryBuilder migrationsAffected, IGraphVisitor graph)
        {
            string indexTail  = string.Join("_", Columns.Select(c => $"{c.ColumnName}").ToArray());
            string columnList = string.Join(", ", Columns.Select(c => $"[{c.ColumnName}]").ToArray());

            string[] sql =
            {
                $"CREATE NONCLUSTERED INDEX [IX_{TableName}_{indexTail}] ON [{DatabaseName}].[{SchemaName}].[{TableName}] ({columnList})"
            };

            return(sql);
        }
示例#12
0
        private ColumnSpecification CreateColumn(string columnName, string typeDescriptor, bool nullable)
        {
            var childMigration = new CreateColumnMigration(
                _migration,
                columnName,
                typeDescriptor,
                nullable,
                Prerequisites);

            MigrationHistoryBuilder.Append(childMigration);
            childMigration.AddToParent();
            return(new ColumnSpecification(childMigration, MigrationHistoryBuilder));
        }
示例#13
0
        public void AddMigration(Migration migration)
        {
            var migrationsAffected = new MigrationHistoryBuilder();

            migrationsAffected.Append(migration);
            string[] rollbackSql = migration.GenerateRollbackSql(migrationsAffected, this);
            var      mementos    = migrationsAffected.MigrationHistory.GetMementos().ToList();

            string[] deleteStatements = GenerateDeleteStatements(_databaseName, mementos);
            _sql   = _sql.InsertRange(0, deleteStatements);
            _sql   = _sql.InsertRange(0, rollbackSql);
            _ahead = _ahead.Subtract(migrationsAffected.MigrationHistory);
        }
示例#14
0
        public ForeignKeySpecification CreateForeignKey(PrimaryKeySpecification referencing, bool cascadeDelete = false, bool cascadeUpdate = false)
        {
            var childMigration = new CreateForeignKeyMigration(
                _migration,
                referencing.Migration,
                cascadeDelete,
                cascadeUpdate,
                Prerequisites);

            MigrationHistoryBuilder.Append(childMigration);
            childMigration.AddToParent();
            return(new ForeignKeySpecification(MigrationHistoryBuilder));
        }
示例#15
0
        public override string[] GenerateSql(MigrationHistoryBuilder migrationsAffected, IGraphVisitor graph)
        {
            string indexTail           = string.Join("_", Columns.Select(c => $"{c.ColumnName}").ToArray());
            string columnList          = string.Join(", ", Columns.Select(c => $"[{c.ColumnName}]").ToArray());
            string referenceColumnList = string.Join(", ", _referencing.Columns.Select(c => $"[{c.ColumnName}]").ToArray());
            string onDelete            = _cascadeDelete ? " ON DELETE CASCADE" : "";
            string onUpdate            = _cascadeUpdate ? " ON UPDATE CASCADE" : "";

            string[] sql =
            {
                $@"ALTER TABLE [{DatabaseName}].[{SchemaName}].[{TableName}]
    ADD CONSTRAINT [FK_{TableName}_{indexTail}] FOREIGN KEY ({columnList})
        REFERENCES [{_referencing.DatabaseName}].[{_referencing.SchemaName}].[{_referencing.TableName}] ({referenceColumnList}){onDelete}{onUpdate}"
            };

            return(sql);
        }
        public override string[] GenerateSql(MigrationHistoryBuilder migrationsAffected, IGraphVisitor graph)
        {
            string[] identityTypes       = { "INT IDENTITY" };
            string[] numericTypes        = { "BIGINT", "INT", "SMALLINT", "TINYINT", "MONEY", "SMALLMONEY", "DECIMAL", "FLOAT", "REAL" };
            string[] dateTypes           = { "DATETIME", "SMALLDATETIME", "DATETIME2", "TIME" };
            string[] dateTimeOffsetTypes = { "DATETIMEOFFSET" };
            string[] stringTypes         = { "NVARCHAR", "NCHAR", "NTEXT" };
            string[] asciiStringTypes    = { "VARCHAR", "CHAR", "TEXT" };
            string[] guidTypes           = { "UNIQUEIDENTIFIER" };

            string defaultExpression =
                _nullable ? null :
                identityTypes.Any(t => TypeDescriptor.StartsWith(t)) ? null :
                numericTypes.Any(t => TypeDescriptor.StartsWith(t)) ? "0" :
                dateTypes.Any(t => TypeDescriptor.StartsWith(t)) ? "GETUTCDATE()" :
                dateTimeOffsetTypes.Any(t => TypeDescriptor.StartsWith(t)) ? "SYSDATETIMEOFFSET()" :
                stringTypes.Any(t => TypeDescriptor.StartsWith(t)) ? "N''" :
                asciiStringTypes.Any(t => TypeDescriptor.StartsWith(t)) ? "''" :
                guidTypes.Any(t => TypeDescriptor.StartsWith(t)) ? "'00000000-0000-0000-0000-000000000000'" :
                null;

            if (defaultExpression == null)
            {
                string[] sql =
                {
                    $@"ALTER TABLE [{DatabaseName}].[{SchemaName}].[{TableName}]
    ADD [{ColumnName}] {TypeDescriptor} {NullableClause}"
                };

                return(sql);
            }
            else
            {
                string[] sql =
                {
                    $@"ALTER TABLE [{DatabaseName}].[{SchemaName}].[{TableName}]
    ADD [{ColumnName}] {TypeDescriptor} {NullableClause}
    CONSTRAINT [DF_{TableName}_{ColumnName}] DEFAULT ({defaultExpression})",
                    $@"ALTER TABLE [{DatabaseName}].[{SchemaName}].[{TableName}]
    DROP CONSTRAINT [DF_{TableName}_{ColumnName}]",
                };

                return(sql);
            }
        }
        public bool AddMigration(Migration migration)
        {
            if (_working.Contains(migration))
            {
                return(false);
            }

            foreach (var prerequisite in migration.AllPrerequisites
                     .Where(p => _difference.Contains(p)))
            {
                if (!AddMigration(prerequisite))
                {
                    return(false);
                }
            }

            _working = _working.Push(migration);

            var migrationsAffected = new MigrationHistoryBuilder();

            migrationsAffected.Append(migration);
            string[] result = migration.GenerateSql(migrationsAffected, this);
            _sql = _sql.AddRange(result);
            var mementos = migrationsAffected.MigrationHistory.GetMementos().ToList();

            _sql = _sql.Add(GenerateInsertStatement(_databaseName, mementos));
            if (mementos.SelectMany(m => m.Prerequisites).SelectMany(p => p.Value).Any())
            {
                _sql = _sql.Add(GeneratePrerequisiteInsertStatements(_databaseName, mementos));
            }
            _difference = _difference.Subtract(migrationsAffected.MigrationHistory);

            _working = _working.Pop();

            return(true);
        }
 internal ForeignKeySpecification(MigrationHistoryBuilder migrationHistoryBuilder) :
     base(migrationHistoryBuilder)
 {
 }
示例#19
0
 internal IndexSpecification(CreateIndexMigration migration, MigrationHistoryBuilder migrationHistoryBuilder) :
     base(migrationHistoryBuilder)
 {
     _migration = migration;
 }
示例#20
0
 internal ColumnSpecification(CreateColumnMigration migration, MigrationHistoryBuilder migrationHistoryBuilder) :
     base(migrationHistoryBuilder)
 {
     _migration = migration;
 }
示例#21
0
 internal TableSpecification(CreateTableMigration migration, MigrationHistoryBuilder migrationHistoryBuilder) :
     base(migrationHistoryBuilder)
 {
     _migration = migration;
 }
示例#22
0
 private DatabaseSpecification(string databaseName, MigrationHistoryBuilder migrationHistoryBuilder, ImmutableList <Migration> prerequisites) :
     base(migrationHistoryBuilder, prerequisites)
 {
     _databaseName = databaseName;
 }
 public override string[] GenerateRollbackSql(MigrationHistoryBuilder migrationsAffected, IGraphVisitor graph)
 {
     throw new NotImplementedException();
 }
 public override string[] GenerateSql(MigrationHistoryBuilder migrationsAffected, IGraphVisitor graph)
 {
     string[] sql = { };
     return(sql);
 }
示例#25
0
 internal PrimaryKeySpecification(CreatePrimaryKeyMigration migration, MigrationHistoryBuilder migrationHistoryBuilder) :
     base(migrationHistoryBuilder)
 {
     _migration = migration;
 }
示例#26
0
 private TableSpecification(CreateTableMigration migration, MigrationHistoryBuilder migrationHistoryBuilder, ImmutableList <Migration> prerequisites) :
     base(migrationHistoryBuilder, prerequisites)
 {
     _migration = migration;
 }
 public override string[] GenerateSql(MigrationHistoryBuilder migrationsAffected, IGraphVisitor graph)
 {
     return(new string[] { $"USE {DatabaseName}", _up });
 }
 internal CustomSqlSpecification(CustomSqlMigration migration, MigrationHistoryBuilder migrationHistoryBuilder) :
     base(migrationHistoryBuilder)
 {
     _migration = migration;
 }
 public override string[] GenerateRollbackSql(MigrationHistoryBuilder migrationsAffected, IGraphVisitor graph)
 {
     return
         (_down != null ? new string[] { $"USE {DatabaseName}", _down } :
          new string[] { });
 }
 internal SchemaSpecification(UseSchemaMigration migration, MigrationHistoryBuilder migrationHistoryBuilder) :
     base(migrationHistoryBuilder)
 {
     _migration = migration;
 }