kogoshvili/meloq

Laravel 迁移生成器

dev-main 2024-03-23 09:36 UTC

This package is not auto-updated.

Last update: 2024-09-22 10:27:06 UTC


README

Meloq 是 Laravel 的迁移生成器。它根据模型定义生成迁移文件,并使用 PHP 属性和类型提示来推断数据库模式。

示例

模型定义

#[Table(primary: "id")]
class Book extends Model
{
    public int $id;
    #[Column(name: "Author")]
    public string $author;
    #[Column(name: "Title")]
    public string $title;
    public Status $status;
    public function authors(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
    {
        return $this->belongsToMany(Author::class);
    }
}

enum Status
{
    case DRAFT;
    case PUBLISHED;
    case ARCHIVED;
}

class Author extends Model
{
    public string $name;
    #[Primary]
    public int $author_id;
    public function books() : \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(Book::class);
    }
}

生成的迁移文件

Schema::create('authors', function (Blueprint $table) {
    $table->integer('id');
    $table->string('name');
    $table->integer('author_id')->unique()->primary()->autoIncrement()->index();
});
Schema::create('books', function (Blueprint $table) {
    $table->string('Author');
    $table->string('Title');
    $table->enum('status', ['DRAFT', 'PUBLISHED', 'ARCHIVED']);
    $table->integer('id');
    $table->primary('id');
});

设置

安装软件包

composer require kogoshvili/meloq:dev-main

将服务提供者添加到 config/app.php 中的 providers 数组

'providers' => ServiceProvider::defaultProviders()->merge([
    //...
    Kogoshvili\Meloq\MeloqServiceProvider::class,
])->toArray(),

发布配置文件(可选)

php artisan vendor:publish --tag=meloq-config

用法

当第一次使用 Meloq 时,您需要记录模型定义,而不创建迁移文件。运行以下命令生成 json 模型定义,这些定义用于生成迁移文件并跟踪更改:

php artisan meloq:record

运行以下命令根据记录的模型定义生成迁移文件:

php artisan meloq:migrate

定义模型

Meloq 使用 PHP 属性和类型提示来推断数据库模式。

类型提示示例

public int $id; => $table->integer('id');
public ?string $name; => $table->string('name')->nullable();
public int $total = 0; => $table->integer('total')->default(0);

属性示例

#[Column(name: "Author", type: "string", nullable: true)]
public string $author; => $table->string('Author')->nullable();

属性

#[Column(name: "Author", type: "string", nullable: true)]
public string $author; => $table->string('Author')->nullable();
#[Table(name: "books", primary: "id")]
  • name: 表名(默认:模型类名称的复数形式)
  • primary: 主键列名称(默认:null)
#[Column(name: "author", type: "string", comment: null, precision: null, scale: null, nullable: false, unique: false, primary: false, increment: false, index: false, default: null, value: null, foreignKey: null, referenceKey: null, referenceTable: null)]
  • name: 列名称(默认:属性名称)
  • type: 列类型(默认:类型提示,例如 int -> integer,string -> string)
  • comment: 列注释(默认:null)
  • precision: 列精度(默认:null)
  • scale: 列比例(默认:null)
  • nullable: 列是否可为空(默认:?类型提示,例如 ?int -> true,int -> false)
  • unique: 列是否唯一(默认:false)
  • primary: 列是否为主键(默认:false)
  • increment: 列是否为自增(默认:false)
  • index: 列是否为索引(默认:false)
  • default: 默认值(默认:null)
  • value: 列值(默认:null)
  • foreignKey: 外键列名称(默认:null)
  • referenceKey: 参考键列名称(默认:null)
  • referenceTable: 参考表名称(默认:null)
#[Timestamp(name: "created_at", precision: 0)]
  • name: 列名称(默认:属性名称)
  • precision: 时间戳精度(默认:0)
#[Primary(name: "author_id", type: "int", increment: true, comment: null)]
  • name: 列名称(默认:属性名称)
  • type: 列类型(默认:类型提示,例如 int -> integer,string -> string)
  • increment: 列是否为自增(默认:false)
  • comment: 列注释(默认:null)
#[Ignore]
  • 忽略模型定义中的属性。

关系

Meloq 依赖于关系方法的返回类型来推断关系类型,因此必须显式定义返回类型。例如

public function license() : \Illuminate\Database\Eloquent\Relations\HasOne
{
    return $this->hasOne(License::class);
}
public function appointments() : \Illuminate\Database\Eloquent\Relations\HasMany
{
    return $this->hasMany(Appointment::class);
}
Schema::create('licenses', function (Blueprint $table) {
    $table->foreignId('client_id')->references('id')->on('drivers');
});
Schema::create('appointments', function (Blueprint $table) {
    $table->foreignId('doctor_id')->references('id')->on('doctors');
});

注意事项

  • 不要在具有 BelongsToMany 关系的情况下定义 id 列,否则您将收到“Typed property App\Models\Book::$id must not be accessed before initialization”错误。相反,在 Table 属性中定义主键,例如。
#[Table(primary: "id")]
class Book extends Model
{
    // public int $id
    public function authors(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
    {
        return $this->belongsToMany(Author::class);
    }
}

待办事项

  • 为外键添加删除和更新操作的支持。
  • 为列添加 after 和 before 操作的支持。
  • 找出如何修复注意事项。