declare_schema 1.0.2 → 1.1.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: 53f77b255392ad9338faad2f2488c3ae34a134c6687cbce5e42d74d33fd2e099
4
- data.tar.gz: f07a30238d79d7d8e4194f28cd6d8f3979a9d40ec326ef93f47f76b125a894c9
3
+ metadata.gz: e974685df9b53809688f8903ceb82a475e50b24a4725275c3cde5949396fcd85
4
+ data.tar.gz: 6efca4e93e98e3880418821c2f3969600a8ac48b8b967a96fcc959824fd0e1fd
5
5
  SHA512:
6
- metadata.gz: aa62f18456e37ab3e809ee1099ee8aa7513dee715f3fd7fafbfb3b56d1f91e5d13cdd31b6d95395f9da46cfa33ff200ea52bd8a57d53416f1909b8ad207d19b9
7
- data.tar.gz: 29be47457d43fe9f1b9f58da7bc8865e0780d172667464af25afa62923e9841439f4c4662cb3e1a89f2eee43944779410602c52be3398a61334afbd6ca993d3a
6
+ metadata.gz: 1f2dac19cdac8a2a5b59f7d20f34c6230e69981a89b180e47080f5c77ba047962ed1f1badeeb69230b14c8a074e96b0588bb9ff9e2430ff2a5496c1d60318cc9
7
+ data.tar.gz: 90098f7d95782c943e17f04b8fc4f1ed23a4cecf63f6208b54fdfff60af19ca4c43ab661eacd055319ca5ec290b1ac1591586a8ec852eda41375d2b2d020346a
data/CHANGELOG.md CHANGED
@@ -4,6 +4,11 @@ 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
+ ## [1.1.0] - 2022-07-22
8
+ ### Changed
9
+ - Fixed a bug where `DeclareSchema::Model::HabtmModelShim` `indexes` and `integer limits` were not being generated properly. Use `limit 8` for ids and primary composite key for habtm model.
10
+ - `index_definitions_with_primary_key`
11
+
7
12
  ## [1.0.2] - 2022-07-18
8
13
  ### Fixed
9
14
  - Fixed a bug where `SchemaChange::ColumnRemove` was not loaded and causing an exception to raise
@@ -224,6 +229,7 @@ using the appropriate Rails configuration attributes.
224
229
  ### Added
225
230
  - Initial version from https://github.com/Invoca/hobo_fields v4.1.0.
226
231
 
232
+ [1.1.0]: https://github.com/Invoca/declare_schema/compare/v1.0.2...v1.1.0
227
233
  [1.0.2]: https://github.com/Invoca/declare_schema/compare/v1.0.1...v1.0.2
228
234
  [1.0.1]: https://github.com/Invoca/declare_schema/compare/v1.0.0...v1.0.1
229
235
  [1.0.0]: https://github.com/Invoca/declare_schema/compare/v0.14.3...v1.0.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- declare_schema (1.0.2)
4
+ declare_schema (1.1.0)
5
5
  rails (>= 5.0)
6
6
 
7
7
  GEM
@@ -36,7 +36,7 @@ module DeclareSchema
36
36
  parent_table: parent_table,
37
37
  foreign_key: foreign_key
38
38
  }
39
- options[:dependent] = :delete if fkc['ON DELETE CASCADE']
39
+ options[:dependent] = :delete if fkc['ON DELETE CASCADE'] || model.is_a?(DeclareSchema::Model::HabtmModelShim)
40
40
 
41
41
  new(model, foreign_key, **options)
42
42
  end
@@ -37,9 +37,13 @@ module DeclareSchema
37
37
  join_table
38
38
  end
39
39
 
40
+ def index_name
41
+ "index_#{table_name}_on_#{foreign_keys.first}_#{foreign_keys.last}"
42
+ end
43
+
40
44
  def field_specs
41
45
  foreign_keys.each_with_index.each_with_object({}) do |(v, position), result|
42
- result[v] = ::DeclareSchema::Model::FieldSpec.new(self, v, :integer, position: position, null: false)
46
+ result[v] = ::DeclareSchema::Model::FieldSpec.new(self, v, :bigint, position: position, null: false)
43
47
  end
