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

Pimped testsuite for PostgreSQL #428

Merged
merged 18 commits into from
Aug 14, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ php:

env:
- DB=mysql DB_USER=root
- DB=pgsql

before_script:
# MySQL
- sh -c "if [ '$DB' = 'mysql' ]; then mysql -u$DB_USER -e 'SET FOREIGN_KEY_CHECKS = 0; DROP DATABASE IF EXISTS test; DROP SCHEMA IF EXISTS second_hand_books; DROP SCHEMA IF EXISTS contest; DROP SCHEMA IF EXISTS bookstore_schemas; SET FOREIGN_KEY_CHECKS = 1;'; fi"
- sh -c "if [ '$DB' = 'mysql' ]; then mysql -u$DB_USER -e 'CREATE DATABASE test; CREATE SCHEMA bookstore_schemas; CREATE SCHEMA contest; CREATE SCHEMA second_hand_books;'; fi"

# Composer
- wget http://getcomposer.org/composer.phar
- php composer.phar install --dev --prefer-source
- php composer.phar install --prefer-source

# MySQL
- sh -c "if [ '$DB' = 'mysql' ]; then ./tests/bin/setup.mysql.sh; fi"

- php bin/propel test:prepare --vendor="$DB" --dsn="$DB:dbname=test" --user="$DB_USER"
# PostgreSQL
- sh -c "if [ '$DB' = 'pgsql' ]; then ./tests/bin/setup.postgres.sh; fi"

script: phpunit
script:
- sh -c "if [ '$DB' = 'mysql' ]; then phpunit; fi"
- sh -c "if [ '$DB' = 'pgsql' ]; then phpunit --exclude-group mysql; fi"
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"symfony/yaml": "~2.2",
"symfony/console": "~2.2",
"symfony/finder": "~2.2",
"symfony/validator": "~2.2",
"symfony/validator": "2.2.*@dev",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why dev here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it seems they don't have yet a official version with my fix included for #427

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense... I forgot about that..

