bakkahit/sql-migration
Composer 安装命令:
composer require bakkahit/sql-migration
包简介
SqlMigration is a Laravel package that allows you to execute custom SQL migrations directly within your Laravel application. Instead of writing SQL queries manually or relying only on Laravel's built-in migrations, this package enables you to create and run .php migration files containing custom SQL
README 文档
README
SqlMigration is a Laravel package designed for executing custom SQL migrations seamlessly within your Laravel application. This package bridges the gap between Laravel's migrations and raw SQL execution, allowing you to write powerful SQL commands directly in your migration files.
Why Use SqlMigration?
When working with complex database operations, Laravel's schema builder may not suffice. SqlMigration empowers developers to:
- Run raw SQL queries directly in migration files.
- Handle advanced database operations that require custom SQL commands.
- Integrate these capabilities seamlessly into Laravel's migration system.
Features
- SQL Command Support: Write
INSERT,UPDATE,DELETE,CREATE TABLE, and other SQL queries. - Laravel Integration: Fully compatible with Laravel's migration commands.
- Custom SQL Logic: Define any SQL query your application needs.
- Private Use: Built for internal projects and teams.
Installation
Install the package in your Laravel project with:
composer require bakkahit/sql-migration
Open the config/app.php file in your Laravel project and add the SqlMigrationServiceProvider class to the list of providers, like this
'providers' => [ // Other service providers... BakkahIT\SqlMigration\SqlMigrationServiceProvider::class, ]
Usage
Step 1: Create a SQL Migration File
You can create a new SQL migration file by using the following Artisan command:
php artisan make:sql-migration create_test_table_sql
This command will generate a migration file inside the database/migrations directory, with a name following this format: YYYY_MM_DD_HHMMSS_create_test_table_sql.php. The create_test_table_sql part of the file name is the name you provide when running the command, and it will be used in the generated migration class as well.
Step 2: Write SQL Queries in the Migration File
Once the migration file is created, you will see a class with up() and down() methods. You can define your SQL queries inside the up() method to apply changes to the database, and in the down() method to reverse them.
Example of a migration file with raw SQL to create a table:
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Support\Facades\DB; class CreateTestTableSql extends Migration { /** * Run the migrations. * * @return void */ public function up() { DB::statement('CREATE TABLE test_table ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )'); } /** * Reverse the migrations. * * @return void */ public function down() { DB::statement('DROP TABLE IF EXISTS test_table'); } }
In this example:
- The
up()method contains the SQL statement to create thetest_table table. - The
down()method contains the SQL statement to drop thetest_table table.
Step 3: Run Migrations
After writing the SQL commands in your migration file, you can run the migrations as usual using the following Artisan command:
php artisan migrate
This will execute all pending migrations, including the SQL-based migrations.
Step 4: Rollback Migrations (Optional)
If you want to rollback the most recent migrations (including the SQL migrations), you can use the following command:
php artisan migrate:rollback
This will call the down() method in each migration, reversing any changes made in the up() method.
Notes
- SQL Query Validity: Ensure the SQL queries in your migration files are valid for your database engine (e.g., MySQL, PostgreSQL).
- No Table Modifications: This package doesn't modify tables automatically. It simply runs the SQL queries you've written in the migration file.
- Custom Queries: You can write any custom SQL logic in your migrations as needed.
Requirements
- Laravel: This package requires Laravel 8 or later.
- Database: It supports MySQL and other databases that allow raw SQL execution.
Contributing
We welcome contributions to the package! If you'd like to improve the package, fix bugs, or add new features, feel free to fork the repository and submit a pull request.
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Write tests to cover your changes.
- Submit a pull request.
License
The SqlMigration package is open-source software licensed under the MIT License.
bakkahit/sql-migration 适用场景与选型建议
bakkahit/sql-migration 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 63 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 11 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sql」 「migration」 「laravel」 「SqlMigration」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bakkahit/sql-migration 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bakkahit/sql-migration 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bakkahit/sql-migration 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Query filtering in your frontend
Lets you add foreign keys in a swift! Foreign key adder in laravel migration.
Database/ORM-agnostic query construction helper
Manage PostgreSQL schemas in Laravel applications
Bitrix db migration core libs
Laravel SQL Queries Dumper
统计信息
- 总下载量: 63
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-11-24