cakephp / docs

CakePHP CookBook
http://book.cakephp.org
Other
680 stars 2.58k forks source link

3.1: Blog Tutorial Problem #3107

Closed midorikocak closed 9 years ago

midorikocak commented 9 years ago

I started initial migration file and added the example code inside:

<?php

use Phinx\Migration\AbstractMigration;

class Initial extends AbstractMigration
{
    public function change()
    {
        $articles = $this->table('articles');
        $articles->addColumn('title', 'string', ['limit' => 50])
            ->addColumn('body', 'text', ['null' => true, 'default' => null])
            ->addColumn('category_id', 'integer', ['null' => true, 'default' => null])
            ->addColumn('created', 'datetime')
            ->addColumn('modified', 'datetime', ['null' => true, 'default' => null])
            ->save();

        $categories = $this->table('categories');
        $categories->addColumn('parent_id', 'integer', ['null' => true, 'default' => null])
            ->addColumn('lft', 'integer', ['null' => true, 'default' => null])
            ->addColumn('rght', 'integer', ['null' => true, 'default' => null])
            ->addColumn('name', 'string', ['limit' => 255])
            ->addColumn('description', 'string', ['limit' => 255, 'null' => true, 'default' => null])
            ->addColumn('created', 'datetime')
            ->addColumn('modified', 'datetime', ['null' => true, 'default' => null])
            ->save();
    }
}

This is my current database:

-- phpMyAdmin SQL Dump
-- version 4.4.7
-- http://www.phpmyadmin.net
--
-- Anamakine: localhost:3306
-- Üretim Zamanı: 15 Ağu 2015, 02:00:48
-- Sunucu sürümü: 5.6.26
-- PHP Sürümü: 5.5.28

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Veritabanı: `gscms`
--

-- --------------------------------------------------------

--
-- Tablo için tablo yapısı `articles`
--

CREATE TABLE IF NOT EXISTS `articles` (
  `id` int(10) unsigned NOT NULL,
  `title` varchar(50) DEFAULT NULL,
  `body` text,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

--
-- Tablo döküm verisi `articles`
--

INSERT INTO `articles` (`id`, `title`, `body`, `created`, `modified`) VALUES
(1, 'The title', 'This is the article body. Deneme', '2015-08-15 10:50:56', '2015-08-15 08:54:04'),
(2, 'A title once again', 'And the article body follows.', '2015-08-15 10:50:56', NULL);

-- --------------------------------------------------------

--
-- Tablo için tablo yapısı `phinxlog`
--

CREATE TABLE IF NOT EXISTS `phinxlog` (
  `version` bigint(20) NOT NULL,
  `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `end_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dökümü yapılmış tablolar için indeksler
--

--
-- Tablo için indeksler `articles`
--
ALTER TABLE `articles`
  ADD PRIMARY KEY (`id`);

--
-- Dökümü yapılmış tablolar için AUTO_INCREMENT değeri
--

--
-- Tablo için AUTO_INCREMENT değeri `articles`
--
ALTER TABLE `articles`
  MODIFY `id` int(10) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=4;

Then I run the "bin/cake migrations migrate" command, here is the result.

Mutlus-MacBook-Pro:app-3.1 midori$ bin/cake migrations migrate

Welcome to CakePHP v3.1.0-beta2 Console
---------------------------------------------------------------
App : src
Path: /Applications/mampstack-5.5.28-0/apache2/htdocs/midori/app-3.1/src/
PHP : 5.5.14
---------------------------------------------------------------
using migration path /Applications/mampstack-5.5.28-0/apache2/htdocs/midori/app-3.1/config/Migrations
using environment default
using adapter mysql
using database gscms

 == 20150815085509 Initial: migrating

  [PDOException]                                                              
  SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'title'  

migrate [-t|--target TARGET] [-p|--plugin PLUGIN] [-c|--connection CONNECTION] [-s|--source SOURCE]

Mutlus-MacBook-Pro:app-3.1 midori$ 
midorikocak commented 9 years ago

This works:

<?php

use Phinx\Migration\AbstractMigration;

class Initial extends AbstractMigration
{
    public function change()
    {
        $articles = $this->table('articles');
        $articles
            ->addColumn('category_id', 'integer', ['null' => true, 'default' => null])
            ->save();

        $categories = $this->table('categories');
        $categories->addColumn('parent_id', 'integer', ['null' => true, 'default' => null])
            ->addColumn('lft', 'integer', ['null' => true, 'default' => null])
            ->addColumn('rght', 'integer', ['null' => true, 'default' => null])
            ->addColumn('name', 'string', ['limit' => 255])
            ->addColumn('description', 'string', ['limit' => 255, 'null' => true, 'default' => null])
            ->addColumn('created', 'datetime')
            ->addColumn('modified', 'datetime', ['null' => true, 'default' => null])
            ->save();
    }
}
ionas commented 9 years ago

Shouldn't it be possible to create those migrations by command line statements (circumventing any changes - except command line options - to cake3migrations / phinx?

midorikocak commented 9 years ago

that would be much more better I think.

markstory commented 9 years ago

Just above the migration create command is the following.

We will use the migrations plugin to create a table in our database. If you already have an articles table in your database, erase it.

I think the intent of this was that the reader would drop the table.

midorikocak commented 9 years ago

that's it. yes I got it.

On 15 August 2015 at 14:40, Mark Story notifications@github.com wrote:

Just above the migration create command is the following.

We will use the migrations plugin to create a table in our database. If you already have an articles table in your database, erase it.

I think the intent of this was that the reader would drop the table.

— Reply to this email directly or view it on GitHub https://github.com/cakephp/docs/issues/3107#issuecomment-131377008.

cake17 commented 9 years ago

Maybe I can add that instead creating this migraton file manually, we can also build it with a command line (and write it just after). Would it be better ?

markstory commented 9 years ago

Building with the command line might be simpler for the tutorial and show more features of the migrations plugin too.

ionas commented 9 years ago

:+1:

cake17 commented 9 years ago

Close this after PR #3124

htstudios commented 9 years ago

Thank you very much. Should teach how to use migrations from the start!