activerecord-sqlite-types 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 67e16d34c7465c93819920e6b15f257bbfed0cb3b4685ca48fdecf003884cceb
4
+ data.tar.gz: 144d483a737fbdcac6227429a09be49843609ac2cbb6b62e4ecfe6d28a0c53cd
5
+ SHA512:
6
+ metadata.gz: 1b21b7c5389a14ab97f7ba46663629445568f2609bfae034ba3e614e3151252f350e41a9dadf6dfdaed8406fab310587842204cc6e2531b79dc40433f38a9397
7
+ data.tar.gz: b3e3b9e618df000b8ad249fb8d46662d601177943f08d55d8d8e9d25ddc68fefe757fa7d976eea748c795bb07d189198b4443d39c3b18f08783b0df9b1a8d957
data/.envrc ADDED
@@ -0,0 +1,5 @@
1
+ export DIRENV_WARN_TIMEOUT=20s
2
+
3
+ eval "$(devenv direnvrc)"
4
+
5
+ use devenv
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-10-21
4
+
5
+ ### Added
6
+
7
+ - Initial release
8
+ - `IpAddressType` for PostgreSQL `inet` type compatibility
9
+ - `ArrayType` for PostgreSQL array type compatibility with JSON storage
10
+ - `IntervalStringType` for PostgreSQL `interval` type compatibility
11
+ - Support for Rails 7.0+
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Wojtek Wrona
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,146 @@
1
+ # ActiveRecord SQLite Types
2
+
3
+ Custom ActiveRecord types for migrating from PostgreSQL to SQLite in Rails applications.
4
+
5
+ This gem provides drop-in replacements for PostgreSQL-specific data types that don't exist natively in SQLite, allowing you to migrate your Rails application from PostgreSQL to SQLite without changing your application code.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem "activerecord-sqlite-types"
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```bash
18
+ bundle install
19
+ ```
20
+
21
+ ## Available Types
22
+
23
+ ### IpAddress
24
+
25
+ Replaces PostgreSQL's `inet` type with a string representation that preserves IP address functionality.
26
+
27
+ **Usage:**
28
+
29
+ ```ruby
30
+ class User < ApplicationRecord
31
+ attribute :current_sign_in_ip, SQLiteTypes::IpAddress.new
32
+ attribute :last_sign_in_ip, SQLiteTypes::IpAddress.new
33
+ end
34
+ ```
35
+
36
+ **Migration:**
37
+
38
+ ```ruby
39
+ class MigrateInetToString < ActiveRecord::Migration[7.0]
40
+ def up
41
+ change_column :users, :current_sign_in_ip, :string
42
+ change_column :users, :last_sign_in_ip, :string
43
+ end
44
+
45
+ def down
46
+ change_column :users, :current_sign_in_ip, :inet, using: 'current_sign_in_ip::inet'
47
+ change_column :users, :last_sign_in_ip, :inet, using: 'last_sign_in_ip::inet'
48
+ end
49
+ end
50
+ ```
51
+
52
+ ### Array
53
+
54
+ Replaces PostgreSQL arrays with JSON-backed arrays, supporting querying via SQLite's JSON functions.
55
+
56
+ **Supported subtypes:** `:integer`, `:string`, `:hash`, `:datetime`
57
+
58
+ **Usage:**
59
+
60
+ ```ruby
61
+ class User < ApplicationRecord
62
+ attribute :personality_traits, SQLiteTypes::Array.new(:string)
63
+ attribute :favorite_numbers, SQLiteTypes::Array.new(:integer)
64
+ attribute :nested_data, SQLiteTypes::Array.new(:integer, nested: true)
65
+ end
66
+ ```
67
+
68
+ **Migration:**
69
+
70
+ ```ruby
71
+ class MigrateArrayToJson < ActiveRecord::Migration[7.0]
72
+ def up
73
+ change_column :users, :personality_traits, :json
74
+ change_column :users, :favorite_numbers, :json
75
+ end
76
+
77
+ def down
78
+ change_column :users, :personality_traits, :text, array: true, default: [], using: 'personality_traits::text[]'
79
+ change_column :users, :favorite_numbers, :integer, array: true, default: [], using: 'favorite_numbers::integer[]'
80
+ end
81
+ end
82
+ ```
83
+
84
+ **Querying arrays:**
85
+
86
+ For array querying functionality, see [Stephen Margheim's article on enhancing Rails SQLite array columns](https://fractaledmind.github.io/2023/09/12/enhancing-rails-sqlite-array-columns/).
87
+
88
+ ### Interval
89
+
90
+ Replaces PostgreSQL's `interval` type with ISO8601 duration strings.
91
+
92
+ **Usage:**
93
+
94
+ ```ruby
95
+ class Event < ApplicationRecord
96
+ attribute :duration, SQLiteTypes::Interval.new
97
+ end
98
+ ```
99
+
100
+ **Migration:**
101
+
102
+ ```ruby
103
+ class MigrateIntervalToString < ActiveRecord::Migration[7.0]
104
+ def up
105
+ change_column :events, :duration, :string
106
+ end
107
+
108
+ def down
109
+ change_column :events, :duration, :interval, using: 'duration::interval'
110
+ end
111
+ end
112
+ ```
113
+
114
+ ## Migration Strategy
115
+
116
+ The recommended approach for migrating from PostgreSQL to SQLite is incremental and reversible:
117
+
118
+ 1. **Prepare while still on PostgreSQL:**
119
+ - Add custom type declarations to your models
120
+ - Run migrations to change column types (e.g., `inet` → `string`)
121
+ - Test thoroughly - migrations are reversible!
122
+
123
+ 2. **Switch to SQLite:**
124
+ - Update `database.yml` to point to SQLite
125
+ - Run your data migration script (e.g., [pg-to-sqlite](https://github.com/hirefrank/pg-to-sqlite))
126
+ - Run your test suite
127
+
128
+ 3. **Handle database constraints:**
129
+ - Drop PostgreSQL-specific constraints before switching
130
+ - Add SQLite-compatible constraints after switching
131
+
132
+ For a detailed migration guide, see [this presentation on migrating from PostgreSQL to SQLite](https://gist.github.com/wojtodzio/538de01f6ba24665fa66d204824ca718).
133
+
134
+ ## Development
135
+
136
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
137
+
138
+ To install this gem onto your local machine, run `bundle exec rake install`.
139
+
140
+ ## Contributing
141
+
142
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wojtodzio/activerecord-sqlite-types.
143
+
144
+ ## License
145
+
146
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[test standard]
data/devenv.lock ADDED
@@ -0,0 +1,171 @@
1
+ {
2
+ "nodes": {
3
+ "devenv": {
4
+ "locked": {
5
+ "dir": "src/modules",
6
+ "lastModified": 1761156818,
7
+ "owner": "cachix",
8
+ "repo": "devenv",
9
+ "rev": "949fc6dc8f36f38e1cceb1bf1673c4e995a6a766",
10
+ "type": "github"
11
+ },
12
+ "original": {
13
+ "dir": "src/modules",
14
+ "owner": "cachix",
15
+ "repo": "devenv",
16
+ "type": "github"
17
+ }
18
+ },
19
+ "flake-compat": {
20
+ "flake": false,
21
+ "locked": {
22
+ "lastModified": 1747046372,
23
+ "owner": "edolstra",
24
+ "repo": "flake-compat",
25
+ "rev": "9100a0f413b0c601e0533d1d94ffd501ce2e7885",
26
+ "type": "github"
27
+ },
28
+ "original": {
29
+ "owner": "edolstra",
30
+ "repo": "flake-compat",
31
+ "type": "github"
32
+ }
33
+ },
34
+ "flake-compat_2": {
35
+ "flake": false,
36
+ "locked": {
37
+ "lastModified": 1747046372,
38
+ "owner": "edolstra",
39
+ "repo": "flake-compat",
40
+ "rev": "9100a0f413b0c601e0533d1d94ffd501ce2e7885",
41
+ "type": "github"
42
+ },
43
+ "original": {
44
+ "owner": "edolstra",
45
+ "repo": "flake-compat",
46
+ "type": "github"
47
+ }
48
+ },
49
+ "flake-utils": {
50
+ "inputs": {
51
+ "systems": "systems"
52
+ },
53
+ "locked": {
54
+ "lastModified": 1731533236,
55
+ "owner": "numtide",
56
+ "repo": "flake-utils",
57
+ "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
58
+ "type": "github"
59
+ },
60
+ "original": {
61
+ "owner": "numtide",
62
+ "repo": "flake-utils",
63
+ "type": "github"
64
+ }
65
+ },
66
+ "git-hooks": {
67
+ "inputs": {
68
+ "flake-compat": "flake-compat",
69
+ "gitignore": "gitignore",
70
+ "nixpkgs": [
71
+ "nixpkgs"
72
+ ]
73
+ },
74
+ "locked": {
75
+ "lastModified": 1760663237,
76
+ "owner": "cachix",
77
+ "repo": "git-hooks.nix",
78
+ "rev": "ca5b894d3e3e151ffc1db040b6ce4dcc75d31c37",
79
+ "type": "github"
80
+ },
81
+ "original": {
82
+ "owner": "cachix",
83
+ "repo": "git-hooks.nix",
84
+ "type": "github"
85
+ }
86
+ },
87
+ "gitignore": {
88
+ "inputs": {
89
+ "nixpkgs": [
90
+ "git-hooks",
91
+ "nixpkgs"
92
+ ]
93
+ },
94
+ "locked": {
95
+ "lastModified": 1709087332,
96
+ "owner": "hercules-ci",
97
+ "repo": "gitignore.nix",
98
+ "rev": "637db329424fd7e46cf4185293b9cc8c88c95394",
99
+ "type": "github"
100
+ },
101
+ "original": {
102
+ "owner": "hercules-ci",
103
+ "repo": "gitignore.nix",
104
+ "type": "github"
105
+ }
106
+ },
107
+ "nixpkgs": {
108
+ "locked": {
109
+ "lastModified": 1758532697,
110
+ "owner": "cachix",
111
+ "repo": "devenv-nixpkgs",
112
+ "rev": "207a4cb0e1253c7658c6736becc6eb9cace1f25f",
113
+ "type": "github"
114
+ },
115
+ "original": {
116
+ "owner": "cachix",
117
+ "ref": "rolling",
118
+ "repo": "devenv-nixpkgs",
119
+ "type": "github"
120
+ }
121
+ },
122
+ "nixpkgs-ruby": {
123
+ "inputs": {
124
+ "flake-compat": "flake-compat_2",
125
+ "flake-utils": "flake-utils",
126
+ "nixpkgs": [
127
+ "nixpkgs"
128
+ ]
129
+ },
130
+ "locked": {
131
+ "lastModified": 1759902829,
132
+ "owner": "bobvanderlinden",
133
+ "repo": "nixpkgs-ruby",
134
+ "rev": "5fba6c022a63f1e76dee4da71edddad8959f088a",
135
+ "type": "github"
136
+ },
137
+ "original": {
138
+ "owner": "bobvanderlinden",
139
+ "repo": "nixpkgs-ruby",
140
+ "type": "github"
141
+ }
142
+ },
143
+ "root": {
144
+ "inputs": {
145
+ "devenv": "devenv",
146
+ "git-hooks": "git-hooks",
147
+ "nixpkgs": "nixpkgs",
148
+ "nixpkgs-ruby": "nixpkgs-ruby",
149
+ "pre-commit-hooks": [
150
+ "git-hooks"
151
+ ]
152
+ }
153
+ },
154
+ "systems": {
155
+ "locked": {
156
+ "lastModified": 1681028828,
157
+ "owner": "nix-systems",
158
+ "repo": "default",
159
+ "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
160
+ "type": "github"
161
+ },
162
+ "original": {
163
+ "owner": "nix-systems",
164
+ "repo": "default",
165
+ "type": "github"
166
+ }
167
+ }
168
+ },
169
+ "root": "root",
170
+ "version": 7
171
+ }
data/devenv.nix ADDED
@@ -0,0 +1,14 @@
1
+ { pkgs, ... }:
2
+
3
+ {
4
+ languages = {
5
+ ruby = {
6
+ version = "3.1";
7
+ enable = true;
8
+ };
9
+ };
10
+
11
+ packages = with pkgs; [
12
+ (sqlite.override { interactive = true; })
13
+ ];
14
+ }
data/devenv.yaml ADDED
@@ -0,0 +1,8 @@
1
+ inputs:
2
+ nixpkgs:
3
+ url: github:cachix/devenv-nixpkgs/rolling
4
+ nixpkgs-ruby:
5
+ url: github:bobvanderlinden/nixpkgs-ruby
6
+ inputs:
7
+ nixpkgs:
8
+ follows: nixpkgs
@@ -0,0 +1,8 @@
1
+ require "active_record"
2
+ require_relative "sqlite_types/version"
3
+ require_relative "sqlite_types/ip_address"
4
+ require_relative "sqlite_types/array"
5
+ require_relative "sqlite_types/interval"
6
+
7
+ module SQLiteTypes
8
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLiteTypes
4
+ class Array < ActiveRecord::Type::Json
5
+ SUPPORTED_SUBTYPES = %i[integer string hash datetime].freeze
6
+
7
+ def initialize(subtype, nested: false)
8
+ raise ArgumentError, "Unsupported subtype: #{subtype}" unless SUPPORTED_SUBTYPES.include?(subtype)
9
+
10
+ @subtype = subtype
11
+ @nested = nested
12
+ super()
13
+ end
14
+
15
+ def deserialize(value)
16
+ return if value.nil?
17
+
18
+ array = parse_to_array(value)
19
+
20
+ if @nested
21
+ array.map { |nested_array| parse_to_array(nested_array).map { |element| cast_element(element) } }
22
+ else
23
+ array.map { |element| cast_element(element) }
24
+ end
25
+ end
26
+
27
+ def serialize(value)
28
+ raise ArgumentError, "Invalid value: #{value}" if !valid?(value)
29
+
30
+ super
31
+ end
32
+
33
+ def force_equality?(value)
34
+ value.is_a?(::Array)
35
+ end
36
+
37
+ private
38
+
39
+ def valid?(value)
40
+ return true if value.nil?
41
+ return false if !value.is_a?(Array)
42
+ return value.all? { |nested_array| nested_array.is_a?(Array) } if @nested
43
+
44
+ true
45
+ end
46
+
47
+ def parse_to_array(value)
48
+ case value
49
+ when String
50
+ begin
51
+ parsed = ActiveSupport::JSON.decode(value)
52
+ parsed.is_a?(Array) ? parsed : nil
53
+ rescue JSON::ParserError
54
+ nil
55
+ end
56
+ when Array
57
+ value
58
+ end
59
+ end
60
+
61
+ def cast_element(elem)
62
+ case @subtype
63
+ when :integer
64
+ elem.to_i
65
+ when :string
66
+ elem.to_s
67
+ when :hash
68
+ elem.to_h
69
+ when :datetime
70
+ elem.is_a?(String) ? DateTime.parse(elem).in_time_zone : elem
71
+ else
72
+ raise ArgumentError, "Unsupported subtype: #{@subtype}"
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLiteTypes
4
+ class Interval < ActiveRecord::Type::Value
5
+ def serialize(value)
6
+ case value
7
+ when ::ActiveSupport::Duration
8
+ value.iso8601(precision: precision)
9
+ when ::Numeric
10
+ ActiveSupport::Duration.build(value).iso8601(precision: precision)
11
+ else
12
+ super
13
+ end
14
+ end
15
+
16
+ def type_cast_for_schema(value)
17
+ serialize(value).inspect
18
+ end
19
+
20
+ private
21
+
22
+ def cast_value(value)
23
+ case value
24
+ when ::ActiveSupport::Duration
25
+ value
26
+ when ::String
27
+ begin
28
+ ::ActiveSupport::Duration.parse(value)
29
+ rescue ::ActiveSupport::Duration::ISO8601Parser::ParsingError
30
+ nil
31
+ end
32
+ else
33
+ super
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLiteTypes
4
+ class IpAddress < ActiveRecord::Type::String
5
+ def serialize(value)
6
+ return if value.nil?
7
+
8
+ case value
9
+ when IPAddr
10
+ "#{value}/#{value.prefix}"
11
+ when String
12
+ ip_addr = IPAddr.new(value)
13
+ "#{ip_addr}/#{ip_addr.prefix}"
14
+ else
15
+ raise ArgumentError, "Invalid IP address: #{value}"
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def cast_value(value)
22
+ case value
23
+ when IPAddr
24
+ value
25
+ when String
26
+ IPAddr.new(value)
27
+ else
28
+ raise ArgumentError, "Invalid IP address: #{value}"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module SQLiteTypes
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ module SQLiteTypes
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-sqlite-types
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Wojtek Wrona
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 1980-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '7.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '7.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ description: Provides custom ActiveRecord types to handle PostgreSQL-specific data
42
+ types (inet, interval, arrays) when migrating to SQLite in Rails applications.
43
+ email:
44
+ - wojtodzio@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".envrc"
50
+ - ".standard.yml"
51
+ - CHANGELOG.md
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - devenv.lock
56
+ - devenv.nix
57
+ - devenv.yaml
58
+ - lib/activerecord-sqlite-types.rb
59
+ - lib/sqlite_types/array.rb
60
+ - lib/sqlite_types/interval.rb
61
+ - lib/sqlite_types/ip_address.rb
62
+ - lib/sqlite_types/version.rb
63
+ - sig/sqlite_types.rbs
64
+ homepage: https://github.com/wojtodzio/activerecord-sqlite-types
65
+ licenses:
66
+ - MIT
67
+ metadata:
68
+ allowed_push_host: https://rubygems.org
69
+ homepage_uri: https://github.com/wojtodzio/activerecord-sqlite-types
70
+ source_code_uri: https://github.com/wojtodzio/activerecord-sqlite-types
71
+ changelog_uri: https://github.com/wojtodzio/activerecord-sqlite-types/blob/main/CHANGELOG.md
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 3.1.0
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.3.27
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Custom ActiveRecord types for SQLite migrations from PostgreSQL
91
+ test_files: []