--- D:/_Webs/www.prevencionintegral.com_php55/public/includes/database/sqlsrv/schema - Copy.inc Fri Nov 15 19:37:01 2013 +++ D:/_Webs/www.prevencionintegral.com_php55/public/includes/database/sqlsrv/schema.inc Sat Nov 16 17:26:53 2013 @@ -120,15 +120,15 @@ // If the table has no primary key, create one for us. // TODO: only necessary on Azure. if (isset($table['primary key']) && is_array($table['primary key'])) { $sql_fields[] = 'CONSTRAINT {' . $name . '}_pkey PRIMARY KEY CLUSTERED (' . implode(', ', $this->connection->quoteIdentifiers($table['primary key'])) . ')'; } else { - $sql_fields[] = '__pk UNIQUEIDENTIFIER DEFAULT NEWID()'; + $sql_fields[] = '__pk UNIQUEIDENTIFIER DEFAULT NEWID() NOT NULL'; $sql_fields[] = 'CONSTRAINT {' . $name . '}_pkey_technical PRIMARY KEY CLUSTERED (__pk)'; } $sql = "CREATE TABLE [{" . $name . "}] (\n\t"; $sql .= implode(",\n\t", $sql_fields); $sql .= "\n)"; return $sql; @@ -344,14 +344,17 @@ public function addField($table, $field, $spec, $new_keys = array()) { if (!$this->tableExists($table)) { throw new DatabaseSchemaObjectDoesNotExistException(t("Cannot add field %table.%field: table doesn't exist.", array('%field' => $field, '%table' => $table))); } if ($this->fieldExists($table, $field)) { throw new DatabaseSchemaObjectExistsException(t("Cannot add field %table.%field: field already exists.", array('%field' => $field, '%table' => $table))); } + + if ($spec['type'] == 'serial' && isset($spec['initial'])) + throw new Exception('Cannot add serial (IDENTITY) field with initial values'); // If the field is declared NOT NULL, we have to first create it NULL insert // the initial data then switch to NOT NULL. if (!empty($spec['not null']) && !isset($spec['default'])) { $fixnull = TRUE; $spec['not null'] = FALSE; } @@ -385,21 +388,28 @@ /** * Override DatabaseSchema::changeField(). * * @status complete */ public function changeField($table, $field, $field_new, $spec, $new_keys = array()) { + + if (!$this->fieldExists($table, $field)) { throw new DatabaseSchemaObjectDoesNotExistException(t("Cannot change the definition of field %table.%name: field doesn't exist.", array('%table' => $table, '%name' => $field))); } if (($field != $field_new) && $this->fieldExists($table, $field_new)) { throw new DatabaseSchemaObjectExistsException(t("Cannot rename field %table.%name to %name_new: target field already exists.", array('%table' => $table, '%name' => $field, '%name_new' => $field_new))); } - + + // After deep research: IDENTITY columns cannot be updated. The closest it can get is to enable IDENTITY_INSERT + // But that will requiere to drop whole table and recreate it with new values + if($spec['type'] == 'serial') + throw new Exception(t('Cannot change field to serial TYPE (IDENTITY) on SQL SERVER')); + // SQL Server supports transactional DDL, so we can just start a transaction // here and pray for the best. $transaction = $this->connection->startTransaction(); // Introspect the schema and save the current primary key if the column // we are modifying is part of it. $primary_key_sql = $this->introspectPrimaryKey($table, $field); @@ -664,15 +674,15 @@ } /** * Add a primary column to the table. */ protected function createTechnicalPrimaryColumn($table) { if (!$this->fieldExists($table, '__pk')) { - $this->connection->query('ALTER TABLE {' . $table . '} ADD __pk UNIQUEIDENTIFIER DEFAULT NEWID()'); + $this->connection->query('ALTER TABLE {' . $table . '} ADD __pk UNIQUEIDENTIFIER DEFAULT NEWID() NOT NULL'); } } /** * Try to clean up the technical primary column if possible. */ protected function cleanUpTechnicalPrimaryColumn($table) {