Skip to content

ehfeng/faker

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
sql
 
 
 
 
 
 
 
 
 
 

Faker

Toying with ideas for generating fake data directly within SQL.

-- Create and populate organizations
create table organizations(
  id serial primary key,
  created timestamp default now(),
  name text not null,
  domain text
);

insert into organizations(created, name, domain)
  with base as (
    select random_uniform((now() - '1 year'::interval)::timestamp, now()::timestamp) as created,
      fake_company_name() as name
    from generate_series(1, 100))
  select created, name, fake_domain(name) as domain from base
on conflict do nothing;

-- Create and populate users
create table users(
  id serial primary key,
  created timestamp default now(),
  name text not null,
  email text not null unique
);

insert into users(created, name, email)
  with base as (
    select
      random_uniform((now() - '1 year'::interval)::timestamp, now()::timestamp) as created,
      fake_name() as name
    from generate_series(1, 1000))
  select created, name, generate_email(name)
  from base
on conflict do nothing;

create table organization_members(
  user_id integer references users(id),
  organization_id integer references organizations(id),
  created timestamp,
  role text not null
);

-- Join them to organization_id
insert into organization_members(user_id, organization_id, created, role)
  with base as (select id, created from users),
  org_array as (select array_agg(id) as ids from organizations)
  select id as user_id,
    random_choice(org_array.ids::integer[]) as organization_id,
    created,
    random_choice('{"owner", "member", "member", "member"}'::text[]) as role
  from base
  cross join org_array
on conflict do nothing;

Install

Clone repo and make install

Within database, create extension faker

Credits

Thanks to big elephants for a great tutorial.

Inspired by Faker and Python's random module.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published