Esempio n. 1
0
 public static void AssertColumnDeleteAllowed(this MigrationOptions opts, string table, string column, string field)
 {
     opts.AssertNotFrozen($"change '{field}' of '{column}' of '{table}'");
     if (!opts.AllowDataloss)
     {
         throw new InvalidOperationException($"Can't migrate column '{column}' of '{table}', {field} can't be changed without dataloss");
     }
 }
Esempio n. 2
0
 public static void AssertColumnDeleteAllowed(this MigrationOptions opts, string table, string column)
 {
     opts.AssertNotFrozen($"delete '{column}' of '{table}'");
     if (!opts.AllowDataloss)
     {
         throw new InvalidOperationException($"Can't delete column '{column}' of '{table}', dataloss is not allowed");
     }
 }
        protected void PlanMigrateColumn(SchemaMigrationPlan report, ColumnSchema existing, ColumnSchema target, MigrationOptions options)
        {
            if (existing == null)
            {
                if (target.KeyIndex > 0)
                {
                    throw new InvalidOperationException($"Cannot migrate column {target.Name} of {report.Target.Name}, keys can't be changed");
                }
                options.AssertNotFrozen($"Create column {target.Name} of {report.Target.Name}");

                report.Steps.Add(new MigrationStep()
                {
                    TargetName = target.Name,
                    Action     = MigrationAction.Create,
                    TargetType = MigrationTarget.Column,
                });
                return;
            }

            if (target == null)
            {
                if (existing.KeyIndex > 0)
                {
                    throw new InvalidOperationException($"Cannot migrate column {existing.Name} of {report.Target.Name}, keys can't be changed");
                }
                options.AssertColumnDeleteAllowed(report.Target.Name, existing.Name);

                report.Steps.Add(new MigrationStep()
                {
                    TargetName = existing.Name,
                    Action     = MigrationAction.Drop,
                    TargetType = MigrationTarget.Column,
                });
                return;
            }

            bool mismatched = false;

            if (target.KeyIndex != existing.KeyIndex)
            {
                throw new InvalidOperationException($"Cannot migrate column {target.Name} of {report.Target.Name}, keys can't be changed");
            }

            if (target.SqlType != existing.SqlType)
            {
                mismatched = true;
                options.AssertColumnDeleteAllowed(report.Target.Name, target.Name, "type");
            }

            if (target.Nullable != existing.Nullable)
            {
                mismatched = true;
                options.AssertColumnDeleteAllowed(report.Target.Name, target.Name, "nullable");
            }

            if (target.MaxLength != existing.MaxLength)
            {
                mismatched = true;
                options.AssertColumnDeleteAllowed(report.Target.Name, target.Name, "maxlength");
            }

            if (mismatched)
            {
                options.AssertColumnDeleteAllowed(report.Target.Name, target.Name);
                report.Steps.Add(new MigrationStep()
                {
                    TargetName = target.Name,
                    Action     = MigrationAction.Drop,
                    TargetType = MigrationTarget.Column,
                });
                report.Steps.Add(new MigrationStep()
                {
                    TargetName = target.Name,
                    Action     = MigrationAction.Create,
                    TargetType = MigrationTarget.Column,
                });
                return;
            }
        }