lazy_migrate 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bb0ab56b35f054e0c36cfa34b6833f9ff08e06010369b5014ae0ae16e0f25b77
4
+ data.tar.gz: f9a007bbf94a7f522d33cd8b62838f6aa23fb32af6e93ccfba7ea0f7919cac80
5
+ SHA512:
6
+ metadata.gz: adf695d57368567d9ce1641bb49d806dff313f764eb6085c153a4ea616b5f8bc253e96899c1ff6589418bc0b4d6011bafa05814d3796f96ed4ebb63ad3f2a844
7
+ data.tar.gz: 167c264d4a7324af4e7481ab48fe364d0095f015d702a2065f90a02899235bd20c9f0df876dcb7b3320de6af0eee3117d2adaa7c10d262cdcdc2d89a39c7de5e
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 2.0.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in lazy_migrate.gemspec
4
+ gemspec
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lazy_migrate (0.1.0)
5
+ tty-prompt
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.4.4)
11
+ pastel (0.8.0)
12
+ tty-color (~> 0.5)
13
+ rake (10.5.0)
14
+ rspec (3.9.0)
15
+ rspec-core (~> 3.9.0)
16
+ rspec-expectations (~> 3.9.0)
17
+ rspec-mocks (~> 3.9.0)
18
+ rspec-core (3.9.2)
19
+ rspec-support (~> 3.9.3)
20
+ rspec-expectations (3.9.2)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-mocks (3.9.1)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.9.0)
26
+ rspec-support (3.9.3)
27
+ tty-color (0.5.1)
28
+ tty-cursor (0.7.1)
29
+ tty-prompt (0.22.0)
30
+ pastel (~> 0.8)
31
+ tty-reader (~> 0.8)
32
+ tty-reader (0.8.0)
33
+ tty-cursor (~> 0.7)
34
+ tty-screen (~> 0.8)
35
+ wisper (~> 2.0)
36
+ tty-screen (0.8.1)
37
+ wisper (2.0.1)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ bundler (~> 2.0)
44
+ lazy_migrate!
45
+ rake (~> 10.0)
46
+ rspec (~> 3.0)
47
+
48
+ BUNDLED WITH
49
+ 2.0.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Jesse Duffield
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # LazyMigrate
2
+
3
+ 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/lazy_migrate`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'lazy_migrate'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install lazy_migrate
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lazy_migrate.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require './lib/lazy_migrate'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
8
+
9
+ task :run do
10
+ LazyMigrate.run
11
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lazy_migrate"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'lazy_migrate'
4
+ LazyMigrate.run
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "lazy_migrate/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "lazy_migrate"
9
+ spec.version = LazyMigrate::VERSION
10
+ spec.authors = ["Jesse Duffield"]
11
+ spec.email = ["jessedduffield@gmail.com"]
12
+
13
+ spec.summary = 'A little terminal UI for managing schema migrations'
14
+ spec.description = 'lazy_migrate lets you easily see which migrations have and have not been run, and makes it easy to up/down/migrate/rollback your migrations through a terminal UI. You can even bump migration versions in the event that you\'ve just pulled master and somebody else merged their migration before yours.'
15
+ spec.homepage = "https://github.com/jesseduffield/lazy_migrate"
16
+ spec.license = "MIT"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
20
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 2.0"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+
35
+ spec.add_runtime_dependency 'tty-prompt', '~> 0.22.0'
36
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lazy_migrate/version"
4
+ require "lazy_migrate/migrator"
5
+
6
+ module LazyMigrate
7
+ def self.run
8
+ Migrator.run
9
+ end
10
+ end
@@ -0,0 +1,186 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tty-prompt'
4
+ require 'active_record'
5
+ require 'rails'
6
+
7
+ module LazyMigrate
8
+ class Migrator
9
+ class << self
10
+ MIGRATE = 'migrate'
11
+ ROLLBACK = 'rollback'
12
+ UP = 'up'
13
+ DOWN = 'down'
14
+ REDO = 'redo'
15
+ BRING_TO_TOP = 'bring to top'
16
+
17
+ def run
18
+ loop do
19
+ catch(:done) do
20
+ on_done = -> { throw :done }
21
+
22
+ load_migration_paths
23
+ migrations = find_migrations
24
+
25
+ prompt = TTY::Prompt.new(active_color: :bright_green)
26
+ prompt.ok("\nDatabase: #{ActiveRecord::Base.connection_config[:database]}\n")
27
+
28
+ select_migration_prompt(prompt: prompt, migrations: migrations, on_done: on_done)
29
+ end
30
+ end
31
+ rescue TTY::Reader::InputInterrupt
32
+ puts
33
+ end
34
+
35
+ private
36
+
37
+ def select_migration_prompt(prompt:, migrations:, on_done:)
38
+ prompt.select('Pick a migration') do |menu|
39
+ migrations.each { |migration|
40
+ name = render_migration_option(migration)
41
+
42
+ menu.choice(
43
+ name,
44
+ -> {
45
+ select_action_prompt(
46
+ on_done: on_done,
47
+ prompt: prompt,
48
+ context: context,
49
+ migration: migration,
50
+ )
51
+ }
52
+ )
53
+ }
54
+ end
55
+ end
56
+
57
+ def select_action_prompt(on_done:, context:, prompt:, migration:)
58
+ if !migration[:has_file]
59
+ prompt.error("\nMigration file not found for migration #{migration[:version]}")
60
+ prompt_any_key(prompt)
61
+ on_done.()
62
+ end
63
+
64
+ option_map = obtain_option_map(context: context)
65
+
66
+ prompt.select("\nWhat would you like to do for #{migration[:version]} #{name}?") do |inner_menu|
67
+ options_for_migration(status: migration[:status]).each do |option|
68
+ inner_menu.choice(option, -> {
69
+ with_unsafe_error_capture do
70
+ option_map[option].(migration)
71
+ end
72
+ prompt_any_key(prompt)
73
+ on_done.()
74
+ })
75
+ end
76
+
77
+ inner_menu.choice('cancel', on_done)
78
+ end
79
+ end
80
+
81
+ def options_for_migration(status:)
82
+ common_options = [MIGRATE, ROLLBACK, BRING_TO_TOP]
83
+ specific_options = if status == 'up'
84
+ [DOWN, REDO]
85
+ else
86
+ [UP]
87
+ end
88
+ specific_options + common_options
89
+ end
90
+
91
+ def obtain_option_map(context:)
92
+ {
93
+ UP => ->(migration) { context.run(:up, migration[:version]) },
94
+ DOWN => ->(migration) { context.run(:down, migration[:version]) },
95
+ REDO => ->(migration) {
96
+ context.run(:down, migration[:version])
97
+ context.run(:up, migration[:version])
98
+ },
99
+ MIGRATE => ->(migration) { context.up(migration[:version]) },
100
+ ROLLBACK => ->(migration) {
101
+ # for some reason in https://github.com/rails/rails/blob/5-2-stable/activerecord/lib/active_record/migration.rb#L1221
102
+ # we stop before the selected version. I have no idea why.
103
+ # I could override the logic here but it wouldn't
104
+ # work when trying to rollback the final migration.
105
+ context.down(migration[:version])
106
+ },
107
+ BRING_TO_TOP => ->(migration) { bring_to_top(migration) },
108
+ }
109
+ end
110
+
111
+ def load_migration_paths
112
+ ActiveRecord::Migrator.migrations_paths.each do |path|
113
+ Dir[Rails.application.root.join("#{path}/**/*.rb")].each { |file| load file }
114
+ end
115
+ end
116
+
117
+ def find_migrations
118
+ context.migrations_status
119
+ .reverse
120
+ .map { |status, version, name|
121
+ # This depends on how rails reports a file is missing.
122
+ # This is no doubt subject to change so be wary.
123
+ has_file = name != '********** NO FILE **********'
124
+
125
+ { status: status, version: version.to_i, name: name, has_file: has_file }
126
+ }
127
+ end
128
+
129
+ def render_migration_option(migration)
130
+ "#{migration[:status].ljust(6)}#{migration[:version].to_s.ljust(16)}#{migration[:name].ljust(50)}"
131
+ end
132
+
133
+ # bring_to_top updates the version of a migration to bring it to the top of the
134
+ # migration list. If the migration had already been up'd it will mark the
135
+ # new migration file as upped as well. The former version number will be
136
+ # removed from the schema_migrations table.
137
+ def bring_to_top(migration)
138
+ filename = context.migrations.find { |m| m.version == migration[:version] }&.filename
139
+
140
+ if filename.nil?
141
+ raise("No file found for migration #{migration[:version]}")
142
+ end
143
+
144
+ last = last_version
145
+ new_version = ActiveRecord::Migration.next_migration_number(last ? last + 1 : 0)
146
+
147
+ # replace the version
148
+ basename = File.basename(filename)
149
+ dir = File.dirname(filename)
150
+ new_basename = "#{new_version}_#{basename.split('_')[1..].join('_')}"
151
+ new_filename = File.join(dir, new_basename)
152
+
153
+ File.rename(filename, new_filename)
154
+
155
+ if migration[:status] == 'up'
156
+ ActiveRecord::SchemaMigration.create(version: new_version)
157
+ end
158
+
159
+ ActiveRecord::SchemaMigration.find_by(version: migration[:version])&.destroy!
160
+ end
161
+
162
+ def last_version
163
+ context.migrations.last&.version
164
+ end
165
+
166
+ def prompt_any_key(prompt)
167
+ prompt.keypress("\nPress any key to continue")
168
+ end
169
+
170
+ def with_unsafe_error_capture
171
+ yield
172
+ rescue Exception => e # rubocop:disable Lint/RescueException
173
+ # I am aware you should not rescue 'Exception' exceptions but I think this is is an 'exceptional' use case
174
+ puts "\n#{e.class}: #{e.message}\n#{e.backtrace.take(5).join("\n")}"
175
+ end
176
+
177
+ def context
178
+ ActiveRecord::MigrationContext.new(Rails.root.join('db', 'migrate'))
179
+ end
180
+ end
181
+ end
182
+
183
+ def self.run
184
+ Migrator.show
185
+ end
186
+ end
@@ -0,0 +1,3 @@
1
+ module LazyMigrate
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazy_migrate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jesse Duffield
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: tty-prompt
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.22.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.22.0
69
+ description: lazy_migrate lets you easily see which migrations have and have not been
70
+ run, and makes it easy to up/down/migrate/rollback your migrations through a terminal
71
+ UI. You can even bump migration versions in the event that you've just pulled master
72
+ and somebody else merged their migration before yours.
73
+ email:
74
+ - jessedduffield@gmail.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".gitignore"
80
+ - ".rspec"
81
+ - ".travis.yml"
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/exe/lazy_migrate
89
+ - bin/setup
90
+ - lazy_migrate.gemspec
91
+ - lib/lazy_migrate.rb
92
+ - lib/lazy_migrate/migrator.rb
93
+ - lib/lazy_migrate/version.rb
94
+ homepage: https://github.com/jesseduffield/lazy_migrate
95
+ licenses:
96
+ - MIT
97
+ metadata:
98
+ homepage_uri: https://github.com/jesseduffield/lazy_migrate
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubygems_version: 3.0.3
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: A little terminal UI for managing schema migrations
118
+ test_files: []