diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 73f47542..dc146ca1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,12 +1,14 @@ on: - - pull_request - - push + pull_request: + push: + branches: + - master name: build jobs: tests: - name: PHP ${{ matrix.php }}-${{ matrix.os }} + name: PHP ${{ matrix.php }}-sphinx-${{ matrix.sphinx }}-${{ matrix.os }} env: extensions: pdo, pdo_mysql @@ -17,8 +19,7 @@ jobs: strategy: matrix: os: - - ubuntu-18.04 - + - ubuntu-20.04 php: - "5.4" - "5.5" @@ -28,6 +29,18 @@ jobs: - "7.2" - "7.3" - "7.4" + sphinx: + - "2.2.11" + - "3.5.1" + + services: + mysql: + image: mysql:5.7 + env: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: yiitest + ports: + - 3306:3306 steps: - name: Checkout @@ -53,14 +66,24 @@ jobs: if: matrix.php == '8.0' run: composer update --ignore-platform-reqs --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + - name: Install sphinx + run: sh tests/data/actions/sphinx-setup-${{ matrix.sphinx }}.sh + - name: Setup source database run: | - sudo /etc/init.d/mysql start - mysql -uroot -proot -e 'CREATE DATABASE `yiitest`;' - mysql -D yiitest -uroot -proot < tests/data/source.sql + mysql -h127.0.0.1 -D yiitest -uroot -proot < tests/data/source.sql - - name: Install sphinx - run: cd tests/data/actions && sh sphinx-setup.sh + - name: Run sphinx 2.2.11 + if: matrix.sphinx == '2.2.11' + run: | + indexer --config tests/data/sphinx-${{ matrix.sphinx }}.conf --all + searchd --config tests/data/sphinx-${{ matrix.sphinx }}.conf + + - name: Run sphinx 3 + if: matrix.sphinx != '2.2.11' + run: | + /opt/sphinx/sphinx-${{ matrix.sphinx }}/bin/indexer --config tests/data/sphinx-${{ matrix.sphinx }}.conf --all + /opt/sphinx/sphinx-${{ matrix.sphinx }}/bin/searchd --config tests/data/sphinx-${{ matrix.sphinx }}.conf - name: Run tests with phpunit if: matrix.php != '7.4' diff --git a/CHANGELOG.md b/CHANGELOG.md index d08849a1..0422cd8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 sphinx extension Change Log 2.0.16 under development ------------------------ -- no changes in this release. +- Enh #144: Add `UINT_SET` attribute type support (@vjik) 2.0.15 November 18, 2022 diff --git a/composer.json b/composer.json index 447f29a8..dc00941f 100644 --- a/composer.json +++ b/composer.json @@ -38,5 +38,11 @@ "branch-alias": { "dev-master": "2.0.x-dev" } + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "yiisoft/yii2-composer": true + } } } diff --git a/src/Schema.php b/src/Schema.php index f5450eea..a1dd76ad 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -76,6 +76,7 @@ class Schema extends BaseObject 'bool' => self::TYPE_BOOLEAN, 'float' => self::TYPE_FLOAT, 'mva' => self::TYPE_INTEGER, + 'uint_set' => self::TYPE_INTEGER, ]; /** @@ -538,7 +539,7 @@ protected function loadColumnSchema($info) $column->isField = ($type === 'field'); $column->isAttribute = !$column->isField; - $column->isMva = ($type === 'mva'); + $column->isMva = $type === 'mva' || $type === 'uint_set'; $column->phpType = $this->getColumnPhpType($column); diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 2ff9b16d..1a502757 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -18,7 +18,7 @@ public function testSelect() $query = new Query(); $query->select('*'); $this->assertEquals(['*' => '*'], $query->select); - $this->assertNull($query->distinct); + $this->assertFalse($query->distinct); $this->assertEquals(null, $query->selectOption); $query = new Query(); @@ -458,6 +458,9 @@ public function testFacets() { $connection = $this->getConnection(); + $rawSphinxVersion = $connection->createCommand("SHOW GLOBAL VARIABLES LIKE 'version'")->queryOne(); + $sphinxVersion = isset($rawSphinxVersion['Value']) ? $rawSphinxVersion['Value'] : ''; + $query = new Query(); $results = $query->from('yii2_test_article_index') ->match('about') @@ -480,18 +483,33 @@ public function testFacets() $this->assertNotEmpty($results['hits'], 'Unable to query with complex facet'); $this->assertNotEmpty($results['facets']['author_id'], 'Unable to fill up complex facet'); - $query = new Query(); - $results = $query->from('yii2_test_article_index') - ->match('about') - ->facets([ - 'range' => [ - 'select' => 'INTERVAL(author_id,200,400,600,800) AS range', - ], - 'authorId' => [ - 'select' => [new Expression('author_id AS authorId')], - ], - ]) - ->search($connection); + $query = (new Query()) + ->from('yii2_test_article_index') + ->match('about'); + + if (strpos($sphinxVersion, '3.') === 0) { + $query = $query + ->select(new Expression('INTERVAL(author_id,200,400,600,800) AS range')) + ->facets([ + 'range' => [ + 'select' => 'range', + ], + 'authorId' => [ + 'select' => [new Expression('author_id AS authorId')], + ], + ]); + } else { + $query = $query + ->facets([ + 'range' => [ + 'select' => 'INTERVAL(author_id,200,400,600,800) AS range', + ], + 'authorId' => [ + 'select' => [new Expression('author_id AS authorId')], + ], + ]); + } + $results = $query->search($connection); $this->assertNotEmpty($results['hits'], 'Unable to query with facet using custom select'); $this->assertNotEmpty($results['facets']['range'], 'Unable to fill up facet using function in select'); $this->assertNotEmpty($results['facets']['authorId'], 'Unable to fill up facet using `Expression` in select'); diff --git a/tests/data/actions/sphinx-setup.sh b/tests/data/actions/sphinx-setup-2.2.11.sh similarity index 54% rename from tests/data/actions/sphinx-setup.sh rename to tests/data/actions/sphinx-setup-2.2.11.sh index 4e67fdb4..ce370b7a 100644 --- a/tests/data/actions/sphinx-setup.sh +++ b/tests/data/actions/sphinx-setup-2.2.11.sh @@ -2,16 +2,15 @@ SCRIPT=$(readlink -f "$0") CWD=$(dirname "$SCRIPT") +# https://askubuntu.com/a/1337909 +echo 'deb http://security.ubuntu.com/ubuntu xenial-security main' | sudo tee /etc/apt/sources.list.d/xenial-security.list +sudo apt update +sudo apt install libmysqlclient20 + # install sphinx from https://sphinxsearch.com/downloads/release/ wget http://sphinxsearch.com/files/sphinxsearch_2.2.11-release-1~xenial_amd64.deb sudo dpkg -i sphinxsearch_2.2.11-release-1~xenial_amd64.deb # make dir that is used in sphinx config mkdir -p sphinx -sed -i s\~SPHINX_BASE_DIR~$PWD/sphinx~g $CWD/../sphinx.conf - -# setup test Sphinx indexes: -indexer --config $CWD/../sphinx.conf --all - -# run searchd: -searchd --config $CWD/../sphinx.conf +sed -i s\~SPHINX_BASE_DIR~$PWD/sphinx~g $CWD/../sphinx-2.2.11.conf diff --git a/tests/data/actions/sphinx-setup-3.5.1.sh b/tests/data/actions/sphinx-setup-3.5.1.sh new file mode 100644 index 00000000..1632cf9c --- /dev/null +++ b/tests/data/actions/sphinx-setup-3.5.1.sh @@ -0,0 +1,12 @@ +#!/bin/sh -e +SCRIPT=$(readlink -f "$0") +CWD=$(dirname "$SCRIPT") + +wget https://sphinxsearch.com/files/sphinx-3.5.1-82c60cb-linux-amd64.tar.gz -O /tmp/sphinxsearch.tar.gz +sudo mkdir /opt/sphinx +cd /opt/sphinx && sudo tar -zxf /tmp/sphinxsearch.tar.gz +rm /tmp/sphinxsearch.tar.gz + +# make dir that is used in sphinx config +mkdir -p $PWD/sphinx +sed -i s\~SPHINX_BASE_DIR~$PWD/sphinx~g $CWD/../sphinx-3.5.1.conf diff --git a/tests/data/source.sql b/tests/data/source.sql index 9f37ad4d..74650476 100644 --- a/tests/data/source.sql +++ b/tests/data/source.sql @@ -1056,4 +1056,4 @@ INSERT INTO `yii2_test_article_tag` (`article_id`, `tag_id`) VALUES (1, 2), (1, 3), (2, 3), -(2, 4); \ No newline at end of file +(2, 4); diff --git a/tests/data/sphinx.conf b/tests/data/sphinx-2.2.11.conf similarity index 98% rename from tests/data/sphinx.conf rename to tests/data/sphinx-2.2.11.conf index 4a9ed53f..377dcd97 100644 --- a/tests/data/sphinx.conf +++ b/tests/data/sphinx-2.2.11.conf @@ -13,7 +13,7 @@ source yii2_test_article_src { type = mysql - sql_host = localhost + sql_host = 127.0.0.1 sql_user = root sql_pass = root sql_db = yiitest @@ -34,7 +34,7 @@ source yii2_test_item_src { type = mysql - sql_host = localhost + sql_host = 127.0.0.1 sql_user = root sql_pass = root sql_db = yiitest diff --git a/tests/data/sphinx-3.5.1.conf b/tests/data/sphinx-3.5.1.conf new file mode 100644 index 00000000..6f304e5f --- /dev/null +++ b/tests/data/sphinx-3.5.1.conf @@ -0,0 +1,116 @@ +# Sphinx configuration for the unit tests +# +# Setup test environment: +# - initialize test database source: +# mysql -D yiitest -u test < /path/to/yii/tests/unit/data/sphinx/source.sql +# - setup test Sphinx indexes: +# indexer --config /path/to/yii/tests/unit/data/sphinx/sphinx.conf --all [--rotate] +# - run the "searchd" daemon: +# searchd --config /path/to/yii/tests/unit/data/sphinx/sphinx.conf + + +source yii2_test_article_src +{ + type = mysql + + sql_host = 127.0.0.1 + sql_user = root + sql_pass = root + sql_db = yiitest + sql_port = 3306 # optional, default is 3306 + + sql_query = \ + SELECT *, UNIX_TIMESTAMP(create_date) AS add_date \ + FROM yii2_test_article + + sql_attr_uint = author_id + sql_attr_uint = add_date + sql_attr_multi = uint tag from query; SELECT article_id AS id, tag_id AS tag FROM yii2_test_article_tag +} + + +source yii2_test_item_src +{ + type = mysql + + sql_host = 127.0.0.1 + sql_user = root + sql_pass = root + sql_db = yiitest + sql_port = 3306 # optional, default is 3306 + + sql_query = \ + SELECT *, CURRENT_TIMESTAMP() AS add_date \ + FROM yii2_test_item \ + WHERE id <= 100 + + sql_attr_uint = category_id + sql_attr_float = price + sql_attr_uint = add_date +} + + +source yii2_test_item_delta_src : yii2_test_item_src +{ + sql_query = \ + SELECT *, CURRENT_TIMESTAMP() AS add_date \ + FROM yii2_test_item \ + WHERE id > 100 +} + + +index yii2_test_article_index +{ + source = yii2_test_article_src +} + + +index yii2_test_item_index +{ + source = yii2_test_item_src +} + + +index yii2_test_item_delta_index : yii2_test_item_index +{ + source = yii2_test_item_delta_src +} + + +index yii2_test_rt_index +{ + type = rt + rt_field = title + rt_attr_string = title + rt_field = content + rt_attr_uint = type_id + rt_attr_multi = category +} + + +index yii2_test_distributed +{ + type = distributed + local = yii2_test_article_index +} + +common { + datadir = SPHINX_BASE_DIR +} + +indexer +{ + mem_limit = 32M +} + +searchd +{ + #listen = 127.0.0.1:9312 + listen = 19306:mysql41 + read_timeout = 5 + max_children = 30 + seamless_rotate = 1 + preopen_indexes = 1 + unlink_old = 1 + workers = threads # for RT to work +}