GithubHelp home page GithubHelp logo

dummify / dummify.php Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 136 KB

Programmatically dummifies your database to non-sensitive data for development use!

License: MIT License

PHP 100.00%
dummy-data dummy fake-data faker database sensitive-data sql-server mysql php

dummify.php's Introduction

Dummify.php

Programmatically dummifies your database to non-sensitive data for development use!

Build Status StyleCI Scrutinizer Code Quality

TL;DR

// Use an array of parameters to connect to a database
$connection = ['driver' => 'sqlite', 'database' => ':memory:'];

// You may populate your database with dummy data
$faker = Faker\Factory::create();

Dummify::connectTo($connection)
->from('users')
->insert(function($row) {
  $row->name = $faker->name;
  $row->email = $faker->email;
  return $row;
});

// Or you can dummify with a new rule
Dummify::connectTo($connection)
->from('users', function ($query) {
  return $query->where('email', '[email protected]'); // (Optional)
})
->update(function ($row) {
  $row->email = '[email protected]';
  return $row;
})

Install Dummify

Thanks to Composer it is quite easy!

$ composer require --dev dummify/dummify.php

And on your code:

include '/vendor/autoload.php';

use Dummify\Dummify;

Setup a connection

Using Illuminate\Database capsule for database connections, Dummify can connect to:

  • MySQL
  • PostgreSQL
  • SQL Server
  • SQLite

To create a new connection you need an array of parameters like this one:

MySQL/MariaDB connection

There is an example here!

$connection = [
  'driver' => 'mysql',
  'host' => '127.0.0.1',
  'port' => '3306',
  'database' => 'example',
  'username' => 'root',
  'password' => '',
  'unix_socket' => '',
  'charset' => 'utf8mb4',
  'collation' => 'utf8mb4_unicode_ci',
  'prefix' => '',
  'strict' => true,
  'engine' => null,
];
PostgreSQL connection

There is an example here!

$connection = [
  'driver' => 'pgsql',
  'host' => '127.0.0.1',
  'port' => '5432',
  'database' => 'example',
  'username' => 'root',
  'password' => '',
  'charset' => 'utf8',
  'prefix' => '',
  'schema' => 'public',
  'sslmode' => 'prefer',
];
SQL Server connection

There is an example here!

$connection = [
  'driver' => 'sqlsrv',
  'host' => '127.0.0.1'),
  'port' => '1433',
  'database' => 'example',
  'username' => 'root',
  'password' => '',
  'charset' => 'utf8',
  'prefix' => '',
];
SQLite connection

There is an example here!

$connection = [
  'driver' => 'sqlite',
  'database' => '/static/path/to/database.sqlite',
  'prefix' => '',
];

Or you can use in memory connection like this:

$connection = [
  'driver' => 'sqlite',
  'database' => ':memory:',
  'prefix' => '',
];

Instantiate a Dummify

Once you have your connection array you can connect into your database using:

$dummify = Dummify::connectTo($connection)

Later you may choose a table using the from($table) method.

$dummify->from('users')

Populate a table with dummy data

You may populate a table using the insert(callable $callable, $iterations = 1) method. In this case we are using Faker to help us generate random data!

$faker = Faker\Factory::create();

$dummify
  ->from('users')
  ->insert(function($row){
    $row->name = $faker->name
    $row->email = $faker->email
    return $row;
  });

// (Optional) You can pass how many you want to create
$dummify
  ->from('users')
  ->insert(function($row){
    $row->name = $faker->name
    $row->email = $faker->email
    return $row;
  }, 100);

Update a table with dummy data

You may setup how the iterator will work over each line using the update(callable $callable) method!

$faker = Faker\Factory::create();

$dummify
  ->from('users')
  ->update(function($row){
    $row->name = $faker->name
    $row->email = $faker->email
    return $row
  });

Making restrictions for updates

If you are interested on limiting or adding conditions to your SQL query, you can use all Illuminate\Database fluent syntax!

For more docs about it follow-up with Laravel docs;

$dummify->from('users', function($query) {
  return $query->where('name', 'like', '%Filipe%');
});

dummify.php's People

Contributors

filipeforattini avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

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.