public IGenerator Create(GeneratorOptions options) { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(_connectionString); var server = new Server(builder.DataSource); var database = server.Databases[builder.InitialCatalog]; database.Refresh(); // loads the meta-data if (database.Tables.Contains(options.VersioningTableName)) { return new SqlAggregateMigrationGenerator(server, database, options); } else { return new SqlFirstMigrationGenerator(server, database, options); } }
public SqlAggregateMigrationGenerator(Server server, Database database, GeneratorOptions options) : base(server, database, options) { var providerLocator = new ProviderLocator(new ProviderFactory()); // CLEAN: use DI container ProviderInfo provider = providerLocator.GetExactly(DbPlatform.SqlServer2008); var versioningTableName = new TableName(options.VersioningTableName, options.VersioningTableSchema); _history = new History(versioningTableName, provider.Metadata); IDbConnection connection = server.ConnectionContext.SqlConnectionObject; connection.Open(); connection.ChangeDatabase(Database.Name); // ATTENTION: possibly has side-effects try { _history.Load(connection, null); } finally { connection.Close(); } }
private static void Main() { CommandLineOptions options; CommandLineParser parser; if (DisplayHelp(Environment.CommandLine, out options, out parser)) { Console.WriteLine(GetUsageMessage(parser)); Environment.Exit(SuccessExitCode); } string connectionString; string migrationNamespace; string moduleName; string versioningTableName; string versioningTableSchema; string[] includedSchemas; string[] excludedTables; bool includeData; try { ParseCommandLineArguments(options, parser, ConfigurationManager.ConnectionStrings, out connectionString, out migrationNamespace, out moduleName, out versioningTableName, out versioningTableSchema, out includedSchemas, out excludedTables, out includeData); } catch (InvalidCommandLineArgumentException x) { Console.Error.WriteLine(x.Message); Environment.Exit(x.ExitCode); throw; // will not be executed; just to satisfy R# } var factory = new SqlMigrationGeneratorFactory(connectionString); var generateOptions = new GeneratorOptions { Namespace = migrationNamespace, ModuleName = moduleName, VersioningTableName = versioningTableName, VersioningTableSchema = versioningTableSchema, IncludeData = includeData, }; foreach (string includedSchema in includedSchemas) { generateOptions.IncludedSchemas.Add(includedSchema); } foreach (string excludedTable in excludedTables) { generateOptions.ExcludedTables.Add(excludedTable); } IGenerator generator = factory.Create(generateOptions); string migration = generator.Generate(); if (generator.Errors.Any()) { Console.Error.WriteLine("Following errors have occured:"); Console.Error.WriteLine(string.Join(Environment.NewLine, generator.Errors)); Environment.Exit(UnsuccessfulExitCode); } Clipboard.SetText(migration, TextDataFormat.UnicodeText); Console.WriteLine("The generation of the migration was successful and is now available in your clipboard."); }
public void TestGenerate() { SetupGenerateTest(); // run all migration on second database Migrator migrator2 = new Migrator(ConnectionString2, DbPlatform); migrator2.MigrateAll(typeof(IntegrationTestsBase).Assembly); // generate base-line migration var options = new GeneratorOptions { Namespace = GetType().Namespace, IncludedSchemas = { "dbo", "Schema25", "Schema 29" }, // these schemas belong to the Default module ExcludedTables = { "Order Space" }, // the excluded tables belong other modules IncludeData = true, }; SqlMigrationGeneratorFactory factory = new SqlMigrationGeneratorFactory(ConnectionString2); IGenerator generator = factory.Create(options); string migration = generator.Generate(); Assert.IsEmpty(generator.Errors); Assert.IsTrue(migration.Contains("[AggregateMigrationExport"), "The generated migration should be an aggregated migration."); // compile and execute base-line migration on usual test database Assembly assembly = Compile(migration); Migrator migrator = new Migrator(ConnectionString, DbPlatform); migrator.MigrateAll(assembly, typeof(IntegrationTestsBase).Assembly); // check if the schema of the two databases is equal CollectionAssert.AreEqual(_database2.Tables.Cast<Table>().Select(t => t.Name).ToArray(), DatabaseSmo.Tables.Cast<Table>().Select(t => t.Name).ToArray(), "The tables differ."); IEnumerable<string> script = ScriptDatabase(DatabaseSmo); IEnumerable<string> script2 = ScriptDatabase(_database2); CollectionAssert.AreEqual(script2.ToArray(), script.ToArray()); // check if the data was scripted too VerifyResultsOfAllMigrations(); }
protected SqlMigrationGeneratorBase(Server server, Database database, GeneratorOptions options) { _server = server; _database = database; _options = options; }
[STAThread] // for setting the clipboard private static void Main() { CommandLineOptions options; CommandLineParser parser; if (DisplayHelp(Environment.CommandLine, out options, out parser)) { Console.WriteLine(GetUsageMessage(parser)); Environment.Exit(SuccessExitCode); } string connectionString; string migrationNamespace; string moduleName; string versioningTableName; string versioningTableSchema; string[] includedSchemas; string[] excludedTables; bool includeData; try { ParseCommandLineArguments(options, parser, ConfigurationManager.ConnectionStrings, out connectionString, out migrationNamespace, out moduleName, out versioningTableName, out versioningTableSchema, out includedSchemas, out excludedTables, out includeData); } catch (InvalidCommandLineArgumentException x) { Console.Error.WriteLine(x.Message); Environment.Exit(x.ExitCode); throw; // will not be executed; just to satisfy R# } var factory = new SqlMigrationGeneratorFactory(connectionString); var generateOptions = new GeneratorOptions { Namespace = migrationNamespace, ModuleName = moduleName, VersioningTableName = versioningTableName, VersioningTableSchema = versioningTableSchema, IncludeData = includeData, }; foreach (string includedSchema in includedSchemas) { generateOptions.IncludedSchemas.Add(includedSchema); } foreach (string excludedTable in excludedTables) { generateOptions.ExcludedTables.Add(excludedTable); } IGenerator generator = factory.Create(generateOptions); string migration = generator.Generate(); if (generator.Errors.Any()) { Console.Error.WriteLine("Following errors have occured:"); Console.Error.WriteLine(string.Join(Environment.NewLine, generator.Errors)); Environment.Exit(UnsuccessfulExitCode); } Clipboard.SetText(migration, TextDataFormat.UnicodeText); Console.WriteLine("The generation of the migration was successful and is now available in your clipboard."); }
public SqlFirstMigrationGenerator(Server server, Database database, GeneratorOptions options) : base(server, database, options) { }