A Rust command-line tool that converts SQL database dumps to CSV files. Table to CSV parses SQL files containing CREATE TABLE and INSERT statements and extracts the data into separate CSV files for each table.
- SQL Parsing: Automatically detects table schemas from
CREATE TABLEstatements - Data Extraction: Extracts data from
INSERTstatements and converts to CSV format - Multiple Tables: Handles databases with multiple tables, creating separate CSV files
- Date Filtering: Filter rows by date range using
--date-filteroption - Parallel Processing: Fast multi-threaded CSV generation using Rayon
- Robust Value Parsing: Properly handles quoted strings, escaped characters, and SQL functions like
replace() - Error Handling: Comprehensive error handling with helpful error messages
- Cross-Platform: Built with Rust for excellent performance and cross-platform compatibility
- Rust (latest stable version)
The easiest way to install Table to CSV is via cargo:
cargo install table-to-csvThis will install the table-to-csv binary to your Cargo bin directory (typically ~/.cargo/bin/).
- Clone or download this repository
- Navigate to the project directory
- Build the project:
cargo build --releaseThe executable will be created at target/release/table-to-csv.
table-to-csv <sql_file>table-to-csv <sql_file> --date-filter <column_name> <start_date> [end_date]Or if built from source:
./target/release/table-to-csv <sql_file> [--date-filter <column_name> <start_date> [end_date]]Or using Cargo (when developing):
cargo run <sql_file> [-- --date-filter <column_name> <start_date> [end_date]]# Using the installed binary (after cargo install table-to-csv)
table-to-csv database.sql
# Convert a database dump to CSV files (when developing)
cargo run database.sql
# Using the compiled binary from source
./target/release/table-to-csv test.sql
# Filter by date range (column name: createdAt, from 2024-01-01 to 2024-12-31)
table-to-csv database.sql --date-filter createdAt 2024-01-01 2024-12-31
# Filter from a start date to today (end date defaults to today)
table-to-csv database.sql --date-filter date 2023-06-15
# Using Cargo with date filter
cargo run database.sql -- --date-filter createdAt 2024-01-01Date Format: YYYY-MM-DD
Note: If end_date is not provided, it defaults to today's date
- Schema Detection: Parses
CREATE TABLEstatements to extract table names and column definitions - Data Extraction: Finds
INSERTstatements for each table and extracts the values - Value Processing: Handles SQL-specific formatting including:
- Quoted strings (single and double quotes)
- Escaped characters (
''for single quotes,""for double quotes) - SQL functions like
replace()for JSON data
- Date Filtering (optional): Filters rows based on date column values within specified date range
- Parallel Processing: Uses Rayon to process multiple tables concurrently for better performance
- CSV Generation: Creates properly formatted CSV files with headers and data
Given a SQL file like test.sql:
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE
);
INSERT INTO users VALUES(1, 'Alice Smith', '[email protected]');
INSERT INTO users VALUES(2, 'Bob Johnson', '[email protected]');Table to CSV will generate users.csv:
id,name,email
1,Alice Smith,[email protected]
2,Bob Johnson,[email protected]CREATE TABLEstatements with various column typesINSERT INTO ... VALUESstatements- Single and double-quoted string values
- Escaped quotes in string values
- SQL
replace()function calls - Multi-line table definitions
- Foreign key constraints (ignored during parsing)
- Date/timestamp columns for filtering (supports various date formats)
csv- CSV file reading and writingregex- Regular expression pattern matchinganyhow- Error handlingrayon- Parallel processing for improved performancechrono- Date and time parsing for date filtering
Run the test suite:
cargo testThe project includes unit tests for:
- Table column parsing
- Value cleaning and unescaping
- CSV value parsing
The tool provides clear error messages for common issues:
- Missing command-line arguments
- File not found
- SQL parsing errors
- CSV writing errors
- Invalid date formats
- Date filter column not found
To build release binaries for all macOS architectures, use the included build script:
./build-release.shThis will:
- Build for x86_64 (Intel Macs)
- Build for aarch64 (Apple Silicon Macs)
- Create a universal binary (works on both)
- Generate compressed archives
- Create SHA256 checksums
All distribution files will be in the dist/ directory.
The project includes a GitHub Actions workflow that automatically builds binaries for:
- macOS (Intel and Apple Silicon)
- Linux (x86_64 and ARM64)
- Windows (x86_64)
Simply update the version in Cargo.toml and push to main:
# Edit Cargo.toml and change the version
# Example: version = "0.4.1"
git add Cargo.toml
git commit -m "Bump version to 0.4.1"
git push origin mainGitHub Actions will automatically:
- Detect the version change
- Create a git tag (e.g.,
v0.4.1) - Build binaries for all platforms
- Create a GitHub Release with all downloadable archives
Alternatively, you can manually create and push a tag:
git tag v0.4.1
git push origin v0.4.1You can also create a release directly from the GitHub web interface:
- Go to "Releases" → "Create a new release"
- Click "Choose a tag" → Type a new tag (e.g.,
v0.4.1) - Click "Create new tag on publish"
- The workflow will automatically build and attach binaries to the release
This project is open source. Please check the license file for details.
Contributions are welcome! Please feel free to submit issues and pull requests.
When processing a SQL file, Table to CSV will output:
Processing SQL file: database.sql
Found table: CallLogs with 9 columns
Found table: CallSession with 9 columns
Found table: d1_migrations with 3 columns
Created calllogs.csv with 35 rows
Created callsession.csv with 89 rows
Created d1_migrations.csv with 3 rows
Conversion complete!
Generated CSV files:
- calllogs.csv
- callsession.csv
- d1_migrations.csv
To view the CSV files, you can use:
cat calllogs.csv | head -5
cat callsession.csv | head -5
Or open them in a spreadsheet application.
When using the date filter feature:
Date filter enabled:
Column: createdAt
Start date: 2024-01-01
End date: 2024-12-31
Processing SQL file: database.sql
Found table: CallLogs with 9 columns
Found table: CallSession with 9 columns
Found table: d1_migrations with 3 columns
Created calllogs.csv with 15 rows
Created callsession.csv with 42 rows
Warning: No rows remain for table 'd1_migrations' after filtering - skipping
Conversion complete!
Generated CSV files:
- calllogs.csv
- callsession.csv
To view the CSV files, you can use:
cat calllogs.csv | head -5
cat callsession.csv | head -5
Or open them in a spreadsheet application.