public void CanReadLongViewDefinition() { // Arrange var create = new CreateTableExpression { TableName = "Foo", Columns = new List<ColumnDefinition> { new ColumnDefinition { Name = "Id", Type = DbType.Int32 } } }; IList<ViewDefinition> views; var createSql = new StringBuilder(); createSql.Append("CREATE VIEW FooView As SELECT Id,"); createSql.Append("'"); createSql.Append(new string('A', 3000)); createSql.Append("'"); createSql.Append(" As LongText1,"); createSql.Append("'"); createSql.Append(new string('B', 3000)); createSql.Append("'"); createSql.Append(" As LongText2"); createSql.Append(" FROM Foo"); // Act using (var connection = new SqlConnection(ConnectionString)) { var processor = new SqlServerProcessor(connection, new SqlServer2005Generator(), new DebugAnnouncer(), new ProcessorOptions()); processor.Process(create); Assert.IsTrue(processor.TableExists(string.Empty, create.TableName), "SqlServer"); processor.Execute(createSql.ToString()); var dumper = new SqlServerSchemaDumper(processor, new DebugAnnouncer()); views = dumper.ReadViewSchema(); processor.CommitTransaction(); } // Assert Assert.AreEqual(1, views.Count); }
public void VersionInfoCreationScriptsOnlyGeneratedOnceInPreviewMode() { if (!IntegrationTestOptions.SqlServer2008.IsEnabled) return; var connection = new SqlConnection(IntegrationTestOptions.SqlServer2008.ConnectionString); var processorOptions = new ProcessorOptions { PreviewOnly = true }; var outputSql = new StringWriter(); var announcer = new TextWriterAnnouncer(outputSql){ ShowSql = true }; var processor = new SqlServerProcessor(connection, new SqlServer2008Generator(), announcer, processorOptions, new SqlServerDbFactory()); try { var asm = typeof(MigrationRunnerTests).Assembly; var runnerContext = new RunnerContext(announcer) { Namespace = "FluentMigrator.Tests.Integration.Migrations", PreviewOnly = true }; var runner = new MigrationRunner(asm, runnerContext, processor); runner.MigrateUp(1, false); processor.CommitTransaction(); string schemaName = new TestVersionTableMetaData().SchemaName; var schemaAndTableName = string.Format("\\[{0}\\]\\.\\[{1}\\]", schemaName, TestVersionTableMetaData.TABLENAME); var outputSqlString = outputSql.ToString(); var createSchemaMatches = new Regex(string.Format("CREATE SCHEMA \\[{0}\\]", schemaName)).Matches(outputSqlString).Count; var createTableMatches = new Regex("CREATE TABLE " + schemaAndTableName).Matches(outputSqlString).Count; var createIndexMatches = new Regex("CREATE UNIQUE CLUSTERED INDEX \\[" + TestVersionTableMetaData.UNIQUEINDEXNAME + "\\] ON " + schemaAndTableName).Matches(outputSqlString).Count; var alterTableMatches = new Regex("ALTER TABLE " + schemaAndTableName).Matches(outputSqlString).Count; System.Console.WriteLine(outputSqlString); createSchemaMatches.ShouldBe(1); createTableMatches.ShouldBe(1); alterTableMatches.ShouldBe(2); createIndexMatches.ShouldBe(1); } finally { CleanupTestSqlServerDatabase(connection, processor); } }
/// <summary> /// Creates tables in source oracle database and asserts that they exist /// </summary> /// <param name="expressions">The tables to be created</param> private void ExecuteMigrations(params IMigrationExpression[] expressions) { if (expressions == null) return; using (var connection = new SqlConnection(_source.ConnectionString)) { var processor = new SqlServerProcessor(connection, new SqlServer2005Generator(), new DebugAnnouncer(), new ProcessorOptions()); foreach (var expression in expressions) { if (expression is CreateTableExpression) { var create = (CreateTableExpression)expression; processor.Process(create); Assert.IsTrue(processor.TableExists(string.Empty, create.TableName), "Source " + create.TableName); } if (expression is CreateForeignKeyExpression) { processor.Process((CreateForeignKeyExpression)expression); } if (expression is InsertDataExpression) { processor.Process((InsertDataExpression)expression); } } processor.CommitTransaction(); } }
/// <summary> /// Creates views in Oracle using the <see cref="ViewDefinition.CreateViewSql"/> /// </summary> /// <param name="view">The views to be created</param> private void CreateViews(params ViewDefinition[] view) { using (var connection = new SqlConnection(_source.ConnectionString)) { var processor = new SqlServerProcessor(connection, new SqlServer2005Generator(), new DebugAnnouncer(), new ProcessorOptions()); foreach (var viewDefinition in view) { processor.Execute(viewDefinition.CreateViewSql); } processor.CommitTransaction(); } }
/// <summary> /// Creates a single column table using the spplied type and retruns its <see cref="ColumnDefinition"/> /// </summary> /// <param name="type">The Sql Server data type to apply to the column</param> /// <returns>The translated <see cref="ColumnDefinition"/></returns> private TableDefinition GetTableColumnColumns(string createSql, string name, params IMigrationExpression[] expresions) { IList<TableDefinition> tables; // Act using (var connection = new SqlConnection(ConnectionString)) { var processor = new SqlServerProcessor(connection, new SqlServer2005Generator(), new DebugAnnouncer(), new ProcessorOptions()); if (!string.IsNullOrEmpty(createSql)) processor.Execute(createSql); foreach (var expresion in expresions) { if (expresion is CreateTableExpression) processor.Process((CreateTableExpression)expresion); if (expresion is CreateIndexExpression) processor.Process((CreateIndexExpression)expresion); if (expresion is CreateForeignKeyExpression) processor.Process((CreateForeignKeyExpression)expresion); } Assert.IsTrue(processor.TableExists(string.Empty, name), "SqlServer"); var dumper = new SqlServerSchemaDumper(processor, new DebugAnnouncer()); tables = dumper.ReadDbSchema(); processor.CommitTransaction(); } if (!string.IsNullOrEmpty(createSql)) tables.Count.ShouldBe(1); return tables.Where(t => t.Name == name).FirstOrDefault(); }
/// <summary> /// Creates tables in SQL Server and asserts that they exist /// </summary> /// <param name="createTables">The tables to be created</param> private void CreateTables(params CreateTableExpression[] createTables) { if (createTables == null) return; using (var connection = new SqlConnection(_sqlContext.ConnectionString)) { var processor = new SqlServerProcessor(connection, new SqlServer2005Generator(), new DebugAnnouncer(), new ProcessorOptions()); foreach (var create in createTables) { processor.Process(create); Assert.IsTrue(processor.TableExists(string.Empty, create.TableName), "SqlServer " + create.TableName); } processor.CommitTransaction(); } }
/// <summary> /// Migrates a set of tables using the provided context /// </summary> /// <param name="indexes">The indexes to be created in SQL Server and migrated to Oracle</param> private void CreateIndexes(params CreateIndexExpression[] indexes) { using (var connection = new SqlConnection(_sqlContext.ConnectionString)) { var processor = new SqlServerProcessor(connection, new SqlServer2005Generator(), new DebugAnnouncer(), new ProcessorOptions()); foreach (var index in indexes) { processor.Process(index); } processor.CommitTransaction(); } }
/// <summary> /// Migrates tables from SQL Server to Oracle and queries the data in the new Oracle table /// </summary> /// <param name="expressions">The expressions to execute as part of the migration</param> /// <param name="expectedMigrations">The number of migrations that should be created</param> /// <param name="contextAction">Delegates that alter the context before the migration is executed</param> /// <returns>The data in the new Oracle table</returns> private DataSet MigrateToOracleWithData(IEnumerable<IMigrationExpression> expressions, int expectedMigrations, params Action<SchemaMigrationContext>[] contextAction) { var context = GetDefaultContext(); context.GenerateAlternateMigrationsFor.Add(DatabaseType.Oracle); context.Type = context.Type | MigrationType.Data; if (contextAction != null) { foreach (var action in contextAction) { action(context); } } CreateTableExpression create = null; using (var connection = new SqlConnection(_sqlContext.ConnectionString)) { var processor = new SqlServerProcessor(connection, new SqlServer2005Generator(), new DebugAnnouncer(), new ProcessorOptions()); foreach (var action in expressions) { if (action is CreateTableExpression) { create = (CreateTableExpression) action; processor.Process(create); Assert.IsTrue(processor.TableExists(string.Empty, create.TableName), "SqlServer " + create.TableName); continue; } if (action is InsertDataExpression) { var insert = (InsertDataExpression)action; processor.Process(insert); continue; } if (action is CreateIndexExpression) { var index = (CreateIndexExpression)action; processor.Process(index); continue; } if (action is CreateForeignKeyExpression) { var index = (CreateForeignKeyExpression)action; processor.Process(index); continue; } } processor.CommitTransaction(); } MigrateTable(context); if (create != null) AssertOracleTablesExist(create); context.MigrationIndex.ShouldBe(expectedMigrations); return create != null ? GetOracleTableData(create.TableName) : null; }
public void CanReadMultipleViews() { // Arrange var create = new CreateTableExpression { TableName = "Foo", Columns = new List<ColumnDefinition> { new ColumnDefinition { Name = "Id", Type = DbType.Int32 } } }; IList<ViewDefinition> views; // Act using (var connection = new SqlConnection(ConnectionString)) { var processor = new SqlServerProcessor(connection, new SqlServer2005Generator(), new DebugAnnouncer(), new ProcessorOptions()); processor.Process(create); Assert.IsTrue(processor.TableExists(string.Empty, create.TableName), "SqlServer"); processor.Execute("CREATE VIEW FooViewC AS SELECT Id FROM Foo"); processor.Execute("CREATE VIEW FooViewB AS SELECT Id FROM Foo"); processor.Execute("CREATE VIEW FooViewA AS SELECT Id FROM Foo"); var dumper = new SqlServerSchemaDumper(processor, new DebugAnnouncer()); views = dumper.ReadViewSchema(); processor.CommitTransaction(); } // Assert Assert.AreEqual(3, views.Count); Assert.AreEqual("FooViewA", views[0].Name); Assert.AreEqual("FooViewB", views[1].Name); Assert.AreEqual("FooViewC", views[2].Name); }