declare_schema 0.1.1 → 0.3.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +37 -0
  3. data/CHANGELOG.md +36 -4
  4. data/Gemfile +0 -2
  5. data/Gemfile.lock +1 -4
  6. data/Rakefile +13 -20
  7. data/gemfiles/rails_4.gemfile +4 -7
  8. data/gemfiles/rails_5.gemfile +4 -7
  9. data/gemfiles/rails_6.gemfile +4 -7
  10. data/lib/declare_schema/model.rb +21 -23
  11. data/lib/declare_schema/model/field_spec.rb +1 -12
  12. data/lib/declare_schema/version.rb +1 -1
  13. data/lib/generators/declare_schema/migration/migration_generator.rb +20 -13
  14. data/lib/generators/declare_schema/migration/migrator.rb +57 -33
  15. data/lib/generators/declare_schema/migration/templates/migration.rb.erb +1 -1
  16. data/lib/generators/declare_schema/support/eval_template.rb +12 -3
  17. data/lib/generators/declare_schema/support/model.rb +77 -2
  18. data/spec/lib/declare_schema/api_spec.rb +125 -0
  19. data/spec/lib/declare_schema/field_declaration_dsl_spec.rb +8 -4
  20. data/spec/lib/declare_schema/generator_spec.rb +57 -0
  21. data/spec/lib/declare_schema/interactive_primary_key_spec.rb +51 -0
  22. data/spec/lib/declare_schema/migration_generator_spec.rb +735 -0
  23. data/spec/lib/declare_schema/prepare_testapp.rb +31 -0
  24. data/spec/lib/generators/declare_schema/migration/migrator_spec.rb +42 -0
  25. data/spec/spec_helper.rb +26 -0
  26. metadata +9 -11
  27. data/.jenkins/Jenkinsfile +0 -72
  28. data/.jenkins/ruby_build_pod.yml +0 -19
  29. data/lib/generators/declare_schema/model/templates/model_injection.rb.erb +0 -25
  30. data/test/api.rdoctest +0 -136
  31. data/test/generators.rdoctest +0 -60
  32. data/test/interactive_primary_key.rdoctest +0 -56
  33. data/test/migration_generator.rdoctest +0 -846
  34. data/test/migration_generator_comments.rdoctestDISABLED +0 -74
  35. data/test/prepare_testapp.rb +0 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c1bc236be22c712ca8a65ef4ed294301fc7d2021167dd0e2501066abe30b3ba
4
- data.tar.gz: b41b86f51293569be553bab41fc245601ad82f98f4546af8d5d9d46601070fe3
3
+ metadata.gz: 2a82a5e54f5dadf8a733520efe057d8b1a0319e1725ef82d41d97004ee68cf0c
4
+ data.tar.gz: 899e9f135706d4883b448bcffc09b5f962cb84892c1bf1be27e6f2e36209454f
5
5
  SHA512:
