public function renameColumn(Connection $connection, $tableName, $oldName, $newName) { $sql = 'SHOW CREATE TABLE `' . $tableName . '`'; $result = $connection->query($sql)->fetch()['Create Table']; preg_match('~^[\\s]+\\`' . $oldName . '\\`[\\s]+(.*?)[\\,]?$~m', $result, $m); $sql = ' ALTER TABLE `' . $tableName . '` CHANGE `' . $oldName . '` `' . $newName . '` ' . $m[1]; $connection->execute($sql); }
/** * get name of the sequence that a serial or bigserial column uses * @param Connection $connection * @param string $tableName * * @return string */ protected function getSequenceName(Connection $connection, $tableName) { $PkColumns = $connection->query("SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type\n FROM pg_index i\n JOIN pg_attribute a ON a.attrelid = i.indrelid\n AND a.attnum = ANY(i.indkey)\n WHERE i.indrelid = :table_name::regclass\n AND i.indisprimary;", [':table_name' => $tableName])->fetchAll(\PDO::FETCH_ASSOC); $PkColumnsCount = count($PkColumns); if ($PkColumnsCount != 1) { return null; } return $connection->query('select pg_get_serial_sequence(:table_name, :column_name)', [':table_name' => $tableName, ':column_name' => $PkColumns[0]['attname']])->fetchScalar(); }