Skip to content

Commit

Permalink
Enh #144: Add UINT_SET attribute type support + Fix tests and CI
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Mar 21, 2024
1 parent 5d02376 commit b42dbbe
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 35 deletions.
43 changes: 33 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,8 +19,7 @@ jobs:
strategy:
matrix:
os:
- ubuntu-18.04

- ubuntu-20.04
php:
- "5.4"
- "5.5"
Expand All @@ -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
Expand All @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,11 @@
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"config": {
"sort-packages": true,
"allow-plugins": {
"yiisoft/yii2-composer": true
}
}
}
3 changes: 2 additions & 1 deletion src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

/**
Expand Down Expand Up @@ -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);

Expand Down
44 changes: 31 additions & 13 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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')
Expand All @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions tests/data/actions/sphinx-setup-3.5.1.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tests/data/source.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1056,4 +1056,4 @@ INSERT INTO `yii2_test_article_tag` (`article_id`, `tag_id`) VALUES
(1, 2),
(1, 3),
(2, 3),
(2, 4);
(2, 4);
4 changes: 2 additions & 2 deletions tests/data/sphinx.conf → tests/data/sphinx-2.2.11.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
116 changes: 116 additions & 0 deletions tests/data/sphinx-3.5.1.conf
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit b42dbbe

Please sign in to comment.