declare_schema 0.1.2 → 0.3.0.pre.2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +24 -0
- data/Gemfile +0 -1
- data/Gemfile.lock +1 -3
- data/README.md +20 -0
- data/Rakefile +13 -20
- data/gemfiles/rails_4.gemfile +0 -1
- data/gemfiles/rails_5.gemfile +0 -1
- data/gemfiles/rails_6.gemfile +0 -1
- data/lib/declare_schema/model.rb +0 -1
- data/lib/declare_schema/model/field_spec.rb +1 -12
- data/lib/declare_schema/version.rb +1 -1
- data/lib/generators/declare_schema/migration/migration_generator.rb +20 -14
- data/lib/generators/declare_schema/migration/migrator.rb +54 -30
- data/lib/generators/declare_schema/migration/templates/migration.rb.erb +1 -1
- data/lib/generators/declare_schema/support/eval_template.rb +12 -3
- data/lib/generators/declare_schema/support/model.rb +77 -2
- data/spec/lib/declare_schema/api_spec.rb +125 -0
- data/spec/lib/declare_schema/field_declaration_dsl_spec.rb +8 -4
- data/spec/lib/declare_schema/generator_spec.rb +57 -0
- data/spec/lib/declare_schema/interactive_primary_key_spec.rb +51 -0
- data/spec/lib/declare_schema/migration_generator_spec.rb +686 -0
- data/spec/lib/declare_schema/prepare_testapp.rb +29 -0
- data/spec/lib/generators/declare_schema/migration/migrator_spec.rb +72 -0
- data/spec/spec_helper.rb +26 -0
- metadata +11 -12
- data/lib/generators/declare_schema/model/templates/model_injection.rb.erb +0 -25
- data/test/api.rdoctest +0 -136
- data/test/generators.rdoctest +0 -62
- data/test/interactive_primary_key.rdoctest +0 -56
- data/test/migration_generator.rdoctest +0 -846
- data/test/migration_generator_comments.rdoctestDISABLED +0 -74
- data/test/prepare_testapp.rb +0 -15
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 3364c39b6579bb69804417a10b5b287208e6fdfe5a23bf57b6b3f928f69fe539
         | 
| 4 | 
            +
              data.tar.gz: 2fbc6abab32d750ef1af34d6888b5954ee27da2563acd8595728d3d23d184244
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 7e884c085c0d72f1146d7b7f52abbf1f44b19389e2f96e2858b855b11041c94406b0be159a300366d0abeec2f36cf9056d3eddd61352600c780a992ca3971817
         | 
| 7 | 
            +
              data.tar.gz: 1d45f652bd8d0de74b71fec913c4005be8f3212de7fa7942ad587d67059f67115ce8d797be9d71121e792ab66aee84c2adaaa013766b6d96cb21eda1d35522b4
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    | @@ -4,6 +4,27 @@ 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.3.0] - Unreleased
         | 
| 8 | 
            +
            ### Added
         | 
| 9 | 
            +
            - Added a new callback `before_generating_migration` to the `Migrator` that can be
         | 
| 10 | 
            +
            defined in order to custom load more models that might be missed by `eager_load!`
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            ## [0.2.0] - 2020-10-26
         | 
| 13 | 
            +
            ### Added
         | 
| 14 | 
            +
            - Automatically eager_load! all Rails::Engines before generating migrations.
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            ### Changed
         | 
| 17 | 
            +
            - Changed tests from rdoctest to rspec.
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            ### Fixed
         | 
| 20 | 
            +
            - Fixed a bug where `:text limit: 0xffff_ffff` (max size) was omitted from migrations.
         | 
| 21 | 
            +
            - Fixed a bug where `:bigint` foreign keys were omitted from the migration.
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            ## [0.1.3] - 2020-10-08
         | 
| 24 | 
            +
            ### Changed
         | 
| 25 | 
            +
            - Updated the `always_ignore_tables` list in `Migrator` to access Rails metadata table names
         | 
| 26 | 
            +
            using the appropriate Rails configuration attributes.
         | 
| 27 | 
            +
             | 
| 7 28 | 
             
            ## [0.1.2] - 2020-09-29
         | 
| 8 29 | 
             
            ### Changed
         | 
| 9 30 | 
             
            - Added travis support and created 2 specs as a starting point.
         | 