44
48
  end
45
49
 
@@ -53,7 +57,7 @@ module DeclareSchema
53
57
 
54
58
  def index_definitions_with_primary_key
55
59
  [
56
- IndexDefinition.new(self, foreign_keys, unique: true, name: ::DeclareSchema::Model::IndexDefinition::PRIMARY_KEY_NAME),
60
+ IndexDefinition.new(self, foreign_keys, unique: true, name: index_name), # creates a primary composite key on both foriegn keys
57
61
  IndexDefinition.new(self, foreign_keys.last) # not unique by itself; combines with primary key to be unique
58
62
  ]
59
63
  end
@@ -33,11 +33,14 @@ module DeclareSchema
33
33
 
34
34
  class << self
35
35
  # extract IndexSpecs from an existing table
36
- # always includes the PRIMARY KEY index
36
+ # always includes the PRIMARY KEY index for Model unless it is a HABTM Model.
37
37
  def for_model(model, old_table_name = nil)
38
38
  t = old_table_name || model.table_name
39
39
 
40
- primary_key_columns = Array(model.connection.primary_key(t)).presence or
40
+ habtm_model = model.is_a?(DeclareSchema::Model::HabtmModelShim)
41
+
42
+ primary_key_columns = Array(model.connection.primary_key(t)).presence
43
+ primary_key_columns || habtm_model or
41
44
  raise "could not find primary key for table #{t} in #{model.connection.columns(t).inspect}"
42
45
 
43
46
  primary_key_found = false
@@ -47,14 +50,14 @@ module DeclareSchema
47
50
  i.columns == primary_key_columns && i.unique or
48
51
  raise "primary key on #{t} was not unique on #{primary_key_columns} (was unique=#{i.unique} on #{i.columns})"
49
52
  primary_key_found = true
50
- elsif i.columns == primary_key_columns && i.unique
53
+ elsif i.columns == primary_key_columns && i.unique && !habtm_model
51
54
  # skip this primary key index since we'll create it below, with PRIMARY_KEY_NAME
52
55
  next
53
56
  end
54
57
  new(model, i.columns, name: i.name, unique: i.unique, where: i.where, table_name: old_table_name)
55
58
  end.compact
56
59
 
57
- if !primary_key_found
60
+ if !primary_key_found && !habtm_model
58
61
  index_definitions << new(model, primary_key_columns, name: PRIMARY_KEY_NAME, unique: true, where: nil, table_name: old_table_name)
59
62
  end
60
63
  index_definitions
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeclareSchema
4
- VERSION = "1.0.2"
4
+ VERSION = "1.1.0"
5
5
  end
@@ -102,14 +102,9 @@ RSpec.describe DeclareSchema::Model::HabtmModelShim do
102
102
  expect(result.size).to eq(2), result.inspect
103
103
 
104
104
  expect(result.first).to be_a(::DeclareSchema::Model::IndexDefinition)
105
- expect(result.first.name).to eq('PRIMARY')
105
+ expect(result.first.name).to eq('index_parent_1_parent_2_on_parent_1_id_parent_2_id')
106
106
  expect(result.first.fields).to eq(['parent_1_id', 'parent_2_id'])
107
107
  expect(result.first.unique).to be_truthy
108
-
109
- expect(result.last).to be_a(::DeclareSchema::Model::IndexDefinition)
110
- expect(result.last.name).to eq('on_parent_2_id')
111
- expect(result.last.unique).to be_falsey
112
- expect(result.last.fields).to eq(['parent_2_id'])
113
108
  end
114
109
  end
115
110
 
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: 1.0.2
4
+ version: 1.1.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: 2022-07-18 00:00:00.000000000 Z
11
+ date: 2022-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
141
  - !ruby/object:Gem::Version
142
142
  version: 1.3.6
143
143
  requirements: []
144
- rubygems_version: 3.1.6
144
+ rubygems_version: 3.3.11
145
145
  signing_key:
146
146
  specification_version: 4
147
147
  summary: Database schema declaration and migration generator for Rails