Skip to content

Instantly share code, notes, and snippets.

@hyphenPaul
Last active December 13, 2017 20:21
Show Gist options
  • Save hyphenPaul/d87c1b7a33f669f5c9ff468bbcc75c46 to your computer and use it in GitHub Desktop.
Save hyphenPaul/d87c1b7a33f669f5c9ff468bbcc75c46 to your computer and use it in GitHub Desktop.
Build a quick test PostgreSQL store
drop database if exists store;
create database store;
\c store
begin;
create table products(
id serial primary key,
name varchar(200) not null,
created_at timestamp default current_timestamp,
updated_at timestamp default current_timestamp
);
create table skus(
sku varchar(20) primary key,
option varchar(20),
created_at timestamp default current_timestamp,
updated_at timestamp default current_timestamp,
product_id integer references products (id)
);
create table prices(
id serial primary key,
cents integer NOT NULL,
created_at timestamp default current_timestamp,
updated_at timestamp default current_timestamp,
sku varchar(20) references skus (sku)
);
create table purchases(
id serial primary key,
purchase_date timestamp
);
create table order_items(
id serial primary key,
quantity integer NOT NULL,
sku varchar(20) references skus (sku),
purchase_id integer references purchases (id)
);
do $$
begin
for i in 1..10 loop
insert into products (name) values ('Example Product ' || i);
for b in 1..5 loop
insert into skus (sku, product_id) values ('SKU_' || i || '_' || b, i);
for c in 1..3 loop
insert into prices (cents, sku) values (floor(random() * 10000), 'SKU_' || i || '_' || b);
end loop;
end loop;
end loop;
for i in 1..20 loop
insert into purchases default values;
for b in 1..(floor(random() * 5) + 1) loop
insert into order_items (quantity, sku, purchase_id) values (floor(random() * 3) + 1, 'SKU_' || floor(random() * 10) + 1 || '_' || floor(random() * 5) + 1, i);
end loop;
end loop;
end;
$$;
commit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment