Skip to content

Instantly share code, notes, and snippets.

@mgramin
Last active March 12, 2023 11:40
Show Gist options
  • Select an option

  • Save mgramin/da716dc6c4d57a1235348c3e6559c8c3 to your computer and use it in GitHub Desktop.

Select an option

Save mgramin/da716dc6c4d57a1235348c3e6559c8c3 to your computer and use it in GitHub Desktop.
SELECT faker.faker(ARRAY['en_US']);
delete from rental;
delete from inventory;
delete from film;
delete from staff;
delete from customer;
delete from store;
delete from address;
delete from city;
delete from country;
delete from language;
insert into country(country)
select faker.unique_country()
from generate_series(1, 100) as id;
insert into city(city, country_id)
select unnest(array(select faker.unique_city()
from generate_series(1, c.cities_count) as id))
, c.country_id
from (select country_id
, floor(random() * 5 + 1)::int as cities_count
from country) c;
insert into address (address, address2, district, city_id, postal_code, phone)
select unnest(array(select faker.address()
from generate_series(1, c.addresses_count) as id))
, faker.address()
, faker.state()
, city_id
, faker.postalcode()
, faker.phone_number()
from (select city_id
, floor(random() * 25 + 1)::int as addresses_count
from city) c;
insert into store(address_id, manager_staff_id)
select address_id
, address_id
from address;
insert into staff(first_name, last_name, address_id, email, store_id, active, username, password)
select faker.first_name()
, faker.last_name()
, a.address_id
, faker.email()
, s.store_id
, case when random() > 0.5 then true else false end
, faker.user_name()
, faker.password()
from store s
full join address a on a.address_id = s.address_id;
insert into customer(store_id, first_name, last_name, email, address_id, activebool)
select s.store_id
, faker.first_name()
, faker.last_name()
, faker.email()
, a.address_id
, case when random() > 0.5 then true else false end
from store s
cross join address a
order by random()
limit 10000;
insert into language(name)
select faker.unique_language_name()
from generate_series(1, 100) as id;
insert into film (title, language_id, original_language_id)
select format('Midnight in %s', faker.unique_city())
, (select l.language_id from "language" l order by random() + f/1e39 limit 1)
, (select l.language_id from "language" l order by random() + f/1e39 limit 1)
from generate_series(1, 1000) f(f);
insert into inventory(film_id, store_id)
select f.film_id
, s.store_id
from film f
cross join store s
order by random()
limit 10000;
insert into rental(rental_date, inventory_id, customer_id, staff_id)
select current_date - (row_number() over())::int
, inventory_id
, customer_id
, staff_id
from (select i.inventory_id
, c.customer_id
, s.staff_id
from inventory i tablesample bernoulli(2)
cross join customer c tablesample bernoulli(2)
cross join staff s tablesample bernoulli(2)
order by random()
limit 10000) s;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment