public void GenerateCanOutputAddColumnStatementWhenNonNullableAndNoDefaultProvided()
        {
            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var column = new ColumnModel(PrimitiveTypeKind.Int32)
            {
                Name = "Bar",
                IsNullable = false
            };
            var addColumnOperation = new AddColumnOperation("Foo", column);

            var sql = migrationSqlGenerator.Generate(new[] { addColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);

            Assert.True(sql.Contains("ALTER TABLE \"Foo\" ADD \"Bar\" integer NOT NULL DEFAULT 0"));
        }
        public void GenerateCanOutputAddColumnStatement(PrimitiveTypeKind type, string typeName)
        {
            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var column = new ColumnModel(type)
            {
                Name = "Bar",
                IsIdentity = true
            };
            var addColumnOperation = new AddColumnOperation("Foo", column);

            var sql = migrationSqlGenerator.Generate(
                new[] { addColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);

            Assert.True(sql.Contains(string.Format("ALTER TABLE \"Foo\" ADD \"Bar\" {0}", typeName)));
        }
        public void GenerateAlterColumn(bool? nullable, string contain, string notContain)
        {
            var migrationProvider = new PostgreSqlMigrationSqlGenerator();

            var column = new ColumnModel(PrimitiveTypeKind.String)
                {
                    Name = "C",
                    MaxLength = 120,
                    IsNullable = nullable
                };

            var alterColumnOperation = new AlterColumnOperation("dbo.T", column, false);

            var sql = migrationProvider.Generate(new[] { alterColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);

            const string returnSqlBase = "ALTER TABLE \"dbo\".\"T\" ALTER COLUMN \"C\" ";

            Assert.IsNotNull(sql);
            Assert.True(sql.Contains(returnSqlBase));
            Assert.True(sql.Contains(returnSqlBase + "TYPE varchar(120)"));
            Assert.True(sql.Contains(returnSqlBase + contain));
            Assert.False(sql.Contains(returnSqlBase + notContain));
        }
        public void GenerateShouldOutputInvariantDecimalsWhenNonInvariantCulture()
        {
            var migrationProvider = new PostgreSqlMigrationSqlGenerator();

            var addColumnOperation
                = new AddColumnOperation(
                    "T",
                    new ColumnModel(PrimitiveTypeKind.Binary)
                    {
                        Name = "C",
                        DefaultValue = 123.45m
                    });

            var lastCulture = Thread.CurrentThread.CurrentCulture;

            try
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("nl-NL");

                var sql = migrationProvider.Generate(new[] { addColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);

                Assert.True(sql.Contains("ALTER TABLE \"T\" ADD \"C\" bytea DEFAULT 123,45"));
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = lastCulture;
            }
        }
        public void GenerateCanOutputTimestampColumn()
        {
            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var createTableOperation = new CreateTableOperation("Customers");
            var column = new ColumnModel(PrimitiveTypeKind.Binary)
            {
                Name = "Version",
                IsTimestamp = true
            };
            createTableOperation.Columns.Add(column);

            var sql = migrationSqlGenerator.Generate(new[] { createTableOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);

            Assert.True(sql.Contains("\"Version\" bytea"));
        }
        public void GenerateCanOutputDropTableStatement()
        {
            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var sql = migrationSqlGenerator.Generate(new[] { new DropTableOperation("dbo.Customers") }, "9.2").Join(
                s => s.Sql, Environment.NewLine);

            Assert.True(sql.Contains("DROP TABLE \"dbo\".\"Customers\""));
        }
        public void GenerateCanOutputDropPrimaryKeyOperation()
        {
            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var dropPrimaryKeyOperation = new DropPrimaryKeyOperation
            {
                Table = "T"
            };

            var sql = migrationSqlGenerator.Generate(new[] { dropPrimaryKeyOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);

            Assert.True(sql.Contains("ALTER TABLE \"T\" DROP CONSTRAINT \"PK_T\""));
        }
        public void GenerateCanOutputDropIndexOperation()
        {
            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var dropIndexOperation = new DropIndexOperation
            {
                Table = "dbo.Custumers"
            };

            dropIndexOperation.Columns.Add("Id");

            var sql = migrationSqlGenerator.Generate(new[] { dropIndexOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);

            Assert.True(sql.Contains("DROP INDEX \"dbo\".\"IX_dbo.Custumers_Id\""));
        }
        public void GenerateCanOutputAddFkStatement()
        {
            var addForeignKeyOperation = new AddForeignKeyOperation
            {
                PrincipalTable = "Customers",
                DependentTable = "Orders",
                CascadeDelete = true
            };
            addForeignKeyOperation.PrincipalColumns.Add("CustomerId");
            addForeignKeyOperation.DependentColumns.Add("CustomerId");

            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var sql = migrationSqlGenerator.Generate(new[] { addForeignKeyOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);

            Assert.True(
                sql.Contains(
                    "ALTER TABLE \"Orders\" ADD CONSTRAINT \"FK_Orders_Customers_CustomerId\" FOREIGN KEY (\"CustomerId\") REFERENCES \"Customers\" (\"CustomerId\") ON DELETE CASCADE"));
        }
        public void GenerateCanOutputDeleteHistoryOperation()
        {
            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var sql = migrationSqlGenerator.Generate(new[] { new DeleteHistoryOperation("__MigrationHistory", "201212120818356_AddHouses") }, "9.2").Join(
                s => s.Sql, Environment.NewLine);

            Assert.True(sql.Contains("DELETE FROM \"dbo\".\"__MigrationHistory\" WHERE \"MigrationId\" = '201212120818356_AddHouses'"));
        }
        public void GenerateCanOutputCustomSqlOperation()
        {
            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var sql = migrationSqlGenerator.Generate(new[] { new SqlOperation("insert into foo") }, "9.2").Join(
                s => s.Sql, Environment.NewLine);

            Assert.True(sql.Contains(@"insert into foo"));
        }
        public void GenerateCanOutputCreateTableStatement()
        {
            var createTableOperation = new CreateTableOperation("dbo.Customers");
            var idColumn = new ColumnModel(PrimitiveTypeKind.Int32)
            {
                Name = "Id",
                IsNullable = true,
                IsIdentity = true
            };
            createTableOperation.Columns.Add(idColumn);
            createTableOperation.Columns.Add(
                new ColumnModel(PrimitiveTypeKind.String)
                {
                    Name = "Name",
                    IsNullable = false
                });
            createTableOperation.PrimaryKey = new AddPrimaryKeyOperation();
            createTableOperation.PrimaryKey.Columns.Add(idColumn.Name);

            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var sql = migrationSqlGenerator.Generate(new[] { createTableOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);

            Assert.True(
                sql.Contains("CREATE TABLE \"dbo\".\"Customers\" (\r\n    \"Id\" serial,\r\n    \"Name\" text NOT NULL,\r\n    CONSTRAINT \"PK_dbo.Customers\" PRIMARY KEY (\"Id\")\r\n)"));
        }
        public void GenerateCanOutputCreateIndexStatement()
        {
            var createTableOperation = new CreateTableOperation("Customers");
            var idColumn = new ColumnModel(PrimitiveTypeKind.Int32)
            {
                Name = "Id",
                IsNullable = true,
                IsIdentity = true
            };
            createTableOperation.Columns.Add(idColumn);
            createTableOperation.Columns.Add(
                new ColumnModel(PrimitiveTypeKind.String)
                {
                    Name = "Name",
                    IsNullable = false
                });
            createTableOperation.PrimaryKey = new AddPrimaryKeyOperation();
            createTableOperation.PrimaryKey.Columns.Add(idColumn.Name);

            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var createIndexOperation = new CreateIndexOperation
            {
                Table = createTableOperation.Name,
                IsUnique = true
            };

            createIndexOperation.Columns.Add(idColumn.Name);

            var sql
                = migrationSqlGenerator.Generate(
                    new[]
                        {
                            createIndexOperation
                        },
                    "9.2").Join(s => s.Sql, Environment.NewLine);

            Assert.True(
                sql.Contains(
                    @"CREATE UNIQUE INDEX ""IX_Customers_Id"" ON ""Customers""(""Id"")"));
        }
        public void GenerateCanOutputAddTimestampStoreTypeColumnOperation()
        {
            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var addColumnOperation
                = new AddColumnOperation(
                    "T",
                    new ColumnModel(PrimitiveTypeKind.Binary)
                    {
                        IsNullable = false,
                        Name = "C",
                        StoreType = "timestamp"
                    });

            var sql = migrationSqlGenerator.Generate(new[] { addColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);

            Assert.True(sql.Contains("ALTER TABLE \"T\" ADD \"C\" timestamp NOT NULL"));
        }
        public void GenerateCanOutputAddPrimaryKeyOperation()
        {
            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var addPrimaryKeyOperation = new AddPrimaryKeyOperation
            {
                Table = "T"
            };

            addPrimaryKeyOperation.Columns.Add("c1");
            addPrimaryKeyOperation.Columns.Add("c2");

            var sql = migrationSqlGenerator.Generate(new[] { addPrimaryKeyOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);

            Assert.True(sql.Contains("ALTER TABLE \"T\" ADD CONSTRAINT \"PK_T\" PRIMARY KEY (\"c1\", \"c2\")"));
        }
        public void GenerateCanOutputDropColumn()
        {
            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var dropColumnOperation = new DropColumnOperation("Customers", "Foo");

            var sql = migrationSqlGenerator.Generate(new[] { dropColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);

            Assert.True(sql.Contains("ALTER TABLE \"Customers\" DROP COLUMN \"Foo\""));
        }
        public void GenerateCanOutputAddColumnStatementWithCustomStoreType()
        {
            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var column = new ColumnModel(PrimitiveTypeKind.String)
            {
                Name = "Bar",
                StoreType = "character",
                MaxLength = 15
            };
            var addColumnOperation = new AddColumnOperation("Foo", column);

            var sql = migrationSqlGenerator.Generate(new[] { addColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);

            Assert.True(sql.Contains("ALTER TABLE \"Foo\" ADD \"Bar\" character(15)"));
        }
        public void GenerateCanOutputAddColumnStatementWithExplicitDefaultValueSql()
        {
            var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();

            var column = new ColumnModel(PrimitiveTypeKind.Guid)
            {
                Name = "Bar",
                IsNullable = false,
                DefaultValueSql = "42"
            };
            var addColumnOperation = new AddColumnOperation("Foo", column);

            var sql = migrationSqlGenerator.Generate(new[] { addColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);

            Assert.True(sql.Contains("ALTER TABLE \"Foo\" ADD \"Bar\" uuid NOT NULL DEFAULT 42"));
        }