![]() |
VOOZH | about |
This document covers the database utility commands (db:connect, db:query, and migrate) that provide direct access to the Maho database using native database clients and manage schema updates. These commands automatically use the database credentials configured in the application.
For general CLI architecture and command discovery, see CLI Architecture. For database abstraction and model-level database access, see Database Layer.
The database commands provide administrators and developers with quick command-line access to the Maho database without manually entering connection credentials. These commands support all three database engines used by Maho: MySQL/MariaDB, PostgreSQL, and SQLite. Additionally, the migration command handles the execution of setup scripts to keep the database schema in sync with the code.
Opens an interactive database client session using credentials from the application configuration lib/MahoCLI/Commands/DBConnect.php21-132
Command: ./maho db:connect
Behavior:
global/resources/default_setup/connection lib/MahoCLI/Commands/DBConnect.php34-36mysql with a temporary .my.cnf config file for secure authentication lib/MahoCLI/Commands/DBConnect.php46-62psql with a temporary .pgpass file set via the PGPASSFILE environment variable lib/MahoCLI/Commands/DBConnect.php64-84sqlite3 pointing to var/db/{dbname} database file lib/MahoCLI/Commands/DBConnect.php86-96proc_open to spawn the client subprocess maintaining an interactive shell session until exit lib/MahoCLI/Commands/DBConnect.php104-131Executes a single SQL query and outputs the result using native database clients lib/MahoCLI/Commands/DBQuery.php22-152
Command: ./maho db:query "SELECT * FROM core_config_data LIMIT 5"
Behavior:
db:connect but non-interactively..my.cnf file and running mysql --defaults-extra-file=... -e "query" lib/MahoCLI/Commands/DBQuery.php58-75.pgpass file, running psql with -c "query" lib/MahoCLI/Commands/DBQuery.php77-98sqlite3 -header -column var/db/{dbname} "query" lib/MahoCLI/Commands/DBQuery.php100-111Runs the Maho database migration system (./maho migrate) to apply pending schema and data updates automatically.
Behavior:
sql subdirectories of all modules.TIMESTAMP columns to DATETIME for consistent handling across engines app/code/core/Mage/Core/sql/maho_setup/maho-26.5.0.php19-79ON UPDATE CURRENT_TIMESTAMP, shifting management to PHP application logic app/code/core/Mage/Catalog/sql/maho_setup/maho-26.5.0.php17-25Sources:
lib/MahoCLI/Commands/DBConnect.php21-132
lib/MahoCLI/Commands/DBQuery.php22-152
app/code/core/Mage/Core/sql/maho_setup/maho-26.5.0.php19-79
app/code/core/Mage/Catalog/sql/maho_setup/maho-26.5.0.php17-25
Database CLI commands extend the BaseMahoCommand abstract base class, which initializes the Maho application environment before command execution. The commands use the DatabaseCliTrait to share helper methods for determining the database engine and creating temporary authentication files.
Title: Database Command Class Structure
This hierarchy reflects the Symfony Console integration and modular design. Both commands reuse the trait for common database client preparation logic.
Sources:
lib/MahoCLI/Commands/DBConnect.php21-132
lib/MahoCLI/Commands/DBQuery.php22-152
lib/MahoCLI/Commands/BaseMahoCommand.php13-26
Maho supports multiple database backends via adapters located at Maho\Db\Adapter\Pdo. The CLI commands adapt to the configured engine, supporting:
| Engine | Config Identifier | CLI Client | Connection Details |
|---|---|---|---|
| MySQL | mysql | mysql | Uses mysql --defaults-extra-file={file} {dbname} for authentication via temporary file lib/MahoCLI/Commands/DBConnect.php55-59 |
| PostgreSQL | pgsql | psql | Uses psql with PGPASSFILE environment variable for password lib/MahoCLI/Commands/DBConnect.php74-81 |
| SQLite | sqlite | sqlite3 | Uses SQLite DB file path under var/db/{dbname} lib/MahoCLI/Commands/DBConnect.php88-93 |
Title: Database Command Execution Flow Sequence
This flow covers interactive connection and query execution, with temporary config file handling for credentials passed securely to native clients.
Sources:
lib/MahoCLI/Commands/DBConnect.php30-44
lib/MahoCLI/Commands/DBQuery.php41-56
lib/MahoCLI/Commands/BaseMahoCommand.php20-24
Maho relies on specialized PDO adapter classes extending AbstractPdoAdapter that internally use Doctrine DBAL connections. Each adapter implements platform-specific details such as data type mappings, connection initialization, and features.
MySQL Adapter:
Maho\Db\Adapter\Pdo\Mysql
TYPE_DATETIME → datetime)SQL_MODE empty and enforces UTC timezone for timestamp consistencyPostgreSQL Adapter:
Maho\Db\Adapter\Pdo\Pgsql
TYPE_BLOB → bytea, TYPE_DATETIME→timestamp)raw_query() method to retry on lost connections automaticallySQLite Adapter:
Maho\Db\Adapter\Pdo\Sqlite
TYPE_BOOLEAN → INTEGER)REGEXP, GREATEST, LEAST) to emulate missing features for parity with MySQL/PostgreSQLpreg_match to implement REGEXP in SQLite lib/Maho/Db/Adapter/Pdo/Sqlite.php19-190Maho defines common data types in Maho\Db\Ddl\Table and translates them into engine-specific SQL types via adapters.
Title: DDL Type Translation Mapping
This mapping ensures that migrations and schema definitions use a unified set of constants, improving portability.
Sources:
lib/Maho/Db/Adapter/Pdo/Mysql.php44-60
lib/Maho/Db/Adapter/Pdo/Pgsql.php45-71
lib/Maho/Db/Adapter/Pdo/Sqlite.php41-57
lib/Maho/Db/Adapter/AbstractPdoAdapter.php152-154
The database commands suite offers direct and native database interaction utilities as CLI commands, enhancing developer productivity when managing Maho stores. They abstract authentication details, adapt to the configured DB engine, and provide consistent cross-engine behavior through unified adapters and migration scripts.
The architecture tightly integrates with the Symfony Console framework, sharing traits for common functionality, and employs platform-specific PDO adapters with Doctrine DBAL to manage connection details and SQL dialect peculiarities. Migration commands evolve the database schema while maintaining compliance across supported engines.
Administrators and developers can use these commands to connect interactively or run SQL queries non-interactively, with consistent environment preparation and secure credential handling.
Sources:
lib/MahoCLI/Commands/DBConnect.php21-132
lib/MahoCLI/Commands/DBQuery.php22-152
app/code/core/Mage/Core/sql/maho_setup/maho-26.5.0.php19-79
app/code/core/Mage/Catalog/sql/maho_setup/maho-26.5.0.php17-25
lib/Maho/Db/Adapter/Pdo/Mysql.php20-155
lib/Maho/Db/Adapter/Pdo/Pgsql.php21-190
lib/Maho/Db/Adapter/Pdo/Sqlite.php19-190
lib/Maho/Db/Adapter/AbstractPdoAdapter.php24-154