ariga/atlas-provider-doctrine

PHP doctrine ORM 的 Atlas 提供者

v4.0.3 2024-03-27 14:07 UTC

This package is auto-updated.

Last update: 2024-09-27 15:17:19 UTC


README

Doctrine 实体加载到 Atlas 项目中。

用例

  1. 声明式迁移 - 使用类似于 Terraform 的 atlas schema apply --env doctrine 命令将您的 Doctrine 架构应用到数据库中。
  2. 自动迁移规划 - 使用 atlas migrate diff --env doctrine 命令自动规划从当前数据库版本到 Doctrine 架构的迁移。

要求

  • DBAL - composer require doctrine/dbal:^4

安装

在 macOS 或 Linux 上运行以下命令安装 Atlas

curl -sSf https://atlasgo.sh | sh

有关更多安装选项,请参阅 atlasgo.io

运行以下命令安装提供者

composer require ariga/atlas-provider-doctrine:^4

Doctrine 控制台命令

如果您的所有 Doctrine 实体都位于单个目录下,您可以将 atlas-provider 命令添加到 Doctrine 控制台文件中

#!/usr/bin/env php
<?php

use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;

require 'bootstrap.php';
+ require "vendor/ariga/atlas-provider-doctrine/src/Command.php";

ConsoleRunner::run(
    new SingleManagerProvider($entityManager),
+   [new AtlasCommand()]
);

然后在您的项目目录中,创建一个名为 atlas.hcl 的新文件,其内容如下

data "external_schema" "doctrine" {
  program = [
    "php",
    "bin/doctrine", // path to your Doctrine Console file
    "atlas:schema",
    "--path", "./path/to/entities",
    "--dialect", "mysql" // mariadb | postgres | sqlite | sqlserver
  ]
}

env "doctrine" {
  src = data.external_schema.doctrine.url
  dev = "docker://mysql/8/dev"
  migration {
    dir = "file://migrations"
  }
  format {
    migrate {
      diff = "{{ sql . \"  \" }}"
    }
  }
}

作为 Symfony Bundle

如果您正在使用 Symfony 项目,您可以将提供者用作 Symfony bundle

将以下 bundle 添加到您的 config/bundles.php 文件中

<?php

require "vendor/autoload.php";

return [
    ...
+  Ariga\AtlasDoctrineBundle::class => ['all' => true],
];

然后在您的项目目录中,创建一个名为 atlas.hcl 的新文件,其内容如下

data "external_schema" "doctrine" {
  program = [
    "php",
    "bin/console", 
    "atlas:schema"
  ]
}

env "doctrine" {
  src = data.external_schema.doctrine.url
  dev = "docker://mysql/8/dev"
  migration {
    dir = "file://migrations"
  }
  format {
    migrate {
      diff = "{{ sql . \"  \" }}"
    }
  }
}

作为 PHP 脚本

如果您有多个包含 Doctrine 实体的文件夹,您可能希望将提供者用作 PHP 脚本。

创建一个名为 atlas.php 的新文件,其内容如下

<?php

require "vendor/autoload.php";
require "vendor/ariga/atlas-provider-doctrine/src/LoadEntities.php";

// `DumpDDL` accepts an array of paths to your Doctrine entities and the database dialect(mysql | mariadb | postgres | sqlite | sqlserver).
print (DumpDDL(["./path/to/first/entities", "./path/to/more/entities"], "mysql"));

然后在您的项目目录中,创建一个名为 atlas.hcl 的新文件,其内容如下

data "external_schema" "doctrine" {
  program = [
    "php",
    "atlas.php"
  ]
}

env "doctrine" {
  src = data.external_schema.doctrine.url
  dev = "docker://mysql/8/dev"
  migration {
    dir = "file://migrations"
  }
  format {
    migrate {
      diff = "{{ sql . \"  \" }}"
    }
  }
}

用法

安装提供者后,您可以使用它将您的 Doctrine 架构应用到数据库中

应用

您可以使用 atlas schema apply 命令来规划并将数据库迁移到当前的 Doctrine 架构。这通过检查目标数据库并将其与 Doctrine 架构进行比较来创建迁移计划。Atlas 会提示您在应用迁移计划到数据库之前确认迁移计划。

atlas schema apply --env doctrine -u "mysql://root:password@localhost:3306/mydb"

其中 -u 标志接受目标数据库的 URL

差异

Atlas 支持一个 版本迁移 工作流程,其中对数据库的每个更改都会进行版本控制和记录在迁移文件中。您可以使用 atlas migrate diff 命令自动生成一个迁移文件,该文件将数据库从最新版本迁移到当前的 Doctrine 架构。

atlas migrate diff --env doctrine 

支持的数据库

提供者支持以下数据库

  • MySQL
  • MariaDB
  • PostgreSQL
  • SQLite
  • Microsoft SQL Server

问题

请将任何问题或功能请求报告到 ariga/atlas 存储库。

许可证

本项目根据 Apache License 2.0 许可。