Restore versions DB table and add learning tests

This commit is contained in:
Artur Beljajev 2019-03-01 19:53:37 +02:00
parent 5c8e507b93
commit 647bf4c397
5 changed files with 84 additions and 8 deletions

View file

@ -0,0 +1,15 @@
class RestoreVersions < ActiveRecord::Migration
def change
drop_table :versions
create_table :versions do |t|
t.string :item_type, :null => false
t.integer :item_id, :null => false
t.string :event, :null => false
t.string :whodunnit
t.text :object
t.datetime :created_at
end
add_index :versions, [:item_type, :item_id]
end
end

View file

@ -0,0 +1,5 @@
class AddObjectChangesToVersions < ActiveRecord::Migration
def change
add_column :versions, :object_changes, :jsonb
end
end

View file

@ -2363,7 +2363,13 @@ ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;
CREATE TABLE public.versions ( CREATE TABLE public.versions (
id integer NOT NULL, id integer NOT NULL,
depricated_table_but_somehow_paper_trail_tests_fails_without_it text item_type character varying NOT NULL,
item_id integer NOT NULL,
event character varying NOT NULL,
whodunnit character varying,
object text,
created_at timestamp without time zone,
object_changes jsonb
); );
@ -3980,6 +3986,13 @@ CREATE INDEX index_users_on_identity_code ON public.users USING btree (identity_
CREATE INDEX index_users_on_registrar_id ON public.users USING btree (registrar_id); CREATE INDEX index_users_on_registrar_id ON public.users USING btree (registrar_id);
--
-- Name: index_versions_on_item_type_and_item_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_versions_on_item_type_and_item_id ON public.versions USING btree (item_type, item_id);
-- --
-- Name: index_whois_records_on_domain_id; Type: INDEX; Schema: public; Owner: -; Tablespace: -- Name: index_whois_records_on_domain_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
-- --
@ -4927,6 +4940,10 @@ INSERT INTO schema_migrations (version) VALUES ('20190102144032');
INSERT INTO schema_migrations (version) VALUES ('20190209150026'); INSERT INTO schema_migrations (version) VALUES ('20190209150026');
INSERT INTO schema_migrations (version) VALUES ('20190302091059');
INSERT INTO schema_migrations (version) VALUES ('20190302111152');
INSERT INTO schema_migrations (version) VALUES ('20190311111718'); INSERT INTO schema_migrations (version) VALUES ('20190311111718');
INSERT INTO schema_migrations (version) VALUES ('20190312211614'); INSERT INTO schema_migrations (version) VALUES ('20190312211614');

View file

@ -1,10 +1,3 @@
# the following line is required for PaperTrail >= 4.0.0 with Rails
PaperTrail::Rails::Engine.eager_load!
PaperTrail::Version.module_eval do
self.abstract_class = true
end
# Store console and rake changes in versions # Store console and rake changes in versions
if defined?(::Rails::Console) if defined?(::Rails::Console)
PaperTrail.whodunnit = "console-#{`whoami`.strip}" PaperTrail.whodunnit = "console-#{`whoami`.strip}"

View file

@ -0,0 +1,46 @@
require 'test_helper'
class Post < ActiveRecord::Base
has_paper_trail
end
class PaperTrailLearningTest < ActiveSupport::TestCase
setup do
ActiveRecord::Base.connection.create_table :posts do |t|
t.string :title
# Otherwise `touch_with_version` fails silently
t.datetime :updated_at
end
end
def test_returns_version_list
@record = Post.create!(title: 'any')
assert_equal 1, @record.versions.count
assert_respond_to @record.versions.first, :item_id
end
def test_creates_new_version_upon_update
@record = Post.create!(title: 'old title')
original_record = @record.clone
assert_difference -> { @record.versions.size } do
@record.update!(title: 'new title')
end
version = @record.versions.last
assert_equal @record.id, version.item_id
assert_equal @record.class.name, version.item_type
assert_equal version.reify, original_record
assert_equal ['old title', 'new title'], version.object_changes['title']
assert_equal 'update', version.event
end
def test_touch_with_version
@record = Post.create!(title: 'any')
assert_difference -> { @record.versions.size } do
@record.touch_with_version
end
end
end