ulid-rails 0.2.0 → 0.3.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: 4d66abad8321b174f9037f061da19837f539c4d42d01d328f267710023a7fc1d
4
- data.tar.gz: df85e60420b30edf90ad5790cf1c70c338950e8b062363c10c29309cff03ecfb
3
+ metadata.gz: 3c37eb5b4b0722d0858a1c08682e89a8285350ab1d151bc90bbb6aaf1c6cff5d
4
+ data.tar.gz: 9bd6b0bff5eb157b998f04b26b7eb75e3b53d90c4820e2dcbc9ede7c8a1246b8
5
5
  SHA512:
6
- metadata.gz: ab2ffb0c44b887852eb56a6fe0b4b93f9881e06f6a8ce6fe6322f28debb90a1b922d7aaff15ecc4296171addd7e73f34c094d7eefde0d451d5554011e821e7e6
7
- data.tar.gz: c32c054dd8f14849f8b96d8ed0b6f83eb6be4006bf2c4c87cf81da381ae902f2a9e36d638a1bb317630c29f853b2aa5ad355e77fdb78de3793a25ed80f12b6d7
6
+ metadata.gz: ab1f5cf9796884e8845a67bf13a3e6b69f2e3d48c2b21c23ff5d0f1836fdbd6da1850dcb9292be74cad38476f4df3f4cd46306d66d2bdc203c7da50c24861ce2
7
+ data.tar.gz: f8300abe5bd288884ae1d8a24a12f08e3b6c248c144cc68f564fe567ff7c7fa213ce9a3110a1b5125a9ec9a38220afa139938cb12ce0b779532b613c6390e9a4
@@ -1,4 +1,9 @@
1
1
  # ulid-rails CHANGELOG
2
+
3
+ ## 0.3
4
+
5
+ - Support PostgresQL
6
+
2
7
  ## 0.2
3
8
 
4
9
  ### Breaking Changes
data/README.md CHANGED
@@ -22,7 +22,7 @@ Or install it yourself as:
22
22
 
23
23
  ### Migrations
24
24
 
25
- If you use MySQL, specify `id: false` to `create_table` and add `id` column as 16-byte binary type.
25
+ Specify `id: false` to `create_table` and add `id` column as 16-byte binary type.
26
26
 
27
27
  ```ruby
28
28
  def change
@@ -33,16 +33,6 @@ If you use MySQL, specify `id: false` to `create_table` and add `id` column as 1
33
33
  end
34
34
  ```
35
35
 
36
- If you use PostgreSQL, just specify `id: :uuid` to `create_table`
37
-
38
- ```ruby
39
- def change
40
- create_table :users, id: :uuid do |t|
41
- # ...
42
- end
43
- end
44
- ```
45
-
46
36
 
47
37
  ### Model Changes
48
38
 
@@ -103,8 +93,6 @@ class Model < ApplicationRecord
103
93
  end
104
94
  ```
105
95
 
106
- ## FAQ
107
-
108
96
  ### Foreign Keys
109
97
 
110
98
  You need to specicfy `type` option
@@ -116,6 +104,16 @@ You need to specicfy `type` option
116
104
  end
117
105
  ```
118
106
 
107
+ ## Development
108
+
109
+ ### Run tests
110
+
111
+ Just run the below command to test with all supported DB engines.
112
+
113
+ ```
114
+ $ docker-compose run test
115
+ ```
116
+
119
117
  ## License
120
118
 
