This is a basic extension to populate arbitrary tables with arbitrary mock data.
Sometimes you just want some data in a table for testing and don't care what it is. There are other extensions that do similar things; we just want it to be downright trivial.
If we don't understand a data type in the table, we just skip/NULL out that field.
We support single-column integer unique and primary keys to allow repeated invocations on the same table without causing conflicts on insert.
$ git clone [email protected]:pgguru/generate_mock.git
$ cd generate_mock
$ make PG_CONFIG=path/to/pg_config && make install PG_CONFIG=path/to/pg_config
$ psql -c 'CREATE EXTENSION generate_mock -U <user> -d <database>
All you have to do to populate a table is pass in the regclass for the table and the number of rows you want to add to it:
CREATE TABLE my_table(id int primary key, name text, blob jsonb);
SELECT insert_mock('my_table', 10);
This module creates two visible functions: generate_mock()
and insert_mock()
.
generate_mock()
is a set-returning function which generates the given number
of rows for the table passed in. This returns setof record, so you need to cast
to a specific record type, which is annoying.
insert_mock()
handles the grunt work of inserting the data into a specific
table using generate_mock()
, and just basically constructs the query needed to
insert the results of generate_mock()
into the passed-in table so you don't
have to. This is the function to actually use.
We currently support arbitrary data creation for the following types:
BOOLOID
BPCHAROID
DATEOID
FLOAT4OID
FLOAT8OID
INT2OID
INT4OID
INT8OID
JSONBOID
JSONOID
NUMERICOID
TEXTOID
TIMEOID
TIMESTAMPOID
TIMESTAMPTZOID
TIMETZOID
UUIDOID
VARCHAROID
David Christensen [email protected]