ikkez/f3-schema-builder

Error handling

KomAuras opened this issue · 2 comments

How do I get an error saying that the table already exists?
Or is that the SQL script is not applied?

<?php

use DB\SQL\Schema;

class migration_2017_02_22_10_12
{
    public function up($f3)
    {
        $table = $f3->get('SCHEMA')->createTable('users');
        $table->primary('user_id');
        $table->addColumn('name')->type(schema::DT_VARCHAR128);
        $table->addColumn('password')->type(schema::DT_VARCHAR256);
        $table->build();

        // again for error test
        $table = $f3->get('SCHEMA')->createTable('users');
        $table->primary('user_id');
        $table->addColumn('name')->type(schema::DT_VARCHAR128);
        $table->addColumn('password')->type(schema::DT_VARCHAR256);
        $table->build(); // How to handling error here?

    }

    public function down($f3)
    {
        $f3->get('SCHEMA')->dropTable('users');
    }
}
ikkez commented

Hi. Well it does it's own check if the table is already existing, right here: https://github.com/ikkez/f3-schema-builder/blob/master/lib/db/sql/schema.php#L464-L466
I just noticed that the thrown error was at a notice level. I fixed this in the latest commit, or you just set error_reporting(-1); and should get the error (fetched by F3's error handler).
For PDO exceptions (like duplicate keys, contrain checks, etc), enable exception handling on your DB object (\PDO::ERRMODE_EXCEPTION) as described in here and use try / catch blocks around your code. That's it.

All. I understood after your comment. Thank you so much.