Skip to content

Commit b42dbbe

Browse files
authored
Enh #144: Add UINT_SET attribute type support + Fix tests and CI
1 parent 5d02376 commit b42dbbe

10 files changed

+210
-35
lines changed

.github/workflows/build.yml

+33-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
on:
2-
- pull_request
3-
- push
2+
pull_request:
3+
push:
4+
branches:
5+
- master
46

57
name: build
68

79
jobs:
810
tests:
9-
name: PHP ${{ matrix.php }}-${{ matrix.os }}
11+
name: PHP ${{ matrix.php }}-sphinx-${{ matrix.sphinx }}-${{ matrix.os }}
1012

1113
env:
1214
extensions: pdo, pdo_mysql
@@ -17,8 +19,7 @@ jobs:
1719
strategy:
1820
matrix:
1921
os:
20-
- ubuntu-18.04
21-
22+
- ubuntu-20.04
2223
php:
2324
- "5.4"
2425
- "5.5"
@@ -28,6 +29,18 @@ jobs:
2829
- "7.2"
2930
- "7.3"
3031
- "7.4"
32+
sphinx:
33+
- "2.2.11"
34+
- "3.5.1"
35+
36+
services:
37+
mysql:
38+
image: mysql:5.7
39+
env:
40+
MYSQL_ROOT_PASSWORD: root
41+
MYSQL_DATABASE: yiitest
42+
ports:
43+
- 3306:3306
3144

3245
steps:
3346
- name: Checkout
@@ -53,14 +66,24 @@ jobs:
5366
if: matrix.php == '8.0'
5467
run: composer update --ignore-platform-reqs --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi
5568

69+
- name: Install sphinx
70+
run: sh tests/data/actions/sphinx-setup-${{ matrix.sphinx }}.sh
71+
5672
- name: Setup source database
5773
run: |
58-
sudo /etc/init.d/mysql start
59-
mysql -uroot -proot -e 'CREATE DATABASE `yiitest`;'
60-
mysql -D yiitest -uroot -proot < tests/data/source.sql
74+
mysql -h127.0.0.1 -D yiitest -uroot -proot < tests/data/source.sql
6175
62-
- name: Install sphinx
63-
run: cd tests/data/actions && sh sphinx-setup.sh
76+
- name: Run sphinx 2.2.11
77+
if: matrix.sphinx == '2.2.11'
78+
run: |
79+
indexer --config tests/data/sphinx-${{ matrix.sphinx }}.conf --all
80+
searchd --config tests/data/sphinx-${{ matrix.sphinx }}.conf
81+
82+
- name: Run sphinx 3
83+
if: matrix.sphinx != '2.2.11'
84+
run: |
85+
/opt/sphinx/sphinx-${{ matrix.sphinx }}/bin/indexer --config tests/data/sphinx-${{ matrix.sphinx }}.conf --all
86+
/opt/sphinx/sphinx-${{ matrix.sphinx }}/bin/searchd --config tests/data/sphinx-${{ matrix.sphinx }}.conf
6487
6588
- name: Run tests with phpunit
6689
if: matrix.php != '7.4'

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Yii Framework 2 sphinx extension Change Log
44
2.0.16 under development
55
------------------------
66

7-
- no changes in this release.
7+
- Enh #144: Add `UINT_SET` attribute type support (@vjik)
88

99

1010
2.0.15 November 18, 2022

composer.json

+6
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,11 @@
3838
"branch-alias": {
3939
"dev-master": "2.0.x-dev"
4040
}
41+
},
42+
"config": {
43+
"sort-packages": true,
44+
"allow-plugins": {
45+
"yiisoft/yii2-composer": true
46+
}
4147
}
4248
}

src/Schema.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class Schema extends BaseObject
7676
'bool' => self::TYPE_BOOLEAN,
7777
'float' => self::TYPE_FLOAT,
7878
'mva' => self::TYPE_INTEGER,
79+
'uint_set' => self::TYPE_INTEGER,
7980
];
8081

8182
/**
@@ -538,7 +539,7 @@ protected function loadColumnSchema($info)
538539
$column->isField = ($type === 'field');
539540
$column->isAttribute = !$column->isField;
540541

541-
$column->isMva = ($type === 'mva');
542+
$column->isMva = $type === 'mva' || $type === 'uint_set';
542543

543544
$column->phpType = $this->getColumnPhpType($column);
544545

tests/QueryTest.php

+31-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function testSelect()
1818
$query = new Query();
1919
$query->select('*');
2020
$this->assertEquals(['*' => '*'], $query->select);
21-
$this->assertNull($query->distinct);
21+
$this->assertFalse($query->distinct);
2222
$this->assertEquals(null, $query->selectOption);
2323

2424
$query = new Query();
@@ -458,6 +458,9 @@ public function testFacets()
458458
{
459459
$connection = $this->getConnection();
460460

461+
$rawSphinxVersion = $connection->createCommand("SHOW GLOBAL VARIABLES LIKE 'version'")->queryOne();
462+
$sphinxVersion = isset($rawSphinxVersion['Value']) ? $rawSphinxVersion['Value'] : '';
463+
461464
$query = new Query();
462465
$results = $query->from('yii2_test_article_index')
463466
->match('about')
@@ -480,18 +483,33 @@ public function testFacets()
480483
$this->assertNotEmpty($results['hits'], 'Unable to query with complex facet');
481484
$this->assertNotEmpty($results['facets']['author_id'], 'Unable to fill up complex facet');
482485

483-
$query = new Query();
484-
$results = $query->from('yii2_test_article_index')
485-
->match('about')
486-
->facets([
487-
'range' => [
488-
'select' => 'INTERVAL(author_id,200,400,600,800) AS range',
489-
],
490-
'authorId' => [
491-
'select' => [new Expression('author_id AS authorId')],
492-
],
493-
])
494-
->search($connection);
486+
$query = (new Query())
487+
->from('yii2_test_article_index')
488+
->match('about');
489+
490+
if (strpos($sphinxVersion, '3.') === 0) {
491+
$query = $query
492+
->select(new Expression('INTERVAL(author_id,200,400,600,800) AS range'))
493+
->facets([
494+
'range' => [
495+
'select' => 'range',
496+
],
497+
'authorId' => [
498+
'select' => [new Expression('author_id AS authorId')],
499+
],
500+
]);
501+
} else {
502+
$query = $query
503+
->facets([
504+
'range' => [
505+
'select' => 'INTERVAL(author_id,200,400,600,800) AS range',
506+
],
507+
'authorId' => [
508+
'select' => [new Expression('author_id AS authorId')],
509+
],
510+
]);
511+
}
512+
$results = $query->search($connection);
495513
$this->assertNotEmpty($results['hits'], 'Unable to query with facet using custom select');
496514
$this->assertNotEmpty($results['facets']['range'], 'Unable to fill up facet using function in select');
497515
$this->assertNotEmpty($results['facets']['authorId'], 'Unable to fill up facet using `Expression` in select');

tests/data/actions/sphinx-setup.sh tests/data/actions/sphinx-setup-2.2.11.sh

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
SCRIPT=$(readlink -f "$0")
33
CWD=$(dirname "$SCRIPT")
44

5+
# https://askubuntu.com/a/1337909
6+
echo 'deb http://security.ubuntu.com/ubuntu xenial-security main' | sudo tee /etc/apt/sources.list.d/xenial-security.list
7+
sudo apt update
8+
sudo apt install libmysqlclient20
9+
510
# install sphinx from https://sphinxsearch.com/downloads/release/
611
wget http://sphinxsearch.com/files/sphinxsearch_2.2.11-release-1~xenial_amd64.deb
712
sudo dpkg -i sphinxsearch_2.2.11-release-1~xenial_amd64.deb
813

914
# make dir that is used in sphinx config
1015
mkdir -p sphinx
11-
sed -i s\~SPHINX_BASE_DIR~$PWD/sphinx~g $CWD/../sphinx.conf
12-
13-
# setup test Sphinx indexes:
14-
indexer --config $CWD/../sphinx.conf --all
15-
16-
# run searchd:
17-
searchd --config $CWD/../sphinx.conf
16+
sed -i s\~SPHINX_BASE_DIR~$PWD/sphinx~g $CWD/../sphinx-2.2.11.conf
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh -e
2+
SCRIPT=$(readlink -f "$0")
3+
CWD=$(dirname "$SCRIPT")
4+
5+
wget https://sphinxsearch.com/files/sphinx-3.5.1-82c60cb-linux-amd64.tar.gz -O /tmp/sphinxsearch.tar.gz
6+
sudo mkdir /opt/sphinx
7+
cd /opt/sphinx && sudo tar -zxf /tmp/sphinxsearch.tar.gz
8+
rm /tmp/sphinxsearch.tar.gz
9+
10+
# make dir that is used in sphinx config
11+
mkdir -p $PWD/sphinx
12+
sed -i s\~SPHINX_BASE_DIR~$PWD/sphinx~g $CWD/../sphinx-3.5.1.conf

tests/data/source.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -1056,4 +1056,4 @@ INSERT INTO `yii2_test_article_tag` (`article_id`, `tag_id`) VALUES
10561056
(1, 2),
10571057
(1, 3),
10581058
(2, 3),
1059-
(2, 4);
1059+
(2, 4);

tests/data/sphinx.conf tests/data/sphinx-2.2.11.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ source yii2_test_article_src
1313
{
1414
type = mysql
1515

16-
sql_host = localhost
16+
sql_host = 127.0.0.1
1717
sql_user = root
1818
sql_pass = root
1919
sql_db = yiitest
@@ -34,7 +34,7 @@ source yii2_test_item_src
3434
{
3535
type = mysql
3636

37-
sql_host = localhost
37+
sql_host = 127.0.0.1
3838
sql_user = root
3939
sql_pass = root
4040
sql_db = yiitest

tests/data/sphinx-3.5.1.conf

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Sphinx configuration for the unit tests
2+
#
3+
# Setup test environment:
4+
# - initialize test database source:
5+
# mysql -D yiitest -u test < /path/to/yii/tests/unit/data/sphinx/source.sql
6+
# - setup test Sphinx indexes:
7+
# indexer --config /path/to/yii/tests/unit/data/sphinx/sphinx.conf --all [--rotate]
8+
# - run the "searchd" daemon:
9+
# searchd --config /path/to/yii/tests/unit/data/sphinx/sphinx.conf
10+
11+
12+
source yii2_test_article_src
13+
{
14+
type = mysql
15+
16+
sql_host = 127.0.0.1
17+
sql_user = root
18+
sql_pass = root
19+
sql_db = yiitest
20+
sql_port = 3306 # optional, default is 3306
21+
22+
sql_query = \
23+
SELECT *, UNIX_TIMESTAMP(create_date) AS add_date \
24+
FROM yii2_test_article
25+
26+
sql_attr_uint = author_id
27+
sql_attr_uint = add_date
28+
sql_attr_multi = uint tag from query; SELECT article_id AS id, tag_id AS tag FROM yii2_test_article_tag
29+
}
30+
31+
32+
source yii2_test_item_src
33+
{
34+
type = mysql
35+
36+
sql_host = 127.0.0.1
37+
sql_user = root
38+
sql_pass = root
39+
sql_db = yiitest
40+
sql_port = 3306 # optional, default is 3306
41+
42+
sql_query = \
43+
SELECT *, CURRENT_TIMESTAMP() AS add_date \
44+
FROM yii2_test_item \
45+
WHERE id <= 100
46+
47+
sql_attr_uint = category_id
48+
sql_attr_float = price
49+
sql_attr_uint = add_date
50+
}
51+
52+
53+
source yii2_test_item_delta_src : yii2_test_item_src
54+
{
55+
sql_query = \
56+
SELECT *, CURRENT_TIMESTAMP() AS add_date \
57+
FROM yii2_test_item \
58+
WHERE id > 100
59+
}
60+
61+
62+
index yii2_test_article_index
63+
{
64+
source = yii2_test_article_src
65+
}
66+
67+
68+
index yii2_test_item_index
69+
{
70+
source = yii2_test_item_src
71+
}
72+
73+
74+
index yii2_test_item_delta_index : yii2_test_item_index
75+
{
76+
source = yii2_test_item_delta_src
77+
}
78+
79+
80+
index yii2_test_rt_index
81+
{
82+
type = rt
83+
rt_field = title
84+
rt_attr_string = title
85+
rt_field = content
86+
rt_attr_uint = type_id
87+
rt_attr_multi = category
88+
}
89+
90+
91+
index yii2_test_distributed
92+
{
93+
type = distributed
94+
local = yii2_test_article_index
95+
}
96+
97+
common {
98+
datadir = SPHINX_BASE_DIR
99+
}
100+
101+
indexer
102+
{
103+
mem_limit = 32M
104+
}
105+
106+
searchd
107+
{
108+
#listen = 127.0.0.1:9312
109+
listen = 19306:mysql41
110+
read_timeout = 5
111+
max_children = 30
112+
seamless_rotate = 1
113+
preopen_indexes = 1
114+
unlink_old = 1
115+
workers = threads # for RT to work
116+
}

0 commit comments

Comments
 (0)