Skip to content

Commit

Permalink
Fix '' in MariaDB columns TEXT DEFAULT
Browse files Browse the repository at this point in the history
With MariaDB 10.3+:
> create table tmp (c text default '', d varchar(5) default '');
> show columns from tmp;
| c     | text       | YES  |     | ''      |       |
| d     | varchar(5) | YES  |     |         |       |

Yii thought the default value in the c column was "''".
It should be "".
  • Loading branch information
François Gannaz committed Jun 25, 2024
1 parent 7d1ac13 commit 6dd0891
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-silecs.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Miscellanous
------------

- CTreeView used an undocumented jQuery parameter which was removed in modern versions.
- With MariaDB 10.3+, Yii misunderstood `TEXT DEFAULT ''` with a default value of "''".
2 changes: 2 additions & 0 deletions framework/db/schema/mysql/CMysqlColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ protected function extractDefault($defaultValue)
{
if(strncmp($this->dbType,'bit',3)===0)
$this->defaultValue=bindec(trim($defaultValue,'b\''));
elseif(substr_compare($this->dbType,'text',-4)===0)
$this->defaultValue=trim($defaultValue,"''");
elseif(($this->dbType==='timestamp' || $this->dbType==='datetime') && ($defaultValue==='CURRENT_TIMESTAMP' || $defaultValue==='current_timestamp()'))
$this->defaultValue=null;
else
Expand Down
4 changes: 2 additions & 2 deletions tests/framework/db/data/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ CREATE TABLE posts
title VARCHAR(128) NOT NULL,
create_time TIMESTAMP NOT NULL,
author_id INTEGER NOT NULL,
content TEXT,
content TEXT DEFAULT '',
CONSTRAINT FK_post_author FOREIGN KEY (author_id)
REFERENCES users (id) ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB;
Expand Down Expand Up @@ -149,4 +149,4 @@ CREATE TABLE types
bool_col2 BOOLEAN DEFAULT 1,
bit_col1 BIT,
bit_col2 BIT(32) DEFAULT b'101010'
) ENGINE=InnoDB;
) ENGINE=InnoDB;
4 changes: 2 additions & 2 deletions tests/framework/db/schema/CMysqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function testColumn()
(
'name'=>array('id', 'title', 'create_time', 'author_id', 'content'),
'rawName'=>array('`id`', '`title`', '`create_time`', '`author_id`', '`content`'),
'defaultValue'=>array(null, null, null, null, null),
'defaultValue'=>array(null, null, null, null, ''),
'size'=>array(11, 128, null, 11, null),
'precision'=>array(11, 128, null, 11, null),
'scale'=>array(null, null, null, null, null),
Expand Down Expand Up @@ -314,4 +314,4 @@ public function testColumnComments()
$this->assertEquals('Hashed password',$usersColumns['password']->comment);
$this->assertEquals('',$usersColumns['email']->comment);
}
}
}

0 comments on commit 6dd0891

Please sign in to comment.