6
- metadata.gz: 3a7b1808736b5f6c1ed69eb88a6afa7a69dbc6fcb014fbd5afb23734abd177348440caf5394ba941e66f6473011fae7543ce5b1e43b81ec8231ab3abfc369627
7
- data.tar.gz: b96afd3ecfc86d51a795e43f5b04859d7e45c7869ffcf9f21a6a8a63da05e8499dd03b66e4e951b967f0e4feb8d21c2c0cf6aa88e952e53b3254c12ab541e0eb
6
+ metadata.gz: 0465ffd5522605f9e0c61beffc2571ab76d077020596d82fe616c60cd631c6663b399c9c5d02c8983d9060223f3bb100932baf411efa69a3a7f236097784ae9e
7
+ data.tar.gz: 4c6b7b49067650cd0791257614dc84e1f6f6a9191346578249fa196dc0d9f621c491d597aaa32f82c64bde8f6e7fe8e18f1974f89c4677ade6c8203aede20ec4
@@ -0,0 +1,37 @@
1
+ ---
2
+ dist: trusty
3
+ os: linux
4
+ language: ruby
5
+ cache: bundler
6
+ rvm:
7
+ - 2.4.5
8
+ - 2.5.8
9
+ - 2.6.5
10
+ - 2.7.1
11
+ - ruby-head
12
+ gemfile:
13
+ - gemfiles/rails_4.gemfile
14
+ - gemfiles/rails_5.gemfile
15
+ - gemfiles/rails_6.gemfile
16
+ jobs:
17
+ fast_finish: false
18
+ exclude:
19
+ - gemfile: gemfiles/rails_4.gemfile
20
+ rvm: 2.7.1
21
+ - gemfile: gemfiles/rails_5.gemfile
22
+ rvm: 2.4.5
23
+ - gemfile: gemfiles/rails_6.gemfile
24
+ rvm: 2.4.5
25
+ allow_failures:
26
+ - rvm: ruby-head
27
+ before_install:
28
+ - rm -f /home/travis/.rvm/rubies/ruby-2.*/lib/ruby/gems/2.*/specifications/default/bundler-2.*.gemspec
29
+ - echo y | rvm @global do gem install bundler -v 1.17.3 --force --default
30
+ - gem install bundler -v 1.17.3 --force --default
31
+ - gem install bundler -v 1.17.3
32
+ install:
33
+ - bundle --version
34
+ - bundle install --jobs=3 --retry=3 --path=${BUNDLE_PATH:-vendor/bundle}
35
+ script:
36
+ - bundle exec rake test:prepare_testapp[force]
37
+ - bundle exec rake test:all < test_responses.txt
@@ -1,11 +1,43 @@
1
- # CHANGELOG for `declarative_schema`
1
+ # CHANGELOG for `declare_schema`
2
2
 
3
3
  Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4
4
 
5
5
  Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
- ## [0.1.0] - Unreleased
7
+ ## [0.3.0] - Unreleased
8
8
  ### Added
9
- - Initial version from https://github.com/Invoca/declare_schema v4.0.0.
9
+ - Added support for `belongs_to optional:`.
10
+ If given, it is passed through to `ActiveRecord`'s `belong_to`.
11
+ If not given in Rails 5+, the `optional:` value is set equal to the `null:` value (default: `false`) and that
12
+ is is passed to `ActiveRecord`'s `belong_to`.
10
13
 
11
- [0.1.0]: https://github.com/Invoca/declarative_schema/tree/v0.1.0
14
+ ## [0.2.0] - 2020-10-26
15
+ ### Added
16
+ - Automatically eager_load! all Rails::Engines before generating migrations.
17
+
18
+ ### Changed
19
+ - Changed tests from rdoctest to rspec.
20
+
21
+ ### Fixed
22
+ - Fixed a bug where `:text limit: 0xffff_ffff` (max size) was omitted from migrations.
23
+ - Fixed a bug where `:bigint` foreign keys were omitted from the migration.
24
+
25
+ ## [0.1.3] - 2020-10-08
26
+ ### Changed
27
+ - Updated the `always_ignore_tables` list in `Migrator` to access Rails metadata table names
28
+ using the appropriate Rails configuration attributes.
29
+
30
+ ## [0.1.2] - 2020-09-29
31
+ ### Changed
32
+ - Added travis support and created 2 specs as a starting point.
33
+
34
+
35
+ ## [0.1.1] - 2020-09-24
36
+ ### Added
37
+ - Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
38
+
39
+ [0.3.0]: https://github.com/Invoca/declare_schema/compare/v0.2.0...v0.3.0
40
+ [0.2.0]: https://github.com/Invoca/declare_schema/compare/v0.1.3...v0.2.0
41
+ [0.1.3]: https://github.com/Invoca/declare_schema/compare/v0.1.2...v0.1.3
42
+ [0.1.2]: https://github.com/Invoca/declare_schema/compare/v0.1.1...v0.1.2
43
+ [0.1.1]: https://github.com/Invoca/declare_schema/tree/v0.1.1
data/Gemfile CHANGED
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  source 'https://rubygems.org'
4
- source 'https://gem.fury.io/invoca'
5
4
 
