iry 0.6.0 → 0.7.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 592290e30f1db830217241ebde319fbf952ee459ac05c3bc1e87b4b47a73e143
4
- data.tar.gz: '09cabb7faf8e78a91e7d2443bdfd5466d7e8f63bc90fcfeb2abdb72f93bd79bf'
3
+ metadata.gz: 7ba546c69426b3b1d4c530d79ed5cfed56b5f3ea5dfe629a37b5061a90213b33
4
+ data.tar.gz: 65aa3e75c36b7fae90d81e20126c0bc134ff16ee7ddc009349754cd17cc14c29
5
5
  SHA512:
6
- metadata.gz: 915d87f39809daa1a0a76ea66c4718e20afe6dbef4ced1853df47e9457927a464a06204cd4c0abd71dad861d8df6f23c974b2a75796ff7136b17f94875fb36ae
7
- data.tar.gz: f8b10f525eab24f478a3bb6ee2c1039258b65d532eb5475d584f68bec23cf6c2e305e47033b4621dfe49be563da584c488683905d12c9abf529c07ec194d46bc
6
+ metadata.gz: ef79029f7a9f7a51dbb20964ad407ea146417a10de1c7c4d9149a9d83acece6e9db2abe2f3ca6779c684e31554bea9e22e6496579b5cc16eaf53ec787d6b4d17
7
+ data.tar.gz: a7daca1aca5c175224affa28884e83c19fcdfd4b01369c8b28f6cea2cfd5cea476fca77d433eb84fa53518a3812c03bc12448617bbd2a104b49dcaec1757cf44
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.7.0
data/db/schema.pgsql CHANGED
@@ -3,14 +3,27 @@ create extension if not exists "btree_gist";
3
3
 
4
4
  create table if not exists users (
5
5
  id uuid primary key default gen_random_uuid(),
6
- unique_text text unique not null default gen_random_uuid()::text check (unique_text != 'invalid'),
6
+ unique_text text not null default gen_random_uuid()::text,
7
7
  exclude_text text not null default gen_random_uuid()::text,
8
- user_id uuid references users (id),
9
- untracked_text text unique not null default gen_random_uuid()::text,
8
+ user_id uuid,
9
+ untracked_text text not null default gen_random_uuid()::text,
10
10
  free_text text not null default '',
11
- friend_user_id uuid references users (id),
11
+ friend_user_id uuid,
12
12
  created_at timestamp(6) not null,
13
13
  updated_at timestamp(6) not null,
14
14
  -- acts similar to unique constraint
15
15
  exclude using gist (exclude_text with =)
16
16
  );
17
+
18
+ create unique index if not exists
19
+ index_users_on_unique_text on users(unique_text);
20
+ create unique index if not exists
21
+ index_users_on_untracked_text on users(untracked_text);
22
+ alter table users
23
+ add constraint chk_rails_15df0d7772 check (unique_text != 'invalid');
24
+ alter table users
25
+ add constraint fk_rails_6d0b8b3c2f
26
+ foreign key (user_id) references users(id);
27
+ alter table users
28
+ add constraint fk_rails_d3f200176b
29
+ foreign key (friend_user_id) references users(id);
@@ -6,7 +6,13 @@ module Iry
6
6
  # @param table_name [String]
7
7
  # @return [String]
8
8
  def self.infer_name(key, table_name)
9
- "#{table_name}_#{key}_check"
9
+ # PostgreSQL convention:
10
+ # "#{table_name}_#{key}_check"
11
+ # Rails convention
12
+ id = "#{table_name}_#{key}_chk"
13
+ hashed_id = OpenSSL::Digest::SHA256.hexdigest(id)[0..9]
14
+
15
+ "chk_rails_#{hashed_id}"
10
16
  end
11
17
 
12
18
  # @return [Symbol]
@@ -6,7 +6,17 @@ module Iry
6
6
  # @param table_name [String]
7
7
  # @return [String]
8
8
  def self.infer_name(keys, table_name)
9
- "#{table_name}_#{keys.join("_")}_fkey"
9
+ if keys.size > 1
10
+ # PostgreSQL convention:
11
+ return "#{table_name}_#{keys.join("_")}_fkey"
12
+ end
13
+
14
+ # Rails convention:
15
+ column = keys.first
16
+ id = "#{table_name}_#{column}_fk"
17
+ hashed_id = OpenSSL::Digest::SHA256.hexdigest(id)[0..9]
18
+
19
+ "fk_rails_#{hashed_id}"
10
20
  end
11
21
 
12
22
  # @return [<Symbol>]
@@ -1,12 +1,32 @@
1
1
  module Iry
2
2
  module Constraint
3
3
  class Unique
4
+ MAX_INFER_NAME_BYTE_SIZE = 62
5
+
4
6
  # Infers the unique constraint name based on keys and table name
5
7
  # @param keys [<Symbol>]
6
8
  # @param table_name [String]
7
9
  # @return [String]
8
10
  def self.infer_name(keys, table_name)
9
- "#{table_name}_#{keys.join("_")}_key"
11
+ # PostgreSQL convention:
12
+ # "#{table_name}_#{keys.join("_")}_key"
13
+
14
+ # Rails convention:
15
+ # index_trip_hikers_on_trip_id_and_hiker_card_id
16
+ # index_TABLENAME_on_COLUMN1_and_COLUMN2
17
+ name = "index_#{table_name}_on_#{keys.join("_and_")}"
18
+ if name.bytesize <= MAX_INFER_NAME_BYTE_SIZE
19
+ return name
20
+ end
21
+
22
+ digest = OpenSSL::Digest::SHA256.hexdigest(name)[0..9]
23
+ hashed_id = "_#{digest}"
24
+ name = "idx_on_#{keys.join("_")}"
25
+
26
+ short_limit = max_index_name_size - hashed_identifier.bytesize
27
+ short_name = name.mb_chars.limit(short_limit).to_s
28
+
29
+ "#{short_name}#{hashed_identifier}"
10
30
  end
11
31
 
12
32
  # @return [<Symbol>]
data/lib/iry.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "active_record"
2
+ require "openssl"
2
3
 
3
4
  require_relative "iry/version"
4
5
  require_relative "iry/handlers"
data/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "iry",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "iry",
9
- "version": "0.6.0",
9
+ "version": "0.7.0",
10
10
  "license": "MIT",
11
11
  "devDependencies": {
12
12
  "conventional-changelog-cli": ">= 3.0.0",
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iry",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Transform database constraint errors into activerecord validation errors",
5
5
  "private": true,
6
6
  "main": "index.js",
data/rbi/iry.rbi CHANGED
@@ -426,6 +426,8 @@ module Iry
426
426
  end
427
427
 
428
428
  class Unique
429
+ MAX_INFER_NAME_BYTE_SIZE = T.let(62, T.untyped)
430
+
429
431
  # Infers the unique constraint name based on keys and table name
430
432
  #
431
433
  # _@param_ `keys`
data/sig/iry.rbs CHANGED
@@ -370,6 +370,8 @@ module Iry
370
370
  end
371
371
 
372
372
  class Unique
373
+ MAX_INFER_NAME_BYTE_SIZE: untyped
374
+
373
375
  # Infers the unique constraint name based on keys and table name
374
376
  #
375
377
  # _@param_ `keys`
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francesco Belladonna
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-13 00:00:00.000000000 Z
11
+ date: 2023-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord