screamers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dd7ad40377447ccb7e9dd576145a16cd5c623024
4
+ data.tar.gz: a023b799d0f13787dfabd217fee6c6511272d84d
5
+ SHA512:
6
+ metadata.gz: e857794a2b2c96c6b5b5928f4339eea66ebc33504eceb7bc6ad2896a3dfa4926f1719a5824198405f9329c0ccf2ca92ab028c42968f3b1c5f70b62d0cbcc4667
7
+ data.tar.gz: af14ce768fb031ae627186b3f8b5bcb270283ba1fe80c9733c8f15c829f4cfac2a16b1f7f2158bb9dd7d06925d7a1c2c2bbd376cba349c607894deec255bc20c
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.7
5
+ - 2.3.4
6
+ - 2.4.1
7
+ - ruby-head
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: ruby-head
11
+ before_install: gem install bundler -v 1.15.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in screamers.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Koichi ITO
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.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Screamers [![Build Status](https://travis-ci.org/koic/screamers.svg)](https://travis-ci.org/koic/screamers)
2
+
3
+ Generate a migration file that converts column types all at once.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your Rails application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'screamers'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```console
16
+ % bundle
17
+ ```
18
+
19
+ The following subcommand will be added to the `rails g` command.
20
+
21
+ ```console
22
+ Screamers:
23
+ screamers:migration
24
+ ```
25
+
26
+ ## Synopsis
27
+
28
+ ```console
29
+ bin/rails g screamers:migration <old-column-type> <new-column-type>
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ Here is an example of converting `date` type to `datetime` type.
35
+
36
+ ```console
37
+ % bin/rails g screamers:migration date datetime
38
+ create db/migrate/20170628144908_change_date_to_datetime_using_screamers.rb
39
+ ```
40
+
41
+ Generate a migration file that converts column types all at once.
42
+
43
+ ## Development
44
+
45
+ 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.
46
+
47
+ 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).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/koic/screamers.
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "screamers"
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__)
data/bin/setup ADDED
@@ -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,31 @@
1
+ # frozen-string-literal: true
2
+
3
+ require 'screamers'
4
+
5
+ module Screamers
6
+ module Generators
7
+ class MigrationGenerator < ::Rails::Generators::Base
8
+ desc 'Creates a migration file made [Screamers Task]'
9
+ argument :old_column_type, type: :string, banner: 'old column type'
10
+ argument :new_column_type, type: :string, banner: 'new column type'
11
+
12
+ def self.source_root
13
+ File.expand_path('../templates', __FILE__)
14
+ end
15
+
16
+ def create_migration_file
17
+ collector = Screamers::SchemaCollector.new(old_column_type, new_column_type)
18
+
19
+ @target_columns = collector.collect_schema
20
+
21
+ if @target_columns.empty?
22
+ puts '[Screamers] There is no change in the schema.'; exit!
23
+ end
24
+
25
+ file_name = "#{Time.current.strftime('%Y%m%d%H%M%S')}_change_#{old_column_type}_to_#{new_column_type}_using_screamers"
26
+
27
+ template 'migration.rb.tt', File.join('db/migrate', "#{file_name}.rb")
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,20 @@
1
+ class Change<%= @old_column_type.capitalize %>To<%= @new_column_type.capitalize %>UsingScreamers < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
2
+ <% target_tables = @target_columns.keys.sort -%>
3
+ def up
4
+ <% target_tables.each do |table| -%>
5
+ <% @target_columns[table].sort.each do |column| -%>
6
+ change_column :<%= table %>, :<%= column %>, :<%= @new_column_type %>
7
+ <% end -%>
8
+ <%= "\n" unless target_tables.last == table -%>
9
+ <% end -%>
10
+ end
11
+
12
+ def down
13
+ <% target_tables.each do |table| -%>
14
+ <% @target_columns[table].sort.each do |column| -%>
15
+ change_column :<%= table %>, :<%= column %>, :<%= @old_column_type %>
16
+ <% end -%>
17
+ <%= "\n" unless target_tables.last == table -%>
18
+ <% end -%>
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ class Railtie < Rails::Railtie
2
+ rake_tasks do
3
+ Dir[File.join(File.dirname(__FILE__), 'tasks/*.rake')].each { |f| load f }
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # frozen-string-literal: true
2
+
3
+ module Screamers
4
+ class SchemaCollector
5
+ def initialize(old_column_type, new_column_type)
6
+ @old_column_type = old_column_type.to_sym
7
+ @new_column_type = new_column_type.to_sym
8
+ end
9
+
10
+ migrations_paths = ActiveRecord::Migrator.migrations_paths.first
11
+
12
+ def collect_schema
13
+ tables = ActiveRecord::Base.connection.tables
14
+ tables.delete('ar_internal_metadata')
15
+
16
+ @target_tables = tables.each_with_object({}) {|table, target|
17
+ columns = Module.const_get(table.classify).columns rescue next
18
+
19
+ target_columns = columns.select {|column|
20
+ column.type == @old_column_type
21
+ }
22
+
23
+ unless (columns = target_columns.map(&:name)).empty?
24
+ target[table] = columns
25
+ end
26
+ }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Screamers
2
+ VERSION = '0.1.0'
3
+ end
data/lib/screamers.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen-string-literal: true
2
+
3
+ require 'active_record'
4
+ require 'screamers/schema_collector'
5
+ require 'screamers/version'
data/screamers.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'screamers/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'screamers'
7
+ spec.version = Screamers::VERSION
8
+ spec.authors = ['Koichi ITO']
9
+ spec.email = ['koic.ito@gmail.com']
10
+
11
+ spec.summary = 'Generate a migration file that converts column types all at once.'
12
+ spec.description = 'Generate a migration file that converts column types all at once.'
13
+ spec.homepage = 'https://github.com/koic/screamers'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_runtime_dependency 'activerecord'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.15'
26
+ spec.add_development_dependency 'generator_spec'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'sqlite3'
30
+ spec.add_development_dependency 'timecop'
31
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: screamers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Koichi ITO
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: generator_spec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: timecop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Generate a migration file that converts column types all at once.
112
+ email:
113
+ - koic.ito@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - lib/rails/generators/screamers/migration/migration_generator.rb
128
+ - lib/rails/generators/screamers/migration/templates/migration.rb.tt
129
+ - lib/screamers.rb
130
+ - lib/screamers/railtie.rb
131
+ - lib/screamers/schema_collector.rb
132
+ - lib/screamers/version.rb
133
+ - screamers.gemspec
134
+ homepage: https://github.com/koic/screamers
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.6.12
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Generate a migration file that converts column types all at once.
158
+ test_files: []