6
5
  gemspec
7
6
 
@@ -19,6 +18,5 @@ gem 'rails', '~> 5.2', '>= 5.2.4.3'
19
18
  gem 'responders'
20
19
  gem 'rspec'
21
20
  gem 'rubocop'
22
- gem 'rubydoctest'
23
21
  gem 'sqlite3'
24
22
  gem 'yard'
@@ -1,12 +1,11 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- declare_schema (0.1.1)
4
+ declare_schema (0.3.0.pre.1)
5
5
  rails (>= 4.2)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
- remote: https://gem.fury.io/invoca/
10
9
  specs:
11
10
  actioncable (5.2.4.4)
12
11
  actionpack (= 5.2.4.4)
@@ -160,7 +159,6 @@ GEM
160
159
  rubocop-ast (0.4.2)
161
160
  parser (>= 2.7.1.4)
162
161
  ruby-progressbar (1.10.1)
163
- rubydoctest (1.1.5)
164
162
  sprockets (4.0.2)
165
163
  concurrent-ruby (~> 1.0)
166
164
  rack (> 1, < 3)
@@ -195,7 +193,6 @@ DEPENDENCIES
195
193
  responders
196
194
  rspec
197
195
  rubocop
198
- rubydoctest
199
196
  sqlite3
200
197
  yard
201
198
 
data/Rakefile CHANGED
@@ -14,7 +14,6 @@ $LOAD_PATH.unshift File.expand_path('lib', __dir__)
14
14
  require 'declare_schema'
15
15
 
16
16
  RUBY = 'ruby'
17
- RUBYDOCTEST = ENV['RUBYDOCTEST'] || "#{RUBY} -S rubydoctest"
18
17
  GEM_ROOT = __dir__
19
18
  TESTAPP_PATH = ENV['TESTAPP_PATH'] || File.join(Dir.tmpdir, 'declare_schema_testapp')
20
19
  BIN = File.expand_path('bin/declare_schema', __dir__)
@@ -24,13 +23,7 @@ task default: 'test:all'
24
23
  include Rake::DSL
25
24
 
26
25
  namespace "test" do
27
- task all: [:doctest, :spec]
28
-
29
- desc "Run the doctests"
30
- task :doctest do |_t|
31
- files = Dir['test/*.rdoctest'].sort.map { |f| File.expand_path(f) }.join(' ')
32
- system("#{RUBYDOCTEST} #{files}") or exit(1)
33
- end
26
+ task all: :spec
34
27
 
35
28
  desc "Prepare a rails application for testing"
36
29
  task :prepare_testapp, :force do |_t, args|
@@ -38,19 +31,19 @@ namespace "test" do
38
31
  FileUtils.remove_entry_secure(TESTAPP_PATH, true)
