Yii2: Create/Drop tables improvements

Created on 19 Jan 2014  路  13Comments  路  Source: yiisoft/yii2

I think QueryBuilder should use additional option from $options param:

$options['ifExists'] = true;

and if it is set, it should be in sql command. Because without this we need to do some dirty hacks like this.

Afaik there already was issue about this, and if it still exists this one can be closed and linked to the previous.
And this can be ported to Yii1.

Will submit PR if needed.

Most helpful comment

Note: you can always check if some table exists using following code:

if ($this->db->schema->getTableSchema('my_table', true) === null) {
   // table 'my_table' exists!
}

All 13 comments

It's MySQL-only feature.

  1. Why does a fixture care about creating and dropping the table?
  2. In migrations this problem only exists with MySQL. Other DBMS such as Postgres and Sqlite can migrate schema within a transaction. See https://github.com/yiisoft/yii2/issues/1442?source=cc#issuecomment-29997482

Btw: your "hack" can be implemented differently by checking whether table exists in schema.
Also options is a string. I see no good way to add this.

Why does a fixture care about creating and dropping the table?

read this https://github.com/yiisoft/yii2/issues/1956#issuecomment-32675275

In migrations this problem only exists with MySQL. Other DBMS such as Postgres and Sqlite can migrate schema within a transaction. See #1442

this problem also exists and in db-fixtures as you see.

K, so as i see there is no good way to solve this.

And also postgresql support this feature according docs.

OK, what about SQLite, MSSQL and Oracle?

they have their own query-builders, no? I understand suggestions of solution like in_array($table, $schema->getTableNames()) but it would be more native in query, no?

Yes, each DB backend has its own query builder. I meant asking if these DBMS have IF EXISTS support.

After some googling looks like oracle dont have good support of this. So other discussion is senseless i think.

Note: you can always check if some table exists using following code:

if ($this->db->schema->getTableSchema('my_table', true) === null) {
   // table 'my_table' exists!
}

@klimov-paul yeah, noted above.i guess this would be the solution.

@klimov-paul: your example shows a table that does not exist

Yes, my mistake. Still I suppose you can get the idea.

Was this page helpful?
0 / 5 - 0 ratings