online_migrations 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 95f3b31c9fe8edb868fade7dbdaed0ebf78d53da7a1ff52786a748663bc93bf5
4
- data.tar.gz: 94c2fed042d39993d85f6641db3b4a86c3a360b3c858f48aeb2da052bb2c52cc
3
+ metadata.gz: 5f0ae9026f83b836b520090730426b5cf93fe18e193f21ea28cd11b96ebb78a8
4
+ data.tar.gz: 939595b3b732d6351fbab4403f0271e47a2c42b770a9732e30cdc0f52479ce7b
5
5
  SHA512:
6
- metadata.gz: f844aa5e502a91739923039a8fb84d10ae9682404bd14bd4b92352400c6dae82963b21166178c72c47ea4ecbd0792b6f46da151d92ce205e217a5788c9a8b27a
7
- data.tar.gz: 2db40b6b3cf81f923251f16a023aedbb18f8f24d28abb9e6b638090030c013c24dd6a41942df3e4f4cd1264c84c997eaaeb6adcb1c2d8d8844bc4934406f9ebc
6
+ metadata.gz: d09f3b4a3592cada77cf27e152669d6c94e5bd15e3c2930aa18c6ca6de6cd3c0e588314e95cd7cb058e43fea22343c8529ed7c2a11ec60d247ce6479bce58374
7
+ data.tar.gz: 42185f369b8adb87c10c2e0cfb29891784d3b55bd0c41cc3b92804fb0d8f71d9b70f4349b26a5752ff1f6b9d8ebbdbc061667157d6b1023190907104f71e93b1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,34 @@
1
1
  ## master (unreleased)
2
2
 
3
+ ## 0.5.2 (2022-10-04)
4
+
5
+ - Fix sequence resetting in tests that use fixtures
6
+
7
+ - Fix `update_column_in_batches` for SQL subquery values
8
+
9
+ It generated inefficient queries before, e.g.:
10
+
11
+ ```ruby
12
+ update_column_in_batches(:users, :comments_count, Arel.sql(<<~SQL))
13
+ (select count(*) from comments where comments.user_id = users.id)
14
+ SQL
15
+ ```
16
+
17
+ Generated SQL queries before:
18
+ ```sql
19
+ update users
20
+ set comments_count = (..count subquery..)
21
+ where comments_count is null or comments_count != (..count subquery..)
22
+ ```
23
+
24
+ Generated SQL queries now:
25
+ ```sql
26
+ update users set comments_count = (..count subquery..)
27
+ ```
28
+
29
+ - Fix check for `add_column` with `default: nil` for PostgreSQL < 11
30
+ - Replacing a unique index when other unique index with the prefix of columns exists is safe
31
+
3
32
  ## 0.5.1 (2022-07-19)
4
33
 
