示例#1
0
        private static void ScriptTo(string dir, long timestamp = -1)
        {
            Directory.CreateDirectory(dir);
            var options = new MigrationOptions();

            options.OnlyScriptSqlTo(dir);
            var             migrator   = new Migrator(_connectionString, DbPlatform.SqlServer2014, options);
            IMigrationBatch migrations = null;

            if (timestamp != -1)
            {
                migrations = migrator.FetchMigrationsTo(_assembly, timestamp);
            }
            else
            {
                migrations = migrator.FetchMigrations(_assembly);
            }
            if (migrations.Steps.First().Direction == MigrationDirection.Down)
            {
                Console.WriteLine("Migration direction = down");
            }
            Console.WriteLine($"{migrations.Steps.Count} pending migrations");
            Console.WriteLine("Writing migration SQL scripts to {dir}");
            migrations.Execute();
            Console.WriteLine("Done");
        }
示例#2
0
        public void TestScriptingAllMigrations()
        {
            DirectoryInfo targetDirectory = PrepareScriptingDirectory();

            _options.VersioningTableName = "My Versioning Table"; // test overriding the default versioning table name
            _options.OnlyScriptSqlTo(targetDirectory);
            Migrator migrator = CreateMigrator();

            migrator.MigrateAll(typeof(Migration1).Assembly);

            // assert that all script files were generated
            List <FileInfo> scriptFiles = targetDirectory.GetFiles(string.Format(CultureInfo.InvariantCulture, "Migration.*.*.sql"))
                                          .OrderBy(f => int.Parse(Regex.Match(f.Name, @"Migration\..*\.(\d+)\.sql").Groups[1].Value, CultureInfo.InvariantCulture))
                                          .ToList();

            Assert.AreEqual(Migrations.Count, scriptFiles.Count);
            Assert.AreEqual("Migration." + MigrationExportAttribute.DefaultModuleName + ".1.sql", scriptFiles[0].Name);
            Assert.AreEqual("Migration." + Migration2.Module + ".2.sql", scriptFiles[1].Name);

            // assert Versioning table was *not* created as we are scripting only
            DataTable versioningTable = GetTable(_options.VersioningTable);

            Assert.IsNull(versioningTable, string.Format(CultureInfo.CurrentCulture, "The '{0}' table was created altough ScriptingMode was ScriptOnly.", _options.VersioningTableName));

            // assert Customer table was *not* created as we are scripting only
            var       migration1    = new Migration1();
            DataTable customerTable = GetTable(migration1.Tables[0].FullName);

            Assert.IsNull(customerTable, string.Format(CultureInfo.CurrentCulture, "The '{0}' table was created altough ScriptingMode was ScriptOnly.", migration1.Tables[0].FullName));

            // execute generated script files against database and recheck results
            IProviderMetadata metadata = IntegrationTestContext.ProviderMetadata;
            var info = new ConnectionInfo(ConnectionString, metadata.InvariantName, metadata.SupportsTransactions, metadata.EnableAnsiQuotesCommand);

            using (IDbConnection connection = migrator.Configuration.ConnectionFactory.OpenConnection(info))
            {
                foreach (FileInfo scriptFile in scriptFiles)
                {
                    Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Reading script '{0}':", scriptFile.FullName));
                    string[] scriptLines = File.ReadAllLines(scriptFile.FullName);
                    foreach (string line in scriptLines)
                    {
                        Trace.WriteLine(line);
                    }

                    // group all lines between empty lines into one command (some database platforms require DDL operations to
                    // be executed in separated commands)
                    Trace.WriteLine(Environment.NewLine + string.Format(CultureInfo.CurrentCulture, "Executing script '{0}':", scriptFile.FullName));
                    string commandText = string.Empty;
                    foreach (string line in scriptLines)
                    {
                        if (line.Trim().Length != 0)
                        {
                            commandText += line;
                        }
                        else
                        {
                            ExecuteCommand(commandText, connection);
                            commandText = string.Empty;
                        }
                    }
                    Assert.IsEmpty(commandText, "The script should end with an empty line.");
                }
            }
            VerifyResultsOfAllMigrations();

            // delete script files
            targetDirectory.Delete(true);
        }