A tool to populate MySQL databases with realistic dummy data
Project description
MySQL Dummy Data Populator
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:
-
Clone this repository:
git clone https://github.com/vitebski/mysql-dummy-populator.git cd mysql-dummy-populator
-
Install the package in development mode:
pip install -e .
-
Create a
.envfile 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):
- Command-line arguments
- Environment variables
- .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:
- Basic statistics about tables, views, and relationships
- Table categories (standalone, dependent, many-to-many, circular)
- Detailed information about circular dependencies
- Many-to-many relationship tables and their connections
- Recommended table insertion order for data population
- Complete ordered list of tables
This mode is useful for understanding complex database schemas and identifying potential issues before populating data.
How It Works
-
Schema Analysis: The tool analyzes your database schema to understand table relationships, foreign keys, and constraints.
-
Dependency Resolution: Tables are sorted in an order that respects foreign key dependencies, starting with tables that have no foreign keys.
-
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.
-
Many-to-Many Relationship Handling: Many-to-many relationship tables are populated after their referenced tables.
-
Data Generation: Realistic fake data is generated for each column based on its data type and constraints.
-
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:
- Check Logs: Increase log level to DEBUG for more detailed information
- Database Permissions: Ensure the user has sufficient permissions to read schema information and insert data
- Circular Dependencies: Complex circular dependencies might require manual intervention
- 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:
- Sets up a MySQL 8.0 database
- Creates a demo schema with various table types and relationships
- Runs the populator in analyze-only mode
- Populates the database with dummy data
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
585732b22d41d8921cf8ba552d32329de9cf4126b225567233efb71e6abc9e40
|
|
| MD5 |
9c6d9521e9e36e579e231e1e26a14e7e
|
|
| BLAKE2b-256 |
8bc6db126efe49d441774336c2cd74510e409df658ff7bbf26cc23d67562f661
|
File details
Details for the file mysql_dummy_populator-0.0.2-py3-none-any.whl.
File metadata
- Download URL: mysql_dummy_populator-0.0.2-py3-none-any.whl
- Upload date:
- Size: 39.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d37db6dce57178f8f248f116be46b6e1cc865e5957262921406819d7c823e7d
|
|
| MD5 |
958e8e7d8500afc121919a1dd4425931
|
|
| BLAKE2b-256 |
1345f7698c1b337bac8cf82032a5f520b6b3ba6bff7bcee0d8f5ced15ba51f3e
|