121
119
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,23 @@
1
+ #!/bin/bash
2
+
3
+ test_with_db() {
4
+ echo "Testing with $1"
5
+ DB=$1 bundle exec rake
6
+ }
7
+
8
+ test_with_db "sqlite3"
9
+
10
+ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
11
+
12
+ # Most features should work with MySQL 5.6 but I couldn't properly set it up in test.
13
+ # $DIR/wait-for-it.sh mysql56:3306
14
+ # test_with_db "mysql56"
15
+
16
+ $DIR/wait-for-it.sh mysql57:3306
17
+ test_with_db "mysql57"
18
+
19
+ $DIR/wait-for-it.sh mysql80:3306
20
+ test_with_db "mysql80"
21
+
22
+ $DIR/wait-for-it.sh pg12:5432
23
+ test_with_db "pg12"
@@ -0,0 +1,178 @@
1
+ #!/usr/bin/env bash
2
+ # Use this script to test if a given TCP host/port are available
3
+
4
+ WAITFORIT_cmdname=${0##*/}
5
+
6
+ echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }
7
+
8
+ usage()
9
+ {
10
+ cat << USAGE >&2
11
+ Usage:
12
+ $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args]
13
+ -h HOST | --host=HOST Host or IP under test
14
+ -p PORT | --port=PORT TCP port under test
15
+ Alternatively, you specify the host and port as host:port
16
+ -s | --strict Only execute subcommand if the test succeeds
17
+ -q | --quiet Don't output any status messages
18
+ -t TIMEOUT | --timeout=TIMEOUT
19
+ Timeout in seconds, zero for no timeout
20
+ -- COMMAND ARGS Execute command with args after the test finishes
21
+ USAGE
22
+ exit 1
23
+ }
24
+
25
+ wait_for()
26
+ {
27
+ if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
28
+ echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
29
+ else
30
+ echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout"
31
+ fi
32
+ WAITFORIT_start_ts=$(date +%s)
33
+ while :
34
+ do
35
+ if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then
36
+ nc -z $WAITFORIT_HOST $WAITFORIT_PORT
37
+ WAITFORIT_result=$?
38
+ else
39
+ (echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1
40
+ WAITFORIT_result=$?
41
+ fi
42
+ if [[ $WAITFORIT_result -eq 0 ]]; then
43
+ WAITFORIT_end_ts=$(date +%s)
44
+ echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds"
45
+ break
46
+ fi
47
+ sleep 1
48
+ done
49
+ return $WAITFORIT_result
50
+ }
51
+
52
+ wait_for_wrapper()
53
+ {
54
+ # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
55
+ if [[ $WAITFORIT_QUIET -eq 1 ]]; then
56
+ timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
57
+ else
58
+ timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
59
+ fi
60
+ WAITFORIT_PID=$!
61
+ trap "kill -INT -$WAITFORIT_PID" INT
62
+ wait $WAITFORIT_PID
63
+ WAITFORIT_RESULT=$?
64
+ if [[ $WAITFORIT_RESULT -ne 0 ]]; then
65
+ echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
66
+ fi
67
+ return $WAITFORIT_RESULT
68
+ }
69
+
70
+ # process arguments
71
+ while [[ $# -gt 0 ]]
72
+ do
73
+ case "$1" in
74
+ *:* )
75
+ WAITFORIT_hostport=(${1//:/ })
76
+ WAITFORIT_HOST=${WAITFORIT_hostport[0]}
77
+ WAITFORIT_PORT=${WAITFORIT_hostport[1]}
78
+ shift 1
79
+ ;;
80
+ --child)
81
+ WAITFORIT_CHILD=1
82
+ shift 1
83
+ ;;
84
+ -q | --quiet)
85
+ WAITFORIT_QUIET=1
86
+ shift 1
87
+ ;;
88
+ -s | --strict)
89
+ WAITFORIT_STRICT=1
90
+ shift 1
91
+ ;;
92
+ -h)
93
+ WAITFORIT_HOST="$2"
94
+ if [[ $WAITFORIT_HOST == "" ]]; then break; fi
95
+ shift 2
96
+ ;;
97
+ --host=*)
98
+ WAITFORIT_HOST="${1#*=}"
99
+ shift 1
100
+ ;;
101
+ -p)
102
+ WAITFORIT_PORT="$2"
103
+ if [[ $WAITFORIT_PORT == "" ]]; then break; fi
104
+ shift 2
105
+ ;;
106
+ --port=*)
107
+ WAITFORIT_PORT="${1#*=}"
108
+ shift 1
109
+ ;;
110
+ -t)
111
+ WAITFORIT_TIMEOUT="$2"
112
+ if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi
113
+ shift 2
114
+ ;;
115
+ --timeout=*)
116
+ WAITFORIT_TIMEOUT="${1#*=}"
117
+ shift 1
118
+ ;;
119
+ --)
120
+ shift
121
+ WAITFORIT_CLI=("$@")
122
+ break
123
+ ;;
124
+ --help)
125
+ usage
126
+ ;;
127
+ *)
128
+ echoerr "Unknown argument: $1"
129
+ usage
130
+ ;;
131
+ esac
132
+ done
133
+
134
+ if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then
135
+ echoerr "Error: you need to provide a host and port to test."
136
+ usage
137
+ fi
138
+
139
+ WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15}
140
+ WAITFORIT_STRICT=${WAITFORIT_STRICT:-0}
141
+ WAITFORIT_CHILD=${WAITFORIT_CHILD:-0}
142
+ WAITFORIT_QUIET=${WAITFORIT_QUIET:-0}
143
+
144
+ # check to see if timeout is from busybox?
145
+ WAITFORIT_TIMEOUT_PATH=$(type -p timeout)
146
+ WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH)
147
+ if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then
148
+ WAITFORIT_ISBUSY=1
149
+ WAITFORIT_BUSYTIMEFLAG="-t"
150
+
151
+ else
152
+ WAITFORIT_ISBUSY=0
153
+ WAITFORIT_BUSYTIMEFLAG=""
154
+ fi
155
+
156
+ if [[ $WAITFORIT_CHILD -gt 0 ]]; then
157
+ wait_for
158
+ WAITFORIT_RESULT=$?
159
+ exit $WAITFORIT_RESULT
160
+ else
161
+ if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
162
+ wait_for_wrapper
163
+ WAITFORIT_RESULT=$?
164
+ else
165
+ wait_for
166
+ WAITFORIT_RESULT=$?
167
+ fi
168
+ fi
169
+
170
+ if [[ $WAITFORIT_CLI != "" ]]; then
171
+ if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then
172
+ echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess"
173
+ exit $WAITFORIT_RESULT
174
+ fi
175
+ exec "${WAITFORIT_CLI[@]}"
176
+ else
177
+ exit $WAITFORIT_RESULT
178
+ fi
@@ -0,0 +1,44 @@
1
+ version: '3.6'
2
+ services:
3
+ test:
4
+ image: ruby:2.6
5
+ command: sh -c "rm Gemfile.lock && bundle install && bin/run_tests"
6
+ depends_on:
7
+ - mysql56
8
+ - mysql57
9
+ - mysql80
10
+ - pg12
11
+ working_dir: /app
12
+ volumes:
13
+ - bundle:/usr/local/bundle
14
+ - .:/app
15
+
16
+ mysql56:
17
+ image: mysql:5.6
18
+ environment:
19
+ MYSQL_ROOT_PASSWORD: password
20
+ command: --innodb-large-prefix --innodb-file-format=barracuda
21
+ healthcheck:
22
+ test: mysql --password=password -e "show databases;"
23
+ mysql57:
24
+ image: mysql:5.7
25
+ environment:
26
+ MYSQL_ROOT_PASSWORD: password
27
+ healthcheck:
28
+ test: mysql --password=password -e "show databases;"
29
+ mysql80:
30
+ image: mysql:8.0
31
+ command: --default-authentication-plugin=mysql_native_password
32
+ environment:
33
+ MYSQL_ROOT_PASSWORD: password
34
+ healthcheck:
35
+ test: mysql --password=password -e "show databases;"
36
+ pg12:
37
+ image: postgres:12
38
+ environment:
39
+ PGDATA: /data
40
+ POSTGRES_DB: db
41
+ healthcheck:
42
+ test: echo "\\l" | psql -U postgre
43
+ volumes:
44
+ bundle:
@@ -1,4 +1,6 @@
1
1
  gem "activesupport", "~> 5.2"
