Skip to content

Commit d6f61d6

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 7fcc902 commit d6f61d6

File tree

5 files changed

+49
-9
lines changed

5 files changed

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

lib/generators/audited/templates/install.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ def self.up
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

+11-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,16 @@ 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+
"action",
70+
"audited_changes",
71+
"version",
72+
"created_at",
73+
]
74+
if columns_not_null.any? { |column| columns[column].null }
75+
yield :change_audits_columns_not_null
76+
end
6777
end
6878
end
6979
end

spec/support/active_record/schema.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@
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"

test/upgrade_generator_test.rb

+13
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,17 @@ class UpgradeGeneratorTest < Rails::Generators::TestCase
9494
assert_includes(content, "class AddCommentToAudits < ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]\n")
9595
end
9696
end
97+
98+
test "should set several audits columns NOT NULL" do
99+
load_schema 1
100+
101+
run_generator %w[upgrade]
102+
103+
assert_migration "db/migrate/change_audits_columns_not_null.rb" do |content|
104+
assert_includes(content, 'change_column_null :audits, :action, false')
105+
assert_includes(content, 'change_column_null :audits, :audited_changes, false')
106+
assert_includes(content, 'change_column_null :audits, :version, false')
107+
assert_includes(content, 'change_column_null :audits, :created_at, false')
108+
end
109+
end
97110
end

0 commit comments

Comments
 (0)