Class: Migration::SafeMigrate

Inherits:
Object
  • Object
show all
Defined in:
lib/migration/safe_migrate.rb

Defined Under Namespace

Modules: NiceErrors, SafeMigration

Class Method Summary collapse

Class Method Details

.disable!Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/migration/safe_migrate.rb', line 96

def self.disable!
  return if !PG::Connection.method_defined?(:exec_migrator_unpatched)
  return if ENV["RAILS_ENV"] == "production"

  PG::Connection.class_eval do
    alias_method :exec, :exec_migrator_unpatched
    alias_method :async_exec, :async_exec_migrator_unpatched

    remove_method :exec_migrator_unpatched
    remove_method :async_exec_migrator_unpatched
  end
end

.earliest_post_deploy_versionObject



153
154
155
156
157
158
159
160
# File 'lib/migration/safe_migrate.rb', line 153

def self.earliest_post_deploy_version
  @@earliest_post_deploy_version ||=
    begin
      first_file = Dir.glob("#{Discourse::DB_POST_MIGRATE_PATH}/*.rb").sort.first
      file_name = File.basename(first_file, ".rb")
      file_name.first(14).to_i
    end
end

.enable!Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/migration/safe_migrate.rb', line 76

def self.enable!
  return if PG::Connection.method_defined?(:exec_migrator_unpatched)
  return if ENV["RAILS_ENV"] == "production"

  PG::Connection.class_eval do
    alias_method :exec_migrator_unpatched, :exec
    alias_method :async_exec_migrator_unpatched, :async_exec

    def exec(*args, &blk)
      Migration::SafeMigrate.protect!(args[0])
      exec_migrator_unpatched(*args, &blk)
    end

    def async_exec(*args, &blk)
      Migration::SafeMigrate.protect!(args[0])
      async_exec_migrator_unpatched(*args, &blk)
    end
  end
end

.patch_active_record!Object



109
110
111
112
113
114
115
116
117
# File 'lib/migration/safe_migrate.rb', line 109

def self.patch_active_record!
  return if ENV["RAILS_ENV"] == "production"

  ActiveSupport.on_load(:active_record) { ActiveRecord::Migration.prepend(SafeMigration) }

  if defined?(ActiveRecord::Tasks::DatabaseTasks)
    ActiveRecord::Tasks::DatabaseTasks.singleton_class.prepend(NiceErrors)
  end
end

.post_migration_pathObject



72
73
74
# File 'lib/migration/safe_migrate.rb', line 72

def self.post_migration_path
  Discourse::DB_POST_MIGRATE_PATH
end

.protect!(sql) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/migration/safe_migrate.rb', line 119

def self.protect!(sql)
  if sql =~ /\A\s*(?:drop\s+table|alter\s+table.*rename\s+to)\s+/i
    $stdout.puts("", <<~TEXT)
      WARNING
      -------------------------------------------------------------------------------------
      An attempt was made to drop or rename a table in a migration
      SQL used was: '#{sql}'
      Please generate a post deployment migration using `rails g post_migration` to drop
      or rename the table.

      This protection is in place to protect us against dropping tables that are currently
      in use by live applications.
    TEXT
    raise Discourse::InvalidMigration, "Attempt was made to drop a table"
  elsif sql =~ /\A\s*alter\s+table.*(?:rename|drop(?!\s+not\s+null))\s+/i
    $stdout.puts("", <<~TEXT)
      WARNING
      -------------------------------------------------------------------------------------
      An attempt was made to drop or rename a column in a migration
      SQL used was: '#{sql}'

      Please generate a post deployment migration using `rails g post_migration` to drop
      or rename columns.

      Note, to minimize disruption use self.ignored_columns = ["column name"] on your
      ActiveRecord model, this can be removed after the post deployment migration has been promoted to a regular migration.

      This protection is in place to protect us against dropping columns that are currently
      in use by live applications.
    TEXT
    raise Discourse::InvalidMigration, "Attempt was made to rename or delete column"
  end
end