2
2
  gem "activemodel", "~> 5.2"
3
3
  gem "activerecord", "~> 5.2"
4
- gem 'sqlite3', '~> 1.3.6'
4
+ gem 'sqlite3', '~> 1.3.6'
5
+ gem 'mysql2'
6
+ gem 'pg'
@@ -1,4 +1,6 @@
1
1
  gem "activesupport", "~> 6.0"
2
2
  gem "activemodel", "~> 6.0"
3
3
  gem "activerecord", "~> 6.0"
4
- gem 'sqlite3', '~> 1.4.1'
4
+ gem 'sqlite3', '~> 1.4.1'
5
+ gem 'mysql2'
6
+ gem 'pg'
@@ -4,7 +4,8 @@ module ULID
4
4
  module Rails
5
5
  module Formatter
6
6
  def self.format(v)
7
- v.length == 32 ? Base32::Crockford.encode(v.hex).rjust(26, "0") : v
7
+ sanitized = v.delete('-').hex
8
+ Base32::Crockford.encode(sanitized).rjust(26, "0")
8
9
  end
9
10
 
10
11
  def self.unformat(v)
@@ -14,20 +14,24 @@ module ULID
14
14
  end
15
15
 
16
16
  def cast(value)
17
- if value.is_a?(Data)
18
- @formatter.format(value.to_s)
19
- elsif value&.encoding == Encoding::ASCII_8BIT
20
- @formatter.format(value.unpack("H*")[0])
21
- elsif value&.length == 32
22
- @formatter.format(value)
23
- else
24
- super
25
- end
17
+ return nil if value.nil?
18
+
19
+ value = value.to_s if value.is_a?(Data)
20
+ value = value.unpack("H*")[0] if value.encoding == Encoding::ASCII_8BIT
21
+ value = value[2..] if value.start_with?("\\x")
22
+
23
+ value.length == 32 ? @formatter.format(value) : super
26
24
  end
27
25
 
28
26
  def serialize(value)
29
27
  return if value.nil?
30
- Data.new(@formatter.unformat(value))
28
+
29
+ case ActiveRecord::Base.connection_config[:adapter]
30
+ when "mysql2", "sqlite3"
31
+ Data.new(@formatter.unformat(value))
32
+ when "postgresql"
33
+ Data.new([@formatter.unformat(value)].pack("H*"))
34
+ end
31
35
  end
32
36
  end
33
37
  end
@@ -1,5 +1,5 @@
1
1
  module ULID
2
2
  module Rails
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ulid-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kazunori Kajihiro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-21 00:00:00.000000000 Z
11
+ date: 2019-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ulid
@@ -137,7 +137,10 @@ files:
137
137
  - README.md
138
138
  - Rakefile
139
139
  - bin/console
140
+ - bin/run_tests
140
141
  - bin/setup
142
+ - bin/wait-for-it.sh
143
+ - docker-compose.yml
141
144
  - gemfiles/5.2.gemfile
142
145
  - gemfiles/6.0.gemfile
143
146
  - lib/ulid/rails.rb