Commit d0bfe2b6 authored by Muri Nicanor's avatar Muri Nicanor

Merge branch 'nina/extend_headers_to_meta_spec' into 'extend_headers_to_meta'

Add spec for data migration and remove inline class definition

See merge request muri/schleuder!1
parents 12607e02 ef90856e
class AddSigEncToHeadersToMetaDefaults < ActiveRecord::Migration
def up
change_column_default :lists, :headers_to_meta, '["from", "to", "cc", "date", "sig", "enc"]'
# Use a temporary class-definition to be independent of the
# complexities of the actual class.
class List < ActiveRecord::Base; end
List.reset_column_information
List.find_each do |list|
list_klass = create_list_klass
list_klass.reset_column_information
list_klass.find_each do |list|
if (list.headers_to_meta & ['sig', 'enc']).empty?
list.update(headers_to_meta: list.headers_to_meta + ['sig', 'enc'])
end
......@@ -14,12 +12,19 @@ class AddSigEncToHeadersToMetaDefaults < ActiveRecord::Migration
def down
change_column_default :lists, :headers_to_meta, '["from", "to", "cc", "date"]'
list_klass = create_list_klass
list_klass.reset_column_information
list_klass.find_each do |list|
list.update(headers_to_meta: list.headers_to_meta - ['enc','sig'])
end
end
def create_list_klass
# Use a temporary class-definition to be independent of the
# complexities of the actual class.
class List < ActiveRecord::Base; end
List.reset_column_information
List.find_each do |list|
list.update(headers_to_meta: list.headers_to_meta - ['enc','sig'])
Class.new(ActiveRecord::Base) do
self.table_name = 'lists'
self.serialize :headers_to_meta, JSON
end
end
end
require 'spec_helper'
require './db/migrate/20180110203100_add_sig_enc_to_headers_to_meta_defaults.rb'
describe 'AddSigEncToHeadersToMetaDefaults' do
let(:migrations_paths) { ENV['SCHLEUDER_ROOT'] + '/db/migrate' }
let(:migration_under_test) { 20180110203100 }
let(:previous_migration) { 20170713215059 }
describe 'up' do
it 'sets the column defaults' do
ActiveRecord::Migrator.migrate(migrations_paths, previous_migration)
list_klass = create_list_klass
list_klass.reset_column_information
ActiveRecord::Migrator.migrate(migrations_paths, migration_under_test)
list_klass.reset_column_information
expect(list_klass.column_defaults['headers_to_meta']).to eql(["from", "to", "cc", "date", "sig", "enc"])
end
it 'adds sig and enc to headers_to_meta for lists wihtout the attributes' do
ActiveRecord::Migrator.migrate(migrations_paths, previous_migration)
list_klass = create_list_klass
list = create(:list, headers_to_meta: list_klass.column_defaults['headers_to_meta'])
expect(list.headers_to_meta).not_to include('enc', 'sig')
ActiveRecord::Migrator.migrate(migrations_paths, migration_under_test)
list_klass.reset_column_information
list.reload
expect(list.headers_to_meta).to include('enc', 'sig')
end
it 'does not add sig and enc to headers to meta if the attributes already exist' do
headers_to_meta_including_sig_and_enc = ["from", "to", "cc", "date", "sig", "enc"]
ActiveRecord::Migrator.migrate(migrations_paths, previous_migration)
list_klass = create_list_klass
list = create(:list, headers_to_meta: headers_to_meta_including_sig_and_enc)
expect(list.headers_to_meta).to eql headers_to_meta_including_sig_and_enc
ActiveRecord::Migrator.migrate(migrations_paths, migration_under_test)
list_klass.reset_column_information
list.reload
expect(list.headers_to_meta).to eql headers_to_meta_including_sig_and_enc
end
end
describe 'down' do
it 'sets the column defaults' do
ActiveRecord::Migrator.migrate(migrations_paths, migration_under_test)
list_klass = create_list_klass
list_klass.reset_column_information
ActiveRecord::Migrator.migrate(migrations_paths, previous_migration)
list_klass.reset_column_information
expect(list_klass.column_defaults['headers_to_meta']).to eql(["from", "to", "cc", "date"])
end
it 'removes sig and enc from headers_to_meta from existing lists' do
ActiveRecord::Migrator.migrate(migrations_paths, migration_under_test)
list_klass = create_list_klass
list_klass.reset_column_information
list = create(:list, headers_to_meta: list_klass.column_defaults['headers_to_meta'])
expect(list.headers_to_meta).to include('enc', 'sig')
ActiveRecord::Migrator.migrate(migrations_paths, previous_migration)
list_klass.reset_column_information
list.reload
expect(list.headers_to_meta).not_to include('enc', 'sig')
end
end
def create_list_klass
Class.new(ActiveRecord::Base) do
self.table_name = 'lists'
self.serialize :headers_to_meta, JSON
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment