diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index dcfd9c755f..c7c948320c 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -18,6 +18,7 @@ Yii Framework 2 Change Log - Bug #17828: Fix `yii\web\UploadedFile::saveAs()` failing when error value in `$_FILES` entry is a string (haveyaseen) - Bug #17829: `yii\helpers\ArrayHelper::filter` now correctly filters data when passing a filter with more than 2 levels (rhertogh) - Enh #7622: Allow `yii\data\ArrayDataProvider` to control the sort flags for `sortModels` through `yii\data\Sort::sortFlags` property (askobara) +- Bug #17667: Fix `CREATE INDEX` failure on sqlite when specifying schema (santilin, samdark) - Enh #16721: Use `Instance::ensure()` to initialize `UrlManager::$cache` (rob006) - Bug #17863: `\yii\helpers\BaseInflector::slug()` doesn't work with an empty string as a replacement argument (haruatari) - Bug #17881: `yii\db\Query::queryScalar()` wasn’t reverting the `select`, `orderBy`, `limit`, and `offset` params if an exception occurred (brandonkelly) diff --git a/framework/db/sqlite/QueryBuilder.php b/framework/db/sqlite/QueryBuilder.php index 38e0fa9273..d47a7fe4cc 100644 --- a/framework/db/sqlite/QueryBuilder.php +++ b/framework/db/sqlite/QueryBuilder.php @@ -545,4 +545,22 @@ class QueryBuilder extends \yii\db\QueryBuilder return trim($result); } + + /** + * {@inheritdoc} + */ + public function createIndex($name, $table, $columns, $unique = false) + { + $tableParts = explode('.', $table); + + $schema = null; + if (count($tableParts) === 2) { + list ($schema, $table) = $tableParts; + } + + return ($unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ') + . $this->db->quoteTableName(($schema ? $schema . '.' : '') . $name) . ' ON ' + . $this->db->quoteTableName($table) + . ' (' . $this->buildColumns($columns) . ')'; + } } diff --git a/tests/framework/db/sqlite/QueryBuilderTest.php b/tests/framework/db/sqlite/QueryBuilderTest.php index e0dd672207..6a4823b9ef 100644 --- a/tests/framework/db/sqlite/QueryBuilderTest.php +++ b/tests/framework/db/sqlite/QueryBuilderTest.php @@ -9,6 +9,7 @@ namespace yiiunit\framework\db\sqlite; use yii\db\Query; use yii\db\Schema; +use yii\db\sqlite\QueryBuilder; use yiiunit\data\base\TraversableObject; /** @@ -65,6 +66,18 @@ class QueryBuilderTest extends \yiiunit\framework\db\QueryBuilderTest { $result = parent::indexesProvider(); $result['drop'][0] = 'DROP INDEX [[CN_constraints_2_single]]'; + + $indexName = 'myindex'; + $schemaName = 'myschema'; + $tableName = 'mytable'; + + $result['with schema'] = [ + "CREATE INDEX {{{$schemaName}}}.[[$indexName]] ON {{{$tableName}}} ([[C_index_1]])", + function (QueryBuilder $qb) use ($tableName, $indexName, $schemaName) { + return $qb->createIndex($indexName, $schemaName . '.' . $tableName, 'C_index_1'); + }, + ]; + return $result; }