Skip to content

Commit

Permalink
Merge e2f6d82 into e2e7193
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed Nov 12, 2018
2 parents e2e7193 + e2f6d82 commit fc28fe6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
5 changes: 2 additions & 3 deletions system/Database/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -950,9 +950,8 @@ protected function _processFields($create_table = false)
{
case 'ENUM':
case 'SET':
$attributes['CONSTRAINT'] = $this->db->escapeString($attributes['CONSTRAINT']);
$field['length'] = is_array($attributes['CONSTRAINT']) ? "('" . implode("','",
$attributes['CONSTRAINT']) . "')" : '(' . $attributes['CONSTRAINT'] . ')';
$attributes['CONSTRAINT'] = $this->db->escape($attributes['CONSTRAINT']);
$field['length'] = is_array($attributes['CONSTRAINT']) ? '(' . implode(',', $attributes['CONSTRAINT']) . ')' : '(' . $attributes['CONSTRAINT'] . ')';
break;
default:
$field['length'] = is_array($attributes['CONSTRAINT']) ? '(' . implode(',',
Expand Down
64 changes: 64 additions & 0 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,68 @@ public function testDropForeignKey()
$this->forge->dropTable('forge_test_invoices', true);
$this->forge->dropTable('forge_test_users', true);
}

public function testEnumFields()
{
if ($this->db->DBDriver !== 'MySQLi')
{
// Postgres uses ENUM to "CREATE TYPE" - so, CREATE TABLE uses the "created type", not ENUM
// https://www.postgresql.org/docs/9.3/datatype-enum.html
$this->markTestSkipped('ENUM Data Type available only in MySQL');
}

$this->forge->addField([
'enum_string' => [
'type' => 'ENUM("a","b")',
],
'enum_array' => [
'type' => 'ENUM',
'constraint' => [
'a',
'b',
],
],
]);
$this->forge->createTable('forge_test_enum');

$fields = $this->db->getFieldData('forge_test_enum');

$this->forge->dropTable('forge_test_enum');

$this->assertEquals('enum_string', $fields[0]->name);
$this->assertEquals('enum', $fields[0]->type);
$this->assertEquals('enum_array', $fields[1]->name);
$this->assertEquals('enum', $fields[1]->type);
}

public function testSetFields()
{
if ($this->db->DBDriver !== 'MySQLi')
{
$this->markTestSkipped('SET Data Type available only in MySQL');
}

$this->forge->addField([
'set_string' => [
'type' => 'SET("a","b")',
],
'set_array' => [
'type' => 'SET',
'constraint' => [
'a',
'b',
],
],
]);
$this->forge->createTable('forge_test_set');

$fields = $this->db->getFieldData('forge_test_set');

$this->forge->dropTable('forge_test_set');

$this->assertEquals('set_string', $fields[0]->name);
$this->assertEquals('set', $fields[0]->type);
$this->assertEquals('set_array', $fields[1]->name);
$this->assertEquals('set', $fields[1]->type);
}
}
18 changes: 15 additions & 3 deletions user_guide_src/source/dbmgmt/forge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ Additionally, the following key/values can be used:
],
];

If your database platform has the ENUM or SET data types, you can define the options as a string in the 'type' index or in the 'constraint' as array::

$fields = array(
'color' => array(
'type' => 'SET("blue","red","yellow")',
),
'status' => array(
'type' => 'ENUM',
'constraint' => array('enabled', 'disabled'),
),
);

After the fields have been defined, they can be added using
``$forge->addField($fields);`` followed by a call to the
``createTable()`` method.
Expand All @@ -132,9 +144,9 @@ string into the field definitions with addField()

$forge->addField("label varchar(100) NOT NULL DEFAULT 'default label'");

.. note:: Passing raw strings as fields cannot be followed by ``add_key()`` calls on those fields.
.. note:: Passing raw strings as fields cannot be followed by ``addKey()`` calls on those fields.

.. note:: Multiple calls to add_field() are cumulative.
.. note:: Multiple calls to addField() are cumulative.

Creating an id field
--------------------
Expand Down Expand Up @@ -320,7 +332,7 @@ Modifying a Column in a Table

**$forge->modifyColumn()**

The usage of this method is identical to ``add_column()``, except it
The usage of this method is identical to ``addColumn()``, except it
alters an existing column rather than adding a new one. In order to
change the name you can add a "name" key into the field defining array.

Expand Down

0 comments on commit fc28fe6

Please sign in to comment.