I have managed to get working a solution for the ENUM datatype for MySQL. I don't yet know how to create patches etc. so I put describe here my work and I hope that if you are looking for a way to make this work you can use my work to help you, or even better, if you are able to make this fix get into core, all the better.

In your database schema add an extra item called 'enum_items' as shown in the example below:

...

 $schema['vchess_games'] = array(
      'description' => 'This table contains a summary of each game',
      'fields' => array(
          'turn' => array(
              'description' => 'Whose turn it is to play, either "w" (white) or "b" (black)',
              'type' => 'enum',
              'enum_items' => array('w', 'b'),
              'not null' => TRUE,
              'default' => 'w',
          ),
       'status' => array(
              'description' => 'Status of the game',
              'type' => 'enum',
              'enum_items' => array('in progress','1/2-1/2','1-0','0-1'),
              'not null' => TRUE,
          ),
...

In the MySQL version of schema.inc in /includes/database/mysql/schema.inc file add code for using these ENUM items:

protected function createFieldSql($name, $spec) {
    $sql = "`" . $name . "` " . $spec['mysql_type'];

    if (in_array($spec['mysql_type'], array('VARCHAR', 'CHAR', 'TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT', 'TEXT')) && isset($spec['length'])) {
      $sql .= '(' . $spec['length'] . ')';
    }
    // New ENUM code begins here:
    elseif ($spec['mysql_type'] == 'ENUM') {
      // Build a string of the enum items like "('a','b','c')"
      $sql .= '(';
      foreach ($spec['enum_items'] as $enum_item) {
        $sql .= "'" . $enum_item . "',";
      }
      $sql = trim($sql, ",");  // Remove the final trailing comma
      $sql .= ')';
    }
    // End of new ENUM code
    elseif (isset($spec['precision']) && isset($spec['scale'])) {
      $sql .= '(' . $spec['precision'] . ', ' . $spec['scale'] . ')';
    }
...

In the same file, add 'enum:normal' => 'ENUM', to the getFieldTypeMap() function:


    public function getFieldTypeMap() {
    // Put :normal last so it gets preserved by array_flip. This makes
    // it much easier for modules (such as schema.module) to map
    // database types back into schema types.
    // $map does not use drupal_static as its value never changes.
    static $map = array(
      'varchar:normal'  => 'VARCHAR',
      'char:normal'     => 'CHAR',

      'text:tiny'       => 'TINYTEXT',
      'text:small'      => 'TINYTEXT',
      'text:medium'     => 'MEDIUMTEXT',
      'text:big'        => 'LONGTEXT',
      'text:normal'     => 'TEXT',

      'serial:tiny'     => 'TINYINT',
      'serial:small'    => 'SMALLINT',
      'serial:medium'   => 'MEDIUMINT',
      'serial:big'      => 'BIGINT',
      'serial:normal'   => 'INT',

      'int:tiny'        => 'TINYINT',
      'int:small'       => 'SMALLINT',
      'int:medium'      => 'MEDIUMINT',
      'int:big'         => 'BIGINT',
      'int:normal'      => 'INT',

      'float:tiny'      => 'FLOAT',
      'float:small'     => 'FLOAT',
      'float:medium'    => 'FLOAT',
      'float:big'       => 'DOUBLE',
      'float:normal'    => 'FLOAT',

      'numeric:normal'  => 'DECIMAL',
        
      'enum:normal'    => 'ENUM',   // This is the line to add for ENUM

      'blob:big'        => 'LONGBLOB',
      'blob:normal'     => 'BLOB',
    );
    return $map;
  }

I'm absolutely delighted it's working for me; it's the first time I've ever tried to patch anything in core! I hope that this work can also help others to get it working.

Issue fork drupal-1464354

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

marcingy’s picture

Version: 7.10 » 8.x-dev
Category: bug » feature

Not a bug and also needs to be against d8.

drupalshrek’s picture

Great, thanks.

drupalshrek’s picture

For reference, a similar fix for TIMESTAMP is available here:
http://drupal.org/node/1466122

drupalshrek’s picture

Here is a patch for fixing this in Drupal 8.x-dev.

This is my first ever patch, so be gentle with it and me.

It fixes both this issue and the issue http://drupal.org/node/1466122. I hope that's OK.

drupalshrek’s picture

Status: Active » Needs review
drupalshrek’s picture

One thing I do want to add is that I decided to use the term 'enum' rather than 'enum_items' in my introductory post on this page.

...

$schema['vchess_games'] = array(
      'description' => 'This table contains a summary of each game',
      'fields' => array(
          'turn' => array(
              'description' => 'Whose turn it is to play, either "w" (white) or "b" (black)',
              'type' => 'enum',
              'enum' => array('w', 'b'),
              'not null' => TRUE,
              'default' => 'w',
          ),
       'status' => array(
              'description' => 'Status of the game',
              'type' => 'enum',
              'enum' => array('in progress','1/2-1/2','1-0','0-1'),
              'not null' => TRUE,
          ),
...
drupalshrek’s picture

Attached is a patch for this for 7.14

Status: Needs review » Needs work

The last submitted patch, core-7.14-ENUM_and_TIMESTAMP-1464354.patch, failed testing.

drupalshrek’s picture

OK, I think I understand how this system works now: one issue, one patch! So I have raised a separate issue for the 7.x patch: http://drupal.org/node/1560974

And here I am putting back the 8.x-dev patch (first posted above on March 30, 2012 at 3:33pm).

drupalshrek’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, drupal-ENUM_and_TIMESTAMP-1464354.patch, failed testing.

drupalshrek’s picture

OK, redone patch for latest 8.x

drupalshrek’s picture

Status: Needs work » Needs review
xbroom’s picture

drupalshrek’s picture

dalin’s picture

dalin’s picture

I do think this is needed because there is no other way that I can see to hack around this limitation. You can *almost* do this:

'mysql_type' => "enum('foo', 'bar', 'baz')",

But DBTNG will convert those enums to uppercase.

I think this patch is a good approach, but it needs some changes:
+ // Build a string of the enum items like "('a','b','c')"

Code comments that describe what a line of code is doing are only useful if it's a really complicated line. Otherwise it's just redundant.

+      $sql .= '(';
+      foreach ($spec['enum'] as $enum) {
+        $sql .= "'" . $enum . "',";
+      }
+      $sql = trim($sql, ",");  // Remove the final trailing comma

This can be replaced with a simple $sql .= '(' . implode(', ', $spec['enum']) . ')';. Also according to Drupal coding standards comments must be on their own line.

+        
+      'timestamp:normal'=> 'TIMESTAMP',

This is unrelated to this issue. It should be in a separate patch on the other issue.

+ $sql .= ')';

dalin’s picture

Status: Needs review » Needs work

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Sophie.SK’s picture

Version: 8.6.x-dev » 8.9.x-dev

I've been using the patch from #12 on a number of different sites over the last few years and find it hella useful. It allows for much better storage of dates.

It still applies on the latest 8.7, but it probably needs updating for 8.9 and beyond. Just wanted to bump this issue up and add extra info in case anyone else is wondering whether this works :)

daffie’s picture

Status: Needs work » Closed (won't fix)

For adding new datatypes to Drupal core they must work for all three by core supported databases. There is no enum datatype for SQLite.
I do not see how we can fix this for Drupal core. This should be fixed in a contrib module.

RhiP made their first commit to this issue’s fork.

RhiP’s picture

FileSize
1.46 KB

Rerolled Patch #12 for those of us yet to remove our ENUMs https://git.drupalcode.org/issue/drupal-1464354/-/merge_requests/1.patch