Skip to content

Commit 14e859c

Browse files
committed
Fix add_column behavior for missing type
When using `add_column`, the definition methods for `ksuid` and `ksuid_binary` are not used like when using `table.ksuid` since `add_column` creates the column directly. This change makes it so any column definition can handle both `ksuid` and `ksuid_binary` correctly by monkey-patching the method that creates the column definitions.
1 parent 568f28d commit 14e859c

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

activerecord-ksuid/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased](https://github.com/michaelherold/ksuid-ruby/compare/v1.0.0...main)
8+
9+
### Fixed
10+
11+
- Using a `ksuid` or `ksuid_binary` type with `add_column` in migrations will now work correctly without throwing an error about an unknown column type.
12+
713
## [1.0.0](https://github.com/michaelherold/ksuid-ruby/compare/v0.5.0...v1.0.0) - 2023-02-25
814

915
### Added

activerecord-ksuid/lib/active_record/ksuid/railtie.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ class Railtie < ::Rails::Railtie
1818
ActiveSupport.on_load :active_record do
1919
require 'active_record/ksuid/table_definition'
2020

21-
ActiveRecord::ConnectionAdapters::TableDefinition.include(
22-
ActiveRecord::KSUID::TableDefinition
23-
)
21+
ActiveRecord::ConnectionAdapters::TableDefinition.descendants.each do |defn|
22+
next unless defn.name
23+
24+
defn.prepend(ActiveRecord::KSUID::TableDefinition)
25+
end
2426
end
2527
end
2628
end

activerecord-ksuid/lib/active_record/ksuid/table_definition.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@ def ksuid(*args, **options)
5252
def ksuid_binary(*args, **options)
5353
args.each { |name| column(name, :binary, **options.merge(limit: 20)) }
5454
end
55+
56+
# Monkey-patches defining a new column within a table
57+
#
58+
# @api private
59+
# @private
60+
#
61+
# @param name [String, Symbol] the name of the column
62+
# @param type [String, Symbol] the type of the column
63+
# @param options [Hash<Symbol, Object>] options for the definition
64+
# @return [ActiveRecord::ConnectionAdapters::ColumnDefinition]
65+
def new_column_definition(name, type, **options)
66+
case type.to_s
67+
when 'ksuid'
68+
prefix_length = options.delete(:prefix)&.length || 0
69+
70+
super(name, :string, **options.merge(limit: 27 + prefix_length))
71+
when 'ksuid_binary'
72+
super(name, :binary, **options.merge(limit: 20))
73+
else
74+
super
75+
end
76+
end
5577
end
5678
end
5779
end

activerecord-ksuid/spec/active_record/ksuid/railtie_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,23 @@ class EventPrefix < ActiveRecord::Base
257257
end
258258
end
259259

260+
context 'when writing a migration that adds KSUID fields' do
261+
it 'can use the ksuid and ksuid_binary field types' do
262+
connection = ActiveRecord::Base.connection
263+
264+
connection.create_table :field_tests do |t|
265+
t.string :name
266+
end
267+
268+
expect do
269+
connection.add_column :field_tests, :id_one, :ksuid
270+
connection.add_column :field_tests, :id_two, :ksuid_binary
271+
end.not_to raise_error
272+
ensure
273+
connection.drop_table :field_tests, if_exists: true
274+
end
275+
end
276+
260277
matcher :issue_sql_queries do |expected|
261278
supports_block_expectations
262279

0 commit comments

Comments
 (0)