public void Inverse_should_return_add_foreign_key_operation()
        {
            var addForeignKeyOperation = new AddForeignKeyOperation();
            var dropForeignKeyOperation = new DropForeignKeyOperation(addForeignKeyOperation);

            Assert.Same(addForeignKeyOperation, dropForeignKeyOperation.Inverse);
        }
        public void Anonymous_args_should_not_throw_when_string_supplied()
        {
            var dropForeignKeyOperation
                = new DropForeignKeyOperation("Foo");

            Assert.Equal(3, dropForeignKeyOperation.AnonymousArguments["Length"]);
        }
        public void Inverse_should_return_add_foreign_key_operation()
        {
            var addForeignKeyOperation  = new AddForeignKeyOperation();
            var dropForeignKeyOperation = new DropForeignKeyOperation(addForeignKeyOperation);

            Assert.Same(addForeignKeyOperation, dropForeignKeyOperation.Inverse);
        }
        public void Anonymous_args_should_not_throw_when_string_supplied()
        {
            var dropForeignKeyOperation
                = new DropForeignKeyOperation("Foo");

            Assert.Equal(3, dropForeignKeyOperation.AnonymousArguments["Length"]);
        }
 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 void Generate(DropForeignKeyOperation dropForeignKeyOperation, IndentedTextWriter writer)
 {
     var isReadonly = ReadonlyEntities.Contains(dropForeignKeyOperation.PrincipalTable);
     if (!isReadonly)
     {
         base.Generate(dropForeignKeyOperation, writer);
     }
 }
        public void Can_get_and_set_properties()
        {
            var dropForeignKeyOperation = new DropForeignKeyOperation
            {
                PrincipalTable = "P",
                DependentTable = "D",
                Name           = "Foo"
            };

            dropForeignKeyOperation.DependentColumns.Add("fk");

            Assert.Equal("P", dropForeignKeyOperation.PrincipalTable);
            Assert.Equal("D", dropForeignKeyOperation.DependentTable);
            Assert.Equal("fk", dropForeignKeyOperation.DependentColumns.Single());
            Assert.Equal("Foo", dropForeignKeyOperation.Name);
            Assert.Equal("FK_D_P_fk", dropForeignKeyOperation.DefaultName);
            Assert.False(dropForeignKeyOperation.HasDefaultName);
        }
        public void Can_get_and_set_properties()
        {
            var dropForeignKeyOperation = new DropForeignKeyOperation
                                              {
                                                  PrincipalTable = "P",
                                                  DependentTable = "D",
                                                  Name = "Foo"
                                              };

            dropForeignKeyOperation.DependentColumns.Add("fk");

            Assert.Equal("P", dropForeignKeyOperation.PrincipalTable);
            Assert.Equal("D", dropForeignKeyOperation.DependentTable);
            Assert.Equal("fk", dropForeignKeyOperation.DependentColumns.Single());
            Assert.Equal("Foo", dropForeignKeyOperation.Name);
            Assert.Equal("FK_D_P_fk", dropForeignKeyOperation.DefaultName);
            Assert.False(dropForeignKeyOperation.HasDefaultName);
        }
        public void CreateDropIndexOperation_should_return_corresponding_drop_index_operation()
        {
            var addForeignKeyOperation
                = new AddForeignKeyOperation
                      {
                          DependentTable = "D"
                      };
            addForeignKeyOperation.DependentColumns.Add("fk");

            var dropForeignKeyOperation
                = new DropForeignKeyOperation(addForeignKeyOperation)
                      {
                          DependentTable = "D"
                      };

            dropForeignKeyOperation.DependentColumns.Add("fk");

            var dropIndexOperation = dropForeignKeyOperation.CreateDropIndexOperation();

            Assert.Equal("D", dropIndexOperation.Table);
            Assert.NotNull(dropIndexOperation.Inverse);
            Assert.Equal("fk", dropIndexOperation.Columns.Single());
        }
        public void CreateDropIndexOperation_should_return_corresponding_drop_index_operation()
        {
            var addForeignKeyOperation
                = new AddForeignKeyOperation
                {
                DependentTable = "D"
                };

            addForeignKeyOperation.DependentColumns.Add("fk");

            var dropForeignKeyOperation
                = new DropForeignKeyOperation(addForeignKeyOperation)
                {
                DependentTable = "D"
                };

            dropForeignKeyOperation.DependentColumns.Add("fk");

            var dropIndexOperation = dropForeignKeyOperation.CreateDropIndexOperation();

            Assert.Equal("D", dropIndexOperation.Table);
            Assert.NotNull(dropIndexOperation.Inverse);
            Assert.Equal("fk", dropIndexOperation.Columns.Single());
        }
        protected override void Generate(DropForeignKeyOperation dropForeignKeyOperation)
        {
            Contract.Requires(dropForeignKeyOperation != null);

            using (var writer = Writer())
            {
                writer.Write("ALTER TABLE ");
                writer.Write(Name(dropForeignKeyOperation.DependentTable));
                writer.Write(" DROP CONSTRAINT ");
                writer.Write(Quote(dropForeignKeyOperation.Name));

                Statement(writer);
            }
        }
