strong_migrations 1.4.0 → 1.4.2

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: 8cbc9cb28c4558aec3f349f8b394db58e0f1a52f5f554259c9ded55d13e70fa4
4
- data.tar.gz: b739a47825673f01c2e7c328ff0e25dbe3e326d09d2af3ae611154861df1a645
3
+ metadata.gz: 30cac1a5432c1dcf806208ab10dbab1fe08f757c650dd5f2af965a02638e17de
4
+ data.tar.gz: 6571d42391b5c4453cf4f529c555ddd2c91ec83625bc2b584fee3c64dfdefe96
5
5
  SHA512:
6
- metadata.gz: 4e86416fab6ffbbdb2838080acf49171f2327f0734d7960aa24476d91725156b94281b8c3661f7d672ea8f462542a74282231543b232155aad45c0ac11418a80
7
- data.tar.gz: e1f11e93dd82cf002d20a9eb9568e011889c13b2c2154f9aae10996a6be17a7e4d8d4c88b85e03c07fd7d3e6feeeeccb31882418aef57c6d75401f556e2821b2
6
+ metadata.gz: 69f972c7960262a4d21824ec4b0ff200b212705f3c246ecdbe5ad0ed14f3518d392b5577c7c3d7c064b024b9061352bd457580aa379d83a3c4676bb352326796
7
+ data.tar.gz: 56fe3d96e3e2e7a467bc4a2c477ca8dea7ce09418e4720119266f7442bb041bd9f5ec024b9d9f1a3213907ec3a975b530575f062d668f41b52aa66bdfbd5c8e3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 1.4.2 (2023-01-29)
2
+
3
+ - Added `alphabetize_schema` option
4
+
5
+ ## 1.4.1 (2023-01-05)
6
+
7
+ - Added support for multiple databases to `target_version`
8
+
1
9
  ## 1.4.0 (2022-10-31)
2
10
 
3
11
  - Added check for `add_exclusion_constraint`
data/README.md CHANGED
@@ -824,6 +824,12 @@ The major version works well for Postgres, while the full version is recommended
824
824
 
825
825
  For safety, this option only affects development and test environments. In other environments, the actual server version is always used.
826
826
 
827
+ If your app has multiple databases with different versions, with Rails 6.1+, you can use:
828
+
829
+ ```ruby
830
+ StrongMigrations.target_version = {primary: 13, catalog: 15}
831
+ ```
832
+
827
833
  ## Analyze Tables
828
834
 
829
835
  Analyze tables automatically (to update planner statistics) after an index is added. Create an initializer with:
@@ -834,23 +840,18 @@ StrongMigrations.auto_analyze = true
834
840
 
835
841
  ## Faster Migrations
836
842
 
837
- Only dump the schema when adding a new migration. If you use Git, add to the end of your `Rakefile`:
843
+ Only dump the schema when adding a new migration. If you use Git, add to `config/environments/development.rb`:
838
844
 
839
845
  ```rb
840
- task :faster_migrations do
841
- ActiveRecord::Base.dump_schema_after_migration = Rails.env.development? &&
842
- `git status db/migrate/ --porcelain`.present?
843
- end
844
-
845
- task "db:migrate": "faster_migrations"
846
+ config.active_record.dump_schema_after_migration = `git status db/migrate/ --porcelain`.present?
846
847
  ```
847
848
 
848
849
  ## Schema Sanity
849
850
 
