declare_schema 1.3.0 → 1.3.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: 3d5e780d92cb93c57b69a6c7641f7dd20fc52114b0b25a2e47ad662039457290
4
+ data.tar.gz: a976df026f62e9a0512d71957898bddd077aa2ab8a63d63ddb8c114211df7060
5
5
  SHA512:
6
- metadata.gz: 3314c3544ffde59d7447dec0c64060355f4dd30ee659eae769029deb4f486df8347290c674ab368321a71be6072bada62a6217c60eaaade2780e2fda0c70e258
7
- data.tar.gz: 5f1e040b3f8edaee24375e79cba08b4da33df9ec400a884dfe8f4445485723110ea85763456708d7cd59f28060a332f6800795ebfae71ea3cd53cf03ddaf0934
6
+ metadata.gz: 9845c4bf47ac17b28b4b4b4eb6070cdaf6a66102bee5649af99ad8de8ed8a151eb76e5b49cbf9322997d7b1a62db3c3104eafc497ff7b81d7245825813c9bb48
7
+ data.tar.gz: 15eb4cc2bf1181bf8b2cd7152a203ceb6fc568920f0bf185a52dca4d5f65e1ead26ca29424c448834739b03fde489f74b7efe13ef01d9e643a6d0e4819691d52
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] - 2023-10-12
8
+ ### Fixed
9
+ - Fix bug in default index name 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)
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
@@ -64,17 +66,35 @@ module DeclareSchema
64
66
  break index_name
65
67
  end
66
68
  end or raise IndexNameTooLongError,
67
- "Index '#{index_name}' exceeds configured limit of #{DeclareSchema.max_index_and_constraint_name_length} characters."
69
+ "Default index name '#{index_name}' exceeds configured limit of #{DeclareSchema.max_index_and_constraint_name_length} characters. Use the `name:` option to give it a shorter name, or adjust DeclareSchema.max_index_and_constraint_name_length if you know your database can accept longer names."
68
70
  end
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"
5
5
  end
@@ -133,6 +133,47 @@ 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
+ 53 => 'user_domains_extra__last_name_first_name_middle_name',
160
+ }.each do |len, index_name|
161
+ context "with max_index_and_constraint_name_length of #{len}" do
162
+ let(:max_index_and_constraint_name_length) { len }
163
+
164
+ it { is_expected.to eq(index_name) }
165
+ end
166
+ end
167
+
168
+ context "with max_index_and_constraint_name_length shorter than columns suffix" do
169
+ let(:max_index_and_constraint_name_length) { 33 }
170
+
171
+ it 'raises' do
172
+ expect { subject }.to raise_exception(DeclareSchema::Model::IndexDefinition::IndexNameTooLongError,
173
+ /Default index name '__last_name_first_name_middle_name' exceeds configured limit of 33 characters\. Use the `name:` option to give it a shorter name, or adjust DeclareSchema\.max_index_and_constraint_name_length/i)
174
+ end
175
+ end
176
+ end
136
177
  end
137
178
  end
138
179
  # 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
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-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails