Class: BackupRestore::DatabaseRestorer

Inherits:
Object
  • Object
show all
Defined in:
lib/backup_restore/database_restorer.rb

Constant Summary collapse

MAIN_SCHEMA =
"public"
BACKUP_SCHEMA =
"backup"
DROP_BACKUP_SCHEMA_AFTER_DAYS =
7

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, current_db) ⇒ DatabaseRestorer

Returns a new instance of DatabaseRestorer.



13
14
15
16
17
# File 'lib/backup_restore/database_restorer.rb', line 13

def initialize(logger, current_db)
  @logger = logger
  @db_was_changed = false
  @current_db = current_db
end

Class Method Details

.core_migration_filesObject



52
53
54
55
# File 'lib/backup_restore/database_restorer.rb', line 52

def self.core_migration_files
  Dir[Rails.root.join(Migration::SafeMigrate.post_migration_path, "**/*.rb")] +
    Dir[Rails.root.join("db/migrate/*.rb")]
end

.drop_backup_schemaObject



48
49
50
# File 'lib/backup_restore/database_restorer.rb', line 48

def self.drop_backup_schema
  ActiveRecord::Base.connection.drop_schema(BACKUP_SCHEMA) if backup_schema_dropable?
end

Instance Method Details

#clean_upObject



44
45
46
# File 'lib/backup_restore/database_restorer.rb', line 44

def clean_up
  drop_created_discourse_functions
end

#restore(db_dump_path) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/backup_restore/database_restorer.rb', line 19

def restore(db_dump_path)
  BackupRestore.move_tables_between_schemas(MAIN_SCHEMA, BACKUP_SCHEMA)

  @db_dump_path = db_dump_path
  @db_was_changed = true

  create_missing_discourse_functions
  restore_dump
  migrate_database
  reconnect_database

  BackupMetadata.update_last_restore_date
end

#rollbackObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/backup_restore/database_restorer.rb', line 33

def rollback
  log "Trying to rollback..."

  if @db_was_changed && BackupRestore.can_rollback?
    log "Rolling back..."
    BackupRestore.move_tables_between_schemas(BACKUP_SCHEMA, MAIN_SCHEMA)
  else
    log "There was no need to rollback"
  end
end