Beispiel #12
0
 protected override void Generate(DropForeignKeyOperation dropForeignKeyOperation)
 {
     dropForeignKeyOperation.Name = getFkName(dropForeignKeyOperation.PrincipalTable, dropForeignKeyOperation.DependentTable, dropForeignKeyOperation.DependentColumns.ToArray());
     base.Generate(dropForeignKeyOperation);
 }
        public void Generate_can_output_statement_to_drop_foreign_key_operation()
        {
            var migrationSqlGenerator = new SqlServerMigrationSqlGenerator();

            var operation =
                new DropForeignKeyOperation
                {
                    Name = "FK_na'me",
                    DependentTable = "sch'ema.DependentTable"
                };

            var sql = migrationSqlGenerator.Generate(new [] { operation }, SqlProviderManifest.TokenAzure11)
                .Join(s => s.Sql, Environment.NewLine);

            const string expectedSql =
@"IF object_id(N'[sch''ema].[FK_na''me]', N'F') IS NOT NULL
    ALTER TABLE [sch'ema].[DependentTable] DROP CONSTRAINT [FK_na'me]";

            Assert.Equal(expectedSql, sql);
        }
        /// <summary>
        ///     Generates code to perform a <see cref = "DropForeignKeyOperation" />.
        /// </summary>
        /// <param name = "dropForeignKeyOperation">The operation to generate code for.</param>
        /// <param name = "writer">Text writer to add the generated code to.</param>
        protected virtual void Generate(DropForeignKeyOperation dropForeignKeyOperation, IndentedTextWriter writer)
        {
            //Contract.Requires(dropForeignKeyOperation != null);
            //Contract.Requires(writer != null);

            writer.Write("DropForeignKey(");
            writer.Write(Quote(dropForeignKeyOperation.DependentTable));
            writer.Write(", ");

            if (!dropForeignKeyOperation.HasDefaultName)
            {
                writer.Write(Quote(dropForeignKeyOperation.Name));
            }
            else
            {
                var compositeKey = dropForeignKeyOperation.DependentColumns.Count() > 1;

                if (compositeKey)
                {
                    writer.Write("New String() { ");
                }

                writer.Write(dropForeignKeyOperation.DependentColumns.Join(Quote));

                if (compositeKey)
                {
                    writer.Write(" }");
                }

                writer.Write(", ");
                writer.Write(Quote(dropForeignKeyOperation.PrincipalTable));
            }

            writer.WriteLine(")");
        }
        /// <summary>
        ///     Generates SQL for a <see cref="DropForeignKeyOperation" />.
        ///     Generated SQL should be added using the Statement method.
        /// </summary>
        /// <param name="dropForeignKeyOperation"> The operation to produce SQL for. </param>
        protected virtual void Generate(DropForeignKeyOperation dropForeignKeyOperation)
        {
            Check.NotNull(dropForeignKeyOperation, "dropForeignKeyOperation");

            using (var writer = Writer())
            {
                writer.Write("ALTER TABLE ");
                writer.Write(Name(dropForeignKeyOperation.DependentTable));
                writer.Write(" DROP CONSTRAINT ");
                writer.Write(Quote(dropForeignKeyOperation.Name));

                Statement(writer);
            }
        }
        private DropForeignKeyOperation BuildDropForeignKeyOperation(XDocument edmx, XElement association)
        {
            DebugCheck.NotNull(edmx);
            DebugCheck.NotNull(association);

            var dropForeignKeyOperation
                = new DropForeignKeyOperation(BuildAddForeignKeyOperation(edmx, association));

            BuildForeignKeyOperation(edmx, association, dropForeignKeyOperation);

            return dropForeignKeyOperation;
        }
 protected virtual MigrationStatement Generate(DropForeignKeyOperation op)
 {
     StringBuilder sb = new StringBuilder();
       sb = sb.AppendFormat("alter table `{0}` drop foreign key `{1}`", op.DependentTable, op.Name);
       return new MigrationStatement { Sql = sb.ToString() };
 }
        /// <summary>
        /// Generates code to perform a <see cref="DropForeignKeyOperation" />.
        /// </summary>
        /// <param name="dropForeignKeyOperation"> The operation to generate code for. </param>
        /// <param name="writer"> Text writer to add the generated code to. </param>
        protected virtual void Generate(DropForeignKeyOperation dropForeignKeyOperation, IndentedTextWriter writer)
        {
            Check.NotNull(dropForeignKeyOperation, "dropForeignKeyOperation");
            Check.NotNull(writer, "writer");

            writer.Write("DropForeignKey(");
            writer.Write(Quote(dropForeignKeyOperation.DependentTable));
            writer.Write(", ");

            if (!dropForeignKeyOperation.HasDefaultName)
            {
                writer.Write(Quote(dropForeignKeyOperation.Name));
            }
            else
            {
                var compositeKey = dropForeignKeyOperation.DependentColumns.Count() > 1;

                if (compositeKey)
                {
                    writer.Write("New String() { ");
                }

                writer.Write(dropForeignKeyOperation.DependentColumns.Join(Quote));

                if (compositeKey)
                {
                    writer.Write(" }");
                }

                writer.Write(", ");
                writer.Write(Quote(dropForeignKeyOperation.PrincipalTable));
            }

            writer.WriteLine(")");
        }
 private void Convert(DropForeignKeyOperation dropForeignKeyOperation)
 {
     StringBuilder sql = new StringBuilder();
     sql.Append("ALTER TABLE ");
     AppendTableName(dropForeignKeyOperation.DependentTable, sql);
     if (serverVersion.Major < 9)
         sql.Append(" DROP CONSTRAINT \"");//TODO: http://piecesformthewhole.blogspot.com/2011/04/dropping-foreign-key-if-it-exists-in.html ?
     else
         sql.Append(" DROP CONSTRAINT IF EXISTS \"");
     sql.Append(dropForeignKeyOperation.Name);
     sql.Append('"');
     AddStatment(sql);
 }
 /// <summary>
 /// Gera SQL para uma operação <see cref="DropForeignKeyOperation" />.
 /// </summary>
 /// <param name="dropForeignKeyOperation"> The operation to produce SQL for. </param>
 protected virtual void Generate(DropForeignKeyOperation dropForeignKeyOperation)
 {
     //throw new NotSupportedException();
     // Inicalmente não haverá a criação de chave estrangeira
 }
 protected override void Generate(DropForeignKeyOperation dfo)
 {
     if (dfo.HasDefaultName)
         dfo.Name = GetForeignKeyName(dfo.DependentTable, dfo.PrincipalTable,dfo.DependentColumns);
     base.Generate(dfo);
 }
