departure 6.2.0 → 6.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7379ce58f2dd5726c2844bb8ad94f24f427d5567bf4b96d6252faba6ac9371c3
4
- data.tar.gz: 607e8a0fb255e7630e2379f7c3f7907723bad228742905fbefc38a053a22f2f5
3
+ metadata.gz: d491f83699ed0a2a2932822f624162223ba8a3b74c018a56fd352a00378f3368
4
+ data.tar.gz: 6b6b6f192dab28cc61431e20bc7b0a35ecd76ab36a56edf5a0a59c42ae893e37
5
5
  SHA512:
6
- metadata.gz: a16e8fa492148d38370dc86bfc2cc3c46696ba7369c0f07bfe6981c01cdfa93e37286a7394507abbb4cb7e7ed2eed7adb6aa43ea92b193c15743d0469cc09e45
7
- data.tar.gz: c01c6cc976f7893c99e3257a3de7075afc004878dc2d04cff15bfd0d344f177669de2be808a67527c39c9ede79b5c8978e85ca797bfecc3258f302f474859995
6
+ metadata.gz: 106fa1f8b21f7ab468d6774147352bb2b631398c04df2eb60b664ae520ea44dd91f76fc084ec0215ab722369ad59b3047d9ff6009767329bf26da708f50e60af
7
+ data.tar.gz: 66396ee515d0a69e65eb8e39dce53d4eec89a5419886b3482f1b4276be1480cb08adf452585f09f7da48b4683e9f87a1d0b4311c6f9ab05a5801577052feb5cf
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
  .byebug_history
11
11
  tags
12
12
  departure_error.log
13
+ .ruby-version
data/.pryrc ADDED
@@ -0,0 +1,11 @@
1
+ if defined?(PryByebug)
2
+ Pry.commands.alias_command 'c', 'continue'
3
+ Pry.commands.alias_command 's', 'step'
4
+ Pry.commands.alias_command 'n', 'next'
5
+ Pry.commands.alias_command 'f', 'finish'
6
+ Pry.commands.alias_command 'w', 'pry-backtrace'
7
+
8
+ Pry::Commands.command(/^$/, 'repeat last command') do
9
+ pry_instance.run_command Pry.history.to_a.last
10
+ end
11
+ end
data/.travis.yml CHANGED
@@ -6,6 +6,7 @@ rvm:
6
6
  - 2.7.0
7
7
 
8
8
  env:
9
+ - RAILS_VERSION="~> 6.1.0"
9
10
  - RAILS_VERSION="~> 6.0.0"
10
11
  - RAILS_VERSION="~> 5.2.0"
11
12
 
