Running tests for postgresql brings exceptions

FileFieldValidateTestCase->testRequired()

SQLSTATE[42P07]: Duplicate table: 7 ERROR: relation "simpletest239413field_data_field_s239413c9d2bf6f_etid_idx" already exists


ListFieldTestCase->testUpdateAllowedValues()

SQLSTATE[42P07]: Duplicate table: 7 ERROR: relation "simpletest727934field_data_field_test_etid_idx" already exists

This caused by renaming tables but not renaming there indexes

Comments

andypost’s picture

Status: Active » Needs review
StatusFileSize
new1.74 KB

This patch adds field[id] to index-name so all index names for fields are unique and does not need rename on delete

damien tournoud’s picture

Title: field_sql_storage_field_storage_delete_field() should rename indexes too » DatabaseSchema_pgsql::renameTable() needs to rename the indexes
Component: field system » postgresql database
StatusFileSize
new1.66 KB

Actually, this is the responsibility of the PostgreSQL driver. We need to rename indexes to match the new table name.

andypost’s picture

I still think that we should change index-names inside field_sql_storage_field_storage_delete_field()

Because after renaming table schema is changed and index-names returned by _field_sql_storage_schema() are different from actually stored in DB for all DB-engines

damien tournoud’s picture

@andypost, no that's not the case. The name of the indexes in hook_schema() are *relative to the table*. We only have to do that in PostgreSQL because the index names are global, not table-specific.

andypost’s picture

So +1 to commit your patch, I dont know is it a good idea to query pg_indexes... waiting for Josh review

Anyway confirm this fix both tests for fields.

damien tournoud’s picture

Related: #718846: DatabaseSchema_pgsql should not prefix the constraints names. @andypost, care to fix that other issue?

andypost’s picture

There's a a test testFieldUpdateIndexesWithData which works only for mysql now

Suppose this onу could be extended

field_sql_storage.module

  function getIndexes($table) {
    $indexes = array();
    $result = db_query("SHOW INDEXES FROM {" . $table . "}");
    foreach ($result as $row) {
      $indexes[$row->key_name][$row->seq_in_index] = $row->column_name;
    }
    return $indexes;
  }
andypost’s picture

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Because PK and UK are indexes this patch works fine.
I think tests from #8 cover index rename

webchick’s picture

Status: Reviewed & tested by the community » Needs work

If we're going to change the name of the first argument here, we should do it in the other back-ends too, for consistency.

Additionally, it sounds like we need expanded test coverage for this?

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new1.39 KB

Suppose Damien does not mean to change the api

Extended tests goes in #718254: Fieldname in tests are too long and #720620: indexExists() for pgsql and sqlite does not prefix tablename
Because this is a different trouble

andypost’s picture

Sequence of patches:
1) This because other issues with tests depends on it
2) #720620: indexExists() for pgsql and sqlite does not prefix tablename - to repair schema (here first part of tests)
3) #718254: Fieldname in tests are too long - Tests for fields storage now could be expanded on posgresql and sqlite

dries’s picture

Can we add a one sentence explanation in the code explaining _why_ we are doing this? Thanks!

damien tournoud’s picture

As a side note: I was under the impression for a while that we also needed to rename the named constraints associated to the table (as described in #718846: DatabaseSchema_pgsql should not prefix the constraints names). But we only actually use named constraints for the unique keys, and it seems that renaming the index that PostgreSQL automatically creates when you add a UNIQUE constraints is enough to rename the constraint itself.

All good.

andypost’s picture

StatusFileSize
new1.49 KB

New patch with improved comment

andypost’s picture

damien tournoud’s picture

Status: Needs review » Reviewed & tested by the community

Let's get this sucker in.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Cool, I think that makes sense. I made a minor English correction and committed to HEAD. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.