GithubHelp home page GithubHelp logo

dbfaker's Introduction

Latest Stable Version Total Downloads Latest Unstable Version License Scrutinizer Code Quality Build Status Coverage Status

DBFakeFiller

An easy to use tool to popuplate your database with fake data. Based on doctrine/dbal so it should me cross plateforme. Fake data are generated using fzaninotto/faker.

This tool will parse your database schema, generate fake data and insert it inside your database. In order to generate the most valuable data, it will look into:

  • columns' types (INT, VARCHAR, TEXT, TIMESTAMP, etc.)
  • columns' attributes (precision, scale, nullable, unsigned, etc.)
  • tables' relations to create valid relation ships (obvious, else the constraints would break)

WARNING : Always backup your database before running the DBFaker in case it breaks !

Install

composer require thecodingmachine/DBFaker

Minimalist usage

//instanciate the DBAL connectioin
$connectionParams = [
    "host" => "localhost",
    "user" => "DBU USER",
    "password" => "DB HOST",
    "port" => null,
    "dbname" => "YOUR DB NAME",
    "charset" => "utf8",
    "driverOptions" => array(
        1002 =>"SET NAMES utf8"
    )
];
$conn = new \Doctrine\DBAL\Connection($connectionParams, new Doctrine\DBAL\Driver\PDOMySql\Driver(), null, new \Doctrine\Common\EventManager());

$faker = new \DBFaker\DBFaker($conn);

//say what tables should be filled (other tables will be considered as reference table or at least already filled)
$faker->setFakeTableRowNumbers([
    "table1" => 300, //generate 300 lines for table 1
    "table2" => 20,
    //...
]);
$faker->fakeDB();

... now have a look at you database :) nice isn't it ?

Advanced usage

Custom data generators

By default, fake data is generated regarding the information retrieved about the column. You can pass a GeneratorFactory instance to DBFaker's constructor in order to specify a custom FakeDataGeneratorInterface.

$generatorFactory = new \DBFaker\Generators\GeneratorFactory();

/* 
Don't hesitate to use the Faker package to generate random data,
there is plenty of data types available (IBAN, address, country code, ...).
You can evenpass the locale to generate localized data !
*/
$generator = \Faker\Factory::create();

//... add your custom generators ...
//... and injectc the Generator Factory
$faker = new \DBFaker\DBFaker($conn, $generatorFactory);
);

Customize for column

You may use the GeneratorFactory::setGeneratorForColumn() to set a specific generator for a given column :

// address.postal_code column is a varchar, so default generated data will be text. Here we want a postal code :
$generatorFactory->setGeneratorForColumn(
    "address", "postal_code", function() use ($generator){ return $generator->postcode; }
);

setGeneratorForColumn takes 3 arguments : string $tableName and string $ColumnName to specify which the column and the third argument can be either :

  • a callable that takes the \Doctrine\DBAL\Schema\Column $column as input parameter,
  • a DBFaker\Generators\FakeDataGeneratorInterface instance

Customize by condition

For more flexible customization, you can use GeneratorFactory::setGeneratorForColumn().

// all columns that end with "_email" or are named exactly "email" should be emails
$generatorFactory->setGenerator(
    function(\Doctrine\DBAL\Schema\Column $column){
        return preg_match("/([(.*_)|_|]|^)email$/", $column->getName()) === 1;
    }, function() use ($generator){
        return $generator->email;
    }
);

This time, first argument is a callable that takes the \Doctrine\DBAL\Schema\Column $column as input parameter and should return a boolean (true if override should happend). The Second argument is the same as for the previous setGeneratorForColumn function.

Set null probability

dbfaker's People

Contributors

moufmouf avatar nguyenk avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dbfaker's Issues

Random number for doubles is broken

We use this formula for random numbers:

return round($min + mt_rand() / mt_getrandmax() * ($max - $min), $nbMaxDecimals);

With $max == 1.79e308 and $min == -1.79e308.

BUT.... $max-$min > $max... therefore, $max-$min == INF!

php > $max = 1.79e+308;
php > $min = -1.79e+308;
php > echo $max-$min;
INF

Optimize BlobGenerator

BlobGenerator is performing a "glob" in the __invoke call.
This means that for each row, we call "glob" to fetch all the files from a directory.
It would be way more efficient to do the "glob" call in the BlogGeneratorFactory and to pass to the BlobGenerator directly the list of files.

Logging from console

Dbfaker class should use a PSR3 compatible logger. Then, you can turn the symfomy/console output object into a PSR3 logger and object it into Dbfaker.
To do this cleanly without relying on a setter for the logger, you will have to use a dbfakerfactory.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.