data/CHANGELOG.md CHANGED
@@ -6,9 +6,13 @@ Please follow the format in [Keep a Changelog](http://keepachangelog.com/)
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [6.2.0] - 2020-06-23
10
+
9
11
  ### Added
10
12
 
13
+ - Support for ActiveRecord 6.0
11
14
  - Support for ActiveRecord 5.2
15
+ - Relax mysql2 requirement to allow mysql2 0.5.3
12
16
  - Support to batch multiple changes at once with #change_table
13
17
  - Support for connection to MySQL server over SSL
14
18
 
@@ -21,6 +25,10 @@ Please follow the format in [Keep a Changelog](http://keepachangelog.com/)
21
25
  ### Fixed
22
26
 
23
27
  - Fix support for removing foreign keys
28
+ - Fix PERCONA_ARGS syntax for critical-load option
29
+ - Make sure quotes in ALTER TABLE get correctly escaped
30
+ - Fixes for regex handling
31
+ - Fix LHM compatibility
24
32
 
25
33
  ## [6.1.0] - 2018-02-27
26
34
 
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Departure [![Build Status](https://travis-ci.org/departurerb/departure.svg?branch=master)](https://travis-ci.org/departurerb/departure) [![Code Climate](https://codeclimate.com/github/departurerb/departure/badges/gpa.svg)](https://codeclimate.com/github/departurerb/departure)
1
+ # Departure [![Build Status](https://travis-ci.org/departurerb/departure.svg?branch=master)](https://travis-ci.org/departurerb/departure) [![Code Climate](https://codeclimate.com/github/departurerb/departure/badges/gpa.svg)](https://codeclimate.com/github/departurerb/departure) ![GitHub commits since latest release (by date)](https://img.shields.io/github/commits-since/departurerb/departure/latest/master?style=plastic)
2
2
 
3
3
  Departure is an **ActiveRecord connection adapter** that allows running
4
4
  **MySQL online and non-blocking DDL** through `ActiveRecord::Migration` without needing
@@ -145,6 +145,39 @@ end
145
145
  It's strongly recommended to name it after this gems name, such as
146
146
  `config/initializers/departure.rb`
147
147
 
148
+ ### Disable on per-migration basis
149
+
150
+ Departure gem is enabled by default.
151
+ In order to disable it on a particular migration the method `disable_departure!` should be used.
152
+
153
+ ```ruby
154
+ class UseDepartureMigration < ActiveRecord::Migration[5.2]
155
+ disable_departure!
156
+
157
+ def up
158
+ # ...
159
+ end
160
+ # ...
161
+ end
162
+ ```
163
+
164
+ ### Enable on per-migration basis
165
+
166
+ If you wish to only have Departure enabled per-migration, set `config.enabled_by_default = false` in the configure block of your departure initializer.
167
+
168
+ Then, add a `uses_departure!` statement in migrations where Departure should be used:
169
+
170
+ ```ruby
171
+ class UseDepartureMigration < ActiveRecord::Migration[5.2]
172
+ uses_departure!
173
+
174
+ def up
175
+ # ...
176
+ end
177
+ # ...
178
+ end
179
+ ```
180
+
148
181
  ## How it works
149
182
 
150
183
  When booting your Rails app, Departure extends the
@@ -208,3 +241,6 @@ You can consult the changelog [here](CHANGELOG.md)
208
241
  The gem is available as open source under the terms of the [MIT
209
242
  License](http://opensource.org/licenses/MIT).
210
243
 
244
+ ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/departurerb/departure?style=for-the-badge)
245
+ ![GitHub issues](https://img.shields.io/github/issues/departurerb/departure?style=for-the-badge)
246
+
data/departure.gemspec CHANGED
@@ -7,7 +7,7 @@ require 'departure/version'
7
7
 
8
8
  # This environment variable is set on CI to facilitate testing with multiple
9
9
  # versions of Rails.
10
- RAILS_DEPENDENCY_VERSION = ENV.fetch('RAILS_VERSION', ['>= 5.2.0', '< 6.1'])
10
+ RAILS_DEPENDENCY_VERSION = ENV.fetch('RAILS_VERSION', ['>= 5.2.0', '<= 6.1'])
11
11
 
12
12
  Gem::Specification.new do |spec|
13
13
  spec.name = 'departure'
@@ -30,6 +30,6 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency 'rake', '~> 10.0'
31
31
  spec.add_development_dependency 'rspec', '~> 3.4', '>= 3.4.0'
32
32
  spec.add_development_dependency 'rspec-its', '~> 1.2'
33
- spec.add_development_dependency 'byebug', '~> 8.2', '>= 8.2.1'
33
+ spec.add_development_dependency 'pry-byebug'
34
34
  spec.add_development_dependency 'climate_control', '~> 0.0.3'
35
35
  end
@@ -9,7 +9,10 @@ module ActiveRecord
9
9
  # Establishes a connection to the database that's used by all Active
10
10
  # Record objects.
11
11
  def percona_connection(config)
12
- config[:username] = 'root' if config[:username].nil?
12
+ if config[:username].nil?
13
+ config = config.dup if config.frozen?
14
+ config[:username] = 'root'
15
+ end
13
16
  mysql2_connection = mysql2_connection(config)
14
17
 
15
18
  connection_details = Departure::ConnectionDetails.new(config)
@@ -74,6 +77,12 @@ module ActiveRecord
74
77
  @prepared_statements = false
75
78
  end
76
79
 
80
+ def write_query?(sql) # :nodoc:
81
+ !ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(
82
+ :desc, :describe, :set, :show, :use
83
+ ).match?(sql)
84
+ end
85
+
77
86
  def exec_delete(sql, name, binds)
78
87
  execute(to_sql(sql, binds), name)
79
88
  @connection.affected_rows
@@ -119,16 +128,28 @@ module ActiveRecord
119
128
  # @param column_name [String, Symbol]
120
129
  # @param options [Hash] optional
121
130
  def add_index(table_name, column_name, options = {})
122
- index_name, index_type, index_columns, index_options = add_index_options(table_name, column_name, options)
123
- execute "ALTER TABLE #{quote_table_name(table_name)} ADD #{index_type} INDEX #{quote_column_name(index_name)} (#{index_columns})#{index_options}" # rubocop:disable Metrics/LineLength
131
+ if ActiveRecord::VERSION::STRING >= '6.1'
132
+ index, algorithm, if_not_exists = add_index_options(table_name, column_name, options)
133
+ create_index = CreateIndexDefinition.new(index, algorithm, if_not_exists)
134
+ execute schema_creation.accept(create_index)
135
+ else
136
+ index_name, index_type, index_columns, index_options = add_index_options(table_name, column_name, options)
137
+ execute "ALTER TABLE #{quote_table_name(table_name)} ADD #{index_type} INDEX #{quote_column_name(index_name)} (#{index_columns})#{index_options}" # rubocop:disable Metrics/LineLength
138
+ end
124
139
  end
125
140
 
126
141
  # Remove the given index from the table.
127
142
  #
128
143
  # @param table_name [String, Symbol]
129
144
  # @param options [Hash] optional
130
- def remove_index(table_name, options = {})
131
- index_name = index_name_for_remove(table_name, options)
145
+ def remove_index(table_name, *args, **options)
146
+ column_name = args.first
147
+ if column_name
148
+ return if options[:if_exists] && !index_exists?(table_name, column_name, **options)
149
+ index_name = index_name_for_remove(table_name, column_name, **options)
150
+ else
151
+ index_name = index_name_for_remove(table_name, options)
152
+ end
132
153
  execute "ALTER TABLE #{quote_table_name(table_name)} DROP INDEX #{quote_column_name(index_name)}"
133
154
  end
134
155
 
data/lib/departure.rb CHANGED
@@ -14,12 +14,19 @@ require 'departure/configuration'
14
14
  require 'departure/errors'
15
15
  require 'departure/command'
16
16
  require 'departure/connection_base'
17
+ require 'departure/migration'
17
18
 
18
19
  require 'departure/railtie' if defined?(Rails)
19
20
 
20
21
  # We need the OS not to buffer the IO to see pt-osc's output while migrating
21
22
  $stdout.sync = true
22
23
 
24
+ ActiveSupport.on_load(:active_record) do
25
+ ActiveRecord::Migration.class_eval do
26
+ include Departure::Migration
27
+ end
28
+ end
29
+
23
30
  module Departure
24
31
  class << self
25
32
  attr_accessor :configuration
@@ -30,40 +37,7 @@ module Departure
30
37
  yield(configuration)
31
38
  end
32
39
 
33
- # Hooks Percona Migrator into Rails migrations by replacing the configured
34
- # database adapter
35
40
  def self.load
36
- ActiveRecord::Migration.class_eval do
37
- alias_method :original_migrate, :migrate
38
-
39
- # Replaces the current connection adapter with the PerconaAdapter and
40
- # patches LHM, then it continues with the regular migration process.
41
- #
42
- # @param direction [Symbol] :up or :down
43
- def migrate(direction)
44
- reconnect_with_percona
45
- include_foreigner if defined?(Foreigner)
46
-
47
- ::Lhm.migration = self
48
- original_migrate(direction)
49
- end
50
-
51
- # Includes the Foreigner's Mysql2Adapter implemention in
52
- # DepartureAdapter to support foreign keys
53
- def include_foreigner
54
- Foreigner::Adapter.safe_include(
55
- :DepartureAdapter,
56
- Foreigner::ConnectionAdapters::Mysql2Adapter
57
- )
58
- end
59
-
60
- # Make all connections in the connection pool to use PerconaAdapter
61
- # instead of the current adapter.
62
- def reconnect_with_percona
63
- connection_config = ActiveRecord::Base
64
- .connection_config.merge(adapter: 'percona')
65
- Departure::ConnectionBase.establish_connection(connection_config)
66
- end
67
- end
41
+ # No-op left for compatibility
68
42
  end
69
43
  end
@@ -1,11 +1,12 @@
1
1
  module Departure
2
2
  class Configuration
3
- attr_accessor :tmp_path, :global_percona_args
3
+ attr_accessor :tmp_path, :global_percona_args, :enabled_by_default
4
4
 
5
5
  def initialize
6
6
  @tmp_path = '.'.freeze
7
7
  @error_log_filename = 'departure_error.log'.freeze
8
8
  @global_percona_args = nil
9
+ @enabled_by_default = true
9
10
  end
10
11
 
11
12
  def error_log_path
@@ -0,0 +1,104 @@
1
+ module Departure
2
+ # Hooks Departure into Rails migrations by replacing the configured database
3
+ # adapter.
4
+ #
5
+ # It also patches ActiveRecord's #migrate method so that it patches LHM
6
+ # first. This will make migrations written with LHM to go through the
7
+ # regular Rails Migration DSL.
8
+ module Migration
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ # Holds the name of the adapter that was configured by the app.
13
+ mattr_accessor :original_adapter
14
+
15
+ # Declare on a per-migration class basis whether or not to use Departure.
16
+ # The default for this attribute is set based on
17
+ # Departure.configuration.enabled_by_default (default true).
18
+ class_attribute :uses_departure
19
+ self.uses_departure = true
20
+
21
+ alias_method :active_record_migrate, :migrate
22
+ remove_method :migrate
23
+ end
24
+
25
+ module ClassMethods
26
+ # Declare `uses_departure!` in the class body of your migration to enable
27
+ # Departure for that migration only when
28
+ # Departure.configuration.enabled_by_default is false.
29
+ def uses_departure!
30
+ self.uses_departure = true
31
+ end
32
+
33
+ # Declare `disable_departure!` in the class body of your migration to
34
+ # disable Departure for that migration only (when
35
+ # Departure.configuration.enabled_by_default is true, the default).
36
+ def disable_departure!
37
+ self.uses_departure = false
38
+ end
39
+ end
40
+
41
+ # Replaces the current connection adapter with the PerconaAdapter and
42
+ # patches LHM, then it continues with the regular migration process.
43
+ #
44
+ # @param direction [Symbol] :up or :down
45
+ def departure_migrate(direction)
46
+ reconnect_with_percona
47
+ include_foreigner if defined?(Foreigner)
48
+
49
+ ::Lhm.migration = self
50
+ active_record_migrate(direction)
51
+ end
52
+
53
+ # Migrate with or without Departure based on uses_departure class
54
+ # attribute.
55
+ def migrate(direction)
56
+ if uses_departure?
57
+ departure_migrate(direction)
58
+ else
59
+ reconnect_without_percona
60
+ active_record_migrate(direction)
61
+ end
62
+ end
63
+
64
+ # Includes the Foreigner's Mysql2Adapter implemention in
65
+ # DepartureAdapter to support foreign keys
66
+ def include_foreigner
67
+ Foreigner::Adapter.safe_include(
68
+ :DepartureAdapter,
69
+ Foreigner::ConnectionAdapters::Mysql2Adapter
70
+ )
71
+ end
72
+
73
+ # Make all connections in the connection pool to use PerconaAdapter
74
+ # instead of the current adapter.
75
+ def reconnect_with_percona
76
+ return if connection_config[:adapter] == 'percona'
77
+ Departure::ConnectionBase.establish_connection(connection_config.merge(adapter: 'percona'))
78
+ end
79
+
80
+ # Reconnect without percona adapter when Departure is disabled but was
81
+ # enabled in a previous migration.
82
+ def reconnect_without_percona
83
+ return unless connection_config[:adapter] == 'percona'
84
+ Departure::ConnectionBase.establish_connection(connection_config.merge(adapter: original_adapter))
85
+ end
86
+
87
+ private
88
+
89
+ # Capture the type of the adapter configured by the app if not already set.
90
+ def connection_config
91
+ configuration_hash.tap do |config|
92
+ self.class.original_adapter ||= config[:adapter]
93
+ end
94
+ end
95
+
96
+ private def configuration_hash
97
+ if ActiveRecord::VERSION::STRING >= '6.1'
98
+ ActiveRecord::Base.connection_db_config.configuration_hash
99
+ else
100
+ ActiveRecord::Base.connection_config
101
+ end
102
+ end
103
+ end
104
+ end
@@ -6,23 +6,16 @@ module Departure
6
6
  class Railtie < Rails::Railtie
7
7
  railtie_name :departure
8
8
 
9
- # It drops all previous database connections and reconnects using this
10
- # PerconaAdapter. By doing this, all later ActiveRecord methods called in
11
- # the migration will use this adapter instead of Mysql2Adapter.
12
- #
13
- # It also patches ActiveRecord's #migrate method so that it patches LHM
14
- # first. This will make migrations written with LHM to go through the
15
- # regular Rails Migration DSL.
16
- initializer 'departure.configure_rails_initialization' do
17
- ActiveSupport.on_load(:active_record) do
18
- Departure.load
19
- end
20
- end
21
-
22
9
  initializer 'departure.configure' do |app|
23
10
  Departure.configure do |config|
24
11
  config.tmp_path = app.paths['tmp'].first
25
12
  end
26
13
  end
14
+
15
+ config.after_initialize do
16
+ Departure.configure do |dc|
17
+ ActiveRecord::Migration.uses_departure = dc.enabled_by_default
18
+ end
19
+ end
27
20
  end
28
21
  end
@@ -1,3 +1,3 @@
1
1
  module Departure
2
- VERSION = '6.2.0'.freeze
2
+ VERSION = '6.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: departure
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.2.0
4
+ version: 6.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Zayats
@@ -15,7 +15,7 @@ authors:
15
15
  autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
- date: 2020-06-23 00:00:00.000000000 Z
18
+ date: 2021-03-17 00:00:00.000000000 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: railties
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 5.2.0
27
- - - "<"
27
+ - - "<="
28
28
  - !ruby/object:Gem::Version
29
29
  version: '6.1'
30
30
  type: :runtime
@@ -34,7 +34,7 @@ dependencies:
34
34
  - - ">="
35
35
  - !ruby/object:Gem::Version
36
36
  version: 5.2.0
37
- - - "<"
37
+ - - "<="
38
38
  - !ruby/object:Gem::Version
39
39
  version: '6.1'
40
40
  - !ruby/object:Gem::Dependency
@@ -44,7 +44,7 @@ dependencies:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
46
  version: 5.2.0
47
- - - "<"
47
+ - - "<="
48
48
  - !ruby/object:Gem::Version
49
49
  version: '6.1'
50
50
  type: :runtime
@@ -54,7 +54,7 @@ dependencies:
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
56
  version: 5.2.0
57
- - - "<"
57
+ - - "<="
58
58
  - !ruby/object:Gem::Version
59
59
  version: '6.1'
60
60
  - !ruby/object:Gem::Dependency
@@ -126,25 +126,19 @@ dependencies:
126
126
  - !ruby/object:Gem::Version
127
127
  version: '1.2'
128
128
  - !ruby/object:Gem::Dependency
129
- name: byebug
129
+ name: pry-byebug
130
130
  requirement: !ruby/object:Gem::Requirement
131
131
  requirements:
132
- - - "~>"
133
- - !ruby/object:Gem::Version
134
- version: '8.2'
135
132
  - - ">="
136
133
  - !ruby/object:Gem::Version
137
- version: 8.2.1
134
+ version: '0'
138
135
  type: :development
139
136
  prerelease: false
140
137
  version_requirements: !ruby/object:Gem::Requirement
141
138
  requirements:
142
- - - "~>"
143
- - !ruby/object:Gem::Version
144
- version: '8.2'
145
139
  - - ">="
146
140
  - !ruby/object:Gem::Version
147
- version: 8.2.1
141
+ version: '0'
148
142
  - !ruby/object:Gem::Dependency
149
143
  name: climate_control
150
144
  requirement: !ruby/object:Gem::Requirement
@@ -175,6 +169,7 @@ extra_rdoc_files: []
175
169
  files:
176
170
  - ".codeclimate.yml"
177
171
  - ".gitignore"
172
+ - ".pryrc"
178
173
  - ".rspec"
179
174
  - ".rubocop.yml"
180
175
  - ".travis.yml"
@@ -207,6 +202,7 @@ files:
207
202
  - lib/departure/log_sanitizers/password_sanitizer.rb
208
203
  - lib/departure/logger.rb
209
204
  - lib/departure/logger_factory.rb
205
+ - lib/departure/migration.rb
210
206
  - lib/departure/null_logger.rb
211
207
  - lib/departure/option.rb
212
208
  - lib/departure/railtie.rb
@@ -237,7 +233,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
237
233
  - !ruby/object:Gem::Version
238
234
  version: '0'
239
235
  requirements: []
240
- rubygems_version: 3.1.2
236
+ rubygems_version: 3.2.3
241
237
  signing_key:
242
238
  specification_version: 4
243
239
  summary: pt-online-schema-change runner for ActiveRecord migrations