Skip to content

Instantly share code, notes, and snippets.

@padi
Last active April 11, 2020 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save padi/7b4f346a9eabeb5fedc8a380621b0c92 to your computer and use it in GitHub Desktop.
Save padi/7b4f346a9eabeb5fedc8a380621b0c92 to your computer and use it in GitHub Desktop.
namespace :gold do
desc "DATABASE will be used to generate the new gold master database"
task :update_db do
db_name = ENV.fetch('DATABASE') { Rails.configuration.database_configuration[Rails.env]["database"] }
destination = Rails.root.join('test/files/gold_master.sql')
puts "Executing command..."
sh "pg_dump #{db_name} --data-only --attribute-inserts --column-inserts --no-tablespaces --no-privileges --no-security-labels --disable-triggers > #{destination}"
puts <<~MSG
Done!
----------------------------------------------------------------------------------------------------
WARNING: The file generated file below:
#{destination}
needs some cleaning up before you can use it on your gold master test/s.
Specifically, you need to remove a bunch of configuration we don't need at the start,
it should look something like this:
```
SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET client_min_messages = warning;
```
Also, you'll need to remove INSERT INTO statement/s to the ar_internal_metadata table:
```
ALTER TABLE public.ar_internal_metadata DISABLE TRIGGER ALL;
INSERT INTO public.ar_internal_metadata (key, value, created_at, updated_at) VALUES ('environment', 'development', '2020-03-20 03:56:06.881918', '2020-03-20 03:56:06.881918');
ALTER TABLE public.ar_internal_metadata ENABLE TRIGGER ALL;
```
----------------------------------------------------------------------------------------------------
MSG
end
end
# https://hashrocket.com/blog/posts/gold-master-testing
describe 'gold master' do
it 'produces a consistent result' do
ActiveRecord::Base.connection.execute <<-SQL
truncate schema_migrations;
#{Rails.root.join('spec/fixtures/gold_master.sql').read}
SQL
actual = Transformer.transform
gold_master_file = Rails.root.join('spec/fixtures/gold_master.txt')
gold_master = gold_master_file.read
if gold_master != actual
gold_master_file.write(actual)
end
expect(actual).to eq(gold_master)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment