declare_schema 0.2.0.pre.1 → 0.2.0

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: 8ce85b3e273a189a4ab8b48e333c2f12b2aea4962c0c277ae3c289f1904cddd8
4
- data.tar.gz: d12345f0a483effa2724e0cd315efa759b2ace23e82ef9a1ae2cdaae28aa2571
3
+ metadata.gz: 3f3116a6fbb5a47de5809f3f813c88783284bad601452679fa59784f3598834b
4
+ data.tar.gz: 8f4b6fee7b4d17e5b644f32a4b624a3f77526f91826a8c0b82d61b6e25130332
5
5
  SHA512:
6
- metadata.gz: 84cfbdd55c37a0404b672fbbca5c12dfeee765b0582cdf369b8b3f95159cfb8f82eb3fb1e6149e830ba4fe4481d6df6ef4df45398e30985380ae6681c731f8a9
7
- data.tar.gz: adeda560e6bb859104b78e3710fb94d438606cf42c10b199527e5d14545505dda6dde3206500a5f7ea38b79530952182954f5e3892779f9d96db9270d59a28f1
6
+ metadata.gz: b833000d28d64e7856de05c9ae822c88dcceeb7861f777f13177ea8017e56bf937aba42b9da14ae1ce4810fd275c5462cb08c18c32839255ab46f1de116eee1d
7
+ data.tar.gz: c41c8cbdafd96eeb92fcf5c646ff9d18f4ce2374f06ecc6977015fa8406bc4639ab3722354212d1a75274c5af982f10b6c2f4c838de94f4b9436da20f836d5d3
@@ -4,14 +4,18 @@ 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.2.0] - Unreleased
7
+ ## [0.2.0] - 2020-10-26
8
8
  ### Added
9
9
  - Automatically eager_load! all Rails::Engines before generating migrations.
10
10
 
11
11
  ### Changed
12
12
  - Changed tests from rdoctest to rspec.
13
13
 
14
- ## [0.1.3] - Unreleased
14
+ ### Fixed
15
+ - Fixed a bug where `:text limit: 0xffff_ffff` (max size) was omitted from migrations.
16
+ - Fixed a bug where `:bigint` foreign keys were omitted from the migration.
17
+
18
+ ## [0.1.3] - 2020-10-08
15
19
  ### Changed
16
20
  - Updated the `always_ignore_tables` list in `Migrator` to access Rails metadata table names
17
21
  using the appropriate Rails configuration attributes.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- declare_schema (0.2.0.pre.1)
4
+ declare_schema (0.2.0)
5
5
  rails (>= 4.2)
6
6
 
7
7
  GEM
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeclareSchema
4
- VERSION = "0.2.0.pre.1"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -96,6 +96,20 @@ module Generators
96
96
  def connection
97
97
  ActiveRecord::Base.connection
98
98
  end
99
+
100
+ def fix_native_types(types)
101
+ case connection.class.name
102
+ when /mysql/i
103
+ types[:integer][:limit] ||= 11
104
+ types[:text][:limit] ||= 0xffff
105
+ types[:binary][:limit] ||= 0xffff
106
+ end
107
+ types
108
+ end
109
+
110
+ def native_types
111
+ @native_types ||= fix_native_types(connection.native_database_types)
112
+ end
99
113
  end
100
114
 
101
115
  def initialize(ambiguity_resolver = {})
@@ -129,22 +143,6 @@ module Generators
129
143
  self.class.connection
130
144
  end
131
145
 
132
- class << self
133
- def fix_native_types(types)
134
- case connection.class.name
135
- when /mysql/i
136
- types[:integer][:limit] ||= 11
137
- types[:text][:limit] ||= 0xffff
138
- types[:binary][:limit] ||= 0xffff
139
- end
140
- types
141
- end
142
-
143
- def native_types
144
- @native_types ||= fix_native_types(connection.native_database_types)
145
- end
146
- end
147
-
148
146
  def native_types
149
147
  self.class.native_types
150
148
  end
@@ -412,7 +410,7 @@ module Generators
412
410
  spec = model.field_specs[c]
413
411
  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
414
412
  change_spec = fk_field_options(model, c)
