Skip to content

Commit

Permalink
Make timestamps (created_at, updated_at) NOT NULL
Browse files Browse the repository at this point in the history
These values are always set by ActiveRecord and so will never be stored
as NULL (or Ruby nil).

Overall, improves data integrity by hardening the schema constraints.
  • Loading branch information
jdufresne committed Aug 30, 2024
1 parent a264ba1 commit 365ffc5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/generators/delayed_job/templates/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.up
table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
table.string :locked_by # Who is working on this object (if locked)
table.string :queue # The name of the queue this job is in
table.timestamps null: true
table.timestamps null: false
end

add_index :delayed_jobs, [:priority, :run_at], name: "delayed_jobs_priority"
Expand Down
6 changes: 6 additions & 0 deletions lib/generators/delayed_job/templates/timestamps_not_null.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class TimestampsNotNull < ActiveRecord::Migration<%= migration_version %>
def change
change_column_null :delayed_jobs, :created_at, false
change_column_null :delayed_jobs, :updated_at, false
end
end
5 changes: 5 additions & 0 deletions lib/generators/delayed_job/upgrade_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def create_migration_file
"db/migrate/add_queue_to_delayed_jobs.rb",
migration_version: migration_version
)
migration_template(
"timestamps_not_null.rb",
"db/migrate/timestamps_not_null.rb",
migration_version: migration_version
)
end
end
end

0 comments on commit 365ffc5

Please sign in to comment.