Skip to main content

A tool to populate MySQL databases with realistic dummy data

Project description

MySQL Dummy Data Populator

Unit Tests E2E Tests codecov Python 3.8+ MySQL 8.0+ License: MIT

A Python tool that populates MySQL databases with realistic dummy data, handling foreign keys, circular dependencies, and many-to-many relationships.

Project Status

The badges at the top of this README provide at-a-glance information about the project:

  • Unit Tests: Status of the automated unit tests
  • E2E Tests: Status of the end-to-end tests with a real MySQL database
  • Code Coverage: Percentage of code covered by tests
  • Python 3.8+: Indicates Python version compatibility
  • MySQL 8.0+: Indicates MySQL version compatibility
  • License: MIT: Shows the project's license

Features

  • Analyzes database schema to understand table relationships
  • Resolves foreign key dependencies and sorts tables in the correct order for population
  • Detects and handles circular dependencies
  • Identifies many-to-many relationship tables
  • Generates realistic fake data using the Faker library
  • Supports all MySQL 8 data types
  • Honors constraints like NOT NULL, UNIQUE, and CHECK
  • Configurable number of records per table
  • Detailed logging and error reporting

Requirements

  • Python 3.6+
  • MySQL 8.0+
  • Required Python packages (see requirements.txt)

Installation

Option 1: Install from PyPI (Recommended)

You can install the package directly from PyPI:

pip install mysql-dummy-populator

Option 2: Install from Source

If you want to install from source:

  1. Clone this repository:

    git clone https://github.com/vitebski/mysql-dummy-populator.git
    cd mysql-dummy-populator
    
  2. Install the package in development mode:

    pip install -e .
    
  3. Create a .env file with your MySQL connection details:

    # Copy the sample file
    cp .env.sample .env
    
    # Edit the .env file with your database details
    nano .env  # or use your preferred editor
    

Configuration

The tool can be configured in three ways, with the following priority order (highest to lowest):

  1. Command-line arguments
  2. Environment variables
  3. .env file

.env File Configuration

The tool automatically looks for a .env file in the current directory. You can use the provided .env.sample as a template:

# MySQL Database Connection
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=your_password
MYSQL_DATABASE=your_database
MYSQL_PORT=3306

# Data Generation Settings
MYSQL_RECORDS=10
MYSQL_LOCALE=en_US

# Application Settings
MYSQL_LOG_LEVEL=INFO

You can also specify a different .env file location using the --env-file parameter.

Environment Variables

All configuration options can be set using environment variables with the MYSQL_ prefix:

export MYSQL_HOST=localhost
export MYSQL_USER=root
export MYSQL_PASSWORD=your_password
export MYSQL_DATABASE=your_database

Usage

Command Line

If you installed the package via pip, you can run the tool directly from the command line:

mysql-dummy-populator

Or specify parameters directly:

mysql-dummy-populator --host localhost --user root --password your_password --database your_database --records 20

You can also specify a custom .env file:

mysql-dummy-populator --env-file /path/to/custom/.env

If you installed from source, you can run the tool using:

# If installed in development mode
mysql-dummy-populator

# Or using the Python module directly
python -m main

Available Options

  • --host: MySQL host (default: from MYSQL_HOST env var or .env file)
  • --user: MySQL user (default: from MYSQL_USER env var or .env file)
  • --password: MySQL password (default: from MYSQL_PASSWORD env var or .env file)
  • --database: MySQL database name (default: from MYSQL_DATABASE env var or .env file)
  • --port: MySQL port (default: from MYSQL_PORT env var or .env file, or 3306)
  • --records: Number of records per table (default: from MYSQL_RECORDS env var or .env file, or 10)
  • --locale: Locale for fake data generation (default: from MYSQL_LOCALE env var or .env file, or en_US)
  • --log-level: Log level (default: from MYSQL_LOG_LEVEL env var or .env file, or INFO)
  • --env-file: Path to .env file (default: .env)
  • --analyze-only: Only analyze the database schema without populating data

Analyze-Only Mode

You can use the tool to analyze your database schema without actually populating any data:

mysql-dummy-populator --analyze-only

This will generate a detailed report about your database schema, including:

  1. Basic statistics about tables, views, and relationships
  2. Table categories (standalone, dependent, many-to-many, circular)
  3. Detailed information about circular dependencies
  4. Many-to-many relationship tables and their connections
  5. Recommended table insertion order for data population
  6. Complete ordered list of tables

This mode is useful for understanding complex database schemas and identifying potential issues before populating data.

