Skip to content

Instantly share code, notes, and snippets.

@criess
Last active January 27, 2023 13:43
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 criess/974ceb29e893f7a6f0a192ec923bf2e8 to your computer and use it in GitHub Desktop.
Save criess/974ceb29e893f7a6f0a192ec923bf2e8 to your computer and use it in GitHub Desktop.
# - all user*
# - all contract*
# - all discounts
# - all credit_memos
# - all product*
# - all subscriptions
# - all invoice*
# - all agent*
# - without any agent_result*
# - all social_media_trackers
# - all reader*
# - all account_manager_records
# - all payment_methods
# - all countries
# - without any reader*_log
# - reader_archive*
# - reader_results
# - reader_categor*
#
# first positional parameter passed is file of psql dumped schema
# script searches for "CREATE TABLE xxxx" lines
# rules are defined as local variables (_positive|_negative)
# output are tablenames evaluated against positive & negative rules …
rules_positive = [
'user.*',
'contract.*',
'discounts',
'credit_memos',
'product.*',
'features',
'subscriptions',
'invoice.*',
'agent.*',
'social_media_trackers',
'reader.*',
'account_manager_records',
'payment_methods',
'countries'
]
rules_negative = [
'agent_result.*',
'reader.*_log',
'reader_archive.*',
'reader_results',
'reader_categor.*'
]
File.read(ARGV[0]).each_line do |i|
next unless i.match?(/CREATE TABLE public\./)
fq_table = i.scan(/public\.[^ ]+/)[0]
table = fq_table.split('.')[1]
if rules_positive.any? { |r| table.match?(/^#{r}$/) } && rules_negative.all? { |r| !table.match?(/^#{r}$/) }
puts fq_table
end
end
ruby contract_info_table_name_extractor.rb schema.sql |\
awk '{print "--table=" $0}' |\
xargs pg_dump -a -h db.host.tld -U db_user 2>pg_error.log |\
gzip -9 > dumped_tables.sql.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment