simple_data_migrations 0.1.0 → 0.2.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: 0a4275c5b1c6f081913056bd954e294c64db0affb8b3d673c0e8e54794dd35e5
4
- data.tar.gz: 37b99f86b67e80b8c176a1b401a8a7fbeaf6a266b55cc4661544c070006ca65e
3
+ metadata.gz: 921613636477da12bf615c10012a25b94d2f3929e22c6959688de8325d6bc209
4
+ data.tar.gz: 5e2c9e1a1cee5b69303c1cf01e18603a530fcf913b92ae1f248a40305dfffd9f
5
5
  SHA512:
6
- metadata.gz: 5731d1a108dcd09d60ca1d9749fe3a449cb08bf6711c5bb3b9662e539135dbf4db8f11e14f5f9705e6d3b0bb81940248699e8818981edf44eac4a55bd9d88b73
7
- data.tar.gz: da8566add2d9d89fc44cf52b62913ef318902a8dba6fec2373ac128b193d358dedc1d7e826d28726131ec822ac582ea272b2fa4de926c908f029f0e1e7795461
6
+ metadata.gz: 5a79a24b40aa650d434154ff9072e39e46192de666f70847a5cd984095a82c69f8cf8252bd743cdc1a7269c5ead89c5b0aab37b663a6884f46d4dc7e9dee82f5
7
+ data.tar.gz: dd95745c38e08e4ea18332cab21ac32a8148d97257a7b0c2bd63f5055407719e441a24561746079437e7370f9e08a7d4b5f4adcebb5d14280cb52bf4311a392f
data/CHANGELOG.md CHANGED
@@ -1,4 +1,6 @@
1
- ## [Unreleased]
1
+ ## [0.2.0] - 2022-01-11
2
+
3
+ - Add support for locked migrations to ensure script is run only once in the app.
2
4
 
3
5
  ## [0.1.0] - 2022-01-06
4
6
 
data/README.md CHANGED
@@ -21,6 +21,12 @@ Generate sample data migration file by `bin/rails generate simple_data_migration
21
21
  Status can be displayed by `bin/rails data:migrate:status`
22
22
  And executed by `bin/rails data:migrate`
23
23
 
24
+ ### Locked Migrations
25
+
26
+ To ensure that migration script is run only once during deploy to multiple machines, one can use `bin/rails data:migrate_with_lock` task.
27
+ It will use database advisory lock, in similar manner to how the Rails database migrations are run.
28
+ In case of multiple concurrent task invocations, the first run will succeed but the others will raise `SimpleDataMigrations::ConcurrentRun::Error`.
29
+
24
30
  ## Development
25
31
 
26
32
  TODO: add configuration
@@ -7,7 +7,7 @@ module SimpleDataMigrations
7
7
  class InstallGenerator < Rails::Generators::Base
8
8
  include ActiveRecord::Generators::Migration
9
9
 
10
- source_paths << File.expand_path("../templates", __FILE__)
10
+ source_paths << File.expand_path("templates", __dir__)
11
11
 
12
12
  def create_migration_file
13
13
  migration_template "install.rb.erb", "db/migrate/create_simple_data_migrations_table.rb"
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleDataMigrations
4
+ module ConcurrentRun
5
+ class Error < StandardError; end
6
+
7
+ MIGRATOR_SALT = 3053462845
8
+ private_constant :MIGRATOR_SALT
9
+
10
+ def self.with_advisory_lock
11
+ with_advisory_lock_connection do |connection|
12
+ lock_id = MIGRATOR_SALT * Zlib.crc32(connection.current_database)
13
+
14
+ got_lock = connection.get_advisory_lock(lock_id)
15
+ raise Error unless got_lock
16
+
17
+ yield
18
+ ensure
19
+ got_lock && connection.release_advisory_lock(lock_id)
20
+ end
21
+ end
22
+
23
+ # Caused by multi db specific connection flow. More on https://github.com/rails/rails/pull/38235
24
+ def self.with_advisory_lock_connection
25
+ pool = ActiveRecord::ConnectionAdapters::ConnectionHandler.new.establish_connection(
26
+ ActiveRecord::Base.connection_db_config
27
+ )
28
+
29
+ pool.with_connection { |connection| yield(connection) }
30
+ end
31
+ private_class_method :with_advisory_lock_connection
32
+ end
33
+ end
@@ -2,7 +2,6 @@
2
2
 
3
3
  module SimpleDataMigrations
4
4
  class Railtie < ::Rails::Railtie
5
-
6
5
  rake_tasks do
7
6
  load File.expand_path("../tasks/data.rake", __dir__)
8
7
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleDataMigrations
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -4,6 +4,7 @@ require "benchmark"
4
4
  require "thor/shell/basic"
5
5
 
6
6
  require_relative "simple_data_migrations/version"
7
+ require_relative "simple_data_migrations/concurrent_run"
7
8
  require_relative "simple_data_migrations/entry"
8
9
  require_relative "simple_data_migrations/utils"
9
10
  require_relative "simple_data_migrations/railtie" if defined?(Rails::Railtie)
@@ -44,6 +45,10 @@ module SimpleDataMigrations
44
45
  end
45
46
  end
46
47
 
48
+ def self.run_with_lock
49
+ ConcurrentRun.with_advisory_lock { run }
50
+ end
51
+
47
52
  def self.bootstrap
48
53
  attributes = Utils.script_files.map { |filename| {version: Utils.version(filename)} }
49
54
  Entry.insert_all!(attributes)
data/lib/tasks/data.rake CHANGED
@@ -8,6 +8,12 @@ namespace :data do
8
8
  SimpleDataMigrations.run
9
9
  end
10
10
 
11
+ task migrate_with_lock: :environment do
12
+ require "simple_data_migrations"
13
+
14
+ SimpleDataMigrations.run_with_lock
15
+ end
16
+
11
17
  namespace :migrate do
12
18
  desc "Display status of data migration scripts"
13
19
  task status: :environment do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_data_migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wojciech Wnętrzak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-06 00:00:00.000000000 Z
11
+ date: 2022-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -56,6 +56,7 @@ files:
56
56
  - lib/generators/simple_data_migrations/template_generator.rb
57
57
  - lib/generators/simple_data_migrations/templates/install.rb.erb
58
58
  - lib/simple_data_migrations.rb
59
+ - lib/simple_data_migrations/concurrent_run.rb
59
60
  - lib/simple_data_migrations/entry.rb
60
61
  - lib/simple_data_migrations/railtie.rb
61
62
  - lib/simple_data_migrations/utils.rb