Ejemplo n.º 1
0
        public static void Initialize(DatabaseTypes databaseType, DatabaseInitializerOptions options)
        {
            _options = options;

            try
            {
                _database = Factory.Create(databaseType);
                using (var connection = _database.GetConnection(_options.MasterConnectionString))
                {
                    connection.Open();

                    _database.RestoreDatabase(connection, _options.DatabaseName, _options.BackupPath);

                    if (!string.IsNullOrEmpty(_options.AfterRestoreScriptFile))
                    {
                        _database.RunScript(_options.MasterConnectionString, _options.AfterRestoreScriptFile);
                    }

                    connection.Close();
                }

                using (var connection = _database.GetConnection(_options.DatabaseConnectionString))
                {
                    connection.Open();

                    var files = Directory.GetFiles(_options.ToRunSqlsPath, _options.SqlFilesFilter);
                    files = files.OrderBy(c => Path.GetFileName(c)).ToArray();

                    foreach (var file in files)
                    {
                        try
                        {
                            _database.RunScript(_options.DatabaseConnectionString, file);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception($"Failed to run the file {file}", ex);
                        }
                    }

                    string newPath = Path.Combine(Path.GetDirectoryName(_options.BackupPath), Path.GetFileName(_options.BackupPath) + "_new" + Path.GetExtension(_options.BackupPath));
                    if (File.Exists(newPath))
                    {
                        File.Delete(newPath);
                    }

                    _database.BackupDatabase(connection, _options.DatabaseName, newPath);

                    NewBackup = newPath;

                    connection.Close();
                }

                Initialized = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 2
0
        public static void Initialize(DatabaseTypes databaseType)
        {
            DatabaseInitializerOptions options = new DatabaseInitializerOptions();

            options.MasterConnectionString   = ConfigurationManager.ConnectionStrings[MasterConnectionStringKey]?.ConnectionString;
            options.DatabaseConnectionString = ConfigurationManager.ConnectionStrings[TargetConnectionStringKey]?.ConnectionString;
            options.DatabaseName             = ConfigurationManager.AppSettings[DatabaseNameSettingKey];
            options.BackupPath             = ConfigurationManager.AppSettings[BackupPathSettingKey];
            options.ToRunSqlsPath          = ConfigurationManager.AppSettings[ScriptFilesPathSettingKey];
            options.SqlFilesFilter         = ConfigurationManager.AppSettings[ScriptFilterSettingKey];
            options.AfterRestoreScriptFile = ConfigurationManager.AppSettings[AfterDeploymentScriptFilePathSettingKey];

            Initialize(databaseType, options);
        }