Skip to content

Commit 6ba36f7

Browse files
committed
Added spatial indexes. Adjusted tests. Attempting to install mysql-5.7 on travis. Updated local-dev Dockerfile.
1 parent a197d13 commit 6ba36f7

File tree

9 files changed

+130
-17
lines changed

9 files changed

+130
-17
lines changed

.travis.install-mysql-5.7.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
echo mysql-apt-config mysql-apt-config/select-server select mysql-5.7 | sudo debconf-set-selections
4+
wget http://dev.mysql.com/get/mysql-apt-config_0.7.3-1_all.deb
5+
sudo dpkg --install mysql-apt-config_0.7.3-1_all.deb
6+
sudo apt-get update -q
7+
sudo apt-get install -q -y -o Dpkg::Options::=--force-confnew mysql-server
8+
sudo mysql_upgrade

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
language: php
2+
3+
dist: trusty
4+
sudo: required
5+
26
php:
37
- '5.5'
48
- '5.6'
@@ -11,6 +15,7 @@ services:
1115
- mysql
1216

1317
before_install:
18+
- bash .travis.install-mysql-5.7.sh
1419
- mysql -e 'CREATE DATABASE test;'
1520

1621
install: composer install

Dockerfile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
FROM php:7.0-cli
22

3+
RUN apt-get update && apt-get install -y unzip git
4+
5+
COPY php.ini /usr/local/etc/php/
6+
7+
# Install and enable xdebug and pdo mysql
38
RUN pecl install xdebug-2.5.1 \
49
&& docker-php-ext-install pdo_mysql \
5-
&& docker-php-ext-enable xdebug pdo_mysql
10+
&& docker-php-ext-enable xdebug pdo_mysql
11+
12+
# Install composer
13+
RUN curl -o /tmp/composer-setup.php https://getcomposer.org/installer \
14+
&& curl -o /tmp/composer-setup.sig https://composer.github.io/installer.sig \
15+
&& php -r "if (hash('SHA384', file_get_contents('/tmp/composer-setup.php')) !== trim(file_get_contents('/tmp/composer-setup.sig'))) { unlink('/tmp/composer-setup.php'); echo 'Invalid installer' . PHP_EOL; exit(1); }" \
16+
&& php /tmp/composer-setup.php --no-ansi --install-dir=/usr/local/bin --filename=composer --snapshot \
17+
&& rm -f /tmp/composer-setup.*

php.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[Date]
2+
date.timezone = UTC

src/Schema/Blueprint.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function point($column)
3131
* @param $column
3232
* @return \Illuminate\Support\Fluent
3333
*/
34-
public function linestring($column)
34+
public function lineString($column)
3535
{
3636
return $this->addColumn('linestring', $column);
3737
}
@@ -53,7 +53,7 @@ public function polygon($column)
5353
* @param $column
5454
* @return \Illuminate\Support\Fluent
5555
*/
56-
public function multipoint($column)
56+
public function multiPoint($column)
5757
{
5858
return $this->addColumn('multipoint', $column);
5959
}
@@ -64,7 +64,7 @@ public function multipoint($column)
6464
* @param $column
6565
* @return \Illuminate\Support\Fluent
6666
*/
67-
public function multilinestring($column)
67+
public function multiLineString($column)
6868
{
6969
return $this->addColumn('multilinestring', $column);
7070
}
@@ -75,7 +75,7 @@ public function multilinestring($column)
7575
* @param $column
7676
* @return \Illuminate\Support\Fluent
7777
*/
78-
public function multipolygon($column)
78+
public function multiPolygon($column)
7979
{
8080
return $this->addColumn('multipolygon', $column);
8181
}
@@ -86,8 +86,30 @@ public function multipolygon($column)
8686
* @param $column
8787
* @return \Illuminate\Support\Fluent
8888
*/
89-
public function geometrycollection($column)
89+
public function geometryCollection($column)
9090
{
9191
return $this->addColumn('geometrycollection', $column);
9292
}
93+
94+
/**
95+
* Specify a spatial index for the table
96+
*
97+
* @param string|array $columns
98+
* @param string $name
99+
* @return \Illuminate\Support\Fluent
100+
*/
101+
public function spatialIndex($columns, $name = null) {
102+
return $this->indexCommand('spatial', $columns, $name);
103+
}
104+
105+
/**
106+
* Indicate that the given index should be dropped.
107+
*
108+
* @param string|array $index
109+
* @return \Illuminate\Support\Fluent
110+
*/
111+
public function dropSpatial($index)
112+
{
113+
return $this->dropIndexCommand('dropIndex', 'spatial', $index);
114+
}
93115
}

