Skip to content

Instantly share code, notes, and snippets.

@jyurek
Last active Aug 16, 2021
Embed
What would you like to do?
Insert lots of rows to test indices
require 'json'
ingredients = %w[
Ham
Water
Sodium\ Bicarbonate
Acetic\ Acid
Salt
Natural\ Flavors
Artificial\ Flavors
Onion
Milk
]
food_adjectives = %w[
Awesome
Party\ Size
Bubbling
Groovy
Extreme
Cheese
New
Red
Mountain
Whole\ Grain
]
food_nouns = %w[
Crisps
Ham
Ghosts
Popplers
Gummies
Dew
Salsa
Cheese
Deliciousness
Experiments
]
f = File.open('out.sql', 'w')
1_000_000.times do |i|
name = [food_adjectives.sample, food_adjectives.sample, food_nouns.sample]
.join(' ')
upc = format('%0.10i', rand(1_000_000_000))
recipe = (0..(rand(5)))
.to_a
.map { { name: ingredients.sample, mg: (rand 1000) + 1 } }
.to_json
f.puts(<<-end_sql)
INSERT INTO foods (name, upc, ingredients)
VALUES ('#{name}', '#{upc}', '#{recipe}');
end_sql
puts i
end
require 'json'
ingredients = %w[
Ham
Water
Sodium\ Bicarbonate
Acetic\ Acid
Salt
Natural\ Flavors
Artificial\ Flavors
Onion
Milk
]
food_adjectives = %w[
Awesome
Party\ Size
Bubbling
Groovy
Extreme
Cheese
New
Red
Mountain
Whole\ Grain
]
food_nouns = %w[
Crisps
Ham
Ghosts
Popplers
Gummies
Dew
Salsa
Cheese
Deliciousness
Experiments
]
f = File.open('out.sql', 'w')
1_000_000.times do |i|
name = [food_adjectives.sample, food_adjectives.sample, food_nouns.sample]
.join(' ')
upc = format('%0.10i', rand(1_000_000_000))
recipe = (0..(rand(5)))
.to_a
.map { { name: ingredients.sample, mg: (rand 1000) + 1 } }
.map(&:to_json)
.map { |s| "'#{s}'" }
.join(',')
f.puts(<<-end_sql)
INSERT INTO foods (name, upc, ingredients)
VALUES ('#{name}', '#{upc}', ARRAY[#{recipe}]::jsonb[]);
end_sql
puts i
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment