Skip to content

Commit 1c4d40d

Browse files
committed
simplify migration generator, add created_at to outbox events, bulk insert events
1 parent e07aa59 commit 1c4d40d

File tree

17 files changed

+40
-154
lines changed

17 files changed

+40
-154
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,10 @@ Journaled includes a built-in Outbox-style delivery adapter with horizontally sc
186186

187187
This feature requires creating database tables and is completely optional. Existing users are unaffected.
188188

189-
1. **Generate migrations:**
189+
1. **Install migrations:**
190190

191191
```bash
192-
rails generate journaled:database_events
192+
rake journaled:install:migrations
193193
rails db:migrate
194194
```
195195

@@ -269,7 +269,6 @@ Inspect and requeue failed events:
269269
```ruby
270270
# Find failed events
271271
Journaled::Outbox::Event.failed.where(stream_name: 'my_stream')
272-
Journaled::Outbox::Event.failed_since(1.hour.ago)
273272
274273
# Requeue a failed event (clears failure info and resets attempts)
275274
failed_event = Journaled::Outbox::Event.failed.find(123)

app/models/journaled/event.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def event_type
1818
end
1919

2020
def created_at
21-
@created_at ||= Time.current
21+
@created_at ||= Time.zone.now
2222
end
2323

2424
# Event metadata and configuration (not serialized)

app/models/journaled/outbox/event.rb

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class Event < Journaled.outbox_base_class_name.constantize
2424
}
2525

2626
scope :failed, -> { where.not(failed_at: nil) }
27-
scope :failed_since, ->(time) { where('failed_at >= ?', time) }
2827

2928
# Fetch a batch of events for processing using SELECT FOR UPDATE
3029
#
@@ -48,28 +47,11 @@ def requeue!
4847
)
4948
end
5049

51-
# Extract timestamp from UUID v7 id
52-
#
53-
# UUID v7 embeds a timestamp in the first 48 bits (milliseconds since Unix epoch)
54-
#
55-
# @return [Time] The timestamp embedded in the UUID
56-
def self.timestamp_from_uuid(uuid)
57-
# Remove dashes and take first 12 hex characters (48 bits)
58-
hex_timestamp = uuid.to_s.delete('-')[0, 12]
59-
# Convert from hex to milliseconds since epoch
60-
milliseconds = hex_timestamp.to_i(16)
61-
# Convert to Time object
62-
Time.zone.at(milliseconds / 1000.0)
63-
end
64-
6550
# Get the oldest non-failed event's timestamp
6651
#
6752
# @return [Time, nil] The timestamp of the oldest event, or nil if no events exist
6853
def self.oldest_non_failed_timestamp
69-
oldest = ready_to_process.order(:id).limit(1).pick(:id)
70-
return nil unless oldest
71-
72-
timestamp_from_uuid(oldest)
54+
ready_to_process.order(:id).limit(1).pick(:created_at)
7355
end
7456
end
7557
end

lib/generators/journaled/database_events/templates/install_uuid_generate_v7.rb.erb renamed to db/migrate/1_install_uuid_generate_v7.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
class InstallUuidGenerateV7 < ActiveRecord::Migration<%= migration_version %>
1+
# frozen_string_literal: true
2+
3+
class InstallUuidGenerateV7 < ActiveRecord::Migration[7.2]
24
def up
35
# Enable pgcrypto extension for gen_random_bytes()
46
enable_extension 'pgcrypto'
57

68
# Install UUID v7 generation function
79
# Source: https://github.com/Betterment/postgresql-uuid-generate-v7
8-
execute <<-SQL
10+
execute <<-SQL.squish
911
CREATE OR REPLACE FUNCTION uuid_generate_v7()
1012
RETURNS uuid
1113
LANGUAGE plpgsql

lib/generators/journaled/database_events/templates/create_journaled_events.rb.erb renamed to db/migrate/2_create_journaled_events.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
class CreateJournaledEvents < ActiveRecord::Migration<%= migration_version %>
1+
# frozen_string_literal: true
2+
3+
class CreateJournaledEvents < ActiveRecord::Migration[7.2]
24
def change
35
# UUID v7 primary key (auto-generated by database using uuid_generate_v7())
46
create_table :journaled_outbox_events, id: :uuid, default: -> { "uuid_generate_v7()" } do |t|
@@ -11,6 +13,7 @@ def change
1113
t.text :failure_reason
1214

1315
t.datetime :failed_at
16+
t.datetime :created_at, null: false, default: -> { "clock_timestamp()" }
1417
end
1518

1619
# Index for querying failed events

journaled.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
1717
s.license = "MIT"
1818
s.metadata['rubygems_mfa_required'] = 'true'
1919

20-
s.files = Dir["{app,config,lib,journaled_schemas}/**/*", "LICENSE", "Rakefile", "README.md"]
20+
s.files = Dir["{app,config,db,lib,journaled_schemas}/**/*", "LICENSE", "Rakefile", "README.md"]
2121

2222
s.required_ruby_version = ">= 3.2"
2323

lib/generators/journaled/database_events/database_events_generator.rb

Lines changed: 0 additions & 42 deletions
This file was deleted.

lib/generators/journaled/database_events/templates/README

Lines changed: 0 additions & 30 deletions
This file was deleted.

lib/journaled.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ module Journaled
3131
mattr_writer(:transactional_batching_enabled) { true }
3232

3333
# Worker configuration (for Outbox-style event processing)
34-
mattr_accessor(:worker_batch_size) { 500 } # Kinesis PutRecords API max
34+
mattr_accessor(:worker_batch_size) { 500 }
3535
mattr_accessor(:worker_poll_interval) { 5 } # seconds
36-
mattr_accessor(:worker_max_attempts) { 3 }
3736

3837
def self.transactional_batching_enabled?
3938
Thread.current[:journaled_transactional_batching_enabled] || @@transactional_batching_enabled

lib/journaled/engine.rb

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

33
module Journaled
44
class Engine < ::Rails::Engine
5+
engine_name 'journaled'
6+
57
config.after_initialize do
68
ActiveSupport.on_load(:active_job) do
79
Journaled.delivery_adapter.validate_configuration! unless Journaled.development_or_test?

0 commit comments

Comments
 (0)