PHP Database Fill: Fill records of MySQL tables with test data

Recommend this page to a friend!
  Info   View files Example   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2021-01-09 (10 days ago) RSS 2.0 feedNot enough user ratingsTotal: 364 This week: 1All time: 6,752 This week: 227Up
Version License PHP version Categories
database-fill 0.54GNU General Publi...5.3PHP 5, Databases, Testing
Description Author

This class can fill records of MySQL tables with test data.

It takes the SQL dump file of a MySQL database and parses it to extract the list of database tables.

The class executes SQL statements to insert records on the MySQL database tables.

The new record field values can be generated randomly or set to a constant value.

The number of records to insert, the range of characters to use in random data and the character set encoding are configurable parameters.

Picture of Martin Latter
  Performance   Level  
Name: Martin Latter <contact>
Classes: 8 packages by
Country: United Kingdom
Age: ???
All time rank: 139963 in United Kingdom
Week rank: 17 Up1 in United Kingdom Up
Innovation award
Innovation award
Nominee: 5x

Details

Database Filler

Populate MySQL database tables with test data by parsing the SQL schema file.

Purpose

Primary

  • Database table reproduction without using any real or sensitive data: + Copy of production database tables required for query analysis, yet sensitive data cannot be imported. However, you possess a structure-only SQL schema file.

Secondary

  • Schema design and development: + Check table field population with specified datatypes, potential data truncation, etc. + Test connection encoding and character encoding, and data insertion speeds.

<br>

[1]: https://tinram.github.io/images/databasefiller-data.png ![Database-Filler database][1]

<br>

Background

Originally, I needed to populate a database containing 14 complex tables. Tools such as Spawner are ideal for populating small tables, but in this case, specifying the datatypes for 300+ fields to initiate Spawner would have been insanity.

Instead, why not parse the SQL schema?

[2]: https://tinram.github.io/images/databasefiller-execute.png ![Database-Filler execute][2]

Database Requirements

  1. The script expects the database schema to already exist in MySQL (`mysql -u root -p < test.sql`).
  2. All table names and column names in the MySQL schema require back-ticks.
  3. Unique keys must be removed from tables when the configuration array option random_data is set to false.

Other

  • The majority of MySQL datatypes are supported.
  • Any foreign keys are disabled on data population.
  • Random character generation is slow in PHP, and such slowness further depends on field length, number of fields, and the number of rows being generated.
  • Multiple INSERTs are added in a single query, which is quite fast. The number of INSERTs per second will depend on MySQL configuration settings (the defaults are not optimised), datatype / length inserted, system load, operating system, hardware etc.

Further Options

Configuration boolean toggles (false by default):

  • incremental_ints + make added integers incremental, enabling simplistic integer foreign keys.
  • populate_primary_key + populate a primary key field, e.g. a UUID used as a primary key (experimental, supports only some definitions).

Set-up

Ensure the database already exists in MySQL e.g. for the test schema:

    mysql -u root -p < test.sql

Adjust the array connection details and parameters in the file databasefiller_example.php

Then execute this file with PHP on the command-line:

    php databasefiller_example.php

or run the file through a web server e.g.

    http://localhost/Database-Filler/databasefiller_example.php

Other Projects

SQLParser would have been used in this project had it been available in 2014.

License

Database Filler is released under the GPL v.3.

  Files folder image Files  
File Role Description
Files folder imageclasses (1 file)
Accessible without login Plain text file basketball.sql Data Auxiliary data
Accessible without login Plain text file databasefiller_example.php Example Example script
Accessible without login Plain text file LICENSE Lic. Auxiliary data
Accessible without login Plain text file README.md Doc. Auxiliary data
Accessible without login Plain text file test.sql Data Auxiliary data

  Files folder image Files  /  classes  
File Role Description
  Plain text file databasefiller.class.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:364
This week:1
All time:6,752
This week:227Up
User Comments (1)
nice
3 years ago (muabshir)
80%StarStarStarStarStar
 

For more information send a message to info at phpclasses dot org.