415
- change_spec[:limit] = spec.limit if (spec.sql_type != :text ||
413
+ change_spec[:limit] ||= spec.limit if (spec.sql_type != :text ||
416
414
  ::DeclareSchema::Model::FieldSpec.mysql_text_limits?) &&
417
415
  (spec.limit || col.limit)
418
416
  change_spec[:precision] = spec.precision unless spec.precision.nil?
@@ -518,8 +516,7 @@ module Generators
518
516
  next if k == :null && v == true
519
517
  end
520
518
 
521
- next if k == :limit && type == :text &&
522
- (!::DeclareSchema::Model::FieldSpec.mysql_text_limits? || v == ::DeclareSchema::Model::FieldSpec::MYSQL_LONGTEXT_LIMIT)
519
+ next if k == :limit && type == :text && !::DeclareSchema::Model::FieldSpec.mysql_text_limits?
523
520
 
524
521
  if k.is_a?(Symbol)
525
522
  "#{k}: #{v.inspect}"
@@ -530,10 +527,20 @@ module Generators
530
527
  end
531
528
 
532
529
  def fk_field_options(model, field_name)
533
- if (foreign_key = model.constraint_specs.find { |fk| field_name == fk.foreign_key.to_s }) && (parent_table = foreign_key.parent_table_name)
530
+ foreign_key = model.constraint_specs.find { |fk| field_name == fk.foreign_key.to_s }
531
+ if foreign_key && (parent_table = foreign_key.parent_table_name)
534
532
  parent_columns = connection.columns(parent_table) rescue []
535
- pk_column = parent_columns.find { |column| column.name == "id" } # right now foreign keys assume id is the target
536
- pk_limit = pk_column ? pk_column.cast_type.limit : 8
533
+ pk_limit =
534
+ if (pk_column = parent_columns.find { |column| column.name.to_s == "id" }) # right now foreign keys assume id is the target
535
+ if Rails::VERSION::MAJOR <= 4
536
+ pk_column.cast_type.limit
537
+ else
538
+ pk_column.limit
539
+ end
540
+ else
541
+ 8
542
+ end
543
+
537
544
  { limit: pk_limit }
538
545
  else
539
546
  {}
@@ -196,7 +196,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
196
196
 
197
197
  up, down = Generators::DeclareSchema::Migration::Migrator.run
198
198
  expect(up).to eq(<<~EOS.strip)
199
- add_column :adverts, :notes, :text, null: false
199
+ add_column :adverts, :notes, :text, null: false, limit: 4294967295
200
200
  add_column :adverts, :description, :text, null: false, limit: 255
201
201
  EOS
202
202
 
@@ -238,7 +238,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
238
238
  end
239
239
 
240
240
  up, down = Generators::DeclareSchema::Migration::Migrator.run
241
- expect(up).to eq("change_column :adverts, :description, :text, null: false")
241
+ expect(up).to eq("change_column :adverts, :description, :text, limit: 4294967295, null: false")
242
242
  expect(down).to eq("change_column :adverts, :description, :text")
243
243
 
244
244
  # TODO TECH-4814: The above test should have this output:
@@ -253,7 +253,7 @@ RSpec.describe 'DeclareSchema Migration Generator' do
253
253
  end
254
254
 
255
255
  up, down = Generators::DeclareSchema::Migration::Migrator.run
256
- expect(up).to eq("change_column :adverts, :description, :text, null: false")
256
+ expect(up).to eq("change_column :adverts, :description, :text, limit: 4294967295, null: false")
257
257
  expect(down).to eq("change_column :adverts, :description, :text")
258
258
  ::DeclareSchema::Model::FieldSpec::instance_variable_set(:@mysql_text_limits, false)
259
259
 
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails'
4
+ require 'rails/generators'
5
+
6
+ module Generators
7
+ module DeclareSchema
8
+ module Migration
9
+ RSpec.describe Migrator do
10
+ before do
11
+ ActiveRecord::Base.connection.tables
12
+ end
13
+
14
+ subject { described_class.new }
15
+
16
+ describe 'format_options' do
17
+ let(:mysql_longtext_limit) { 0xffff_ffff }
18
+
19
+ context 'MySQL' do
20
+ before do
21
+ expect(::DeclareSchema::Model::FieldSpec).to receive(:mysql_text_limits?).and_return(true)
22
+ end
23
+
24
+ it 'returns text limits' do
25
+ expect(subject.format_options({ limit: mysql_longtext_limit }, :text)).to eq(["limit: #{mysql_longtext_limit}"])
26
+ end
27
+ end
28
+
29
+ context 'non-MySQL' do
30
+ before do
31
+ expect(::DeclareSchema::Model::FieldSpec).to receive(:mysql_text_limits?).and_return(false)
32
+ end
33
+
34
+ it 'returns text limits' do
35
+ expect(subject.format_options({ limit: mysql_longtext_limit }, :text)).to eq([])
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: declare_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0.pre.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Invoca Development adapted from hobo_fields by Tom Locke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-21 00:00:00.000000000 Z
11
+ date: 2020-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -77,6 +77,7 @@ files:
77
77
  - spec/lib/declare_schema/interactive_primary_key_spec.rb
78
78
  - spec/lib/declare_schema/migration_generator_spec.rb
79
79
  - spec/lib/declare_schema/prepare_testapp.rb
80
+ - spec/lib/generators/declare_schema/migration/migrator_spec.rb
80
81
  - spec/spec_helper.rb
81
82
  - test_responses.txt
82
83
  homepage: https://github.com/Invoca/declare_schema