Beispiel #22
0
        private DropForeignKeyOperation BuildDropForeignKeyOperation(XDocument edmx, XElement association)
        {
            Contract.Requires(edmx != null);
            Contract.Requires(association != null);

            var dropForeignKeyOperation
                = new DropForeignKeyOperation(BuildAddForeignKeyOperation(edmx, association));

            BuildForeignKeyOperation(edmx, association, dropForeignKeyOperation);

            return dropForeignKeyOperation;
        }
        /// <summary>
        /// Generates SQL for a <see cref="DropForeignKeyOperation" />.
        /// Generated SQL should be added using the Statement method.
        /// </summary>
        /// <param name="dropForeignKeyOperation"> The operation to produce SQL for. </param>
        protected virtual void Generate(DropForeignKeyOperation dropForeignKeyOperation)
        {
            Check.NotNull(dropForeignKeyOperation, "dropForeignKeyOperation");

            using (var writer = Writer())
            {
                writer.Write("IF object_id(N'");

                var schema = DatabaseName.Parse(dropForeignKeyOperation.DependentTable).Schema;
                if (schema != null)
                {
                    writer.Write(Escape(Quote(schema)));
                    writer.Write(".");
                }

                writer.Write(Escape(Quote(dropForeignKeyOperation.Name)));
                writer.WriteLine("', N'F') IS NOT NULL");

                writer.Indent++;
                writer.Write("ALTER TABLE ");
                writer.Write(Name(dropForeignKeyOperation.DependentTable));
                writer.Write(" DROP CONSTRAINT ");
                writer.Write(Quote(dropForeignKeyOperation.Name));
                writer.Indent--;

                Statement(writer);
            }
        }
 public void TestDropForeignKeyOperation()
 {
     var operations = new List<MigrationOperation>();
     var operation = new DropForeignKeyOperation();
     operation.Name = "someFK";
     operation.DependentTable = "someTable";
     operations.Add(operation);
     var statments = new NpgsqlMigrationSqlGenerator().Generate(operations, BackendVersion.ToString());
     Assert.AreEqual(1, statments.Count());
     if (BackendVersion.Major > 8)
         Assert.AreEqual("ALTER TABLE \"someTable\" DROP CONSTRAINT IF EXISTS \"someFK\"", statments.ElementAt(0).Sql);
     else
         Assert.AreEqual("ALTER TABLE \"someTable\" DROP CONSTRAINT \"someFK\"", statments.ElementAt(0).Sql);
 }
 protected override void Generate(DropForeignKeyOperation dropForeignKeyOperation, IndentedTextWriter writer)
 {
     dropForeignKeyOperation.PrincipalTable = TrimSchemaPrefix(dropForeignKeyOperation.PrincipalTable);
       dropForeignKeyOperation.DependentTable = TrimSchemaPrefix(dropForeignKeyOperation.DependentTable);
       base.Generate(dropForeignKeyOperation, writer);
 }
		protected virtual IEnumerable<MigrationStatement> Generate(DropForeignKeyOperation operation)
		{
			using (var writer = SqlWriter())
			{
				writer.Write("ALTER TABLE ");
				writer.Write(Quote(ExtractName(operation.DependentTable)));
				writer.Write(" DROP CONSTRAINT ");
				writer.Write(Quote(CreateItemName(operation.Name)));
				yield return Statement(writer);
			}
		}
 protected virtual IEnumerable<MigrationStatement> Generate(DropForeignKeyOperation operation)
 {
     using (var writer = SqlWriter())
     {
         writer.Write("ALTER TABLE ");
         writer.Write(Quote(operation.DependentTable));
         writer.Write(" DROP FOREIGN KEY (");
         WriteColumns(writer, operation.DependentColumns.Select(Quote));
         writer.Write(") REFERENCES ");
         writer.Write(Quote(operation.PrincipalTable));
         yield return Statement(writer);
     }
 }
 protected override MigrationStatement Generate(DropForeignKeyOperation dropForeignKeyOperation)
 {
     dropForeignKeyOperation.Name = StripDbo(dropForeignKeyOperation.Name);
     dropForeignKeyOperation.DependentTable = StripDbo(dropForeignKeyOperation.DependentTable);
       return  base.Generate(dropForeignKeyOperation);
 }