/** * Check to see if the back end schema is up to date - if not update it. * * @param Repository $inRepository The repository in which to check the schema */ public function checkSchema(Repository $inRepository) { try { $repos = get_class($inRepository); if (stripos($repos, "MySql") === false) { // If our repos has been switched to something that isn't MySql (e.g. Offline if unit testing) // we need to exit. return; } $existingSchema = MySqlComparisonSchema::fromTable($this->schemaName); $testSchema = MySqlComparisonSchema::fromMySqlSchema($this); $alterStatement = $testSchema->createAlterTableStatementFor($existingSchema); if ($alterStatement != false) { $alterStatement = "ALTER TABLE " . $this->schemaName . "\r\n" . $alterStatement; try { $repos::executeStatement($alterStatement); } catch (RepositoryStatementException $er) { // The update of the schema failed - probably meaning bad news! /// TODO: Handle reporting of this information. } } } catch (RepositoryStatementException $er) { $this->createTable(); } }
public function testSchemaDetectsWhenItCanUpdate() { $comparisonSchema = MySqlComparisonSchema::fromTable("tblCompany"); $example = new Company(); $schema = $example->getSchema(); $compareTo = MySqlComparisonSchema::fromMySqlSchema($schema); $this->assertFalse($compareTo->createAlterTableStatementFor($comparisonSchema)); $schema->addColumn(new String("Town", 60, null)); $compareTo = MySqlComparisonSchema::fromMySqlSchema($schema); $this->assertContains("ADD COLUMN `Town` varchar(60) DEFAULT NULL", $compareTo->createAlterTableStatementFor($comparisonSchema)); }
public function testSchemaIsModified() { // Note this test relies on the previous test to leave tblExample behind. $schema = new MySqlModelSchema("tblExample"); $schema->addColumn(new AutoIncrement("ID")); $schema->addColumn(new String("Name", 40, "StrangeDefault")); $schema->addColumn(new MySqlEnum("Type", "A", ["A", "B", "C"])); $schema->addColumn(new MySqlEnum("Type", "B", ["A", "B", "C", "D"])); $schema->addColumn(new String("Town", 60, null)); $schema->addIndex(new Index("ID", Index::PRIMARY)); $schema->checkSchema(Repository::getNewDefaultRepository(new Example())); $newSchema = MySqlComparisonSchema::fromTable("tblExample"); $columns = $newSchema->columns; $this->assertCount(4, $columns); $this->assertEquals("`Town` varchar(60) DEFAULT NULL", $columns["Town"]); $this->assertEquals("`Type` enum('A','B','C','D') NOT NULL DEFAULT 'B'", $columns["Type"]); }