Class: Webhookdb::Organization::DatabaseMigration

Inherits:
Object
  • Object
show all
Includes:
Dbutil
Defined in:
lib/webhookdb/organization/database_migration.rb

Defined Under Namespace

Classes: MigrationAlreadyFinished, MigrationInProgress

Constant Summary

Constants included from Dbutil

Dbutil::MOCK_CONN

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dbutil

borrow_conn, configured_connection_options, conn_opts, displaysafe_url, reduce_expr, take_conn

Class Method Details

.enqueue(admin_connection_url_raw:, readonly_connection_url_raw:, public_host:, started_by:, organization:) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/webhookdb/organization/database_migration.rb', line 33

def self.enqueue(admin_connection_url_raw:, readonly_connection_url_raw:, public_host:, started_by:, organization:)
  self.guard_ongoing!(organization)
  self.db.transaction do
    dbm = self.create(
      started_by:,
      organization:,
      organization_schema: organization.replication_schema,
      source_admin_connection_url: organization.admin_connection_url_raw,
      destination_admin_connection_url: admin_connection_url_raw,
    )
    organization.update(
      public_host:,
      admin_connection_url_raw:,
      readonly_connection_url_raw:,
    )
    return dbm
  end
end

.guard_ongoing!(org) ⇒ Object



26
27
28
29
30
31
# File 'lib/webhookdb/organization/database_migration.rb', line 26

def self.guard_ongoing!(org)
  dbm = self.where(organization: org).ongoing.first
  return if dbm.nil?
  raise MigrationInProgress, "Organization #{org.name} has an ongoing database host migration so " \
                             "cannot be modified. We'll let admins know when it's done. Try again soon."
end

Instance Method Details

#displaysafe_destination_urlObject



56
57
58
# File 'lib/webhookdb/organization/database_migration.rb', line 56

def displaysafe_destination_url
  return displaysafe_url(self.destination_admin_connection_url)
end

#displaysafe_source_urlObject



52
53
54
# File 'lib/webhookdb/organization/database_migration.rb', line 52

def displaysafe_source_url
  return displaysafe_url(self.source_admin_connection_url)
end

#finish(now: Time.now) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/webhookdb/organization/database_migration.rb', line 121

def finish(now: Time.now)
  self.update(
    finished_at: now,
    source_admin_connection_url: displaysafe_source_url,
    destination_admin_connection_url: displaysafe_destination_url,
  )
  return self
end

#finished?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/webhookdb/organization/database_migration.rb', line 66

def finished?
  return !!self.finished_at
end

#migrateObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/webhookdb/organization/database_migration.rb', line 70

def migrate
  raise MigrationAlreadyFinished if self.finished?
  self.update(started_at: Time.now) if self.started_at.nil?
  borrow_conn(self.source_admin_connection_url) do |srcdb|
    borrow_conn(self.destination_admin_connection_url) do |dstdb|
      self.organization.service_integrations.sort_by(&:id).each do |sint|
        next if sint.id <= self.last_migrated_service_integration_id
        self.migrate_service_integration(sint, srcdb, dstdb)
        self.update(last_migrated_service_integration_id: sint.id, last_migrated_timestamp: nil)
      end
    end
  end
  self.update(finished_at: Time.now)
end

#statusObject



60
61
62
63
64
# File 'lib/webhookdb/organization/database_migration.rb', line 60

def status
  return "finished" if self.finished_at.present?
  return "in_progress" if self.started_at.present?
  return "enqueued"
end