agashe / sigmaphp-db
PHP Database Tools
Requires
- doctrine/inflector: 2.*
Requires (Dev)
- phpunit/phpunit: ^9.5
README
SigmaPHP-DB is a collection of PHP Database Tools. That support primarily MySQL RDBMS (Other RDBMS support will be added in future). Including migrations with rollback , seeding , query builder , ORM and much more , all these features can be used through an elegant CLI script in your terminal.
Installation
composer require agashe/sigmaphp-db
Configurations
After installation , you should create a new config file , to include your database connection parameters , and also edit other options like migrations / seeders paths.
To generate new config file , run the following command :
php ./vendor/bin/sigma-db create:config
A new config file with name database.php wil be created in the root of your project's directory.
You can simply change the name and the location of the config file , but this will require you , to pass the config file path , when use the CLI script :
php ./vendor/bin/sigma-db migrate --config=/path/to/my-config.php
Documentation
CLI Usage
In the table below , you can find all available commands :
Command | Description |
create:config {path} | Create new config file, if no path was provided , a default config file (database.php) will be created in the root of the project's folder. |
create:migration {migration name} | Create new migration file. It's recommended for the migration file to follow class naming rules , like using only nouns and PascalCase .. etc. Also no need to add .php extension , "Migration.php". will be appended automatically to the file name. |
create:model {model name} | Create new model class. In addition this command will generate a migration file for creating the corresponding table. |
create:seeder {seeder name} | Create new seeder file. Please note that , the same rules for naming the migration file is also applied to the seeder. |
drop | Drop all tables in the database. A confirmation message will ask you to confirm before executing this command. |
fresh | Drop all tables in the database. then will run all migrations and seed the database. (will ask for confirmation) |
help | Print a list by all available commands. |
migrate {migration name} | Run all migrations files. Optionally you can pass migration file name , to run specific migration. |
rollback {date} | Rollback latest migration. or choose specific date to rollback to. |
seed {seeder name} | Run seeders. or run specific seeder. |
version | Print the current version of SigmaPHP-DB Package. |
truncate | Delete the data in all tables. (will ask for confirmation) |
And here few examples on how to use the commands:
php ./vendor/bin/sigma-db create:migration UsersTable
// will create UsersTableMigration.php file into /path/to/migrations
php ./vendor/bin/sigma-db create:seed UsersRolesSeeder
// run seeder UsersRolesSeeder.php
php ./vendor/bin/sigma-db create:rollback 2023-1-20
// rollback all migrations up to 2023-1-20 , the migrations running dates all saved into the migrations logs table (default name is db_logs). And of course you can change it in the config file
php ./vendor/bin/sigma-db drop
// to drop all tables
php ./vendor/bin/sigma-db fresh --config=/path/to/db-testing-config.php
// to drop all tables then migrate and seed , and in this example we assume that you have put the config in your path of choice
License
(SigmaPHP-DB) released under the terms of the MIT license.