/// <summary> /// Adds the version located in the migration directory that are lower or equal to the version of the old /// migration table to the new migration table. /// </summary> /// <param name="currentVersion">The version that current exists in the old migration table.</param> /// <remarks>This process assumes that no migration scripts were skipped.</remarks> private void AddOldVersionsToNewTable(long currentVersion) { const string scriptNamePattern = "*.sql"; string migrationDirectory = ConfigurationManager.AppSettings[AppSettingKeys.MigrateFolder]; if (!Directory.Exists(migrationDirectory)) { return; } var files = new List <string>(Directory.GetFiles(migrationDirectory, scriptNamePattern)); files.Sort(); string fileName; using (var tran = _dataAccess.BeginTransaction()) { try { foreach (string file in files) { fileName = Path.GetFileName(file); string sVers = fileName.Split('_')[0]; long iVers; if (long.TryParse(sVers, out iVers)) { if (iVers <= currentVersion) { var cmdText = "INSERT INTO [schema_migrations]([version]) VALUES (" + iVers + ")"; using (var cmd = tran.CreateCommand()) { cmd.CommandText = cmdText; cmd.ExecuteNonQuery(); } } } } tran.Commit(); } catch { tran.Rollback(); throw; } } }
/// <summary> /// Creates the migration table into the database. /// </summary> private void CreateMigrationTable() { const string createTableCommand = "CREATE TABLE [schema_migrations]([id] INT NOT NULL IDENTITY(1,1) CONSTRAINT [PK_schema_migrations] PRIMARY KEY, [version] [nvarchar](14) NOT NULL)"; const string firstRecordCommand = "INSERT INTO [schema_migrations] ([version]) VALUES (0)"; using (var tran = _dataAccess.BeginTransaction()) { try { using (var cmd = tran.CreateCommand()) { cmd.CommandText = createTableCommand; cmd.ExecuteNonQuery(); } using (var cmd = tran.CreateCommand()) { cmd.CommandText = firstRecordCommand; cmd.ExecuteNonQuery(); } tran.Commit(); } catch (Exception) { tran.Rollback(); throw; } } }
public void ExecuteScript_should_use_CommandTimeout_specified_in_the_connection_string() { // arrange _subject = DataAccessFactory.Create(TestConnectionString + ";CommandTimeout=1"); // act _subject.OpenConnection(); using (var tran = _subject.BeginTransaction()) { try { _subject.ExecuteScript(tran, "WaitFor Delay '00:00:01'"); } catch(Exception ex) { // assert // Sql Server Compact (which is being used for these tests) does not // support CommandTimeout values other than 0. So we will assume // that if it barfs it is because our non-zero CommandTimeout was successfully (attempted to be) set. Assert.IsInstanceOfType(typeof (ArgumentException), ex); Assert.AreEqual("SqlCeCommand.CommandTimeout does not support non-zero values.", ex.Message); return; } } Assert.Fail("Expected command timeout exception to be thrown."); }