Function Signature
Description
Dumps the current database schema to a SQL file. This creates a snapshot of your database structure (tables, indexes, constraints, etc.) that can be used for version control, documentation, or backup purposes. The schema dump is automatically created after running migrations ifdump_schema is enabled.
Parameters
The database connection URL:
- PostgreSQL:
postgres://user:password@host:port/database?sslmode=disable - MySQL:
mysql://root:password@localhost:3306/app - MariaDB:
mariadb://root:password@localhost:3307/app - SQLite:
sqlite://./database.sqlite - LibSQL/Turso:
https://your-database.turso.io
Authentication token for LibSQL/Turso databases. Use
None for other database types.Name of the migrations tracking table. This table will be included in the schema dump.
Path to the directory where the schema file will be saved. Example:
"./migrations".Name of the file where the schema will be dumped. Example:
"schema.sql" or "database_schema.sql".Timeout in seconds to wait for the database to be ready. Use
Some(30) for 30 seconds, or None for default timeout.Return Value
ReturnsResult<()> which:
- Returns
Ok(())if the schema was dumped successfully - Returns
Errif there was a connection error, permission issue, or dump command failure
Usage Example
PostgreSQL Example
MySQL Example
LibSQL/Turso Example
Database-Specific Behavior
PostgreSQL
- Uses SQL queries to extract schema information
- No external tools required
- Includes tables, views, indexes, constraints, and sequences
- Excludes data, only structure is dumped
MySQL
- Requires
mysqldumpbinary to be installed on the system - Dumps table structures, indexes, and constraints
- The
mysqldumpcommand is executed as a subprocess - Already available in Docker MySQL images
MariaDB
- Requires
mariadb-dumpbinary to be installed - Similar functionality to MySQL dumping
- Already available in Docker MariaDB images
- Includes table definitions, indexes, and foreign keys
SQLite
- Uses SQL queries to extract schema (no external tools needed)
- Includes all table definitions and indexes
- Lightweight and fast
- Schema is extracted using SQLite’s built-in
.schemafunctionality
LibSQL
- Uses SQL queries to extract schema (no external tools needed)
- Similar to SQLite schema dumping
- Works with both local and remote LibSQL databases
- Includes all DDL statements
Output File Format
The generated schema file contains SQL statements that recreate the database structure:Use Cases
Version Control
Pre-Deployment Backup
Documentation Generation
Automated Migration Workflow
Error Handling
The function may return errors in these cases:- Database connection failure
- Missing required dump utilities (
mysqldump,mariadb-dump) - Insufficient permissions to read database schema
- Insufficient permissions to write to the output file
- Invalid migration folder path
- Disk space issues when writing the dump file
Requirements by Database
External Tool Requirements
- PostgreSQL: No external tools needed
- MySQL: Requires
mysqldump(usually pre-installed with MySQL) - MariaDB: Requires
mariadb-dump(usually pre-installed with MariaDB) - SQLite: No external tools needed
- LibSQL: No external tools needed
Docker Considerations
When using Geni in Docker:- Use the standard Docker image (not the
-slimvariant) for MySQL/MariaDB support - The slim image doesn’t include
mysqldumpormariadb-dump - PostgreSQL, SQLite, and LibSQL work in both standard and slim images
Best Practices
-
Commit to version control: Always commit
schema.sqlto track database structure changes -
Review schema changes: Check
schema.sqldiffs in pull requests to review database changes -
Automate dumping: Enable automatic schema dumping after migrations with
dump_schema: true - Create backups: Dump schema before major migrations or deployments
-
Use consistent naming: Use the same
schema_filename across all environments -
Document schema: Keep
schema.sqlas living documentation of your database structure
See Also
- migrate_database - Automatically dumps schema after migrations
- create_database - Create database before dumping
- status_migrations - Check what changed before dumping