src/Schema/Grammars/MySqlGrammar.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Grimzy\LaravelSpatial\Schema\Grammars;
33

4+
use Grimzy\LaravelSpatial\Schema\Blueprint;
45
use Illuminate\Support\Fluent;
56

67
class MySqlGrammar extends \Illuminate\Database\Schema\Grammars\MySqlGrammar
@@ -92,4 +93,32 @@ public function typeGeometrycollection(Fluent $column)
9293
{
9394
return 'GEOMETRYCOLLECTION';
9495
}
96+
97+
/**
98+
* Compile a spatial index key command.
99+
*
100+
* @param \Grimzy\LaravelSpatial\Schema\Blueprint $blueprint
101+
* @param \Illuminate\Support\Fluent $command
102+
* @return string
103+
*/
104+
public function compileSpatial(Blueprint $blueprint, Fluent $command)
105+
{
106+
return $this->compileKey($blueprint, $command, 'spatial');
107+
}
108+
109+
/**
110+
* Compile a drop index command.
111+
*
112+
* @param \Grimzy\LaravelSpatial\Schema\Blueprint $blueprint
113+
* @param \Illuminate\Support\Fluent $command
114+
* @return string
115+
*/
116+
public function compileDropSpatial(Blueprint $blueprint, Fluent $command)
117+
{
118+
$table = $this->wrapTable($blueprint);
119+
120+
$index = $this->wrap($command->index);
121+
122+
return "alter table {$table} drop index {$index}";
123+
}
95124
}

tests/Feature/Migrations/CreateTables.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
use Illuminate\Support\Facades\Schema;
4-
use Illuminate\Database\Schema\Blueprint;
53
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
66

77
class CreateLocationTable extends Migration
88
{
@@ -16,13 +16,13 @@ public function up()
1616
Schema::create('geometry', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->geometry('geo')->default(null)->nullable();
19-
$table->point('location')->default(null)->nullable();
20-
$table->linestring('line')->default(null)->nullable();
19+
$table->point('location'); // required to be not null in order to add an index
20+
$table->lineString('line')->default(null)->nullable();
2121
$table->polygon('shape')->default(null)->nullable();
22-
$table->multipoint('multi_locations')->default(null)->nullable();
23-
$table->multilinestring('multi_lines')->default(null)->nullable();
24-
$table->multipolygon('multi_shapes')->default(null)->nullable();
25-
$table->geometrycollection('multi_geometries')->default(null)->nullable();
22+
$table->multiPoint('multi_locations')->default(null)->nullable();
23+
$table->multiLineString('multi_lines')->default(null)->nullable();
24+
$table->multiPolygon('multi_shapes')->default(null)->nullable();
25+
$table->geometryCollection('multi_geometries')->default(null)->nullable();
2626
$table->timestamps();
2727
});
2828
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class UpdateLocationTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('geometry', function (Blueprint $table) {
17+
$table->spatialIndex('location');
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('geometry', function (Blueprint $table) {
29+
$table->dropSpatial(['location']);
30+
});
31+
}
32+
}

tests/Feature/SpatialTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function tearDown()
4545
{
4646
$this->onMigrations(function ($migrationClass) {
4747
(new $migrationClass)->down();
48-
});
48+
}, true);
4949

5050
parent::tearDown();
5151
}
@@ -59,12 +59,15 @@ protected function assertDatabaseHas($table, array $data, $connection = null)
5959
}
6060
}
6161

62-
private function onMigrations(\Closure $closure)
62+
private function onMigrations(\Closure $closure, $reverse_sort = false)
6363
{
6464
$fileSystem = new Filesystem();
6565
$classFinder = new Tools\ClassFinder();
6666

67-
foreach ($fileSystem->files(__DIR__ . "/Migrations") as $file) {
67+
$migrations = $fileSystem->files(__DIR__ . "/Migrations");
68+
$reverse_sort ? rsort($migrations, SORT_STRING) : sort($migrations, SORT_STRING);
69+
70+
foreach ($migrations as $file) {
6871
$fileSystem->requireOnce($file);
6972
$migrationClass = $classFinder->findClass($file);
7073

0 commit comments

Comments
 (0)