"symfony/filesystem": "~2.2",
"psr/log": "~1.0"
},
Expand Down
8 changes: 4 additions & 4 deletions src/Propel/Generator/Command/SqlInsertCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class SqlInsertCommand extends AbstractCommand
protected function configure()
{
$this
->addOption('output-dir', null, InputOption::VALUE_REQUIRED, 'The output directory', self::DEFAULT_OUTPUT_DIRECTORY)
->addOption('connection', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Connection to use', array())
->addOption('input-dir', null, InputOption::VALUE_REQUIRED, 'The input directory', self::DEFAULT_OUTPUT_DIRECTORY)
->addOption('connection', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Connection to use. Example: bookstore=mysql:host=127.0.0.1;dbname=test;user=root;password=foobar')
->setName('sql:insert')
->setAliases(array('insert-sql'))
->setDescription('Insert SQL statements')
Expand All @@ -45,7 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$manager = new SqlManager();

if (!$input->hasOption('connection')) {
if (!$input->hasOption('connection') || !$input->getOption('connection')) {
throw new InvalidArgumentException(sprintf('At least one connection is required'));
}

Expand All @@ -61,7 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln($message);
}
});
$manager->setWorkingDirectory($input->getOption('output-dir'));
$manager->setWorkingDirectory($input->getOption('input-dir'));

$manager->insertSql();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Propel/Generator/Command/TestPrepareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected function buildFixtures($fixturesDir, $connections, InputInterface $inp

$in = new ArrayInput(array(
'command' => 'sql:insert',
'--output-dir' => 'build/sql/',
'--input-dir' => 'build/sql/',
'--connection' => $conParams,
'--verbose' => $input->getOption('verbose'),
));
Expand Down
11 changes: 9 additions & 2 deletions src/Propel/Generator/Manager/SqlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,22 @@ public function insertSql($datasource = null)

try {
foreach ($sqls as $sql) {
$stmt = $con->prepare($sql);
$stmt->execute();
try {
$stmt = $con->prepare($sql);
$stmt->execute();
} catch (\Exception $e) {
$message = sprintf('SQL insert failed: %s', $sql);
throw new \Exception($message, 0, $e);
}
}

$con->commit();
} catch (\PDOException $e) {
$con->rollback();
throw $e;
}

$this->log(sprintf('%d queries executed for %s database.', count($sqls), $database));
}

return true;
Expand Down
52 changes: 51 additions & 1 deletion src/Propel/Generator/Model/ForeignKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,65 @@ class ForeignKey extends MappingModel
const SETDEFAULT = 'SET DEFAULT';
const SETNULL = 'SET NULL';

/**
* @var string
*/
private $foreignTableCommonName;

/**
* @var string
*/
private $foreignSchemaName;

/**
* @var string
*/
private $name;

/**
* @var string
*/
private $phpName;

/**
* @var string
*/
private $refPhpName;

/**
* @var string
*/
private $defaultJoin;

/**
* @var string
*/
private $onUpdate = '';

/**
* @var string
*/
private $onDelete = '';

/**
* @var Table
*/
private $parentTable;

/**
* @var string[]
*/
private $localColumns;

/**
* @var string[]
*/
private $foreignColumns;


/**
* @var bool
*/
private $skipSql;

/**
Expand Down Expand Up @@ -290,7 +338,9 @@ public function setForeignTableCommonName($tableName)
*/
public function getForeignTable()
{
return $this->parentTable->getDatabase()->getTable($this->getForeignTableName());
if ($this->parentTable->getDatabase()) {
return $this->parentTable->getDatabase()->getTable($this->getForeignTableName());
}
}

/**
Expand Down
24 changes: 24 additions & 0 deletions src/Propel/Generator/Model/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,24 @@
*/
class Index extends MappingModel
{
/**
* @var string
*/
protected $name;

/**
* @var string
*/
protected $table;

/**
* @var string[]
*/
private $columns;

/**
* @var string[]
*/
private $columnsSize;

/**
Expand Down Expand Up @@ -150,6 +165,15 @@ public function addColumn($data)
}
}

/**
* @param string $name
* @return bool
*/
public function hasColumn($name)
{
return in_array($name, $this->columns);
}

/**
* Sets an array of columns to use for the index.
*
Expand Down
83 changes: 82 additions & 1 deletion src/Propel/Generator/Model/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,26 @@
*/
class Table extends ScopedMappingModel implements IdMethod
{

/**
* @var Column[]
*/
private $columns;

/**
* @var ForeignKey[]
*/
private $foreignKeys;
private $foreignTableNames;

/**
* @var Index[]
*/
private $indices;

/**
* @var Unique[]
*/
private $unices;
private $idMethodParameters;
private $commonName;
Expand Down Expand Up @@ -1344,7 +1360,7 @@ public function hasEnumColumns()
/**
* Returns the list of all foreign keys.
*
* @return array
* @return ForeignKey[]
*/
public function getForeignKeys()
{
Expand Down Expand Up @@ -1380,6 +1396,71 @@ public function getUnices()
return $this->unices;
}

/**
* Checks if $keys are a unique constraint in the table.
* (through primaryKey, through a regular unices constraints or for single keys when it has isUnique=true)
*
* @param Column[]|string[] $keys
* @return bool
*/
public function isUnique(array $keys)
{
if (1 === count($keys)) {
$column = $keys[0] instanceof Column ? $keys[0] : $this->getColumn($keys[0]);
if ($column){
if ($column->isUnique()) return true;
if ($column->isPrimaryKey() && 1 === count($column->getTable()->getPrimaryKey())) {
return true;
}
}
}

// check if pk == $keys
if (count($this->getPrimaryKey()) === count($keys)) {
$allPk = true;
$stringArray = is_string($keys[0]);
foreach ($this->getPrimaryKey() as $pk) {
if ($stringArray) {
if (!in_array($pk->getName(), $keys)) {
$allPk = false;
break;
}
} else {
if (!in_array($pk, $keys)) {
$allPk = false;
break;
}
}
}

if ($allPk) {
return true;
}
}

// check if there is a unique constrains that contains exactly the $keys
if ($this->unices) {
foreach ($this->unices as $unique) {
if (count($unique->getColumns()) === count($keys)) {
$allAvailable = true;
foreach ($keys as $key) {
if (!$unique->hasColumn($key instanceof Column ? $key->getName() : $key)) {
$allAvailable = false;
break;
}
}
if ($allAvailable) {
return true;
}
} else {
continue;
}
}
}

return false;
}

/**
* Returns whether or not the table has a column.
*
Expand Down
23 changes: 23 additions & 0 deletions src/Propel/Generator/Platform/PgsqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,29 @@ public function getAddTablesDDL(Database $database)
return $ret;
}

/**
* {@inheritDoc}
*/
public function getAddForeignKeysDDL(Table $table)
{
$ret = '';
foreach ($table->getForeignKeys() as $fk) {
//PostgreSQL requires the keys of the foreignTable of a foreignKeys to be unique.
//check if there is already a unique constraint with exactly
//the keys of the FK, if not define it.
if ($fk->getForeignTable() && !$fk->getForeignTable()->isUnique($fk->getForeignColumnObjects())) {
$unique = new Unique();
$unique->setTable($fk->getForeignTable());
$unique->setColumns($fk->getForeignColumnObjects());
$ret .= $this->getAddIndexDDL($unique);
}

$ret .= $this->getAddForeignKeyDDL($fk);
}

return $ret;
}

public function getAddTableDDL(Table $table)
{
$ret = '';
Expand Down
1 change: 1 addition & 0 deletions src/Propel/Generator/Reverse/AbstractSchemaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Propel\Generator\Config\GeneratorConfigInterface;
use Propel\Generator\Model\VendorInfo;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Connection\SqlConnectionInterface;

/**
* Base class for reverse engineering a database schema.
Expand Down
Loading