Class: Migration::SafeMigrate
- Inherits:
-
Object
- Object
- Migration::SafeMigrate
show all
- Defined in:
- lib/migration/safe_migrate.rb
Defined Under Namespace
Modules: NiceErrors, SafeMigration
Class Method Summary
collapse
Class Method Details
.disable! ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/migration/safe_migrate.rb', line 95
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_version ⇒ Object
152
153
154
155
156
157
158
159
|
# File 'lib/migration/safe_migrate.rb', line 152
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/migration/safe_migrate.rb', line 75
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
108
109
110
111
112
113
114
115
116
|
# File 'lib/migration/safe_migrate.rb', line 108
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_path ⇒ Object
71
72
73
|
# File 'lib/migration/safe_migrate.rb', line 71
def self.post_migration_path
Discourse::DB_POST_MIGRATE_PATH
end
|
.protect!(sql) ⇒ Object
118
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
|
# File 'lib/migration/safe_migrate.rb', line 118
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 6 months or so later.
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
|