How It Works

  1. Schema Analysis: The tool analyzes your database schema to understand table relationships, foreign keys, and constraints.

  2. Dependency Resolution: Tables are sorted in an order that respects foreign key dependencies, starting with tables that have no foreign keys.

  3. Circular Dependency Detection: The tool identifies circular dependencies (e.g., Table A references Table B, which references Table A) and handles them using a multi-pass approach.

  4. Many-to-Many Relationship Handling: Many-to-many relationship tables are populated after their referenced tables.

  5. Data Generation: Realistic fake data is generated for each column based on its data type and constraints.

  6. Data Insertion: Data is inserted into tables in the correct order, ensuring foreign key constraints are satisfied.

Supported Data Types

The tool supports all MySQL 8 data types, including:

  • Numeric types: INT, TINYINT, SMALLINT, MEDIUMINT, BIGINT, FLOAT, DOUBLE, DECIMAL
  • String types: CHAR, VARCHAR, TEXT, TINYTEXT, MEDIUMTEXT, LONGTEXT
  • Date and time types: DATE, DATETIME, TIMESTAMP, TIME, YEAR
  • Binary types: BINARY, VARBINARY, BLOB, TINYBLOB, MEDIUMBLOB, LONGBLOB
  • Other types: ENUM, SET, BIT, BOOLEAN, JSON

Handling Constraints

The tool respects various MySQL constraints:

  • NOT NULL: Ensures non-null values are generated
  • UNIQUE/PRIMARY KEY: Generates unique values for these columns
  • FOREIGN KEYS: References existing values in the referenced tables
  • CHECK: Honors check constraints (via column comments with BETWEEN)
  • Type Ranges: Respects the valid ranges for each data type

Troubleshooting

If you encounter issues:

  1. Check Logs: Increase log level to DEBUG for more detailed information
  2. Database Permissions: Ensure the user has sufficient permissions to read schema information and insert data
  3. Circular Dependencies: Complex circular dependencies might require manual intervention
  4. Memory Usage: For large databases, consider reducing the number of records per table

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Automated Testing

This project uses GitHub Actions for automated testing to ensure code quality and functionality.

Unit Tests

The unit tests workflow runs all the unit tests in the project across multiple Python versions:

  • Python 3.8
  • Python 3.9
  • Python 3.10
  • Python 3.11

To run the unit tests locally:

# Activate your virtual environment
source venv/bin/activate

# Run all unit tests
python -m unittest discover -p "test_*.py"

# Run tests with coverage report
pip install coverage
coverage run -m unittest discover -p "test_*.py"
coverage report

End-to-End Tests

The E2E tests workflow:

  1. Sets up a MySQL 8.0 database
  2. Creates a demo schema with various table types and relationships
  3. Runs the populator in analyze-only mode
  4. Populates the database with dummy data
  5. Verifies that all tables have been populated correctly

To run the E2E tests locally, you'll need a MySQL instance:

# Create the demo schema
mysql -h localhost -u your_user -p your_database < tests/schemas/demo_schema.sql

# Run the populator
mysql-dummy-populator --host localhost --user your_user --password your_password --database your_database --verify

The demo schema includes:

  • Tables with primary keys
  • Tables with foreign keys
  • Tables with circular dependencies
  • Many-to-many relationship tables
  • Tables with check constraints
  • Tables with columns using MySQL reserved keywords

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mysql_dummy_populator-0.0.2.tar.gz (49.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mysql_dummy_populator-0.0.2-py3-none-any.whl (39.1 kB view details)

Uploaded Python 3

File details

Details for the file mysql_dummy_populator-0.0.2.tar.gz.

File metadata

  • Download URL: mysql_dummy_populator-0.0.2.tar.gz
  • Upload date:
  • Size: 49.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mysql_dummy_populator-0.0.2.tar.gz
Algorithm Hash digest
SHA256 585732b22d41d8921cf8ba552d32329de9cf4126b225567233efb71e6abc9e40
MD5 9c6d9521e9e36e579e231e1e26a14e7e
BLAKE2b-256 8bc6db126efe49d441774336c2cd74510e409df658ff7bbf26cc23d67562f661

See more details on using hashes here.

File details

Details for the file mysql_dummy_populator-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for mysql_dummy_populator-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8d37db6dce57178f8f248f116be46b6e1cc865e5957262921406819d7c823e7d
MD5 958e8e7d8500afc121919a1dd4425931
BLAKE2b-256 1345f7698c1b337bac8cf82032a5f520b6b3ba6bff7bcee0d8f5ced15ba51f3e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page