declare_schema 1.3.0 → 1.3.1.colin.1

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: 51aba0260c648b9baab8e50a93ff384d4e8b2f39d6dd9284f9fbd900773f9207
4
- data.tar.gz: 9cb9859880e62e726fc87b5e9d6893eabfc9ec102be35139a1a390fffba6a7ce
3
+ metadata.gz: a0f533d7dc8d88f7a84002c3f683cc4afa1a50af764fb6f478fb6d21f06d3d00
4
+ data.tar.gz: a033eb5411618bf7d0e1beb51176aa682a7362820df942df52f9d87818875237
5
5
  SHA512:
6
- metadata.gz: 3314c3544ffde59d7447dec0c64060355f4dd30ee659eae769029deb4f486df8347290c674ab368321a71be6072bada62a6217c60eaaade2780e2fda0c70e258
7
- data.tar.gz: 5f1e040b3f8edaee24375e79cba08b4da33df9ec400a884dfe8f4445485723110ea85763456708d7cd59f28060a332f6800795ebfae71ea3cd53cf03ddaf0934
6
+ metadata.gz: eb1c42caab31adbd01450a2c94c7a15d4c535bc1e8b89a50370f95796adad9bc21d297969c463febc77f7ba3f64ccd4a79cb68480bf02baff2f864302f435cd1
7
+ data.tar.gz: f2882b79e6eadd987d064e9adb742e5d2adea31c1de9a716fdd5a05a1c6a9e54491b8d5825ef9f310038e430ca2c54af08dad0c389202ff101a659065dc6b942
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.3.1] - Unreleased
8
+ ### Fixed
9
+ - Fix bug in default index anme when table name + __ + column suffix exceeds `DeclareSchema.max_index_and_constraint_name_length`.
10
+ In this case we truncate the table name and append part of its hash.
11
+
7
12
  ## [1.3.0] - 2023-07-10
8
13
  ### Added
9
14
  - Added `DeclareSchema.max_index_and_constraint_name_length` with default of 64.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- declare_schema (1.3.0)
4
+ declare_schema (1.3.1.colin.1)
5
5
  rails (>= 5.0)
6
6
 
7
7
  GEM
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'digest/sha2'
4
+
3
5
  module DeclareSchema
4
6
  module Model
5
7
  class IndexDefinition
@@ -69,12 +71,30 @@ module DeclareSchema
69
71
 
70
72
  private
71
73
 
74
+ SHA_SUFFIX_LENGTH = 4
75
+
76
+ def shorten_name(name, max_len)
77
+ if name.size <= max_len
78
+ name
79
+ else
80
+ name_prefix = name.first(max_len >= SHA_SUFFIX_LENGTH*2 ? (max_len - SHA_SUFFIX_LENGTH) : ((max_len + 1)/2))
81
+ sha = Digest::SHA256.hexdigest(name)
82
+ (name_prefix + sha).first(max_len)
83
+ end
84
+ end
85
+
72
86
  def long_index_name(table_name, columns)
73
87
  "index_#{table_name}_on_#{Array(columns).join("_and_")}"
74
88
  end
75
89
 
76
90
  def short_index_name(table_name, columns)
77
- "#{table_name}__#{Array(columns).join("_")}"
91
+ columns_suffix = "__" + Array(columns).join('_')
92
+ if DeclareSchema.max_index_and_constraint_name_length.nil?
93
+ table_name + columns_suffix
94
+ else
95
+ max_name_len = [DeclareSchema.max_index_and_constraint_name_length - columns_suffix.length, 0].max
96
+ shorten_name(table_name, max_name_len) + columns_suffix
97
+ end
78
98
  end
79
99
  end
80
100
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeclareSchema
4
- VERSION = "1.3.0"
4
+ VERSION = "1.3.1.colin.1"
5
5
  end
@@ -133,6 +133,46 @@ RSpec.describe DeclareSchema::Model::IndexDefinition do
133
133
 
134
134
  it { is_expected.to eq("users__last_name_first_name_middle_name") }
135
135
  end
136
+
137
+ context 'with long table name' do
138
+ let(:table_name2) { 'user_domains_extra' }
139
+ {
140
+ 34 => '__last_name_first_name_middle_name',
141
+ 35 => 'u__last_name_first_name_middle_name',
142
+ 36 => 'u4__last_name_first_name_middle_name',
143
+ 37 => 'us4__last_name_first_name_middle_name',
144
+ 38 => 'us48__last_name_first_name_middle_name',
145
+ 39 => 'use48__last_name_first_name_middle_name',
146
+ 40 => 'use481__last_name_first_name_middle_name',
147
+ 41 => 'user481__last_name_first_name_middle_name',
148
+ 42 => 'user4814__last_name_first_name_middle_name',
149
+ 43 => 'user_4814__last_name_first_name_middle_name',
150
+ 44 => 'user_d4814__last_name_first_name_middle_name',
151
+ 45 => 'user_do4814__last_name_first_name_middle_name',
152
+ 46 => 'user_dom4814__last_name_first_name_middle_name',
153
+ 47 => 'user_doma4814__last_name_first_name_middle_name',
154
+ 48 => 'user_domai4814__last_name_first_name_middle_name',
155
+ 49 => 'user_domain4814__last_name_first_name_middle_name',
156
+ 50 => 'user_domains4814__last_name_first_name_middle_name',
157
+ 51 => 'user_domains_4814__last_name_first_name_middle_name',
158
+ 52 => 'user_domains_extra__last_name_first_name_middle_name',
159
+ }.each do |len, index_name|
160
+ context "with max_index_and_constraint_name_length of #{len}" do
161
+ let(:max_index_and_constraint_name_length) { len }
162
+
163
+ it { is_expected.to eq(index_name) }
164
+ end
165
+ end
166
+
167
+ context "with max_index_and_constraint_name_length shorter than columns suffix" do
168
+ let(:max_index_and_constraint_name_length) { 33 }
169
+
170
+ it 'raises' do
171
+ expect { subject }.to raise_exception(DeclareSchema::Model::IndexDefinition::IndexNameTooLongError,
172
+ /Index '__last_name_first_name_middle_name' exceeds configured limit of 33 characters/)
173
+ end
174
+ end
175
+ end
136
176
  end
137
177
  end
138
178
  # TODO: fill out remaining tests
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.3.0
4
+ version: 1.3.1.colin.1
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: 2023-07-11 00:00:00.000000000 Z
11
+ date: 2023-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails