Skip to content

Commit 530a8b9

Browse files
respect rails application record setup
1 parent 0cafc52 commit 530a8b9

File tree

6 files changed

+52
-10
lines changed

6 files changed

+52
-10
lines changed

lib/chrono_forge.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ module ChronoForge
1111
end
1212

1313
class Error < StandardError; end
14+
15+
def self.ApplicationRecord() = defined?(::ApplicationRecord) ? ::ApplicationRecord : ActiveRecord::Base
1416
end

lib/chrono_forge/error_log.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#
2424

2525
module ChronoForge
26-
class ErrorLog < ActiveRecord::Base
26+
class ErrorLog < ApplicationRecord()
2727
self.table_name = "chrono_forge_error_logs"
2828

2929
belongs_to :workflow

lib/chrono_forge/execution_log.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# workflow_id (workflow_id => chrono_forge_workflows.id)
2929
#
3030
module ChronoForge
31-
class ExecutionLog < ActiveRecord::Base
31+
class ExecutionLog < ApplicationRecord()
3232
self.table_name = "chrono_forge_execution_logs"
3333

3434
belongs_to :workflow

lib/chrono_forge/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module ChronoForge
4-
VERSION = "0.4.0"
4+
VERSION = "0.5.0"
55
end

lib/chrono_forge/workflow.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# index_chrono_forge_workflows_on_key (key) UNIQUE
2323
#
2424
module ChronoForge
25-
class Workflow < ActiveRecord::Base
25+
class Workflow < ApplicationRecord()
2626
self.table_name = "chrono_forge_workflows"
2727

2828
has_many :execution_logs, -> { order(id: :asc) }, dependent: :destroy

lib/generators/chrono_forge/install/templates/install_chrono_forge.rb

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class InstallChronoForge < ActiveRecord::Migration[7.1]
44
def change
5-
create_table :chrono_forge_workflows do |t|
5+
create_table :chrono_forge_workflows, id: primary_key_type do |t|
66
t.string :key, null: false, index: true
77
t.string :job_class, null: false
88

@@ -27,18 +27,23 @@ def change
2727
t.index %i[job_class key], unique: true
2828
end
2929

30-
create_table :chrono_forge_execution_logs do |t|
31-
t.references :workflow, null: false, foreign_key: {to_table: :chrono_forge_workflows}
30+
create_table :chrono_forge_execution_logs, id: primary_key_type do |t|
31+
t.references :workflow, null: false,
32+
foreign_key: {to_table: :chrono_forge_workflows},
33+
type: reference_type
34+
3235
t.string :step_name, null: false
3336
t.integer :attempts, null: false, default: 0
3437
t.datetime :started_at
3538
t.datetime :last_executed_at
3639
t.datetime :completed_at
40+
3741
if t.respond_to?(:jsonb)
3842
t.jsonb :metadata
3943
else
4044
t.json :metadata
4145
end
46+
4247
t.integer :state, null: false, default: 0
4348
t.string :error_class
4449
t.text :error_message
@@ -47,11 +52,15 @@ def change
4752
t.index %i[workflow_id step_name], unique: true
4853
end
4954

50-
create_table :chrono_forge_error_logs do |t|
51-
t.references :workflow, null: false, foreign_key: {to_table: :chrono_forge_workflows}
55+
create_table :chrono_forge_error_logs, id: primary_key_type do |t|
56+
t.references :workflow, null: false,
57+
foreign_key: {to_table: :chrono_forge_workflows},
58+
type: reference_type
59+
5260
t.string :error_class
5361
t.text :error_message
5462
t.text :backtrace
63+
5564
if t.respond_to?(:jsonb)
5665
t.jsonb :context
5766
else
@@ -61,4 +70,35 @@ def change
6170
t.timestamps
6271
end
6372
end
64-
end
73+
74+
private
75+
76+
def primary_key_type
77+
# Check if the application is configured to use UUIDs
78+
if ActiveRecord.respond_to?(:default_id) && ActiveRecord.default_id.respond_to?(:to_s) &&
79+
ActiveRecord.default_id.to_s.include?('uuid')
80+
return :uuid
81+
end
82+
83+
# Rails 6+ configuration style
84+
if ActiveRecord.respond_to?(:primary_key_type) &&
85+
ActiveRecord.primary_key_type.to_s == 'uuid'
86+
return :uuid
87+
end
88+
89+
# Check application config
90+
app_config = Rails.application.config.generators
91+
if app_config.options.key?(:active_record) &&
92+
app_config.options[:active_record].key?(:primary_key_type) &&
93+
app_config.options[:active_record][:primary_key_type].to_s == 'uuid'
94+
return :uuid
95+
end
96+
97+
# Default to traditional integer keys
98+
return :bigint
99+
end
100+
101+
def reference_type
102+
primary_key_type == :uuid ? :uuid : nil
103+
end
104+
end

0 commit comments

Comments
 (0)