namespace LeafWeb.Core.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class LeafOutputFileContents : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.LeafOutputFileContents",
                c => new
                    {
                        Id = c.Int(nullable: false),
                        Contents = c.Binary(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.LeafOutputFile", t => t.Id)
                .Index(t => t.Id);

			Sql(
				"INSERT INTO dbo.LeafOutputFileContents "
				+ "(Id, Contents) "
				+ "SELECT Id, Contents " 
				+ "FROM dbo.LeafOutputFile");
            
            DropColumn("dbo.LeafOutputFile", "Contents");
        }
        
        public override void Down()
        {
            AddColumn("dbo.LeafOutputFile", "Contents", c => c.Binary());

			Sql(
			"UPDATE dbo.LeafOutputFile "
			+ "SET dbo.LeafOutputFile.Contents = FileContents.Contents "
			+ "FROM dbo.LeafOutputFileContents as FileContents "
			+ "WHERE dbo.LeafOutputFile.Id = FileContents.Id");

			DropForeignKey("dbo.LeafOutputFileContents", "Id", "dbo.LeafOutputFile");
            DropIndex("dbo.LeafOutputFileContents", new[] { "Id" });
            DropTable("dbo.LeafOutputFileContents");
        }
    }
}
