online_migrations 0.9.2 → 0.11.0
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 +4 -4
- data/CHANGELOG.md +41 -0
- data/README.md +155 -150
- data/docs/background_migrations.md +43 -10
- data/docs/configuring.md +23 -18
- data/lib/generators/online_migrations/install_generator.rb +3 -7
- data/lib/generators/online_migrations/templates/add_sharding_to_online_migrations.rb.tt +18 -0
- data/lib/generators/online_migrations/templates/initializer.rb.tt +12 -3
- data/lib/generators/online_migrations/templates/migration.rb.tt +8 -3
- data/lib/generators/online_migrations/upgrade_generator.rb +33 -0
- data/lib/online_migrations/background_migrations/application_record.rb +13 -0
- data/lib/online_migrations/background_migrations/backfill_column.rb +1 -1
- data/lib/online_migrations/background_migrations/copy_column.rb +11 -19
- data/lib/online_migrations/background_migrations/delete_orphaned_records.rb +2 -20
- data/lib/online_migrations/background_migrations/migration.rb +123 -34
- data/lib/online_migrations/background_migrations/migration_helpers.rb +0 -4
- data/lib/online_migrations/background_migrations/migration_job.rb +15 -12
- data/lib/online_migrations/background_migrations/migration_job_runner.rb +2 -2
- data/lib/online_migrations/background_migrations/migration_runner.rb +56 -11
- data/lib/online_migrations/background_migrations/reset_counters.rb +3 -9
- data/lib/online_migrations/background_migrations/scheduler.rb +5 -15
- data/lib/online_migrations/change_column_type_helpers.rb +71 -86
- data/lib/online_migrations/command_checker.rb +50 -46
- data/lib/online_migrations/config.rb +19 -15
- data/lib/online_migrations/copy_trigger.rb +15 -10
- data/lib/online_migrations/error_messages.rb +13 -25
- data/lib/online_migrations/foreign_keys_collector.rb +2 -2
- data/lib/online_migrations/indexes_collector.rb +3 -3
- data/lib/online_migrations/lock_retrier.rb +4 -9
- data/lib/online_migrations/schema_cache.rb +0 -6
- data/lib/online_migrations/schema_dumper.rb +21 -0
- data/lib/online_migrations/schema_statements.rb +80 -256
- data/lib/online_migrations/utils.rb +36 -55
- data/lib/online_migrations/verbose_sql_logs.rb +3 -2
- data/lib/online_migrations/version.rb +1 -1
- data/lib/online_migrations.rb +9 -6
- metadata +9 -7
- data/lib/online_migrations/background_migrations/advisory_lock.rb +0 -62
- data/lib/online_migrations/foreign_key_definition.rb +0 -17
|
@@ -7,6 +7,7 @@ module OnlineMigrations
|
|
|
7
7
|
def enable
|
|
8
8
|
@activerecord_logger_was = ActiveRecord::Base.logger
|
|
9
9
|
@verbose_query_logs_was = verbose_query_logs
|
|
10
|
+
return if @activerecord_logger_was.nil?
|
|
10
11
|
|
|
11
12
|
stdout_logger = ActiveSupport::Logger.new($stdout)
|
|
12
13
|
stdout_logger.formatter = @activerecord_logger_was.formatter
|
|
@@ -34,7 +35,7 @@ module OnlineMigrations
|
|
|
34
35
|
def verbose_query_logs
|
|
35
36
|
if Utils.ar_version >= 7.0
|
|
36
37
|
ActiveRecord.verbose_query_logs
|
|
37
|
-
|
|
38
|
+
else
|
|
38
39
|
ActiveRecord::Base.verbose_query_logs
|
|
39
40
|
end
|
|
40
41
|
end
|
|
@@ -42,7 +43,7 @@ module OnlineMigrations
|
|
|
42
43
|
def set_verbose_query_logs(value) # rubocop:disable Naming/AccessorMethodName
|
|
43
44
|
if Utils.ar_version >= 7.0
|
|
44
45
|
ActiveRecord.verbose_query_logs = value
|
|
45
|
-
|
|
46
|
+
else
|
|
46
47
|
ActiveRecord::Base.verbose_query_logs = value
|
|
47
48
|
end
|
|
48
49
|
end
|
data/lib/online_migrations.rb
CHANGED
|
@@ -16,8 +16,8 @@ module OnlineMigrations
|
|
|
16
16
|
autoload :VerboseSqlLogs
|
|
17
17
|
autoload :Migration
|
|
18
18
|
autoload :Migrator
|
|
19
|
+
autoload :SchemaDumper
|
|
19
20
|
autoload :DatabaseTasks
|
|
20
|
-
autoload :ForeignKeyDefinition
|
|
21
21
|
autoload :ForeignKeysCollector
|
|
22
22
|
autoload :IndexDefinition
|
|
23
23
|
autoload :IndexesCollector
|
|
@@ -54,12 +54,12 @@ module OnlineMigrations
|
|
|
54
54
|
autoload :DeleteOrphanedRecords
|
|
55
55
|
autoload :PerformActionOnRelation
|
|
56
56
|
autoload :ResetCounters
|
|
57
|
+
autoload :ApplicationRecord
|
|
57
58
|
autoload :MigrationJob
|
|
58
59
|
autoload :Migration
|
|
59
60
|
autoload :MigrationJobRunner
|
|
60
61
|
autoload :MigrationRunner
|
|
61
62
|
autoload :MigrationHelpers
|
|
62
|
-
autoload :AdvisoryLock
|
|
63
63
|
autoload :Scheduler
|
|
64
64
|
end
|
|
65
65
|
|
|
@@ -75,12 +75,19 @@ module OnlineMigrations
|
|
|
75
75
|
@config ||= Config.new
|
|
76
76
|
end
|
|
77
77
|
|
|
78
|
+
# Run background migrations
|
|
79
|
+
def run_background_migrations
|
|
80
|
+
BackgroundMigrations::Scheduler.run
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# @private
|
|
78
84
|
def load
|
|
79
85
|
require "active_record/connection_adapters/postgresql_adapter"
|
|
80
86
|
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(OnlineMigrations::SchemaStatements)
|
|
81
87
|
|
|
82
88
|
ActiveRecord::Migration.prepend(OnlineMigrations::Migration)
|
|
83
89
|
ActiveRecord::Migrator.prepend(OnlineMigrations::Migrator)
|
|
90
|
+
ActiveRecord::SchemaDumper.prepend(OnlineMigrations::SchemaDumper)
|
|
84
91
|
|
|
85
92
|
ActiveRecord::Tasks::DatabaseTasks.singleton_class.prepend(OnlineMigrations::DatabaseTasks)
|
|
86
93
|
ActiveRecord::Migration::CommandRecorder.include(OnlineMigrations::CommandRecorder)
|
|
@@ -90,10 +97,6 @@ module OnlineMigrations
|
|
|
90
97
|
else
|
|
91
98
|
ActiveRecord::ConnectionAdapters::SchemaCache.prepend(OnlineMigrations::SchemaCache)
|
|
92
99
|
end
|
|
93
|
-
|
|
94
|
-
if OnlineMigrations::Utils.ar_version <= 5.1
|
|
95
|
-
ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.prepend(OnlineMigrations::ForeignKeyDefinition)
|
|
96
|
-
end
|
|
97
100
|
end
|
|
98
101
|
end
|
|
99
102
|
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.
|
|
4
|
+
version: 0.11.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- fatkodima
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2024-01-08 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|
|
@@ -16,14 +16,14 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
19
|
+
version: '6.1'
|
|
20
20
|
type: :runtime
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
24
|
- - ">="
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
26
|
+
version: '6.1'
|
|
27
27
|
description:
|
|
28
28
|
email:
|
|
29
29
|
- fatkodima123@gmail.com
|
|
@@ -38,12 +38,14 @@ files:
|
|
|
38
38
|
- docs/configuring.md
|
|
39
39
|
- lib/generators/online_migrations/background_migration_generator.rb
|
|
40
40
|
- lib/generators/online_migrations/install_generator.rb
|
|
41
|
+
- lib/generators/online_migrations/templates/add_sharding_to_online_migrations.rb.tt
|
|
41
42
|
- lib/generators/online_migrations/templates/background_migration.rb.tt
|
|
42
43
|
- lib/generators/online_migrations/templates/initializer.rb.tt
|
|
43
44
|
- lib/generators/online_migrations/templates/migration.rb.tt
|
|
45
|
+
- lib/generators/online_migrations/upgrade_generator.rb
|
|
44
46
|
- lib/online_migrations.rb
|
|
45
47
|
- lib/online_migrations/background_migration.rb
|
|
46
|
-
- lib/online_migrations/background_migrations/
|
|
48
|
+
- lib/online_migrations/background_migrations/application_record.rb
|
|
47
49
|
- lib/online_migrations/background_migrations/backfill_column.rb
|
|
48
50
|
- lib/online_migrations/background_migrations/background_migration_class_validator.rb
|
|
49
51
|
- lib/online_migrations/background_migrations/config.rb
|
|
@@ -68,7 +70,6 @@ files:
|
|
|
68
70
|
- lib/online_migrations/copy_trigger.rb
|
|
69
71
|
- lib/online_migrations/database_tasks.rb
|
|
70
72
|
- lib/online_migrations/error_messages.rb
|
|
71
|
-
- lib/online_migrations/foreign_key_definition.rb
|
|
72
73
|
- lib/online_migrations/foreign_keys_collector.rb
|
|
73
74
|
- lib/online_migrations/index_definition.rb
|
|
74
75
|
- lib/online_migrations/indexes_collector.rb
|
|
@@ -76,6 +77,7 @@ files:
|
|
|
76
77
|
- lib/online_migrations/migration.rb
|
|
77
78
|
- lib/online_migrations/migrator.rb
|
|
78
79
|
- lib/online_migrations/schema_cache.rb
|
|
80
|
+
- lib/online_migrations/schema_dumper.rb
|
|
79
81
|
- lib/online_migrations/schema_statements.rb
|
|
80
82
|
- lib/online_migrations/utils.rb
|
|
81
83
|
- lib/online_migrations/verbose_sql_logs.rb
|
|
@@ -95,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
95
97
|
requirements:
|
|
96
98
|
- - ">="
|
|
97
99
|
- !ruby/object:Gem::Version
|
|
98
|
-
version: 2.
|
|
100
|
+
version: '2.7'
|
|
99
101
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
102
|
requirements:
|
|
101
103
|
- - ">="
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "zlib"
|
|
4
|
-
|
|
5
|
-
module OnlineMigrations
|
|
6
|
-
module BackgroundMigrations
|
|
7
|
-
# @private
|
|
8
|
-
class AdvisoryLock
|
|
9
|
-
attr_reader :name, :connection
|
|
10
|
-
|
|
11
|
-
def initialize(name:, connection: ActiveRecord::Base.connection)
|
|
12
|
-
@name = name
|
|
13
|
-
@connection = connection
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def try_lock
|
|
17
|
-
locked = connection.select_value("SELECT pg_try_advisory_lock(#{lock_key})")
|
|
18
|
-
Utils.to_bool(locked)
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
def unlock
|
|
22
|
-
connection.select_value("SELECT pg_advisory_unlock(#{lock_key})")
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
# Runs the given block if an advisory lock is able to be acquired.
|
|
26
|
-
def with_lock
|
|
27
|
-
if try_lock
|
|
28
|
-
begin
|
|
29
|
-
yield
|
|
30
|
-
ensure
|
|
31
|
-
unlock
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
def active?
|
|
37
|
-
objid = lock_key & 0xffffffff
|
|
38
|
-
classid = (lock_key & (0xffffffff << 32)) >> 32
|
|
39
|
-
|
|
40
|
-
active = connection.select_value(<<-SQL.strip_heredoc)
|
|
41
|
-
SELECT granted
|
|
42
|
-
FROM pg_locks
|
|
43
|
-
WHERE locktype = 'advisory'
|
|
44
|
-
AND pid = pg_backend_pid()
|
|
45
|
-
AND mode = 'ExclusiveLock'
|
|
46
|
-
AND classid = #{classid}
|
|
47
|
-
AND objid = #{objid}
|
|
48
|
-
SQL
|
|
49
|
-
|
|
50
|
-
Utils.to_bool(active)
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
private
|
|
54
|
-
SALT = 936723412
|
|
55
|
-
|
|
56
|
-
def lock_key
|
|
57
|
-
name_hash = Zlib.crc32(name)
|
|
58
|
-
SALT * name_hash
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
end
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module OnlineMigrations
|
|
4
|
-
# @private
|
|
5
|
-
module ForeignKeyDefinition
|
|
6
|
-
if Utils.ar_version <= 4.2
|
|
7
|
-
def defined_for?(to_table: nil, **options)
|
|
8
|
-
(to_table.nil? || to_table.to_s == self.to_table) &&
|
|
9
|
-
options.all? { |k, v| self.options[k].to_s == v.to_s }
|
|
10
|
-
end
|
|
11
|
-
elsif Utils.ar_version <= 5.1
|
|
12
|
-
def defined_for?(*args, **options)
|
|
13
|
-
super(*args, **options.except(:validate))
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
end
|