Created
August 10, 2011 10:27
-
-
Save chaliy/1136525 to your computer and use it in GitHub Desktop.
My super migraiton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Configuration; | |
using System.Data.Common; | |
public static class Migrate | |
{ | |
public static void Start() | |
{ | |
try | |
{ | |
EnsureInit(); | |
ExecuteMigration("StoreCategory_SearchOptimization", | |
@"ALTER TABLE dbo.[Category] | |
ADD SearchOptimisationPageTitle nvarchar(max) NULL, | |
SearchOptimisationPageKeywords nvarchar(max) NULL, | |
SearchOptimisationPageDescription nvarchar(max) NULL"); | |
} | |
catch (Exception ex) | |
{ | |
Trace.TraceLogFactory.Create().Error("Error during app migration. " + ex.Message, ex); | |
} | |
} | |
private static void ExecuteMigration(string migrationId, string migration) | |
{ | |
ExecuteScript(@"IF NOT EXISTS (SELECT * FROM [dbo].[__Migrations] WHERE MigrationID = '" + migrationId + @"') | |
BEGIN | |
" + migration + @" | |
INSERT INTO [dbo].[__Migrations] ([MigrationId] ,[LastExecuted]) VALUES ('" + migrationId + @"', GETDATE()) | |
END | |
"); | |
} | |
private static void EnsureInit() | |
{ | |
ExecuteScript(@"IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[__Migrations]') AND type in (N'U')) | |
CREATE TABLE [dbo].[__Migrations]( | |
[MigrationID] [nvarchar](450) NOT NULL PRIMARY KEY, | |
[LastExecuted] [datetime2](7) NOT NULL )"); | |
} | |
private static void ExecuteScript(string sql) | |
{ | |
using(var con = CreateOpenConnection()) | |
{ | |
var cmd = con.CreateCommand(); | |
cmd.CommandText = sql; | |
cmd.ExecuteNonQuery(); | |
} | |
} | |
private static DbConnection CreateOpenConnection() | |
{ | |
var connectionConfig = ConfigurationManager.ConnectionStrings["JA.Catalogue.Properties.Settings.CatalogueConnectionString"]; | |
var factory = DbProviderFactories.GetFactory("System.Data.SqlClient"); | |
var con = factory.CreateConnection(); | |
con.ConnectionString = connectionConfig.ConnectionString; | |
con.Open(); | |
return con; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment