Skip to content

Commit 7a36eb2

Browse files
committed
Make several columns in the audits table NOT NULL
Improve data integrity by adding NOT NULL constraints to columns that should _always_ have a value. This helps catch application and library bugs earlier by catching mishandling of data. Fixes collectiveidea#572
1 parent a04d8be commit 7a36eb2

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
class <%= migration_class_name %> < <%= migration_parent %>
4+
def self.up
5+
change_column_null :audits, :auditable_id, false
6+
change_column_null :audits, :auditable_type, false
7+
change_column_null :audits, :action, false
8+
change_column_null :audits, :audited_changes, false
9+
change_column_null :audits, :version, false
10+
change_column_null :audits, :created_at, false
11+
end
12+
13+
def self.down
14+
change_column_null :audits, :auditable_id, true
15+
change_column_null :audits, :auditable_type, true
16+
change_column_null :audits, :action, true
17+
change_column_null :audits, :audited_changes, true
18+
change_column_null :audits, :version, true
19+
change_column_null :audits, :created_at, true
20+
end
21+
end

lib/generators/audited/templates/install.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
class <%= migration_class_name %> < <%= migration_parent %>
44
def self.up
55
create_table :audits, :force => true do |t|
6-
t.column :auditable_id, :integer
7-
t.column :auditable_type, :string
6+
t.column :auditable_id, :integer, null: false
7+
t.column :auditable_type, :string, null: false
88
t.column :associated_id, :integer
99
t.column :associated_type, :string
1010
t.column :user_id, :<%= options[:audited_user_id_column_type] %>
1111
t.column :user_type, :string
1212
t.column :username, :string
13-
t.column :action, :string
14-
t.column :audited_changes, :<%= options[:audited_changes_column_type] %>
15-
t.column :version, :integer, :default => 0
13+
t.column :action, :string, null: false
14+
t.column :audited_changes, :<%= options[:audited_changes_column_type] %>, null: false
15+
t.column :version, :integer, :default => 0, null: false
1616
t.column :comment, :string
1717
t.column :remote_address, :string
1818
t.column :request_uuid, :string
19-
t.column :created_at, :datetime
19+
t.column :created_at, :datetime, null: false
2020
end
2121

2222
add_index :audits, [:auditable_type, :auditable_id, :version], :name => 'auditable_index'

lib/generators/audited/upgrade_generator.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def copy_templates
2626

2727
def migrations_to_be_applied
2828
Audited::Audit.reset_column_information
29-
columns = Audited::Audit.columns.map(&:name)
29+
columns = Audited::Audit.columns.map { |column| [column.name, column] }.to_h
3030
indexes = Audited::Audit.connection.indexes(Audited::Audit.table_name)
3131

3232
yield :add_comment_to_audits unless columns.include?("comment")
@@ -64,6 +64,18 @@ def migrations_to_be_applied
6464
if indexes.any? { |i| i.columns == %w[auditable_type auditable_id] }
6565
yield :add_version_to_auditable_index
6666
end
67+
68+
columns_not_null = [
69+
"auditable_id",
70+
"auditable_type",
71+
"action",
72+
"audited_changes",
73+
"version",
74+
"created_at",
75+
]
76+
if columns_not_null.any? { |column| columns[column].null }
77+
yield :change_audits_columns_not_null
78+
end
6779
end
6880
end
6981
end

spec/audited/auditor_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ def non_column_attr=(val)
447447
end
448448

449449
it "should be able to reconstruct a destroyed record without history" do
450-
@user.audits.delete_all
450+
Audited::Audit.where(auditable: @user).delete_all
451451
@user.destroy
452452

453453
revision = @user.audits.first.revision
@@ -634,7 +634,7 @@ def stub_global_max_audits(max_audits)
634634
end
635635

636636
it "should be empty if no audits exist" do
637-
user.audits.delete_all
637+
Audited::Audit.where(auditable: user).delete_all
638638
expect(user.revisions).to be_empty
639639
end
640640

spec/support/active_record/schema.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,20 @@
6666
end
6767

6868
create_table :audits do |t|
69-
t.column :auditable_id, :integer
70-
t.column :auditable_type, :string
69+
t.column :auditable_id, :integer, null: false
70+
t.column :auditable_type, :string, null: false
7171
t.column :associated_id, :integer
7272
t.column :associated_type, :string
7373
t.column :user_id, :integer
7474
t.column :user_type, :string
7575
t.column :username, :string
76-
t.column :action, :string
77-
t.column :audited_changes, :text
78-
t.column :version, :integer, default: 0
76+
t.column :action, :string, null: false
77+
t.column :audited_changes, :text, null: false
78+
t.column :version, :integer, default: 0, null: false
7979
t.column :comment, :string
8080
t.column :remote_address, :string
8181
t.column :request_uuid, :string
82-
t.column :created_at, :datetime
82+
t.column :created_at, :datetime, null: false
8383
end
8484

8585
add_index :audits, [:auditable_id, :auditable_type], name: "auditable_index"

0 commit comments

Comments
 (0)