5
34
  - Raise for possible index corruption in all environments (previously, the check was made only
@@ -10,21 +10,21 @@ module OnlineMigrations
10
10
 
11
11
  source_root File.expand_path("templates", __dir__)
12
12
 
13
- def create_migration_file
14
- migration_template("migration.rb", File.join(migrations_dir, "install_online_migrations.rb"))
15
- end
16
-
17
13
  def copy_initializer_file
18
14
  template("initializer.rb", "config/initializers/online_migrations.rb")
19
15
  end
20
16
 
17
+ def create_migration_file
18
+ migration_template("migration.rb", File.join(migrations_dir, "install_online_migrations.rb"))
19
+ end
20
+
21
21
  private
22
22
  def migration_parent
23
23
  Utils.migration_parent_string
24
24
  end
25
25
 
26
26
  def start_after
27
- self.class.next_migration_number(migrations_dir)
27
+ self.class.current_migration_number(migrations_dir)
28
28
  end
29
29
 
30
30
  def migrations_dir
@@ -59,11 +59,11 @@ OnlineMigrations.configure do |config|
59
59
 
60
60
  # Add custom checks. Use the `stop!` method to stop migrations.
61
61
  #
62
- # config.add_check do |method, args|
63
- # if method == :add_column && args[0].to_s == "users"
64
- # stop!("No more columns on the users table")
65
- # end
62
+ # config.add_check do |method, args|
63
+ # if method == :add_column && args[0].to_s == "users"
64
+ # stop!("No more columns on the users table")
66
65
  # end
66
+ # end
67
67
 
68
68
  # ==> Background migrations configuration
69
69
  # The number of rows to process in a single background migration run.
@@ -174,8 +174,8 @@ module OnlineMigrations
174
174
  def add_column(table_name, column_name, type, **options)
175
175
  default = options[:default]
176
176
  volatile_default = false
177
- if !new_or_small_table?(table_name) && !default.nil? &&
178
- (postgresql_version < Gem::Version.new("11") || (volatile_default = Utils.volatile_default?(connection, type, default)))
177
+ if !new_or_small_table?(table_name) && options.key?(:default) &&
178
+ (postgresql_version < Gem::Version.new("11") || (!default.nil? && (volatile_default = Utils.volatile_default?(connection, type, default))))
179
179
 
180
180
  raise_error :add_column_with_default,
181
181
  code: command_str(:add_column_with_default, table_name, column_name, type, options),
@@ -478,9 +478,17 @@ module OnlineMigrations
478
478
  command: command_str(:remove_index, table_name, **options.merge(algorithm: :concurrently))
479
479
  end
480
480
 
481
- if options[:column] || options[:name]
482
- options[:column] ||= connection.indexes(table_name).find { |index| index.name == options[:name].to_s }
483
- @removed_indexes << IndexDefinition.new(table: table_name, columns: options.delete(:column), **options)
481
+ index_def = connection.indexes(table_name).find do |index|
482
+ index.name == options[:name].to_s ||
483
+ Array(index.columns).map(&:to_s) == Array(options[:column]).map(&:to_s)
484
+ end
485
+
486
+ if index_def
487
+ existing_options = [:name, :columns, :unique, :where, :type, :using, :opclasses].map do |option|
488
+ [option, index_def.public_send(option)] if index_def.respond_to?(option)
489
+ end.compact.to_h
490
+
491
+ @removed_indexes << IndexDefinition.new(table: table_name, **existing_options)
484
492
  end
485
493
  end
486
494
 
@@ -30,10 +30,7 @@ module OnlineMigrations
30
30
  return false if where != other.where
31
31
  return false if other.respond_to?(:opclasses) && opclasses != other.opclasses
32
32
 
33
- case [unique, other.unique]
34
- when [true, true]
35
- columns == other.columns
36
- when [true, false]
33
+ if unique && !other.unique
37
34
  false
38
35
  else
39
36
  prefix?(self, other)
@@ -69,8 +69,9 @@ module OnlineMigrations
69
69
  def renamed_tables
70
70
  @renamed_tables ||= begin
71
71
  table_renames = OnlineMigrations.config.table_renames
72
+ views = connection.views
72
73
  table_renames.select do |old_name, _|
73
- connection.views.include?(old_name)
74
+ views.include?(old_name)
74
75
  end
75
76
  end
76
77
  end
@@ -78,8 +79,9 @@ module OnlineMigrations
78
79
  def renamed_columns
79
80
  @renamed_columns ||= begin
80
81
  column_renames = OnlineMigrations.config.column_renames
82
+ views = connection.views
81
83
  column_renames.select do |table_name, _|
82
- connection.views.include?(table_name)
84
+ views.include?(table_name)
83
85
  end
84
86
  end
85
87
  end
@@ -85,9 +85,13 @@ module OnlineMigrations
85
85
 
86
86
  conditions = columns_and_values.map do |(column_name, value)|
87
87
  value = Arel.sql(value.call.to_s) if value.is_a?(Proc)
88
- arel_column = model.arel_table[column_name]
89
- arel_column.not_eq(value).or(arel_column.eq(nil))
90
- end
88
+
89
+ # Ignore subqueries in conditions
90
+ unless value.is_a?(Arel::Nodes::SqlLiteral) && value.to_s =~ /select\s+/i
91
+ arel_column = model.arel_table[column_name]
92
+ arel_column.not_eq(value).or(arel_column.eq(nil))
93
+ end
94
+ end.compact
91
95
 
92
96
  batch_relation = model.where(conditions.inject(:and))
93
97
  batch_relation = yield batch_relation if block_given?
@@ -820,6 +824,29 @@ module OnlineMigrations
820
824
  end
821
825
  end
822
826
 
827
+ # @private
828
+ def pk_and_sequence_for(table)
829
+ views = self.views
830
+
831
+ table_renames = OnlineMigrations.config.table_renames
832
+ renamed_tables = table_renames.select do |old_name, _|
833
+ views.include?(old_name)
834
+ end
835
+
836
+ column_renames = OnlineMigrations.config.column_renames
837
+ renamed_columns = column_renames.select do |table_name, _|
838
+ views.include?(table_name)
839
+ end
840
+
841
+ if renamed_tables.key?(table)
842
+ super(renamed_tables[table])
843
+ elsif renamed_columns.key?(table)
844
+ super("#{table}_column_rename")
845
+ else
846
+ super
847
+ end
848
+ end
849
+
823
850
  # Disables statement timeout while executing &block
824
851
  #
825
852
  # Long-running migrations may take more than the timeout allowed by the database.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OnlineMigrations
4
- VERSION = "0.5.1"
4
+ VERSION = "0.5.2"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: online_migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - fatkodima
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-18 00:00:00.000000000 Z
11
+ date: 2022-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord