verbose_migrations 0.0.1 → 1.0.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: ad7f17b9ff82b8e574b88002acbf340b8195a4aa34a50729bd836c2dde8fc1d8
4
- data.tar.gz: 558746287b7911ae80562b1a2408ca15bfbe46fbe8533bd9d8abef259790c82e
3
+ metadata.gz: 46016287293e9f2266c8b54d6a517502c5574aef77538f1a1fd40328b91566eb
4
+ data.tar.gz: 3c73fac2184feaa70d1fc86697bddba6e7b86143943152ea55dadc9b1ed5f4db
5
5
  SHA512:
6
- metadata.gz: fcedca9580af8e1e09cc40baeb63957a6441711f3e443b06de46d5c126c0cc9e39361b7a08a2b7fe8fc65a63931f73cb510b225c3255df97f7d902f4d41f0deb
7
- data.tar.gz: d386a64316f74ddee8596a21775db48a027e8254c9eae606b8942f71c83f218ce3a7e2aea31d97a2fe3ad22f1bd19a3264e9ffc308033679b68b89f510483c5c
6
+ metadata.gz: 83c379ae1c1fc8cc7cb1163253ce09740b627b10700a75997d242d0a7e11874e17fbff95ac6075d1646754904cef83727dbded3f636623bb0dbc4b268338b6f5
7
+ data.tar.gz: 8c4e5eb736d08ad411de79d987e7f4333a90070b3e5c138b23aaa6422d195323ef5bb6c3bc089f618073b80a47cb522a9d8e117b5b7355cf0bfff3fa087d2ede
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
1
  # Changelog
2
2
 
3
3
  ## [Unreleased]
4
+
5
+ ## v1.0.1
6
+
7
+ Fix load order: [#1](https://github.com/keygen-sh/verbose_migrations/issues/1).
8
+
9
+ ## v1.0.0
10
+
11
+ Initial release.
data/README.md CHANGED
@@ -1,31 +1,118 @@
1
- # VerboseMigrations
1
+ # verbose_migrations
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ [![CI](https://github.com/keygen-sh/verbose_migrations/actions/workflows/test.yml/badge.svg)](https://github.com/keygen-sh/verbose_migrations/actions)
4
+ [![Gem Version](https://badge.fury.io/rb/verbose_migrations.svg)](https://badge.fury.io/rb/verbose_migrations)
4
5
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/verbose_migrations`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+ Enable verbose logging for Active Record migrations, regardless of configured
7
+ log level. Monitor query speed, query execution, and overall progress when
8
+ executing long running or otherwise risky migrations.
9
+
10
+ This gem was extracted from [Keygen](https://keygen.sh).
11
+
12
+ Sponsored by:
13
+
14
+ <a href="https://keygen.sh?ref=verbose_migrations">
15
+ <div>
16
+ <img src="https://keygen.sh/images/logo-pill.png" width="200" alt="Keygen">
17
+ </div>
18
+ </a>
19
+
20
+ _A fair source software licensing and distribution API._
6
21
 
7
22
  ## Installation
8
23
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
24
+ Add this line to your application's `Gemfile`:
10
25
 
11
- Install the gem and add to the application's Gemfile by executing:
26
+ ```ruby
27
+ gem 'verbose_migrations'
28
+ ```
12
29
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
30
+ And then execute:
14
31
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
32
+ ```bash
33
+ $ bundle
34
+ ```
16
35
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
36
+ Or install it yourself as:
37
+
38
+ ```bash
39
+ $ gem install verbose_migrations
40
+ ```
18
41
 
19
42
  ## Usage
20
43
 
21
- TODO: Write usage instructions here
44
+ Use the `verbose!` class method to enable debug logging. It accepts an optional log
45
+ `level:` and `logger:`.
46
+
47
+ ```ruby
48
+ class SeedTagsFromPosts < ActiveRecord::Migration[7.2]
49
+ disable_ddl_transaction!
50
+ verbose!
51
+
52
+ def up
53
+ update_count = nil
54
+ batch_count = 0
55
+
56
+ until update_count == 0
57
+ batch_count += 1
58
+ update_count = exec_update(<<~SQL.squish, batch_count:, batch_size: 10_000)
59
+ WITH batch AS (
60
+ SELECT id, unnest(tags) AS tag_name
61
+ FROM posts
62
+ WHERE tags IS NOT NULL
63
+ LIMIT :batch_size
64
+ ), inserted_tags AS (
65
+ INSERT INTO tags (name)
66
+ SELECT DISTINCT tag_name
67
+ FROM batch
68
+ ON CONFLICT (name) DO NOTHING
69
+ RETURNING id, name
70
+ )
71
+ INSERT INTO post_tags (post_id, tag_id)
72
+ SELECT batch.id, tags.id
73
+ FROM batch
74
+ JOIN tags ON tags.name = batch.tag_name
75
+ /* batch=:batch_count */
76
+ SQL
77
+ end
78
+ end
79
+ end
22
80
 
23
- ## Development
81
+ ```
24
82
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
83
+ Before, you have a black box:
26
84
 
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
85
+ ```
86
+ == 20240817010101 SeedTagsFromPosts: migrating ================================
87
+ == 20240817010101 SeedTagsFromPosts: migrated (42.0312s) =======================
88
+ ```
89
+
90
+ After, you see progress:
91
+
92
+ ```
93
+ == 20240817010101 SeedTagsFromPosts: migrating ================================
94
+ ==> DEBUG -- : (2.2ms) WITH batch AS ( SELECT id, unnest(tags) AS tag_name FROM posts WHERE tags IS NOT NULL LIMIT 10000 ), inserted_tags AS ( INSERT INTO tags (name) SELECT DISTINCT tag_name FROM batch ON CONFLICT (name) DO NOTHING RETURNING id, name ) INSERT INTO post_tags (post_id, tag_id) SELECT batch.id, tags.id FROM batch JOIN tags ON tags.name = batch.tag_name /* batch=1 */
95
+ ==> DEBUG -- : (1.1ms) WITH batch AS ( SELECT id, unnest(tags) AS tag_name FROM posts WHERE tags IS NOT NULL LIMIT 10000 ), inserted_tags AS ( INSERT INTO tags (name) SELECT DISTINCT tag_name FROM batch ON CONFLICT (name) DO NOTHING RETURNING id, name ) INSERT INTO post_tags (post_id, tag_id) SELECT batch.id, tags.id FROM batch JOIN tags ON tags.name = batch.tag_name /* batch=2 */
96
+ ==> DEBUG -- : (1.3ms) WITH batch AS ( SELECT id, unnest(tags) AS tag_name FROM posts WHERE tags IS NOT NULL LIMIT 10000 ), inserted_tags AS ( INSERT INTO tags (name) SELECT DISTINCT tag_name FROM batch ON CONFLICT (name) DO NOTHING RETURNING id, name ) INSERT INTO post_tags (post_id, tag_id) SELECT batch.id, tags.id FROM batch JOIN tags ON tags.name = batch.tag_name /* batch=3 */
97
+ ==> DEBUG -- : (1.7ms) ...
98
+ == 20240817010101 SeedTagsFromPosts: migrated (42.0312s) =======================
99
+ ```
100
+
101
+ ## Supported Rubies
102
+
103
+ **`verbose_migrations` supports Ruby 3.1 and above.** We encourage you to upgrade
104
+ if you're on an older version. Ruby 3 provides a lot of great features, like
105
+ better pattern matching and a new shorthand hash syntax.
106
+
107
+ ## Is it any good?
108
+
109
+ Yes.
28
110
 
29
111
  ## Contributing
30
112
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/verbose_migrations.
113
+ If you have an idea, or have discovered a bug, please open an issue or create a
114
+ pull request.
115
+
116
+ ## License
117
+
118
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VerboseMigrations
4
+ module MigrationExtension
5
+ cattr_accessor :verbose_logger, default: nil
6
+ cattr_accessor :verbosity, default: nil
7
+
8
+ def verbose? = verbosity.present? && verbose_logger.present?
9
+ def verbose!(logger: ActiveRecord::Base.logger, level: Logger::DEBUG)
10
+ self.verbose_logger = logger
11
+ self.verbosity = level
12
+ end
13
+
14
+ def migrate(...)
15
+ verbosity_was, verbose_logger.level = verbose_logger.level, verbosity if verbose?
16
+
17
+ super
18
+ ensure
19
+ verbose_logger.level = verbosity_was if verbose?
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module VerboseMigrations
4
+ ActiveSupport.on_load :active_record do
5
+ ActiveRecord::Migration.prepend(MigrationExtension)
6
+ end
7
+ end
8
+
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module VerboseMigrations
4
- VERSION = '0.0.1'
4
+ VERSION = '1.0.1'
5
5
  end
@@ -1,6 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_support'
4
+ require 'active_record'
5
+ require 'logger'
6
+
7
+ require_relative 'verbose_migrations/ext'
3
8
  require_relative 'verbose_migrations/version'
9
+ require_relative 'verbose_migrations/railtie'
4
10
 
5
- module VerboseMigrations
6
- end
11
+ module VerboseMigrations; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: verbose_migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zeke Gabrielse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-20 00:00:00.000000000 Z
11
+ date: 2024-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,9 +38,51 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: Override Active Record logger to DEBUG mode during Active Record migrations
42
- to easily follow along and spot blocking queries in a migration, even when the logger
43
- is set to e.g. WARN.
41
+ - !ruby/object:Gem::Dependency
42
+ name: transition_through
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: prism
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Enable verbose logging for Active Record migrations, regardless of configured
84
+ log level. Monitor query speed, query execution, and overall progress when executing
85
+ long running or otherwise risky migrations.
44
86
  email:
45
87
  - oss@keygen.sh
46
88
  executables: []
@@ -53,6 +95,8 @@ files:
53
95
  - README.md
54
96
  - SECURITY.md
55
97
  - lib/verbose_migrations.rb
98
+ - lib/verbose_migrations/ext.rb
99
+ - lib/verbose_migrations/railtie.rb
56
100
  - lib/verbose_migrations/version.rb
57
101
  homepage: https://github.com/keygen-sh/verbose_migrations
58
102
  licenses:
@@ -76,5 +120,6 @@ requirements: []
76
120
  rubygems_version: 3.4.13
77
121
  signing_key:
78
122
  specification_version: 4
79
- summary: Set Active Record logger to DEBUG mode during Active Record migrations.
123
+ summary: Enable verbose logging for Active Record migrations, regardless of configured
124
+ log level, to monitor query speed and execution.
80
125
  test_files: []