Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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;
 }
Ejemplo n.º 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"));
            }
        }
Ejemplo n.º 4
0
 public void UpdateVersion(DatabaseContext context, int newVersion)
 {
     context.ExecuteNonQuery(string.Format("UPDATE dbo.{0} SET MigrationVersion = '{1}'", _tableName, newVersion));
 }
Ejemplo n.º 5
0
 public int GetDatabaseVersionNumber(DatabaseContext context)
 {
     var currentVersion = context.GetSingleValue<int>(_tableName, "MigrationVersion");
     return currentVersion;
 }
Ejemplo n.º 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));
 }
Ejemplo n.º 7
0
 private void EnsureDatabaseHasVersionMetaData()
 {
     using (var context = new DatabaseContext(_dbConnection))
     {
         context.NewTransaction();
         _communicator.EnsureSchema(context);
         context.Commit();
     }
 }
Ejemplo n.º 8
0
 private int GetDatabaseVersionNumber(DatabaseContext context)
 {
     return _communicator.GetDatabaseVersionNumber(context);
 }
Ejemplo n.º 9
0
 private int GetDatabaseVersionNumber()
 {
     using (var context = new DatabaseContext(_dbConnection))
     {
         return GetDatabaseVersionNumber(context);
     }
 }
Ejemplo n.º 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);
            }
        }