Skip to content

Instantly share code, notes, and snippets.

@roktas
Last active April 19, 2019 22:12
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 roktas/dee717315660bb75c4e7d8e355c9b9fc to your computer and use it in GitHub Desktop.
Save roktas/dee717315660bb75c4e7d8e355c9b9fc to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require 'pg'
# Untested
module Nokul
module Support
class PgExec
def initialize(**options)
@options = { bypass_triggers: true }.merge options
@args = args
end
def call(*sql)
db = PG.connect(**args)
db.exec prep(*sql)
ensure
db.close
end
private
def args
config = ActiveRecord::Base.configurations[Rails.env]
{
host: config['host'],
dbname: config['database'],
user: config['username'],
password: config['password']
}
end
def prep(*sql)
# https://www.endpoint.com/blog/2015/01/28/postgres-sessionreplication-role
!options[:bypass_triggers] ? sql : <<~SQL
SET session_replication_role = replica;
#{sql.join(';')}
SET session_replication_role = origin;
SQL
end
class << self
def call(*sql, **options)
new(**options).(*sql)
end
end
end
end
end
# Bunun ismini mi değiştirsek, artık beta yok
def load_seed_data
Dir[Rails.root.join('db', 'beta_seed', '*.rb')].sort.each do |seed|
load seed
end
end
ENCRYTED_DATA_DIR = 'db/encrypted_data'
def restore_seed_data(encrypted_data_name)
path = Rails.root.join(ENCRYTED_DATA_DIR, encrypted_data_name)
PgExec.(ActiveSupport::Gzip.decompress(Support::Sensitive.read(path)))
rescue Pg::Error => e
abort("SQL error during exec: #{e.message}")
end
# Seed logic goes below
if ENV['SYNC'].eql?('true')
Rake::Task['fetch:references'].invoke
Rake::Task['import:all'].invoke
if ENV['SAMPLE_DATA'].eql?('true')
Rake::Task['fetch:academic_staff'].invoke
Osym::ImportProspectiveStudentsJob.perform_later('db/encrypted_data/prospective_students.csv')
end
else
restore_seed_data('static_data')
if ENV['SAMPLE_DATA'].eql?('true')
restore_seed_data('sample_data')
load_seed_data
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment