NAME

DBIx::Class::MockData - Generate mock test data for DBIx::Class schemas

VERSION

Version 0.02

SYNOPSIS

use DBIx::Class::MockData;

my $schema = MyApp::Schema->connect($dsn, $user, $pass);

DBIx::Class::MockData
    ->new(
        schema     => $schema,
        schema_dir => 't/lib',
    )
    ->deploy
    ->generate;

With options:

DBIx::Class::MockData
    ->new(
        schema     => $schema,
        schema_dir => 't/lib',
        rows       => 10,
        verbose    => 1,
        seed       => 42,
    )
    ->wipe
    ->generate;

DESCRIPTION

Accepts a connected DBIx::Class::Schema object, the return value of YourSchema->connect(...), introspects every result source, resolves foreign-key insertion order via topological sort, and inserts randomly generated rows. Values are produced from each column's declared data_type. Unique and primary-key columns are salted with a per-run random value to avoid cross-run collisions.

Connection and schema loading are entirely the caller's responsibility. schema_dir is accepted only to ensure the lib root is present in @INC so result classes remain resolvable at runtime.

All public methods return $self, enabling call chaining.

METHODS

CONSTRUCTOR

my $mock = DBIx::Class::MockData->new(
    schema     => $connected_schema,   # required
    schema_dir => 't/lib',             # required
    rows       => 5,                   # rows per table  (default: 5)
    verbose    => 0,                   # debug output    (default: 0)
    seed       => 42,                  # random seed     (optional)
);

ARGUMENTS

schema (required)

A connected DBIx::Class::Schema instance - the return value of YourSchema->connect(...).

schema_dir (required)

The lib root containing your schema classes. Added to @INC so result classes referenced by relationships remain resolvable.

rows

Number of rows to generate per table. Default: 5.

verbose

Print debug output. Default: 0.

seed

Integer seed passed to srand for reproducible output. Optional.

METHODS

deploy

$mock->deploy;
$mock->deploy->generate;   # chainable

Creates all tables that do not yet exist. Safe to call when some or all tables are already present. Returns $self.

wipe

$mock->wipe->generate;   # chainable

Drops all tables then redeploys the schema. Destructive -- for test environments only. Returns $self.

generate

$mock->generate;

Inserts mock rows into every table respecting FK insertion order, data types, nullability, and uniqueness constraints. Returns $self.

dry_run

$mock->dry_run;

Prints the values that would be inserted without touching the database. Returns $self.

GENERATED VALUES

Values are produced from each column's declared data_type:

integer / bigint / serial    random integer  (unique: salt + row_num)
numeric / decimal / float    random decimal (2 d.p.)
boolean                      0 or 1
datetime / timestamp         random datetime 2020-2024
date                         random date 2020-2024
time                         random HH:MM:SS
uuid                         random UUID-shaped hex string
json / jsonb                 {"generated":true,"row":N}
text / varchar / char        contextual string matched on column name
unknown / blank dtype        colname_N  (colname_N_SALT if unique)

Nullable columns receive NULL roughly 17% of the time (never for unique cols).

CLI TOOL

This distribution ships with a command-line tool dbic-mockdata in the script/ directory, installed into your PATH by make install.

dbic-mockdata --schema-dir t/lib --namespace MyApp::Schema \
              --dsn "dbi:SQLite:dbname=test.db" --deploy --rows 5

Run dbic-mockdata --help for the full list of options, or see dbic-mockdata for the complete manual.

DEPENDENCIES

DBIx::Class, Carp, File::Spec, Scalar::Util

AUTHOR

Mohammad Sajid Anwar, <mohammad.anwar at yahoo.com>

REPOSITORY

https://github.com/manwar/DBIx-Class-MockData

BUGS

Please report any bugs or feature requests through the web interface at https://github.com/manwar/DBIx-Class-MockData/issues. I will be notified and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc DBIx::Class::MockData

You can also look for information at:

LICENSE AND COPYRIGHT

Copyright (C) 2026 Mohammad Sajid Anwar.

This program is free software; you can redistribute it and / or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at: http://www.perlfoundation.org/artistic_license_2_0 Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License.By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license. If your Modified Version has been derived from a Modified Version made by someone other than you,you are nevertheless required to ensure that your Modified Version complies with the requirements of this license. This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder. This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement,then this Artistic License to you shall terminate on the date that such litigation is filed. Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.