propelorm/Propel — pull request #144 by c33s (2011-10-14 00:35:49+00:00)
Title: Postgres supports supports the "IF EXISTS" on a "DROP TABLE" since versio

Postgres supports supports the "IF EXISTS" on a "DROP TABLE" since version 8.2. It is necessary to add the "IF EXIST" part, because if you try to drop a table which doesn't exists an PDOException is thown. so no table is created.

since postgres 8.2 there exists a DROP TABLE IF EXISTS function
http://www.postgresql.org/docs/8.2/static/sql-droptable.html
http://www.postgresql.org/docs/8.1/static/sql-droptable.html
http://www.postgresql.org/docs/8.0/static/sql-droptable.html

exception with postgres 9.0 if the following code is executed:

```
php .\app\console propel:build-sql
php .\app\console propel:insert-sql --force
```

```php
exception 'PDOException' with message 'SQLSTATE[42P01]: Undefined table: 7 ERROR: table "book" does not exist' in .\vendor\propel\generator\lib\util\PropelSqlManager.php:226
Stack trace:
#0 .\vendor\propel\generator\lib\util\PropelSqlManager.php(226): PDOStatement->execute()
#1 .\vendor\bundles\Propel\PropelBundle\Command\InsertSqlCommand.php(95): PropelSqlManager->insertSql('default')
#2 .\vendor\bundles\Propel\PropelBundle\Command\InsertSqlCommand.php(79): Propel\PropelBundle\Command\InsertSqlCommand->doInsertSql(Object(PropelSqlManager), Object(Symfony\Component\Console\Output\ConsoleOutput), 'default')
#3 .\vendor\symfony\src\Symfony\Component\Console\Command\Command.php(214): Propel\PropelBundle\Command\InsertSqlCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#4 .\vendor\symfony\src\Symfony\Component\Console\Application.php(194): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#5 .\vendor\symfony\src\Symfony\Bundle\FrameworkBundle\Console\Application.php(75): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#6 .\vendor\symfony\src\Symfony\Component\Console\Application.php(118): Symfony\Bundle\FrameworkBundle\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#7 .\app\console(18): Symfony\Component\Console\Application->run()
#8 {main}
```

old workarounds for postgres emulating if exists (for me they don't work on pg 9.0, it seem it need plpgsql):

```
if exists (select 1 from pg_tables where tablename = "thetable")
drop table thetable
```
http://stackoverflow.com/questions/1766046/postgresql-create-table-if-not-exists
http://stackoverflow.com/questions/435424/postgresql-how-to-create-table-only-if-it-does-not-already-exist

so i think adding the sql code compatible since pg 8.2 is the best solution, even debian ships pg 8.3 since lenny.

```
lenny (oldstable) (misc): object-relational SQL database (supported version)
8.3.14-0lenny1: all
lenny-backports (database): object-relational SQL database (supported version)
8.4.8-0squeeze2~bpo50+1 [backports]: all
lenny-backports-sloppy (database): object-relational SQL database (supported version)
9.1+124~bpo50+1 [backports]: all
squeeze (stable) (database): object-relational SQL database (supported version)
8.4.8-0squeeze2: all
squeeze-backports (database): object-relational SQL database (supported version)
9.1+124~bpo60+1 [backports]: all
```