850
- Columns can flip order in `db/schema.rb` when you have multiple developers. One way to prevent this is to [alphabetize them](https://www.pgrs.net/2008/03/12/alphabetize-schema-rb-columns/). Add to the end of your `Rakefile`:
851
+ Columns can flip order in `db/schema.rb` when you have multiple developers. One way to prevent this is to [alphabetize them](https://www.pgrs.net/2008/03/12/alphabetize-schema-rb-columns/). Add to `config/initializers/strong_migrations.rb`:
851
852
 
852
853
  ```ruby
853
- task "db:schema:dump": "strong_migrations:alphabetize_columns"
854
+ StrongMigrations.alphabetize_schema = true
854
855
  ```
855
856
 
856
857
  ## Permissions
@@ -50,7 +50,23 @@ module StrongMigrations
50
50
  target_version ||= StrongMigrations.target_version
51
51
  version =
52
52
  if target_version && StrongMigrations.developer_env?
53
- target_version.to_s
53
+ if target_version.is_a?(Hash)
54
+ # Active Record 6.0 supports multiple databases
55
+ # but connection.pool.spec.name always returns "primary"
56
+ # in migrations with rails db:migrate
57
+ if ActiveRecord::VERSION::STRING.to_f < 6.1
58
+ # error class is not shown in db:migrate output so ensure message is descriptive
59
+ raise StrongMigrations::Error, "StrongMigrations.target_version does not support multiple databases for Active Record < 6.1"
60
+ end
61
+
62
+ db_config_name = connection.pool.db_config.name
63
+ target_version.stringify_keys.fetch(db_config_name) do
64
+ # error class is not shown in db:migrate output so ensure message is descriptive
65
+ raise StrongMigrations::Error, "StrongMigrations.target_version is not configured for :#{db_config_name} database"
66
+ end.to_s
67
+ else
68
+ target_version.to_s
69
+ end
54
70
  else
55
71
  yield
56
72
  end
@@ -0,0 +1,21 @@
1
+ module StrongMigrations
2
+ module SchemaDumper
3
+ def initialize(connection, *args, **options)
4
+ return super unless StrongMigrations.alphabetize_schema
5
+
6
+ super(WrappedConnection.new(connection), *args, **options)
7
+ end
8
+ end
9
+
10
+ class WrappedConnection
11
+ delegate_missing_to :@connection
12
+
13
+ def initialize(connection)
14
+ @connection = connection
15
+ end
16
+
17
+ def columns(*args, **options)
18
+ @connection.columns(*args, **options).sort_by(&:name)
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module StrongMigrations
2
- VERSION = "1.4.0"
2
+ VERSION = "1.4.2"
3
3
  end
@@ -28,7 +28,8 @@ module StrongMigrations
28
28
  attr_accessor :auto_analyze, :start_after, :checks, :error_messages,
29
29
  :target_postgresql_version, :target_mysql_version, :target_mariadb_version,
30
30
  :enabled_checks, :lock_timeout, :statement_timeout, :check_down, :target_version,
31
- :safe_by_default, :target_sql_mode, :lock_timeout_retries, :lock_timeout_retry_delay
31
+ :safe_by_default, :target_sql_mode, :lock_timeout_retries, :lock_timeout_retry_delay,
32
+ :alphabetize_schema
32
33
  attr_writer :lock_timeout_limit
33
34
  end
34
35
  self.auto_analyze = false
@@ -38,6 +39,7 @@ module StrongMigrations
38
39
  self.checks = []
39
40
  self.safe_by_default = false
40
41
  self.check_down = false
42
+ self.alphabetize_schema = false
41
43
 
42
44
  # private
43
45
  def self.developer_env?
@@ -93,4 +95,7 @@ ActiveSupport.on_load(:active_record) do
93
95
  if defined?(ActiveRecord::Tasks::DatabaseTasks)
94
96
  ActiveRecord::Tasks::DatabaseTasks.singleton_class.prepend(StrongMigrations::DatabaseTasks)
95
97
  end
98
+
99
+ require "strong_migrations/schema_dumper"
100
+ ActiveRecord::SchemaDumper.prepend(StrongMigrations::SchemaDumper)
96
101
  end
@@ -4,11 +4,6 @@ namespace :strong_migrations do
4
4
  $stderr.puts "Dumping schema"
5
5
  ActiveRecord::Base.logger.level = Logger::INFO
6
6
 
7
- require "strong_migrations/alphabetize_columns"
8
- ActiveRecord::Base.connection.class.prepend StrongMigrations::AlphabetizeColumns
9
- if ActiveRecord::ConnectionAdapters.const_defined?('PostGISAdapter')
10
- ActiveRecord::ConnectionAdapters::PostGISAdapter.prepend StrongMigrations::AlphabetizeColumns
11
- end
12
- ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend StrongMigrations::AlphabetizeColumns
7
+ StrongMigrations.alphabetize_schema = true
13
8
  end
14
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strong_migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2022-10-31 00:00:00.000000000 Z
13
+ date: 2023-01-30 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -45,7 +45,6 @@ files:
45
45
  - lib/strong_migrations/adapters/mariadb_adapter.rb
46
46
  - lib/strong_migrations/adapters/mysql_adapter.rb
47
47
  - lib/strong_migrations/adapters/postgresql_adapter.rb
48
- - lib/strong_migrations/alphabetize_columns.rb
49
48
  - lib/strong_migrations/checker.rb
50
49
  - lib/strong_migrations/checks.rb
51
50
  - lib/strong_migrations/database_tasks.rb
@@ -54,6 +53,7 @@ files:
54
53
  - lib/strong_migrations/migrator.rb
55
54
  - lib/strong_migrations/railtie.rb
56
55
  - lib/strong_migrations/safe_methods.rb
56
+ - lib/strong_migrations/schema_dumper.rb
57
57
  - lib/strong_migrations/version.rb
58
58
  - lib/tasks/strong_migrations.rake
59
59
  homepage: https://github.com/ankane/strong_migrations
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  - !ruby/object:Gem::Version
76
76
  version: '0'
77
77
  requirements: []
78
- rubygems_version: 3.3.7
78
+ rubygems_version: 3.4.1
79
79
  signing_key:
80
80
  specification_version: 4
81
81
  summary: Catch unsafe migrations in development
@@ -1,11 +0,0 @@
1
- module StrongMigrations
2
- module AlphabetizeColumns
3
- def columns(*args)
4
- super.sort_by(&:name)
5
- end
6
-
7
- def extensions(*args)
8
- super.sort
9
- end
10
- end
11
- end