Пример #1
0
        public void Migrate(DatabaseConnectionInfo databaseConnectionInfo, MigrationInfo migrationInfo)
        {
            using (var connection = _connectionFactory.Create(databaseConnectionInfo))
            {
                Output.Text("Connected to oracle database on connection string '{0}'.".FormatWith(databaseConnectionInfo.ConnectionString));
                Output.EmptyLine();

                Output.Text("Checking for the current database version.");
                var oracleMetadataProvider = _databaseMetadataProviderFactory.Create(connection);
                var oracleVersioning = _versionRespositoryFactory.Create(connection);
                var oracleAppliedScriptsRepository = _appliedScriptsRepositoryFactory.Create(connection);

                var currentVersion = oracleMetadataProvider.SchemaExists(migrationInfo.TargetSchema) ? oracleVersioning.GetCurrentVersion(migrationInfo.TargetSchema) : null;
                Output.Info("The current database version is: {0}".FormatWith(currentVersion.IsNotNull() ? currentVersion.Version.ToString(CultureInfo.InvariantCulture) : "<empty>"));
                Output.EmptyLine();

                Output.Info("Scanning input folder '{0}' for scripts...".FormatWith(migrationInfo.ScriptFolder));

                var initScripts = ScanForScripts(migrationInfo, ScriptFolders.Init, _scriptScannerFactory.Create(ScriptFolders.Init)).ToArray();
                var upScripts = ScanForScripts(migrationInfo, ScriptFolders.Up, _scriptScannerFactory.Create(ScriptFolders.Up)).ToArray();
                var downScripts = ScanForScripts(migrationInfo, ScriptFolders.Down, _scriptScannerFactory.Create(ScriptFolders.Down)).ToArray();
                var termScripts = ScanForScripts(migrationInfo, ScriptFolders.Term, _scriptScannerFactory.Create(ScriptFolders.Term)).ToArray();
                Output.EmptyLine();

                if (migrationInfo.TargetVersion.HasValue)
                {
                    Output.Info("Target database version was provided, will target migrating the database to version {0}".FormatWith(migrationInfo.TargetVersion.Value));
                }

                VerifyDownScripts(upScripts, downScripts);

                var storedMigrationSteps = new List<IStep>();

                if (migrationInfo.UseStored)
                {
                    Output.Info("--usestored was specified, fetching set of applied scripts stored in the target database...".FormatWith());

                    storedMigrationSteps = oracleAppliedScriptsRepository.GetAppliedScripts(migrationInfo.TargetSchema).ToList();

                    Output.Text("    {0} stored applied scripts found.".FormatWith(storedMigrationSteps.Count));
                    Output.EmptyLine();
                }

                var scriptRunner = _scriptRunnerFactory.Create(databaseConnectionInfo);
                RunMigration(migrationInfo, currentVersion, initScripts, upScripts, downScripts, termScripts, storedMigrationSteps, scriptRunner, oracleMetadataProvider, oracleVersioning, oracleAppliedScriptsRepository);

                if (oracleMetadataProvider.SchemaExists(migrationInfo.TargetSchema))
                {
                    var newVersion = oracleVersioning.GetCurrentVersion(migrationInfo.TargetSchema);
                    Output.Info("Database version is now: {0}".FormatWith(newVersion.IsNotNull() ? newVersion.Version.ToString(CultureInfo.InvariantCulture) : "<empty>"));
                }
                else
                {
                    Output.Info("Target schema '{0}' no longer exists.".FormatWith(migrationInfo.TargetSchema));
                }
            }
        }
Пример #2
0
        public void Migrate(DatabaseConnectionInfo databaseConnectionInfo, MigrationInfo migrationInfo)
        {
            using (var connection = _connectionFactory.Create(databaseConnectionInfo))
            {
                Output.Text("Connected to oracle database on connection string '{0}'.".FormatWith(databaseConnectionInfo.ConnectionString));
                Output.EmptyLine();

                Output.Text("Checking for the current database version.");
                var oracleMetadataProvider         = _databaseMetadataProviderFactory.Create(connection);
                var oracleVersioning               = _versionRespositoryFactory.Create(connection);
                var oracleAppliedScriptsRepository = _appliedScriptsRepositoryFactory.Create(connection);

                var currentVersion = oracleMetadataProvider.SchemaExists(migrationInfo.TargetSchema) ? oracleVersioning.GetCurrentVersion(migrationInfo.TargetSchema) : null;
                Output.Info("The current database version is: {0}".FormatWith(currentVersion.IsNotNull() ? currentVersion.Version.ToString(CultureInfo.InvariantCulture) : "<empty>"));
                Output.EmptyLine();

                Output.Info("Scanning input folder '{0}' for scripts...".FormatWith(migrationInfo.ScriptFolder));

                var initScripts = ScanForScripts(migrationInfo, ScriptFolders.Init, _scriptScannerFactory.Create(ScriptFolders.Init)).ToArray();
                var upScripts   = ScanForScripts(migrationInfo, ScriptFolders.Up, _scriptScannerFactory.Create(ScriptFolders.Up)).ToArray();
                var downScripts = ScanForScripts(migrationInfo, ScriptFolders.Down, _scriptScannerFactory.Create(ScriptFolders.Down)).ToArray();
                var termScripts = ScanForScripts(migrationInfo, ScriptFolders.Term, _scriptScannerFactory.Create(ScriptFolders.Term)).ToArray();
                Output.EmptyLine();

                if (migrationInfo.TargetVersion.HasValue)
                {
                    Output.Info("Target database version was provided, will target migrating the database to version {0}".FormatWith(migrationInfo.TargetVersion.Value));
                }

                VerifyDownScripts(upScripts, downScripts);

                var storedMigrationSteps = new List <IStep>();

                if (migrationInfo.UseStored)
                {
                    Output.Info("--usestored was specified, fetching set of applied scripts stored in the target database...".FormatWith());

                    storedMigrationSteps = oracleAppliedScriptsRepository.GetAppliedScripts(migrationInfo.TargetSchema).ToList();

                    Output.Text("    {0} stored applied scripts found.".FormatWith(storedMigrationSteps.Count));
                    Output.EmptyLine();
                }

                var scriptRunner = _scriptRunnerFactory.Create(databaseConnectionInfo);
                RunMigration(migrationInfo, currentVersion, initScripts, upScripts, downScripts, termScripts, storedMigrationSteps, scriptRunner, oracleMetadataProvider, oracleVersioning, oracleAppliedScriptsRepository);

                if (oracleMetadataProvider.SchemaExists(migrationInfo.TargetSchema))
                {
                    var newVersion = oracleVersioning.GetCurrentVersion(migrationInfo.TargetSchema);
                    Output.Info("Database version is now: {0}".FormatWith(newVersion.IsNotNull() ? newVersion.Version.ToString(CultureInfo.InvariantCulture) : "<empty>"));
                }
                else
                {
                    Output.Info("Target schema '{0}' no longer exists.".FormatWith(migrationInfo.TargetSchema));
                }
            }
        }
 public IScriptRunner Create(DatabaseConnectionInfo connectionInfo)
 {
     return(new SqlPlusScriptRunner(_processRunner, connectionInfo));
 }
 public IScriptRunner Create(DatabaseConnectionInfo connectionInfo)
 {
     return new SqlPlusScriptRunner(_processRunner, connectionInfo);
 }