Doctrine2迁移向下迁移并从浏览器而不是命令行迁移

时间:2012-03-09 10:43:15

标签: php mysql symfony doctrine-orm

我正在使用Doctrine2迁移。我需要一些关于我怀疑的答案,我无法在文件中找到一个好的解决方案

我用:

  doctrine migrations:diff // generate migrations files
  doctrine migrations:migrate // migrates up to new version
  1. 我如何向下迁移?指定以前的版本不起作用(没有更新它说f.e. doctrine迁移:迁移版本20120211163332它说

    Migrating up to Version20120211163332 from 20120309112058
    
    [Doctrine\DBAL\Migrations\MigrationException]  
    Could not find any migrations to execute.      
    

    但它不起来它应该失败!你也可以看到回应中的版本

  2. 如果我必须进行一些数据库更新,是否可以添加一些SQL查询(更改一些与其他数据相关的数据)?我没有尝试过,因为失败不起作用:((

  3. 有没有办法在浏览器中使用migrate命令?我没有控制台访问共享主机,所以我需要这个功能,而不是逐个复制查询:D在phpMyAdmin

5 个答案:

答案 0 :(得分:89)

您可以选择手动指定要迁移到的版本:

 php doctrine.php migrations:migrate YYYYMMDDHHMMSS

或执行向上/向下迁移

php doctrine.php migrations:execute YYYYMMDDHHMMSS  --down
php doctrine.php migrations:execute YYYYMMDDHHMMSS  --up

你可以使用:

找到YYYYMMDDHHMMSS
php doctrine.php migrations:status
>> Current Version:           2012-12-20 23:38:47 (20121220233847)
>> Latest Version:            2012-12-20 23:38:47 (20121220233847)

答案 1 :(得分:25)

我在Symfony的网站上看到了这个文档:http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html#usage

doctrine:migrations:execute允许您手动执行单个迁移版本...但从未尝试过,抱歉。

希望这有帮助!

让我们发布。

答案 2 :(得分:10)

以下是如何从浏览器运行迁移:

<强> composer.json

{
    "require": {
        "doctrine/dbal": "*",
        "doctrine/migrations": "dev-master"
    },
    "minimum-stability": "dev",
    "autoload": {
        "psr-0": {"": "src/"}
    }
}

<强>的src / Acme公司/迁移/ Version1.php

<?php # src/Acme/Migrations/Version1.php
namespace Acme\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

/**
 * Class Version1
 *
 * Notice that file and class names MUST be Version*.php
 *
 * @package Acme\Migrations
 */
class Version1 extends AbstractMigration
{
    public function up(Schema $schema)
    {
        $users = $schema->createTable('users');
        $users->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
        $users->addColumn('username', 'string', array('length' => 128));
        $users->addColumn('password', 'string', array('length' => 128));
        $users->setPrimaryKey(array('id'));

        // You can also add any queries
        // $this->addSql('CREATE TABLE addresses (id INT NOT NULL, street VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB');
    }

    public function down(Schema $schema)
    {
        $schema->dropTable('users');

        //$this->addSql('DROP TABLE addresses');
    }

    // Use this functions to prepare your migrations
    //public function preUp(Schema $schema) {}
    //public function postUp(Schema $schema) {}
    //public function preDown(Schema $schema) {}
    //public function postDown(Schema $schema) {}
}

<强>的index.php

<?php # index.php
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\DBAL\Migrations\Migration;
use Doctrine\DBAL\Migrations\OutputWriter;

require_once 'vendor/autoload.php';

$nl = PHP_SAPI == 'cli' ? PHP_EOL : '<br>'; // Optional will be used for output

$to = null; // Optional integer - migrate to version, if null - will migrate to latest available version
#region Optional get argument
$index = PHP_SAPI == 'cli' ? 1 : 'to';
$arguments = PHP_SAPI == 'cli' ? $argv : $_REQUEST;
$to = isset($arguments[$index]) && filter_var($arguments[$index], FILTER_VALIDATE_INT) ? intval($arguments[$index]) : null;
#endregion

#region Doctrine Connection
// Silex: $app['db']
// Symfony controller: $this->get('database_connection')
$db = DriverManager::getConnection(array(
    'dbname' => 'doctine_migrations',
    'user' => 'root',
    'password' => 'root',
    'host' => 'localhost',
    'driver' => 'pdo_mysql',
    'charset' => 'utf8',
    'driverOptions' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
    )
));
#endregion

#region Config
$config = new Configuration($db /*, new OutputWriter(function ($message) { echo $message . PHP_EOL; })*/); // OutputWriter is optional and by default do nothing, accepts closure for writing logs

//$config->setName('My Migrations'); // Optional name for your migrations
$config->setMigrationsTableName('version'); // Table name that will store migrations log (will be created automatically, default name is: doctrine_migration_versions)
$config->setMigrationsNamespace('Acme\\Migrations'); // Namespace of your migration classes, do not forget escape slashes, do not add last slash
$config->setMigrationsDirectory('src/Acme/Migrations'); // Directory where your migrations are located
$config->registerMigrationsFromDirectory($config->getMigrationsDirectory()); // Load your migrations
#endregion

$migration = new Migration($config); // Create Migration based on provided configuration

$versions = $migration->getSql($to); // Retrieve SQL queries that should be run to migrate you schema to $to version, if $to == null - schema will be migrated to latest version

#region Some dummy output
foreach ($versions as $version => $queries) {
    echo 'VERSION: ' . $version . $nl;
    echo '----------------------------------------------' . $nl . $nl;

    foreach ($queries as $query) {
        echo $query . $nl . $nl;
    }

    echo $nl . $nl;
}
#endregion

try {
    $migration->migrate($to); // Execute migration!
    echo 'DONE' . $nl;
} catch (Exception $ex) {
    echo 'ERROR: ' . $ex->getMessage() . $nl;
}

现在你可以:

从控制台运行:

php index.php - 将迁移到lates版本

php index.php 2 - 将迁移到版本2(如果当前版本更大 - 它将向下迁移

从网络浏览器运行:

http://localhost/index.phphttp://localhost/index.php?to=2也会这样做。

答案 3 :(得分:2)

如果要向下迁移1,可以使用以下语法:

./doctrine migrations:migrate prev

要进行第一次迁移:

./doctrine migrations:migrate first

要进行下一个迁移:

./doctrine migrations:migrate next

来源:https://www.doctrine-project.org/projects/doctrine-migrations/en/1.8/reference/managing_migrations.html#managing-migrations

答案 4 :(得分:0)

在我的情况下,如下所示:

还原更改:

php bin/console doctrine:migrations:execute "VendorName\ProjectName\Account\Migrations\Version20201002092643" --down

处理迁移:

php bin/console doctrine:migrations:execute "VendorName\ProjectName\Account\Migrations\Version20201002092643" --up

或者只是

php bin/console doctrine:migrations:execute "VendorName\ProjectName\Account\Migrations\Version20201002092643"

和我用于迁移的Symfony配置如下所示:

doctrine_migrations:
    migrations_paths:
        # namespace is arbitrary but should be different from App\Migrations
        # as migrations classes should NOT be autoloaded
        'DoctrineMigrations': '%kernel.project_dir%/migrations'
        'VendorName\ProjectName\Account\Migrations': '%kernel.project_dir%/component/account/migrations'