online_migrations 0.19.0 → 0.19.1

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: 57661cb5e096da3eaed31d4dbe5cda862f9f75271b59c28045412642acaf786b
4
- data.tar.gz: 50ed46eaa89a4ee95ecd8b249b0fce616047006c91339dbc1069e572a96f0454
3
+ metadata.gz: 259343358d51032a296c3745f92aa0e7ab5fc8f2d5dc6cd124873876e523d525
4
+ data.tar.gz: d07240d06faac048145ae8f54da1a947d197f9ff40bb2adf276625eb76bcafb5
5
5
  SHA512:
6
- metadata.gz: be91a43493896dbf0787cf9045c0ac187920a55946b98b0c653b8a50604031aee1bba3b59591fd4c22f7996af05254c71001e098289ded180eec9cdd8ae47d43
7
- data.tar.gz: 840c35a008b4b949d1d686222d0722f98aa579fb31ccb14ffe3f6bd80147100bbcb323a9bdba657f230efbd3b2a97f799385e21f43218d8dbf19ab9a484d1147
6
+ metadata.gz: c67adbf061095fdcbb5fcaa5305f671d81cf434dc182bfe9e43d66dae41f87d24cf5a79bb4aca160a955c74dff20de5e217b20731fe022df1dece6246e0239c0
7
+ data.tar.gz: 705397632e3afe3628776e854f74ff0ee70ada9d73d0d9e84c113289623d6240fc79b7823ca0dc05d3dab8dfd90c421c667b60ee394dbfb03c49afabd31ddc95
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## master (unreleased)
2
2
 
3
+ ## 0.19.1 (2024-05-24)
4
+
5
+ - Fix `add_index_in_background` to be idempotent
6
+
3
7
  ## 0.19.0 (2024-05-21)
4
8
 
5
9
  - Add ability to cancel background migrations
@@ -6,14 +6,24 @@ module OnlineMigrations
6
6
  def add_index_in_background(table_name, column_name, **options)
7
7
  migration_options = options.extract!(:max_attempts, :statement_timeout, :connection_class_name)
8
8
 
9
+ options[:algorithm] = :concurrently
10
+ index, algorithm, if_not_exists = add_index_options(table_name, column_name, **options)
11
+
12
+ # Need to check this first, because `index_exists?` does not check for `:where`s.
13
+ if index_name_exists?(table_name, index.name)
14
+ Utils.raise_or_say(<<~MSG)
15
+ Index creation was not enqueued because the index with name '#{index.name}' already exists.
16
+ This can be due to an aborted migration or you need to explicitly provide another name
17
+ via `:name` option.
18
+ MSG
19
+ return
20
+ end
21
+
9
22
  if index_exists?(table_name, column_name, **options)
10
23
  Utils.raise_or_say("Index creation was not enqueued because the index already exists.")
11
24
  return
12
25
  end
13
26
 
14
- options[:algorithm] = :concurrently
15
- index, algorithm, if_not_exists = add_index_options(table_name, column_name, **options)
16
-
17
27
  create_index = ActiveRecord::ConnectionAdapters::CreateIndexDefinition.new(index, algorithm, if_not_exists)
18
28
  schema_creation = ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaCreation.new(self)
19
29
  definition = schema_creation.accept(create_index)
@@ -77,22 +87,21 @@ module OnlineMigrations
77
87
  # @private
78
88
  def create_background_schema_migration(migration_name, table_name, **options)
79
89
  options.assert_valid_keys(:definition, :max_attempts, :statement_timeout, :connection_class_name)
80
- migration = Migration.new(migration_name: migration_name, table_name: table_name, **options)
81
-
82
- shards = Utils.shard_names(migration.connection_class)
83
- if shards.size > 1
84
- migration.children = shards.map do |shard|
85
- child = migration.dup
86
- child.shard = shard
87
- child
88
- end
89
90
 
90
- migration.composite = true
91
- end
91
+ Migration.find_or_create_by!(migration_name: migration_name, shard: nil) do |migration|
92
+ migration.assign_attributes(**options, table_name: table_name)
92
93
 
93
- # This will save all the records using a transaction.
94
- migration.save!
95
- migration
94
+ shards = Utils.shard_names(migration.connection_class)
95
+ if shards.size > 1
96
+ migration.children = shards.map do |shard|
97
+ child = migration.dup
98
+ child.shard = shard
99
+ child
100
+ end
101
+
102
+ migration.composite = true
103
+ end
104
+ end
96
105
  end
97
106
  end
98
107
  end
@@ -11,7 +11,7 @@ module OnlineMigrations
11
11
  end
12
12
 
13
13
  def run
14
- return if migration.cancelled?
14
+ return if migration.cancelled? || migration.succeeded?
15
15
 
16
16
  mark_as_running if migration.enqueued? || migration.failed?
17
17
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OnlineMigrations
4
- VERSION = "0.19.0"
4
+ VERSION = "0.19.1"
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.19.0
4
+ version: 0.19.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - fatkodima
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-21 00:00:00.000000000 Z
11
+ date: 2024-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord