예제 #1
0
        public void EnsureSchema(DatabaseContext context)
        {
            var sql = string.Format(@"

            SELECT * FROM INFORMATION_SCHEMA.TABLES
            WHERE TABLE_SCHEMA = 'dbo'
            AND  TABLE_NAME = '{0}'

            ", _tableName);

            var properties = context.ExecuteQuery(sql);

            if (properties.Count == 0)
            {
                var newSchemaSql = string.Format(@"

            CREATE TABLE [dbo].[{0}]
            (
            [MigrationVersion] int NOT NULL,
            CONSTRAINT PK_MigrationVersion PRIMARY KEY CLUSTERED ([MigrationVersion])
            );

            INSERT INTO [dbo].[{0}] VALUES('0');

            ",
                    _tableName);

                context.ExecuteNonQuery(newSchemaSql);
            }
        }
예제 #2
0
 public int GetDatabaseVersionNumber(DatabaseContext context)
 {
     var versionProperty = context.ExecuteQuery(string.Format("select * from sys.extended_properties where [class] = 0 and [name] = '{0}'", ExtProp.DatabaseVersion))
         .Single();
     var currentVersion = int.Parse(versionProperty["value"].ToString());
     return currentVersion;
 }
예제 #3
0
        public void EnsureSchema(DatabaseContext context)
        {
            var sql = string.Format("select * from sys.extended_properties where [class] = 0 and [name] = '{0}'", ExtProp.DatabaseVersion);

            var properties = context.ExecuteQuery(sql);

            if (properties.Count == 0)
            {
                context.ExecuteNonQuery(string.Format("exec sys.sp_addextendedproperty @name=N'{0}', @value=N'{1}'", ExtProp.DatabaseVersion, "0"));
            }
        }
예제 #4
0
 public void UpdateVersion(DatabaseContext context, int newVersion)
 {
     context.ExecuteNonQuery(string.Format("UPDATE dbo.{0} SET MigrationVersion = '{1}'", _tableName, newVersion));
 }
예제 #5
0
 public int GetDatabaseVersionNumber(DatabaseContext context)
 {
     var currentVersion = context.GetSingleValue<int>(_tableName, "MigrationVersion");
     return currentVersion;
 }
예제 #6
0
 public void UpdateVersion(DatabaseContext context, int newVersion)
 {
     context.ExecuteNonQuery(string.Format("exec sys.sp_updateextendedproperty @name=N'{0}', @value=N'{1}'", ExtProp.DatabaseVersion, newVersion));
 }
예제 #7
0
 private void EnsureDatabaseHasVersionMetaData()
 {
     using (var context = new DatabaseContext(_dbConnection))
     {
         context.NewTransaction();
         _communicator.EnsureSchema(context);
         context.Commit();
     }
 }
예제 #8
0
 private int GetDatabaseVersionNumber(DatabaseContext context)
 {
     return _communicator.GetDatabaseVersionNumber(context);
 }
예제 #9
0
 private int GetDatabaseVersionNumber()
 {
     using (var context = new DatabaseContext(_dbConnection))
     {
         return GetDatabaseVersionNumber(context);
     }
 }
예제 #10
0
        void ExecuteMigration(IMigration migration)
        {
            BeforeExecute(migration);

            try
            {
                using (var context = new DatabaseContext(_dbConnection))
                {
                    context.NewTransaction();
                    foreach (var sqlStatement in migration.SqlStatements)
                    {
                        try
                        {
                            context.ExecuteNonQuery(sqlStatement);
                        }
                        catch (Exception e)
                        {
                            throw new DatabaseMigrationException(e,
                                @"The following SQL could not be executed:

{0}

Exception:

{1}",
                                              sqlStatement, e);
                        }
                    }

                    var currentVersion = GetDatabaseVersionNumber(context);
                    var newVersion = currentVersion + 1;

                    _communicator.UpdateVersion(context, newVersion);

                    context.Commit();
                }

                AfterExecuteSuccess(migration);
            }
            catch (Exception e)
            {
                AfterExecuteError(migration, e);

                throw new DatabaseMigrationException(e, "The migration {0} (db version -> {1}) could not be executed", migration.Description, migration.TargetDatabaseVersion);
            }
        }