Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database migration fails #89

Closed
darkpixel opened this issue Dec 23, 2014 · 23 comments
Closed

Database migration fails #89

darkpixel opened this issue Dec 23, 2014 · 23 comments
Labels
Bug Bugs with Cachet
Milestone

Comments

@darkpixel
Copy link
Contributor

Changed the database.php file to have the following entry:

    'default' => 'sqlite',

    'connections' => [
        'sqlite' => [
            'driver'   => 'sqlite',
            'database' => '/var/hosting/-redacted-/sfdstatus.db',
            'prefix'   => '',
        ],
    ],

Create a new sqlite3 database (sqlite3 /var/hosting/-redacted-/sfdstatus.db "") and then ran:

# php composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
Generating optimized class loader
Migration table created successfully.



  [Illuminate\Database\QueryException]                                                                                                                              
  SQLSTATE[HY000]: General error: 1 near "CHANGE": syntax error (SQL: ALTER TABLE `incidents` CHANGE `component` `component_id` TINYINT(4)  NOT NULL  DEFAULT '1')  






  [PDOException]                                                 
  SQLSTATE[HY000]: General error: 1 near "CHANGE": syntax error  



migrate [--bench[="..."]] [--database[="..."]] [--force] [--path[="..."]] [--package[="..."]] [--pretend] [--seed]


Script php artisan migrate handling the post-install-cmd event returned with an error



  [RuntimeException]                                                                                                                                                    
  Error Output:                                                                                                                                                         

    [Illuminate\Database\QueryException]                                                                                                                                
    SQLSTATE[HY000]: General error: 1 near "CHANGE": syntax error (SQL: ALTER TABLE `incidents` CHANGE `component` `component_id` TINYINT(4)  NOT NULL  DEFAULT '1')    


    [PDOException]                                                                                                                                                      
    SQLSTATE[HY000]: General error: 1 near "CHANGE": syntax error                                                                                                       

  migrate [--bench[="..."]] [--database[="..."]] [--force] [--path[="..."]] [--package[="..."]] [--pretend] [--seed]                                                    



install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-plugins] [--no-custom-installers] [--no-scripts] [--no-progress] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [--ignore-platform-reqs] [packages1] ... [packagesN]

#
@jbrooksuk
Copy link
Member

Looks like SQLite doesn't like the ALTER TABLE syntax that was generated from MySQL.

@GrahamCampbell is there a way to change the syntax per-DB driver?

@jbrooksuk jbrooksuk added the Bug Bugs with Cachet label Dec 23, 2014
@GrahamCampbell
Copy link
Contributor

@jbrooksuk Actually, there's an easier way. Installing doctrine dbal should fix this. Laravel uses it to fill out missing sqlite functionality.

@jbrooksuk
Copy link
Member

@GrahamCampbell Oh really? Is it easy to get integrated within a migration, or does it just work once installed?

@GrahamCampbell
Copy link
Contributor

It should just work. This is an odd case. Usually laravel will complain that doctrine isn't available.

@GrahamCampbell
Copy link
Contributor

Oh yes, you can't use raw statements like that in migrations. That will never work.

@jbrooksuk
Copy link
Member

Ok, how do you advise we write migrations? Is there a better way?

@darkpixel
Copy link
Contributor Author

Also tested this against a postgresql database thinking maybe it was a DB specific issue:

# php composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
Generating optimized class loader
Migration table created successfully.



  [Illuminate\Database\QueryException]                                                                                      
  SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "`"                                                      
  LINE 1: ALTER TABLE `incidents` CHANGE `component` `component_id` TI...                                                   
                      ^ (SQL: ALTER TABLE `incidents` CHANGE `component` `component_id` TINYINT(4)  NOT NULL  DEFAULT '1')  






  [PDOException]                                                           
  SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "`"     
  LINE 1: ALTER TABLE `incidents` CHANGE `component` `component_id` TI...  
                      ^                                                    



migrate [--bench[="..."]] [--database[="..."]] [--force] [--path[="..."]] [--package[="..."]] [--pretend] [--seed]


Script php artisan migrate handling the post-install-cmd event returned with an error



  [RuntimeException]                                                                                                            
  Error Output:                                                                                                                 

    [Illuminate\Database\QueryException]                                                                                        
    SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "`"                                                        
    LINE 1: ALTER TABLE `incidents` CHANGE `component` `component_id` TI...                                                     
                        ^ (SQL: ALTER TABLE `incidents` CHANGE `component` `component_id` TINYINT(4)  NOT NULL  DEFAULT '1')    


    [PDOException]                                                                                                              
    SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "`"                                                        
    LINE 1: ALTER TABLE `incidents` CHANGE `component` `component_id` TI...                                                     
                        ^                                                                                                       

  migrate [--bench[="..."]] [--database[="..."]] [--force] [--path[="..."]] [--package[="..."]] [--pretend] [--seed]            



install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-plugins] [--no-custom-installers] [--no-scripts] [--no-progress] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [--ignore-platform-reqs] [packages1] ... [packagesN]

# 

I'm out of my element with the new PHP packing systems, but I tried:
php composer.phar require doctrine/dbal followed by php composer.phar install and I'm hitting the same errors.

@GrahamCampbell
Copy link
Contributor

Is there a better way?

Yes - use laravel's actual functions and classes instead of writing raw code that only works on mysql.

@GrahamCampbell
Copy link
Contributor

Sorry - I haven't got time to look into this today for you.

@jbrooksuk
Copy link
Member

Ok, I didn't know there was inbuilt functions for things like this, cool! :)

@darkpixel
Copy link
Contributor Author

...just tested with MySQL--works perfectly.

@jbrooksuk
Copy link
Member

Rewriting them now :)

@jbrooksuk
Copy link
Member

I'm not sure about moving the columns, so I'll drop these migrations as they don't matter really.

@jbrooksuk jbrooksuk reopened this Dec 23, 2014
@jbrooksuk
Copy link
Member

@GrahamCampbell I'm not sure how you write a migration (using sans-DB::statement) to change the default value?

@darkpixel
Copy link
Contributor Author

Just pulled down the patched version and tried against postgres:

# php composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
Generating optimized class loader



  [Illuminate\Database\QueryException]                                                                                          
  SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "`"                                                          
  LINE 1: ALTER TABLE `incidents` CHANGE `component_id` `component_id`...                                                       
                      ^ (SQL: ALTER TABLE `incidents` CHANGE `component_id` `component_id` TINYINT(4)  NOT NULL  DEFAULT '0';)  






  [PDOException]                                                           
  SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "`"     
  LINE 1: ALTER TABLE `incidents` CHANGE `component_id` `component_id`...  
                      ^                                                    



migrate [--bench[="..."]] [--database[="..."]] [--force] [--path[="..."]] [--package[="..."]] [--pretend] [--seed]


Script php artisan migrate handling the post-install-cmd event returned with an error



  [RuntimeException]                                                                                                                
  Error Output:                                                                                                                     

    [Illuminate\Database\QueryException]                                                                                            
    SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "`"                                                            
    LINE 1: ALTER TABLE `incidents` CHANGE `component_id` `component_id`...                                                         
                        ^ (SQL: ALTER TABLE `incidents` CHANGE `component_id` `component_id` TINYINT(4)  NOT NULL  DEFAULT '0';)    


    [PDOException]                                                                                                                  
    SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "`"                                                            
    LINE 1: ALTER TABLE `incidents` CHANGE `component_id` `component_id`...                                                         
                        ^                                                                                                           

  migrate [--bench[="..."]] [--database[="..."]] [--force] [--path[="..."]] [--package[="..."]] [--pretend] [--seed]                



install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-plugins] [--no-custom-installers] [--no-scripts] [--no-progress] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [--ignore-platform-reqs] [packages1] ... [packagesN]

#

@jbrooksuk
Copy link
Member

@darkpixel looks like the migration doesn't like back ticks in PgSql?

@jbrooksuk jbrooksuk reopened this Dec 24, 2014
@darkpixel
Copy link
Contributor Author

Yeah--backticks are non-standard. MySQL doesn't require them, PostgreSQL doesn't support them.

@jbrooksuk
Copy link
Member

Would you mind making a PR and removing the back ticks?

Sent from my iPhone

On 24 Dec 2014, at 18:08, Aaron C. de Bruyn notifications@github.com wrote:

Yeah--backticks are non-standard. MySQL doesn't require them, PostgreSQL doesn't support them.


Reply to this email directly or view it on GitHub.

@darkpixel
Copy link
Contributor Author

I'll give it a shot.

@darkpixel
Copy link
Contributor Author

It looks like the Laraval schema editor doesn't allow changing column types (http://stackoverflow.com/questions/22060398/how-do-i-change-a-column-type-with-laravel-schema-builder) and you have to manually write an SQL alter statement for your databases.

I have PostgreSQL and MySQL available on my test server. I'll give it a shot over the next few days.

@darkpixel
Copy link
Contributor Author

Maybe someone can shed some light on the migration process. It's not working like I think it should.

I create a clean, new Postgres DB with nothing in it. I run php artisian migrate, and it crashes on the raw SQL statement here: https://github.com/cachethq/Cachet/blob/master/app/database/migrations/2014_12_13_121410_AlterTableIncidentsRemoveDefaultComponent.php#L17

...as expected. But when I look at the DB, I don't even see an 'incidents' table. The migrations table shows everything applied up to but excluding 2014_12_13_121410_AlterTableIncidentsRemoveDefaultComponent.php.

Manually reviewing the migration files, I can see the incidents table being created in 2014_11_16_224719_CreateIncidentsTable.php which shows up in the migrations table in the database. None of the other migration files appear to drop the incidents table...

Am I missing some PHP magic?

@darkpixel
Copy link
Contributor Author

Disregard. Found it.

https://github.com/cachethq/Cachet/blob/master/app/database/migrations/2014_12_01_121947_AlterTableIncidentsRenameComponentColumn.php#L17

Line 17 and 29 should be renameColumn instead of rename.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bugs with Cachet
Projects
None yet
Development

No branches or pull requests

3 participants