| @@ -13,5 +34,8 @@ Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0 | |
| 13 34 | 
             
            ### Added
         | 
| 14 35 | 
             
            - Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
         | 
| 15 36 |  | 
| 37 | 
            +
            [0.3.0]: https://github.com/Invoca/declare_schema/compare/v0.2.0...v0.3.0
         | 
| 38 | 
            +
            [0.2.0]: https://github.com/Invoca/declare_schema/compare/v0.1.3...v0.2.0
         | 
| 39 | 
            +
            [0.1.3]: https://github.com/Invoca/declare_schema/compare/v0.1.2...v0.1.3
         | 
| 16 40 | 
             
            [0.1.2]: https://github.com/Invoca/declare_schema/compare/v0.1.1...v0.1.2
         | 
| 17 41 | 
             
            [0.1.1]: https://github.com/Invoca/declare_schema/tree/v0.1.1
         | 
    
        data/Gemfile
    CHANGED
    
    
    
        data/Gemfile.lock
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            PATH
         | 
| 2 2 | 
             
              remote: .
         | 
| 3 3 | 
             
              specs:
         | 
| 4 | 
            -
                declare_schema (0. | 
| 4 | 
            +
                declare_schema (0.3.0.pre.2)
         | 
| 5 5 | 
             
                  rails (>= 4.2)
         | 
| 6 6 |  | 
| 7 7 | 
             
            GEM
         | 
| @@ -159,7 +159,6 @@ GEM | |
| 159 159 | 
             
                rubocop-ast (0.4.2)
         | 
| 160 160 | 
             
                  parser (>= 2.7.1.4)
         | 
| 161 161 | 
             
                ruby-progressbar (1.10.1)
         | 
| 162 | 
            -
                rubydoctest (1.1.5)
         | 
| 163 162 | 
             
                sprockets (4.0.2)
         | 
| 164 163 | 
             
                  concurrent-ruby (~> 1.0)
         | 
| 165 164 | 
             
                  rack (> 1, < 3)
         | 
| @@ -194,7 +193,6 @@ DEPENDENCIES | |
| 194 193 | 
             
              responders
         | 
| 195 194 | 
             
              rspec
         | 
| 196 195 | 
             
              rubocop
         | 
| 197 | 
            -
              rubydoctest
         | 
| 198 196 | 
             
              sqlite3
         | 
| 199 197 | 
             
              yard
         | 
| 200 198 |  | 
    
        data/README.md
    CHANGED
    
    | @@ -50,6 +50,26 @@ Migration filename: [<enter>=declare_schema_migration_1|<custom_name>]: add_comp | |
| 50 50 | 
             
            ```
         | 
| 51 51 | 
             
            Note that the migration generator is interactive -- it can't tell the difference between renaming something vs. adding one thing and removing another, so sometimes it will ask you to clarify.
         | 
| 52 52 |  | 
| 53 | 
            +
            ## Migrator Configuration
         | 
| 54 | 
            +
             | 
| 55 | 
            +
            The following configuration options are available for the gem and can be used
         | 
| 56 | 
            +
            during the initialization of your Rails application.
         | 
| 57 | 
            +
             | 
| 58 | 
            +
            ### before_generating_migration callback
         | 
| 59 | 
            +
             | 
| 60 | 
            +
            During the initializtion process for generating migrations, `DeclareSchema` will
         | 
| 61 | 
            +
            trigger the `eager_load!` on the `Rails` application and all `Rails::Engine`s loaded
         | 
| 62 | 
            +
            into scope.  If you need to generate migrations for models that aren't automatically loaded by `eager_load!`,
         | 
| 63 | 
            +
            load them in the `before_generating_migration` block.
         | 
| 64 | 
            +
             | 
| 65 | 
            +
            **Example Configuration**
         | 
| 66 | 
            +
             | 
| 67 | 
            +
            ```ruby
         | 
| 68 | 
            +
            DeclareSchema::Migration::Migrator.before_generating_migration do
         | 
| 69 | 
            +
              require 'lib/some/hidden/models.rb'
         | 
| 70 | 
            +
            end
         | 
| 71 | 
            +
            ```
         | 
| 72 | 
            +
             | 
| 53 73 | 
             
            ## Installing
         | 
| 54 74 |  | 
| 55 75 | 
             
            Install the `DeclareSchema` gem directly:
         | 
    
        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:  | 
| 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} --trace --verbose #{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  | 
| 42 | 
            -
                  sh  | 
| 43 | 
            -
             | 
| 44 | 
            -
             | 
| 45 | 
            -
             | 
| 46 | 
            -
                  sh  | 
| 47 | 
            -
                  rm  | 
| 48 | 
            -
                  sh  | 
| 49 | 
            -
                  puts  | 
| 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 | 
| 52 | 
            -
                  sh  | 
| 53 | 
            -
                  sh  | 
| 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
         | 
    
        data/gemfiles/rails_4.gemfile
    CHANGED
    
    
    
        data/gemfiles/rails_5.gemfile
    CHANGED
    
    
    
        data/gemfiles/rails_6.gemfile
    CHANGED
    
    
    
        data/lib/declare_schema/model.rb
    CHANGED
    
    | @@ -117,7 +117,6 @@ module DeclareSchema | |
| 117 117 | 
             
                    end
         | 
| 118 118 | 
             
                    column_options = {}
         | 
| 119 119 | 
             
                    column_options[:null] = options.delete(:null) || false
         | 
| 120 | 
            -
                    column_options[:comment] = options.delete(:comment) if options.has_key?(:comment)
         | 
| 121 120 | 
             
                    column_options[:default] = options.delete(:default) if options.has_key?(:default)
         | 
| 122 121 | 
             
                    column_options[:limit] = options.delete(:limit) if options.has_key?(:limit)
         | 
| 123 122 |  | 
| @@ -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]
         | 
| @@ -3,7 +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 | 
            -
             | 
| 6 | 
            +
            require 'declare_schema/model/field_spec'
         | 
| 7 7 |  | 
| 8 8 | 
             
            module DeclareSchema
         | 
| 9 9 | 
             
              class MigrationGenerator < Rails::Generators::Base
         | 
| @@ -86,7 +86,7 @@ module DeclareSchema | |
| 86 86 | 
             
                    @down = down
         | 
| 87 87 | 
             
                    @migration_class_name = final_migration_name.camelize
         | 
| 88 88 |  | 
| 89 | 
            -
                    migration_template | 
| 89 | 
            +
                    migration_template('migration.rb.erb', "db/migrate/#{final_migration_name.underscore}.rb")
         | 
| 90 90 | 
             
                    if action == 'm'
         | 
| 91 91 | 
             
                      case Rails::VERSION::MAJOR
         | 
| 92 92 | 
             
                      when 4
         | 
| @@ -118,14 +118,13 @@ module DeclareSchema | |
| 118 118 | 
             
                                         ActiveRecord::Migrator.new(:up, migrations, ActiveRecord::SchemaMigration).pending_migrations
         | 
| 119 119 | 
             
                                       end
         | 
| 120 120 |  | 
| 121 | 
            -
                   | 
| 122 | 
            -
                     | 
| 123 | 
            -
             | 
| 124 | 
            -
                       | 
| 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
         | 
| 125 127 | 
             
                    end
         | 
| 126 | 
            -
                    true
         | 
| 127 | 
            -
                  else
         | 
| 128 | 
            -
                    false
         | 
| 129 128 | 
             
                  end
         | 
| 130 129 | 
             
                end
         | 
| 131 130 |  | 
| @@ -139,10 +138,10 @@ module DeclareSchema | |
| 139 138 | 
             
                      loop do
         | 
| 140 139 | 
             
                        if rename_to_choices.empty?
         | 
| 141 140 | 
             
                          say "\nCONFIRM DROP! #{kind_str} #{name_prefix}#{t}"
         | 
| 142 | 
            -
                          resp = ask("Enter 'drop #{t}' to confirm or press enter to keep:")
         | 
| 143 | 
            -
                          if resp | 
| 141 | 
            +
                          resp = ask("Enter 'drop #{t}' to confirm or press enter to keep:").strip
         | 
| 142 | 
            +
                          if resp == "drop #{t}"
         | 
| 144 143 | 
             
                            break
         | 
| 145 | 
            -
                          elsif resp. | 
| 144 | 
            +
                          elsif resp.empty?
         | 
| 146 145 | 
             
                            to_drop.delete(t)
         | 
| 147 146 | 
             
                            break
         | 
| 148 147 | 
             
                          else
         | 
| @@ -151,8 +150,7 @@ module DeclareSchema | |
| 151 150 | 
             
                        else
         | 
| 152 151 | 
             
                          say "\nDROP, RENAME or KEEP?: #{kind_str} #{name_prefix}#{t}"
         | 
| 153 152 | 
             
                          say "Rename choices: #{to_create * ', '}"
         | 
| 154 | 
            -
                          resp = ask | 
| 155 | 
            -
                          resp = resp.strip
         | 
| 153 | 
            +
                          resp = ask("Enter either 'drop #{t}' or one of the rename choices or press enter to keep:").strip
         | 
| 156 154 |  | 
| 157 155 | 
             
                          if resp == "drop #{t}"
         | 
| 158 156 | 
             
                            # Leave things as they are
         | 
| @@ -183,3 +181,11 @@ module DeclareSchema | |
| 183 181 | 
             
                end
         | 
| 184 182 | 
             
              end
         | 
| 185 183 | 
             
            end
         | 
| 184 | 
            +
             | 
| 185 | 
            +
            module Generators
         | 
| 186 | 
            +
              module DeclareSchema
         | 
| 187 | 
            +
                module Migration
         | 
| 188 | 
            +
                  MigrationGenerator = ::DeclareSchema::MigrationGenerator
         | 
| 189 | 
            +
                end
         | 
| 190 | 
            +
              end
         | 
| 191 | 
            +
            end
         | 
| @@ -69,12 +69,14 @@ module Generators | |
| 69 69 | 
             
                  class Migrator
         | 
| 70 70 | 
             
                    class Error < RuntimeError; end
         | 
| 71 71 |  | 
| 72 | 
            -
                    @ignore_models | 
| 73 | 
            -
                    @ignore_tables | 
| 74 | 
            -
                    @ | 
| 72 | 
            +
                    @ignore_models                    = []
         | 
| 73 | 
            +
                    @ignore_tables                    = []
         | 
| 74 | 
            +
                    @before_generating_migration_callback = nil
         | 
| 75 | 
            +
                    @active_record_class              = ActiveRecord::Base
         | 
| 75 76 |  | 
| 76 77 | 
             
                    class << self
         | 
| 77 78 | 
             
                      attr_accessor :ignore_models, :ignore_tables, :disable_indexing, :disable_constraints, :active_record_class
         | 
| 79 | 
            +
                      attr_reader :before_generating_migration_callback
         | 
| 78 80 |  | 
| 79 81 | 
             
                      def active_record_class
         | 
| 80 82 | 
             
                        @active_record_class.is_a?(Class) or @active_record_class = @active_record_class.to_s.constantize
         | 
| @@ -96,6 +98,25 @@ module Generators | |
| 96 98 | 
             
                      def connection
         | 
| 97 99 | 
             
                        ActiveRecord::Base.connection
         | 
| 98 100 | 
             
                      end
         | 
| 101 | 
            +
             | 
| 102 | 
            +
                      def fix_native_types(types)
         | 
| 103 | 
            +
                        case connection.class.name
         | 
| 104 | 
            +
                        when /mysql/i
         | 
| 105 | 
            +
                          types[:integer][:limit] ||= 11
         | 
| 106 | 
            +
                          types[:text][:limit]    ||= 0xffff
         | 
| 107 | 
            +
                          types[:binary][:limit]  ||= 0xffff
         | 
| 108 | 
            +
                        end
         | 
| 109 | 
            +
                        types
         | 
| 110 | 
            +
                      end
         | 
| 111 | 
            +
             | 
| 112 | 
            +
                      def native_types
         | 
| 113 | 
            +
                        @native_types ||= fix_native_types(connection.native_database_types)
         | 
| 114 | 
            +
                      end
         | 
| 115 | 
            +
             | 
| 116 | 
            +
                      def before_generating_migration(&block)
         | 
| 117 | 
            +
                        block or raise ArgumentError, 'A block is required when setting the before_generating_migration callback'
         | 
| 118 | 
            +
                        @before_generating_migration_callback = block
         | 
| 119 | 
            +
                      end
         | 
| 99 120 | 
             
                    end
         | 
| 100 121 |  | 
| 101 122 | 
             
                    def initialize(ambiguity_resolver = {})
         | 
| @@ -107,36 +128,26 @@ module Generators | |
| 107 128 | 
             
                    attr_accessor :renames
         | 
| 108 129 |  | 
| 109 130 | 
             
                    def load_rails_models
         | 
| 131 | 
            +
                      ActiveRecord::Migration.verbose = false
         | 
| 132 | 
            +
             | 
| 110 133 | 
             
                      Rails.application.eager_load!
         | 
| 134 | 
            +
                      Rails::Engine.subclasses.each(&:eager_load!)
         | 
| 135 | 
            +
                      self.class.before_generating_migration_callback&.call
         | 
| 111 136 | 
             
                    end
         | 
| 112 137 |  | 
| 113 138 | 
             
                    # Returns an array of model classes that *directly* extend
         | 
| 114 139 | 
             
                    # ActiveRecord::Base, excluding anything in the CGI module
         | 
| 115 140 | 
             
                    def table_model_classes
         | 
| 116 141 | 
             
                      load_rails_models
         | 
| 117 | 
            -
                      ActiveRecord::Base.send(:descendants). | 
| 142 | 
            +
                      ActiveRecord::Base.send(:descendants).select do |klass|
         | 
| 143 | 
            +
                        klass.base_class == klass && !klass.name.starts_with?("CGI::")
         | 
| 144 | 
            +
                      end
         | 
| 118 145 | 
             
                    end
         | 
| 119 146 |  | 
| 120 147 | 
             
                    def connection
         | 
| 121 148 | 
             
                      self.class.connection
         | 
| 122 149 | 
             
                    end
         | 
| 123 150 |  | 
| 124 | 
            -
                    class << self
         | 
| 125 | 
            -
                      def fix_native_types(types)
         | 
| 126 | 
            -
                        case connection.class.name
         | 
| 127 | 
            -
                        when /mysql/i
         | 
| 128 | 
            -
                          types[:integer][:limit] ||= 11
         | 
| 129 | 
            -
                          types[:text][:limit]    ||= 0xffff
         | 
| 130 | 
            -
                          types[:binary][:limit]  ||= 0xffff
         | 
| 131 | 
            -
                        end
         | 
| 132 | 
            -
                        types
         | 
| 133 | 
            -
                      end
         | 
| 134 | 
            -
             | 
| 135 | 
            -
                      def native_types
         | 
| 136 | 
            -
                        @native_types ||= fix_native_types(connection.native_database_types)
         | 
| 137 | 
            -
                      end
         | 
| 138 | 
            -
                    end
         | 
| 139 | 
            -
             | 
| 140 151 | 
             
                    def native_types
         | 
| 141 152 | 
             
                      self.class.native_types
         | 
| 142 153 | 
             
                    end
         | 
| @@ -217,7 +228,7 @@ module Generators | |
| 217 228 | 
             
                      end
         | 
| 218 229 | 
             
                    end
         | 
| 219 230 |  | 
| 220 | 
            -
                    def always_ignore_tables
         | 
| 231 | 
            +
                    def self.always_ignore_tables
         | 
| 221 232 | 
             
                      sessions_table =
         | 
| 222 233 | 
             
                        begin
         | 
| 223 234 | 
             
                          if defined?(CGI::Session::ActiveRecordStore::Session) &&
         | 
| @@ -229,7 +240,12 @@ module Generators | |
| 229 240 | 
             
                          nil
         | 
| 230 241 | 
             
                        end
         | 
| 231 242 |  | 
| 232 | 
            -
                      [ | 
| 243 | 
            +
                      [
         | 
| 244 | 
            +
                        'schema_info',
         | 
| 245 | 
            +
                        ActiveRecord::Base.try(:schema_migrations_table_name) || 'schema_migrations',
         | 
| 246 | 
            +
                        ActiveRecord::Base.try(:internal_metadata_table_name) || 'ar_internal_metadata',
         | 
| 247 | 
            +
                        sessions_table
         | 
| 248 | 
            +
                      ].compact
         | 
| 233 249 | 
             
                    end
         | 
| 234 250 |  | 
| 235 251 | 
             
                    def generate
         | 
| @@ -251,7 +267,7 @@ module Generators | |
| 251 267 | 
             
                      model_table_names = models_by_table_name.keys
         | 
| 252 268 |  | 
| 253 269 | 
             
                      to_create = model_table_names - db_tables
         | 
| 254 | 
            -
                      to_drop = db_tables - model_table_names - always_ignore_tables
         | 
| 270 | 
            +
                      to_drop = db_tables - model_table_names - self.class.always_ignore_tables
         | 
| 255 271 | 
             
                      to_change = model_table_names
         | 
| 256 272 | 
             
                      to_rename = extract_table_renames!(to_create, to_drop)
         | 
| 257 273 |  | 
| @@ -399,14 +415,13 @@ module Generators | |
| 399 415 | 
             
                        spec = model.field_specs[c]
         | 
| 400 416 | 
             
                        if spec.different_to?(col) # TODO: DRY this up to a diff function that returns the differences. It's different if it has differences. -Colin
         | 
| 401 417 | 
             
                          change_spec = fk_field_options(model, c)
         | 
| 402 | 
            -
                          change_spec[:limit]      | 
| 418 | 
            +
                          change_spec[:limit]     ||= spec.limit   if (spec.sql_type != :text ||
         | 
| 403 419 | 
             
                                                                     ::DeclareSchema::Model::FieldSpec.mysql_text_limits?) &&
         | 
| 404 420 | 
             
                                                                      (spec.limit || col.limit)
         | 
| 405 421 | 
             
                          change_spec[:precision] = spec.precision unless spec.precision.nil?
         | 
| 406 422 | 
             
                          change_spec[:scale]     = spec.scale     unless spec.scale.nil?
         | 
| 407 423 | 
             
                          change_spec[:null]      = spec.null      unless spec.null && col.null
         | 
| 408 424 | 
             
                          change_spec[:default]   = spec.default   unless spec.default.nil? && col.default.nil?
         | 
| 409 | 
            -
                          change_spec[:comment]   = spec.comment   unless spec.comment.nil? && (col.comment if col.respond_to?(:comment)).nil?
         | 
| 410 425 |  | 
| 411 426 | 
             
                          changes << "change_column :#{new_table_name}, :#{c}, " +
         | 
| 412 427 | 
             
                                     ([":#{spec.sql_type}"] + format_options(change_spec, spec.sql_type, changing: true)).join(", ")
         | 
| @@ -506,8 +521,7 @@ module Generators | |
| 506 521 | 
             
                          next if k == :null && v == true
         | 
| 507 522 | 
             
                        end
         | 
| 508 523 |  | 
| 509 | 
            -
                        next if k == :limit && type == :text &&
         | 
| 510 | 
            -
                                (!::DeclareSchema::Model::FieldSpec.mysql_text_limits? || v == ::DeclareSchema::Model::FieldSpec::MYSQL_LONGTEXT_LIMIT)
         | 
| 524 | 
            +
                        next if k == :limit && type == :text && !::DeclareSchema::Model::FieldSpec.mysql_text_limits?
         | 
| 511 525 |  | 
| 512 526 | 
             
                        if k.is_a?(Symbol)
         | 
| 513 527 | 
             
                          "#{k}: #{v.inspect}"
         | 
| @@ -518,10 +532,20 @@ module Generators | |
| 518 532 | 
             
                    end
         | 
| 519 533 |  | 
| 520 534 | 
             
                    def fk_field_options(model, field_name)
         | 
| 521 | 
            -
                       | 
| 535 | 
            +
                      foreign_key = model.constraint_specs.find { |fk| field_name == fk.foreign_key.to_s }
         | 
| 536 | 
            +
                      if foreign_key && (parent_table = foreign_key.parent_table_name)
         | 
| 522 537 | 
             
                        parent_columns = connection.columns(parent_table) rescue []
         | 
| 523 | 
            -
                         | 
| 524 | 
            -
             | 
| 538 | 
            +
                        pk_limit  =
         | 
| 539 | 
            +
                          if (pk_column = parent_columns.find { |column| column.name.to_s == "id" }) # right now foreign keys assume id is the target
         | 
| 540 | 
            +
                            if Rails::VERSION::MAJOR <= 4
         | 
| 541 | 
            +
                              pk_column.cast_type.limit
         | 
| 542 | 
            +
                            else
         | 
| 543 | 
            +
                              pk_column.limit
         | 
| 544 | 
            +
                            end
         | 
| 545 | 
            +
                          else
         | 
| 546 | 
            +
                            8
         | 
| 547 | 
            +
                          end
         | 
| 548 | 
            +
             | 
| 525 549 | 
             
                        { limit: pk_limit }
         | 
| 526 550 | 
             
                      else
         | 
| 527 551 | 
             
                        {}
         |