renameTable example

// For PostgreSQL, we also need to check that the sequence has been renamed.     // The initial name of the sequence has been generated automatically by     // PostgreSQL when the table was created, however, on subsequent table     // renames the name is generated by Drupal and can not be easily     // re-constructed. Hence we can only check that we still have a sequence on     // the new table name.     $sequenceExists = (bool) $this->connection->query("SELECT pg_get_serial_sequence('{" . $tableName . "}', 'id')")->fetchField();
    $this->assertTrue($sequenceExists, 'Sequence was renamed.');

    // Rename the table again and repeat the check.     $anotherTableName = strtolower($this->getRandomGenerator()->name(63 - strlen($this->getDatabasePrefix())));
    $this->schema->renameTable($tableName$anotherTableName);

    $sequenceExists = (bool) $this->connection->query("SELECT pg_get_serial_sequence('{" . $anotherTableName . "}', 'id')")->fetchField();
    $this->assertTrue($sequenceExists, 'Sequence was renamed.');
  }

  /** * {@inheritdoc} */
  public function testTableWithSpecificDataType(): void {
    $table_specification = [
      'description' => 'Schema table description.',
      

    public function run(): bool
    {
        $this->db->query('PRAGMA foreign_keys = OFF');

        $this->db->transStart();

        $this->forge->renameTable($this->tableName, "temp_{$this->tableName}");

        $this->forge->reset();

        $this->createTable();

        $this->copyData();

        $this->forge->dropTable("temp_{$this->tableName}");

        $success = $this->db->transComplete();

        


    // Execute the data migration query.     $this->connection->insert($new_table)
      ->from($select)
      ->execute();

    $old_count = $this->connection->query('SELECT COUNT(*) FROM {' . $table . '}')->fetchField();
    $new_count = $this->connection->query('SELECT COUNT(*) FROM {' . $new_table . '}')->fetchField();
    if ($old_count == $new_count) {
      $this->dropTable($table);
      $this->renameTable($new_table$table);
    }
  }

  /** * Find out the schema of a table. * * This function uses introspection methods provided by the database to * create a schema array. This is useful, for example, during update when * the old schema is not available. * * @param $table * Name of the table. * * @return array * An array representing the schema. * * @throws \Exception * If a column of the table could not be parsed. */
$backup_table_mapping = $sandbox['backup_table_mapping'];

    // Rename the original tables so we can put them back in place in case     // anything goes wrong.     $backup_table_names = array_combine(
      $this->getTableNames($original$original_field_storage_definitions$original_table_mapping),
      $this->getTableNames($original$original_field_storage_definitions$backup_table_mapping)
    );
    $renamed_tables = [];
    try {
      foreach ($backup_table_names as $original_table_name => $backup_table_name) {
        $this->database->schema()->renameTable($original_table_name$backup_table_name);
        $renamed_tables[$original_table_name] = $backup_table_name;
      }
    }
    catch (\Exception $e) {
      foreach ($renamed_tables as $original_table_name => $backup_table_name) {
        $this->database->schema()->renameTable($backup_table_name$original_table_name);
      }

      // Re-throw the original exception.       throw $e;
    }

    
$this->assertCount(1, $results);
    $this->assertSame('id', $results[0]);
  }

  /** * @covers ::renameTable * @covers ::tableExists * @covers ::findTables * @covers ::dropTable */
  public function testTable(): void {
    $this->testingFakeConnection->schema()->renameTable('faking_table', 'new_faking_table');

    $tables = $this->testingFakeConnection->schema()->findTables('%');
    $result = $this->testingFakeConnection->query("SELECT * FROM information_schema.tables WHERE table_schema = 'testing_fake'")->fetchAll();
    $this->assertFalse($this->testingFakeConnection->schema()->tableExists('faking_table'));
    $this->assertTrue($this->testingFakeConnection->schema()->tableExists('new_faking_table'));
    $this->assertEquals($this->testingFakeConnection->getPrefix() . 'new_faking_table', $result[0]->table_name);
    $this->assertEquals('testing_fake', $result[0]->table_schema);
    sort($tables);
    $this->assertEquals(['new_faking_table']$tables);

    $this->testingFakeConnection->schema()->dropTable('new_faking_table');
    
// Test for fake index and test for the boolean result of indexExists().     $index_exists = $this->schema->indexExists('test_table', 'test_field');
    $this->assertFalse($index_exists, 'Fake index does not exist');
    // Add index.     $this->schema->addIndex('test_table', 'test_field', ['test_field']$table_specification);
    // Test for created index and test for the boolean result of indexExists().     $index_exists = $this->schema->indexExists('test_table', 'test_field');
    $this->assertTrue($index_exists, 'Index created.');

    // Rename the table.     $this->assertNull($this->schema->renameTable('test_table', 'test_table2'));

    // Index should be renamed.     $index_exists = $this->schema->indexExists('test_table2', 'test_field');
    $this->assertTrue($index_exists, 'Index was renamed.');

    // We need the default so that we can insert after the rename.     $this->schema->changeField('test_table2', 'test_field', 'test_field', ['type' => 'int', 'not null' => TRUE, 'default' => 0]);
    $this->assertFalse($this->tryInsert(), 'Insert into the old table failed.');
    $this->assertTrue($this->tryInsert('test_table2'), 'Insert into the new table succeeded.');

    // We should have successfully inserted exactly two rows.
Home | Imprint | This part of the site doesn't use cookies.