public static function diff_constraints_table($ofs, $old_schema, $old_table, $new_schema, $new_table, $type, $drop_constraints = false) { if ($drop_constraints) { // drop constraints that no longer exist or are modified $old_constraints = static::get_drop_constraints($old_schema, $old_table, $new_schema, $new_table, $type); if (count($old_constraints) > 0) { $ofs->write(static::get_multiple_drop_sql($old_schema, $old_table, $old_constraints)); } } else { if (dbsteward::$old_database != NULL) { list($old_schema, $old_table) = dbx::find_old_table(dbsteward::$old_database, $new_schema, $new_table); } if ($old_schema === NULL || $old_table === NULL) { $new_constraints = format_constraint::get_table_constraints(dbsteward::$new_database, $new_schema, $new_table, $type); } else { $new_constraints = static::get_new_constraints($old_schema, $old_table, $new_schema, $new_table, $type); } $bits = self::get_multiple_create_bits($new_schema, $new_table, $new_constraints); if ($type == 'primaryKey') { $index_bits = mysql5_diff_indexes::diff_indexes_table_bits($old_schema, $old_table, $new_schema, $new_table); $bits = array_merge($index_bits, $bits); } // add new constraints if (count($bits) > 0) { $table = mysql5::get_fully_qualified_table_name($new_schema['name'], $new_table['name']); $ofs->write("ALTER TABLE {$table}\n " . implode(",\n ", $bits) . ";\n"); } } }
/** * Does this table constrain against a renamed table? * * @param object $db_doc * @param object $schema * @param object $table * return boolean */ public static function constrains_against_renamed_table($db_doc, $schema, $table) { foreach (format_constraint::get_table_constraints($db_doc, $schema, $table, 'constraint') as $constraint) { if (pgsql8_table::constraint_depends_on_renamed_table($db_doc, $constraint)) { dbsteward::info("NOTICE: " . $schema['name'] . "." . $table['name'] . " constrains against a renamed table with constraint " . $constraint['name']); return TRUE; } } return FALSE; }
private function _testConstraintGetTableConstraints() { $doc = simplexml_load_string($this->xml); $schema = $doc->schema; $table1 = $schema->table[0]; $table1_t1id = $table1->column[0]; $table1_t2id = $table1->column[1]; $table2 = $schema->table[1]; $table2_t2id = $table2->column[0]; $constraints = format_constraint::get_table_constraints($doc, $schema, $table1, 'foreignKey'); $this->assertCount(1, $constraints); $foreign = $constraints[0]['foreign_key_data']; $this->assertEquals($schema, $foreign['schema']); $this->assertEquals($table2, $foreign['table']); $this->assertEquals($table2_t2id, $foreign['column']); }
/** * Returns true if table contains given constraint $name * * @param $name name of the constraint * * @return boolean true if table contains given constraint $name otherwise false */ public static function contains_constraint($db_doc, $node_schema, $node_table, $name) { return format_constraint::get_table_constraint($db_doc, $node_schema, $node_table, $name) != null; }
public static function column_type($db_doc, $node_schema, $node_table, $node_column, &$foreign = NULL) { // if the column is a foreign key, solve for the foreignKey type if (isset($node_column['foreignTable'])) { $foreign = format_constraint::foreign_key_lookup($db_doc, $node_schema, $node_table, $node_column); $foreign_type = static::un_auto_increment($foreign['column']['type']); if (static::is_serial($foreign_type)) { return static::convert_serial($foreign_type); } return $foreign_type; } // if there's no type specified, that's a problem if (!isset($node_column['type'])) { throw new Exception("column missing type -- " . $table['name'] . "." . $column['name']); } // get the type of the column, ignoring any possible auto-increment flag $type = static::un_auto_increment($node_column['type']); // if the column type matches an enum type, inject the enum declaration here if ($node_type = mysql5_type::get_type_node($db_doc, $node_schema, $type)) { return mysql5_type::get_enum_type_declaration($node_type); } // translate serials to their corresponding int types if (static::is_serial($type)) { return static::convert_serial($type); } // nothing special about this type return $type; }
/** * Returns list of constraints that should be added. * * @param old_table original table * @param new_table new table * @param type whether primary keys should be processed or other constraints should be processed * * @return list of constraints that should be added */ protected static function get_new_constraints($old_schema, $old_table, $new_schema, $new_table, $type) { $list = array(); if ($new_table != null) { if ($old_table == null) { foreach (format_constraint::get_table_constraints(dbsteward::$new_database, $new_schema, $new_table, $type) as $constraint) { $list[] = $constraint; } } else { foreach (format_constraint::get_table_constraints(dbsteward::$new_database, $new_schema, $new_table, $type) as $constraint) { $old_constraint = format_constraint::get_table_constraint(dbsteward::$old_database, $old_schema, $old_table, $constraint['name']); if (!format_table::contains_constraint(dbsteward::$old_database, $old_schema, $old_table, $constraint['name']) || !format_constraint::equals($old_constraint, $constraint)) { $list[] = $constraint; } } } } return $list; }
private function _testExplicitFKeyDefaultColumns($local, $foreign) { $xml = <<<XML <schema name="s1"> <table name="t1" primaryKey="t2c1"> <column name="t2c1"/> <column name="t2c2"/> <foreignKey columns="t2c1, t2c2" foreignSchema="s2" foreignTable="t2" constraintName="fkey1" indexName="fkey1_idx"/> </table> </schema> <schema name="s2"> <table name="t2" primaryKey="t2c1"> <column name="t2c1" type="serial"/> <column name="t2c2" type="varchar(21)"/> </table> </schema> XML; $doc = $this->getDoc($xml); $constraints = format_constraint::get_table_constraints($doc, $doc->schema[0], $doc->schema[0]->table[0], 'foreignKey'); $this->assertCount(1, $constraints); $c = $constraints[0]; $this->assertEquals('fkey1', $c['name']); $this->assertEquals('s1', $c['schema_name']); $this->assertEquals('t1', $c['table_name']); $this->assertEquals('FOREIGN KEY', $c['type']); $this->assertEquals("{$local} REFERENCES {$foreign}", $c['definition']); $this->assertEquals(array('schema' => $doc->schema[1], 'table' => $doc->schema[1]->table[0], 'column' => array($doc->schema[1]->table[0]->column[0], $doc->schema[1]->table[0]->column[1]), 'name' => 'fkey1_idx', 'references' => $foreign), $c['foreign_key_data']); }