Skip to content

Commit

Permalink
Use multiple transactions to load custom network prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-sinina committed Oct 21, 2024
1 parent dc97c50 commit efb2a14
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions db/custom_seeds/network_prefixes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,29 @@
System::Network.insert_all!(networks) if networks.any?
System::Country.insert_all!(countries) if countries.any?
System::NetworkPrefix.insert_all!(network_prefixes) if network_prefixes.any?
end

Dir.glob('db/network_data/**/*.yml') do |f|
puts "processing file #{f}:" # rubocop:disable Rails/Output
data = YAML.load_file(f, aliases: true)
n = data['networks']
System::Network.insert_all!(n) if n.any?
puts " loaded #{n.length} networks" # rubocop:disable Rails/Output
n_ids = n.map { |ni| ni['id'] }.uniq
System::NetworkPrefix.transaction do
puts "processing file #{f}:" # rubocop:disable Rails/Output
data = YAML.load_file(f, aliases: true)
n = data['networks']

np = data['network_prefixes']
System::NetworkPrefix.insert_all!(np) if np.any?
puts " loaded #{np.length} network prefixes" # rubocop:disable Rails/Output
np_ids = np.map { |npi| npi['network_id'] }.uniq
System::Network.insert_all!(n) if n.any?
puts " loaded #{n.length} networks" # rubocop:disable Rails/Output
n_ids = n.map { |ni| ni['id'] }.uniq

no_prefixes = n_ids - np_ids
puts " networks without prefixes: #{no_prefixes}" if no_prefixes.any? # rubocop:disable Rails/Output
np = data['network_prefixes']
System::NetworkPrefix.insert_all!(np) if np.any?
puts " loaded #{np.length} network prefixes" # rubocop:disable Rails/Output
np_ids = np.map { |npi| npi['network_id'] }.uniq

no_prefixes = n_ids - np_ids
puts " networks without prefixes: #{no_prefixes}" if no_prefixes.any? # rubocop:disable Rails/Output
end
end

System::NetworkPrefix.transaction do
SqlCaller::Yeti.execute "SELECT pg_catalog.setval('sys.network_types_id_seq', MAX(id), true) FROM sys.network_types"
SqlCaller::Yeti.execute "SELECT pg_catalog.setval('sys.networks_id_seq', MAX(id), true) FROM sys.networks"
SqlCaller::Yeti.execute "SELECT pg_catalog.setval('sys.countries_id_seq', MAX(id), true) FROM sys.countries"
Expand Down

0 comments on commit efb2a14

Please sign in to comment.