public void MigrateSqlScript() { MockRepository mocks = new MockRepository(); IAdoMigrationContext context = mocks.CreateMock<IAdoMigrationContext>(); FakeDatabase db = mocks.CreateMock<FakeDatabase>(new object[] { "connString", SqlClientFactory.Instance }); DbTransaction trans = mocks.CreateMock<DbTransaction>(); DbCommand cmd = mocks.CreateMock<DbCommand>(); DatabaseType dbType = mocks.CreateMock<DatabaseType>("sqlserver"); // We have 3 DatabaseType property access on the execution path Expect.Call(context.DatabaseType).Return(dbType).Repeat.Times(3); Expect.Call(context.Database).Return(db); context.Commit(); LastCall.On(context).Repeat.Once(); // CREATE TABLE statement Expect.Call(context.Transaction).Return(trans); Expect.Call(db.ExecuteNonQuery(cmd, trans)).IgnoreArguments().Return(1); // INSERT statement Expect.Call(context.Transaction).Return(trans); Expect.Call(db.ExecuteNonQuery(cmd, trans)).IgnoreArguments().Return(1); mocks.ReplayAll(); using (StreamReader sr = File.OpenText("..\\..\\sql\\oracle\\patch0003_dummy_SQL_file.sql")) { try { SqlScriptMigrationTask task = new SqlScriptMigrationTask("patch0003_dummy_SQL_file", 3, sr); task.Migrate(context); } catch (MigrationException) { Assert.Fail("We should not have got an exception"); } finally { sr.Close(); } } mocks.VerifyAll(); }
public void MigrateSqlString() { String sql = "INSERT INTO order_table_1 (id, value) VALUES (1, 'order_table_1')"; MockRepository mocks = new MockRepository(); IAdoMigrationContext context = mocks.CreateMock<IAdoMigrationContext>(); FakeDatabase db = mocks.CreateMock<FakeDatabase>(new object[] {"connString", SqlClientFactory.Instance}); DbTransaction trans = mocks.CreateMock<DbTransaction>(); DbCommand cmd = mocks.CreateMock<DbCommand>(); DatabaseType dbType = mocks.CreateMock<DatabaseType>("sqlserver"); Expect.Call(context.DatabaseType).Return(dbType); Expect.Call(context.Database).Return(db); context.Commit(); LastCall.On(context).Repeat.Once(); Expect.Call(context.Transaction).Return(trans); Expect.Call(db.ExecuteNonQuery(cmd, trans)).IgnoreArguments().Return(1); mocks.ReplayAll(); SqlScriptMigrationTask task = new SqlScriptMigrationTask("test", 1, sql); try { task.Migrate(context); } catch (MigrationException) { Assert.Fail("We should not have got an exception"); } mocks.VerifyAll(); }
public void MigrateSqlStringWithDbException() { String sql = "INSERT INTO order_table_1 (id, value) VALUES (1, 'order_table_1')"; MockRepository mocks = new MockRepository(); IAdoMigrationContext context = mocks.CreateMock<IAdoMigrationContext>(); FakeDatabase db = mocks.CreateMock<FakeDatabase>(new object[] { "connString", SqlClientFactory.Instance }); DbTransaction trans = mocks.CreateMock<DbTransaction>(); DbCommand cmd = mocks.CreateMock<DbCommand>(); DatabaseType dbType = mocks.CreateMock<DatabaseType>("sqlserver"); Expect.Call(context.DatabaseType).Return(dbType); Expect.Call(context.Database).Return(db); context.Commit(); LastCall.On(context).Repeat.Once(); Expect.Call(context.Transaction).Return(trans); Expect.Call(db.ExecuteNonQuery(cmd, trans)).IgnoreArguments() .Throw(new MigrationException("Something bad happened")); mocks.ReplayAll(); try { SqlScriptMigrationTask task = new SqlScriptMigrationTask("test", 1, sql); task.Migrate(context); } catch (MigrationException me) { Assert.AreEqual("Something bad happened", me.InnerException.Message); mocks.VerifyAll(); throw me; } }
public void ParsingMultipleStatements2() { MockRepository mocks = new MockRepository(); IAdoMigrationContext context = mocks.CreateMock<IAdoMigrationContext>(); DatabaseType dbType = mocks.CreateMock<DatabaseType>("sqlserver"); using (mocks.Ordered()) { Expect.Call(context.DatabaseType).Return(dbType); } mocks.ReplayAll(); using (StreamReader sr = File.OpenText("..\\..\\sql\\oracle\\patch0005_fifth_patch.sql")) { try { SqlScriptMigrationTask task = new SqlScriptMigrationTask("patch0005_fifth_patch", 4, sr); IList<String> list = task.GetSqlStatements(context); Assert.AreEqual(1, list.Count); Assert.AreEqual("insert into user_role_assoc (user_role_id, application_user_id, " + "role_code, project_id) " + Environment.NewLine + "\t\t\tvalues (nextval('role_id_seq'),2, 'SYSA', 3)", list[0]); } catch (MigrationException) { Assert.Fail("We should not have got an exception"); } finally { sr.Close(); } } mocks.VerifyAll(); }
/// <summary> /// Creates a list of <code>SqlScriptMigrationTask</code>s based on the array /// of SQL scripts. /// </summary> /// <param name="scripts"> /// the classpath-relative array of SQL migration scripts /// </param> /// <returns> /// a list of <code>SqlScriptMigrationTask</code>s based on the array of SQL scripts /// </returns> /// <exception cref="MigrationException"> /// if a <code>SqlScriptMigrationTask</code> could no be created /// </exception> private IList<IMigrationTask> CreateMigrationScripts(IList<String> scripts) { IList<IMigrationTask> tasks = new List<IMigrationTask>(); //Pattern p = Pattern.compile(SQL_PATCH_REGEX); Regex p = new Regex(SQL_PATCH_REGEX, RegexOptions.Compiled); foreach (String script in scripts) { //script = script.Replace('\\', '/'); log.Debug("Examining possible SQL patch file \"" + script + "\""); //Console.WriteLine("Examining possible SQL patch file \"" + script + "\""); FileInfo fileInfo = new FileInfo(script); if (!fileInfo.Exists) { log.Warn("File \"" + script + "\" does not exist. Skipping"); //Console.WriteLine("File \"" + script + "\" does not exist. Skipping"); continue; } String fileName = fileInfo.Name; Match matcher = p.Match(fileName.ToLower()); // Get the version out of the script name if (!matcher.Success || matcher.Groups.Count != 3) { throw new MigrationException("Invalid SQL script name: " + fileName); } int level = 0; if (!Int32.TryParse(matcher.Groups[1].Value, out level)) { log.Warn("Could not parse patch level. Skipping"); //Console.WriteLine("Could not parse patch level. Skipping"); continue; } using (StreamReader sr = File.OpenText(script)) { try { // We should send in the script file location so // it doesn't have to buffer the whole thing into RAM SqlScriptMigrationTask task = new SqlScriptMigrationTask(fileName.Replace(".sql", ""), level, sr); tasks.Add(task); } catch (Exception e) { throw new MigrationException("Error reading script " + script, e); } finally { sr.Close(); } } } return tasks; }