39
32
  sh %(#{BIN} new #{TESTAPP_PATH} --skip-wizard --skip-bundle)
40
33
  FileUtils.chdir TESTAPP_PATH
41
- sh %(bundle install)
42
- sh %(echo "" >> Gemfile)
43
- sh %(echo "gem 'irt', :group => :development" >> Gemfile) # to make the bundler happy
44
- sh %(echo "gem 'therubyracer'" >> Gemfile)
45
- sh %(echo "gem 'kramdown'" >> Gemfile)
46
- sh %(echo "" > app/models/.gitignore) # because git reset --hard would rm the dir
47
- rm %(.gitignore) # we need to reset everything in a testapp
48
- sh %(git init && git add . && git commit -m "initial commit")
49
- puts %(The testapp has been created in '#{TESTAPP_PATH}')
34
+ sh "bundle install"
35
+ sh "(echo '';
36
+ echo \"gem 'irt', :group => :development\";
37
+ echo \"gem 'therubyracer'\";
38
+ echo \"gem 'kramdown'\") > Gemfile"
39
+ sh "echo '' > app/models/.gitignore" # because git reset --hard would rm the dir
40
+ rm ".gitignore" # we need to reset everything in a testapp
41
+ sh "git init && git add . && git commit -m \"initial commit\""
42
+ puts "The testapp has been created in '#{TESTAPP_PATH}'"
50
43
  else
51
- FileUtils.chdir TESTAPP_PATH
52
- sh %(git add .)
53
- sh %(git reset --hard -q HEAD)
44
+ FileUtils.chdir(TESTAPP_PATH)
45
+ sh "git add ."
46
+ sh "git reset --hard -q HEAD"
54
47
  end
55
48
  end
56
49
  end
@@ -1,25 +1,22 @@
1
- # frozen_string_literal: true
2
-
3
1
  # This file was generated by Appraisal
4
2
 
5
3
  source "https://rubygems.org"
6
- source "https://gem.fury.io/invoca"
7
4
 
8
5
  gem "appraisal"
6
+ gem "bundler", "< 2"
7
+ gem "climate_control", "~> 0.2"
9
8
  gem "pry"
10
9
  gem "pry-byebug"
11
10
  gem "rails", "~> 4.2"
12
11
  gem "responders"
13
- gem "rubydoctest"
12
+ gem "rspec"
13
+ gem "rubocop"
14
14
  gem "sqlite3", "~> 1.3.0"
15
- gem "test_overrides"
16
15
  gem "yard"
17
16
 
18
17
  group :testapp do
19
18
  gem "bootsnap", ">= 1.1.0", require: false
20
- gem "kramdown"
21
19
  gem "listen"
22
- gem "RedCloth"
23
20
  end
24
21
 
25
22
  gemspec path: "../"
@@ -1,25 +1,22 @@
1
- # frozen_string_literal: true
2
-
3
1
  # This file was generated by Appraisal
4
2
 
5
3
  source "https://rubygems.org"
6
- source "https://gem.fury.io/invoca"
7
4
 
8
5
  gem "appraisal"
6
+ gem "bundler", "< 2"
7
+ gem "climate_control", "~> 0.2"
9
8
  gem "pry"
10
9
  gem "pry-byebug"
11
10
  gem "rails", "~> 5.2"
12
11
  gem "responders"
13
- gem "rubydoctest"
12
+ gem "rspec"
13
+ gem "rubocop"
14
14
  gem "sqlite3"
15
- gem "test_overrides"
16
15
  gem "yard"
17
16
 
18
17
  group :testapp do
19
18
  gem "bootsnap", ">= 1.1.0", require: false
20
- gem "kramdown"
21
19
  gem "listen"
22
- gem "RedCloth"
23
20
  end
24
21
 
25
22
  gemspec path: "../"
@@ -1,25 +1,22 @@
1
- # frozen_string_literal: true
2
-
3
1
  # This file was generated by Appraisal
4
2
 
5
3
  source "https://rubygems.org"
6
- source "https://gem.fury.io/invoca"
7
4
 
8
5
  gem "appraisal"
6
+ gem "bundler", "< 2"
7
+ gem "climate_control", "~> 0.2"
9
8
  gem "pry"
10
9
  gem "pry-byebug"
11
10
  gem "rails", "~> 6.0"
12
11
  gem "responders"
13
- gem "rubydoctest"
12
+ gem "rspec"
13
+ gem "rubocop"
14
14
  gem "sqlite3"
15
- gem "test_overrides"
16
15
  gem "yard"
17
16
 
18
17
  group :testapp do
19
18
  gem "bootsnap", ">= 1.1.0", require: false
20
- gem "kramdown"
21
19
  gem "listen"
22
- gem "RedCloth"
23
20
  end
24
21
 
25
22
  gemspec path: "../"
@@ -106,18 +106,9 @@ module DeclareSchema
106
106
  end
107
107
 
108
108
  # Extend belongs_to so that it creates a FieldSpec for the foreign key
109
- def belongs_to(name, *args, &block)
110
- if args.size == 0 || (args.size == 1 && args[0].is_a?(Proc))
111
- options = {}
112
- args.push(options)
113
- elsif args.size == 1
114
- options = args[0]
115
- else
116
- options = args[1]
117
- end
109
+ def belongs_to(name, scope = nil, **options, &block)
118
110
  column_options = {}
119
111
  column_options[:null] = options.delete(:null) || false
120
- column_options[:comment] = options.delete(:comment) if options.has_key?(:comment)
121
112
  column_options[:default] = options.delete(:default) if options.has_key?(:default)
122
113
  column_options[:limit] = options.delete(:limit) if options.has_key?(:limit)
123
114
 
@@ -130,20 +121,27 @@ module DeclareSchema
130
121
  fk_options[:constraint_name] = options.delete(:constraint) if options.has_key?(:constraint)
131
122
  fk_options[:index_name] = index_options[:name]
132
123
 
124
+ fk = options[:foreign_key]&.to_s || "#{name}_id"
125
+
126
+ if !options.has_key?(:optional) && Rails::VERSION::MAJOR >= 5
127
+ options[:optional] = column_options[:null]
128
+ end
129
+
133
130
  fk_options[:dependent] = options.delete(:far_end_dependent) if options.has_key?(:far_end_dependent)
134
- super(name, *args, &block).tap do |_bt|
135
- refl = reflections[name.to_s] or raise "Couldn't find reflection #{name} in #{reflections.keys}"
136
- fkey = refl.foreign_key
137
- declare_field(fkey.to_sym, :integer, column_options)
138
- if refl.options[:polymorphic]
139
- foreign_type = options[:foreign_type] || "#{name}_type"
140
- declare_polymorphic_type_field(foreign_type, column_options)
141
- index([foreign_type, fkey], index_options) if index_options[:name] != false
142
- else
143
- index(fkey, index_options) if index_options[:name] != false
144
- options[:constraint_name] = options
145
- constraint(fkey, fk_options) if fk_options[:constraint_name] != false
146
- end
131
+
132
+ super(name, scope, options)
133
+
134
+ refl = reflections[name.to_s] or raise "Couldn't find reflection #{name} in #{reflections.keys}"
135
+ fkey = refl.foreign_key or raise "Couldn't find foreign_key for #{name} in #{refl.inspect}"
136
+ declare_field(fkey.to_sym, :integer, column_options)
137
+ if refl.options[:polymorphic]
138
+ foreign_type = options[:foreign_type] || "#{name}_type"
139
+ declare_polymorphic_type_field(foreign_type, column_options)
140
+ index([foreign_type, fkey], index_options) if index_options[:name] != false
141
+ else
142
+ index(fkey, index_options) if index_options[:name] != false
143
+ options[:constraint_name] = options
144
+ constraint(fkey, fk_options) if fk_options[:constraint_name] != false
147
145
  end
148
146
  end
149
147
 
@@ -53,7 +53,7 @@ module DeclareSchema
53
53
  @options[:limit] = self.class.round_up_mysql_text_limit(@options[:limit] || MYSQL_LONGTEXT_LIMIT)
54
54
  end
55
55
  when :string
56
- @options[:limit] or raise "limit must be given for :string field #{model}##{@name}: #{@options.inspect}; do you want 255?"
56
+ @options[:limit] or raise "limit must be given for :string field #{model}##{@name}: #{@options.inspect}; do you want `limit: 255`?"
57
57
  end
58
58
  @position = position_option || model.field_specs.length
59
59
  end
@@ -102,10 +102,6 @@ module DeclareSchema
102
102
  @options[:default]
103
103
  end
104
104
 
105
- def comment
106
- @options[:comment]
107
- end
108
-
109
105
  def same_type?(col_spec)
110
106
  type = sql_type
111
107
  normalized_type = TYPE_SYNONYMS[type] || type
@@ -115,13 +111,6 @@ module DeclareSchema
115
111
 
116
112
  def different_to?(col_spec)
117
113
  !same_type?(col_spec) ||
118
- # we should be able to use col_spec.comment, but col_spec has
119
- # a nil table_name for some strange reason.
120
- (model.table_exists? &&
121
- ActiveRecord::Base.respond_to?(:column_comment) &&
122
- !(col_comment = ActiveRecord::Base.column_comment(col_spec.name, model.table_name)).nil? &&
123
- col_comment != comment
124
- ) ||
125
114
  begin
126
115
  native_type = native_types[type]
127
116
  check_attributes = [:null, :default]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeclareSchema
4
- VERSION = "0.1.1"
4
+ VERSION = "0.3.0.pre.1"
5
5
  end
@@ -3,6 +3,7 @@
3
3
  require 'rails/generators/migration'
4
4
  require 'rails/generators/active_record'
5
5
  require 'generators/declare_schema/support/thor_shell'
6
+ require 'declare_schema/model/field_spec'
6
7
 
7
8
  module DeclareSchema
8
9
  class MigrationGenerator < Rails::Generators::Base
@@ -85,7 +86,7 @@ module DeclareSchema
85
86
  @down = down
86
87
  @migration_class_name = final_migration_name.camelize
87
88
 
88
- migration_template 'migration.rb.erb', "db/migrate/#{final_migration_name.underscore}.rb"
89
+ migration_template('migration.rb.erb', "db/migrate/#{final_migration_name.underscore}.rb")
89
90
  if action == 'm'
90
91
  case Rails::VERSION::MAJOR
91
92
  when 4
@@ -117,14 +118,13 @@ module DeclareSchema
117
118
  ActiveRecord::Migrator.new(:up, migrations, ActiveRecord::SchemaMigration).pending_migrations
118
119
  end
119
120
 
120
- if pending_migrations.any?
121
- say "You have #{pending_migrations.size} pending migration#{'s' if pending_migrations.size > 1}:"
122
- pending_migrations.each do |pending_migration|
123
- say format(' %4d %s', pending_migration.version, pending_migration.name)
121
+ pending_migrations.any?.tap do |any|
122
+ if any
123
+ say "You have #{pending_migrations.size} pending migration#{'s' if pending_migrations.size > 1}:"
124
+ pending_migrations.each do |pending_migration|
125
+ say format(' %4d %s', pending_migration.version, pending_migration.name)
126
+ end
124
127
  end
125
- true
126
- else
127
- false
128
128
  end
129
129
  end
130
130
 
@@ -138,10 +138,10 @@ module DeclareSchema
138
138
  loop do
139
139
  if rename_to_choices.empty?
140
140
  say "\nCONFIRM DROP! #{kind_str} #{name_prefix}#{t}"
141
- resp = ask("Enter 'drop #{t}' to confirm or press enter to keep:")
142
- if resp.strip == "drop #{t}"
141
+ resp = ask("Enter 'drop #{t}' to confirm or press enter to keep:").strip
142
+ if resp == "drop #{t}"
143
143
  break
144
- elsif resp.strip.empty?
144
+ elsif resp.empty?
145
145
  to_drop.delete(t)
146
146
  break
147
147
  else
@@ -150,8 +150,7 @@ module DeclareSchema
150
150
  else
151
151
  say "\nDROP, RENAME or KEEP?: #{kind_str} #{name_prefix}#{t}"
152
152
  say "Rename choices: #{to_create * ', '}"
153
- resp = ask "Enter either 'drop #{t}' or one of the rename choices or press enter to keep:"
154
- resp = resp.strip
153
+ resp = ask("Enter either 'drop #{t}' or one of the rename choices or press enter to keep:").strip
155
154
 
156
155
  if resp == "drop #{t}"
157
156
  # Leave things as they are
@@ -182,3 +181,11 @@ module DeclareSchema
182
181
  end
183
182
  end
184
183
  end
184
+
185
+ module Generators
186
+ module DeclareSchema
187
+ module Migration
188
+ MigrationGenerator = ::DeclareSchema::MigrationGenerator
189
+ end
190
+ end
191
+ end