public void DropOneToOneRightTable() { var configTo = new CustomConfig(); var configFrom = new CustomConfig(); // remove onetooneleft from the config var mappedTypes = (IDictionary<Type, IMap>) typeof(ConfigurationBase).GetField("mappedTypes", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(configTo); mappedTypes.Remove(typeof(OneToOneRight)); configTo.GetMap<OneToOneLeft>().Columns.Remove("Right"); var dialect = new SqlServer2012Dialect(); var migrator = new Migrator( dialect, new CreateTableWriter(dialect), new AlterTableWriter(dialect), new DropTableWriter(dialect), GetMockStatisticsProvider(configFrom)); IEnumerable<string> warnings; IEnumerable<string> errors; var script = migrator.GenerateSqlDiff( configFrom.Maps, configTo.Maps, null, new Mock<ILogger>().Object, new string[0], out warnings, out errors); var dropColIdx = script.IndexOf("alter table [OneToOneLefts] drop column [RightId];", StringComparison.Ordinal); var dropTableIdx = script.IndexOf("drop table [OneToOneRights];", StringComparison.Ordinal); Assert.True(dropColIdx > -1); Assert.True(dropTableIdx > -1); Assert.True(dropColIdx < dropTableIdx); }
public MultipleFetchManyWithNonRootAndThenFetchSqlServerFixture() { this.config = new MultipleFetchManyWithNonRootAndThenFetchConfig(); this.DatabaseName = "DashingIntegration_" + Guid.NewGuid().ToString("D").Substring(0, 8); // load the data using (var transactionLessSession = this.config.BeginTransactionLessSession()) { var dialect = new SqlServer2012Dialect(); var migrator = new Migrator( dialect, new CreateTableWriter(dialect), new AlterTableWriter(dialect), new DropTableWriter(dialect), new StatisticsProvider(null, dialect)); IEnumerable<string> warnings, errors; var createStatement = migrator.GenerateSqlDiff( new List<IMap>(), this.config.Maps, null, new Mock<ILogger>().Object, new string[0], out warnings, out errors); transactionLessSession.Dapper.Execute("create database " + this.DatabaseName); transactionLessSession.Dapper.Execute("use " + this.DatabaseName); transactionLessSession.Dapper.Execute(createStatement); } this.Session = this.config.BeginSession(); this.Session.Dapper.Execute("use " + this.DatabaseName); this.InsertData(); }
private static Migrator MakeMigrator() { var mockStatisticsProvider = new Mock<IStatisticsProvider>(); mockStatisticsProvider.Setup(s => s.GetStatistics(It.IsAny<IEnumerable<IMap>>())) .Returns(new Dictionary<string, Statistics> { { "SimpleClass", new Statistics { HasRows = false } } }); var migrator = new Migrator( new SqlServerDialect(), new CreateTableWriter(new SqlServerDialect()), new AlterTableWriter(new SqlServerDialect()), new DropTableWriter(new SqlServerDialect()), mockStatisticsProvider.Object); return migrator; }
public TestSessionWrapper Initialize() { var dialect = new DialectFactory().Create(this.configuration.ConnectionStringSettings); // load the data using (var transactionLessSession = this.configuration.BeginTransactionLessSession()) { // create database if exists if (!dialect.IgnoreMultipleDatabases) { if (transactionLessSession.Dapper.Query(dialect.CheckDatabaseExists(DatabaseName)).Any()) { transactionLessSession.Dapper.Execute("drop database " + DatabaseName); } transactionLessSession.Dapper.Execute("create database " + DatabaseName); transactionLessSession.Dapper.Execute("use " + DatabaseName); } var migrator = new Migrator( dialect, new CreateTableWriter(dialect), new AlterTableWriter(dialect), new DropTableWriter(dialect), new StatisticsProvider(null, dialect)); IEnumerable<string> warnings, errors; var createStatement = migrator.GenerateSqlDiff( new List<IMap>(), this.configuration.Maps, null, new Mock<ILogger>().Object, new string[0], new string[0], out warnings, out errors); var statements = createStatement.Split(';'); foreach (var statement in statements.Where(s => !string.IsNullOrWhiteSpace(s.Trim()))) { transactionLessSession.Dapper.Execute(statement); } } var session = this.configuration.BeginSession(); if (!dialect.IgnoreMultipleDatabases) { session.Dapper.Execute("use " + DatabaseName); } this.InsertData(session); return new TestSessionWrapper(session); }
private static Migrator MakeMigrator(IConfiguration config, bool hasRows = false) { var mockStatisticsProvider = new Mock<IStatisticsProvider>(); mockStatisticsProvider.Setup(s => s.GetStatistics(It.IsAny<IEnumerable<IMap>>())) .Returns(config.Maps.ToDictionary(m => m.Type.Name, m => new Statistics { HasRows = hasRows })); var migrator = new Migrator( new SqlServerDialect(), new CreateTableWriter(new SqlServerDialect()), new AlterTableWriter(new SqlServerDialect()), new DropTableWriter(new SqlServerDialect()), mockStatisticsProvider.Object); return migrator; }
public void DropSelfReferencedWorks() { var configTo = new CustomConfig(); var configFrom = new CustomConfig(); // remove onetooneleft from the config var mappedTypes = (IDictionary<Type, IMap>) typeof(ConfigurationBase).GetField("mappedTypes", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(configTo); mappedTypes.Remove(typeof(Pair)); var dialect = new SqlServer2012Dialect(); var migrator = new Migrator( dialect, new CreateTableWriter(dialect), new AlterTableWriter(dialect), new DropTableWriter(dialect), GetMockStatisticsProvider(configFrom)); IEnumerable<string> warnings; IEnumerable<string> errors; var script = migrator.GenerateSqlDiff( configFrom.Maps, configTo.Maps, null, new Mock<ILogger>().Object, new string[0], new string[0], out warnings, out errors); Assert.Equal(@"drop table [Pairs];", script.Trim()); }
private static string GenerateMigrationScript( ConnectionStringSettings connectionStringSettings, ReverseEngineerSettings reverseEngineerSettings, IConfiguration configuration, bool naive, out IEnumerable<string> warnings, out IEnumerable<string> errors) { // fetch the from state var dialectFactory = new DialectFactory(); var dialect = dialectFactory.Create(connectionStringSettings.ToSystem()); var factory = DbProviderFactories.GetFactory(connectionStringSettings.ProviderName); // create database if not exists CreateDatabaseIfNotExists(connectionStringSettings, factory, dialect); DatabaseSchema schema; using (new TimedOperation("-- Reading database contents...")) { var databaseReader = new DatabaseReader(connectionStringSettings.ConnectionString, connectionStringSettings.ProviderName); schema = databaseReader.ReadAll(); } IEnumerable<IMap> fromMaps; using (new TimedOperation("-- Reverse engineering...")) { Console.WriteLine(); var engineer = new Engineer(reverseEngineerSettings.ExtraPluralizationWords); fromMaps = engineer.ReverseEngineer(schema, dialect, reverseEngineerSettings.GetTablesToIgnore(), consoleAnswerProvider, false); Console.Write("-- "); } using (var connection = factory.CreateConnection()) { connection.ConnectionString = connectionStringSettings.ConnectionString; // set up migrator IMigrator migrator; if (naive) { throw new NotSupportedException("The Naive Migrator is no longer supported"); } migrator = new Migrator( dialect, new CreateTableWriter(dialect), new AlterTableWriter(dialect), new DropTableWriter(dialect), new StatisticsProvider(connection, dialect)); // run the migrator string script; using (new TimedOperation("-- Generating diff...")) { script = migrator.GenerateSqlDiff( fromMaps, configuration.Maps, consoleAnswerProvider, new ConsoleLogger(isVerbose), reverseEngineerSettings.GetIndexesToIgnore(), out warnings, out errors); // TODO: do things with warnings and errors return script; } } }