Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 9c477ab

Browse files
authored
Merge pull request #152 from gbuckingham89/postgis-existence
2 parents 1e9b896 + f343b5d commit 9c477ab

File tree

6 files changed

+159
-4
lines changed

6 files changed

+159
-4
lines changed

README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,57 @@ If you are using Laravel < 5.5, you also need to add the DatabaseServiceProvider
5050
```
5151
## Usage
5252

53-
First of all, make sure to enable postgis.
53+
To start, ensure you have PostGIS enabled in your database - you can do this in a Laravel migration or manually via SQL.
54+
55+
### Enable PostGIS via a Laravel migration
56+
57+
Create a new migration file by running
58+
59+
php artisan make:migration enable_postgis
60+
61+
Update the newly created migration file to call the `enablePostgisIfNotExists()` and `disablePostgisIfExists()` methods on the `Schema` facade. For example:
62+
63+
```PHP
64+
<?php
65+
66+
use Illuminate\Database\Migrations\Migration;
67+
use Illuminate\Support\Facades\Schema;
68+
69+
class EnablePostgis extends Migration
70+
{
71+
/**
72+
* Run the migrations.
73+
*
74+
* @return void
75+
*/
76+
public function up()
77+
{
78+
Schema::enablePostgisIfNotExists();
79+
}
80+
81+
/**
82+
* Reverse the migrations.
83+
*
84+
* @return void
85+
*/
86+
public function down()
87+
{
88+
Schema::disablePostgisIfExists();
89+
}
90+
}
91+
```
92+
93+
These methods are safe to use and will only enable / disable the PostGIS extension if relevant - they won't cause an error if PostGIS is / isn't already enabled.
94+
95+
If you prefer, you can use the `enablePostgis()` method which will throw an error if PostGIS is already enabled, and the `disablePostgis()` method twhich will throw an error if PostGIS isn't enabled.
96+
97+
### Enable PostGIS manually
98+
99+
Use an SQL client to connect to your database and run the following command:
54100

55101
CREATE EXTENSION postgis;
56102

57-
To verify that postgis is enabled
103+
To verify that PostGIS is enabled you can run:
58104

59105
SELECT postgis_full_version();
60106

src/Schema/Blueprint.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,37 @@ public function enablePostgis()
126126
return $this->addCommand('enablePostgis');
127127
}
128128

129+
/**
130+
* Enable postgis on this database.
131+
* Will create the extension in the database if it doesn't already exist.
132+
*
133+
* @return \Illuminate\Support\Fluent
134+
*/
135+
public function enablePostgisIfNotExists()
136+
{
137+
return $this->addCommand('enablePostgisIfNotExists');
138+
}
139+
129140
/**
130141
* Disable postgis on this database.
131142
* WIll drop the extension in the database.
143+
*
132144
* @return \Illuminate\Support\Fluent
133145
*/
134146
public function disablePostgis()
135147
{
136148
return $this->addCommand('disablePostgis');
137149
}
138150

151+
/**
152+
* Disable postgis on this database.
153+
* WIll drop the extension in the database if it exists.
154+
*
155+
* @return \Illuminate\Support\Fluent
156+
*/
157+
public function disablePostgisIfExists()
158+
{
159+
return $this->addCommand('disablePostgisIfExists');
160+
}
161+
139162
}

src/Schema/Builder.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public function createBlueprint($table, Closure $callback = null)
1717
}
1818

1919
/**
20-
* Enable foreign key constraints.
20+
* Enable postgis on this database.
21+
* Will create the extension in the database.
2122
*
2223
* @return bool
2324
*/
@@ -29,7 +30,21 @@ public function enablePostgis()
2930
}
3031

3132
/**
32-
* Disable foreign key constraints.
33+
* Enable postgis on this database.
34+
* Will create the extension in the database if it doesn't already exist.
35+
*
36+
* @return bool
37+
*/
38+
public function enablePostgisIfNotExists()
39+
{
40+
return $this->connection->statement(
41+
$this->grammar->compileEnablePostgisIfNotExists()
42+
);
43+
}
44+
45+
/**
46+
* Disable postgis on this database.
47+
* WIll drop the extension in the database.
3348
*
3449
* @return bool
3550
*/
@@ -39,4 +54,17 @@ public function disablePostgis()
3954
$this->grammar->compileDisablePostgis()
4055
);
4156
}
57+
58+
/**
59+
* Disable postgis on this database.
60+
* WIll drop the extension in the database if it exists.
61+
*
62+
* @return bool
63+
*/
64+
public function disablePostgisIfExists()
65+
{
66+
return $this->connection->statement(
67+
$this->grammar->compileDisablePostgisIfExists()
68+
);
69+
}
4270
}

src/Schema/Grammars/PostgisGrammar.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ public function compileEnablePostgis()
133133
return 'CREATE EXTENSION postgis';
134134
}
135135

136+
/**
137+
* Adds a statement to create the postgis extension, if it doesn't already exist
138+
*
139+
* @return string
140+
*/
141+
public function compileEnablePostgisIfNotExists()
142+
{
143+
return 'CREATE EXTENSION IF NOT EXISTS postgis';
144+
}
145+
136146
/**
137147
* Adds a statement to drop the postgis extension
138148
*
@@ -143,6 +153,16 @@ public function compileDisablePostgis()
143153
return 'DROP EXTENSION postgis';
144154
}
145155

156+
/**
157+
* Adds a statement to drop the postgis extension, if it exists
158+
*
159+
* @return string
160+
*/
161+
public function compileDisablePostgisIfExists()
162+
{
163+
return 'DROP EXTENSION IF EXISTS postgis';
164+
}
165+
146166
/**
147167
* Adds a statement to add a geometry column
148168
*

tests/Schema/BlueprintTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ public function testEnablePostgis()
9090
$this->blueprint->enablePostgis();
9191
}
9292

93+
public function testEnablePostgisIfNotExists()
94+
{
95+
$this->blueprint
96+
->shouldReceive('addCommand')
97+
->with('enablePostgis', []);
98+
99+
$this->blueprint->enablePostgisIfNotExists();
100+
}
101+
93102
public function testDisablePostgis()
94103
{
95104
$this->blueprint
@@ -98,4 +107,13 @@ public function testDisablePostgis()
98107

99108
$this->blueprint->disablePostgis();
100109
}
110+
111+
public function testDisablePostgisIfExists()
112+
{
113+
$this->blueprint
114+
->shouldReceive('addCommand')
115+
->with('disablePostgis', []);
116+
117+
$this->blueprint->disablePostgisIfExists();
118+
}
101119
}

tests/Schema/Grammars/PostgisGrammarTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,16 @@ public function testEnablePostgis()
274274
$this->assertStringContainsString('CREATE EXTENSION postgis', $statements[0]);
275275
}
276276

277+
public function testEnablePostgisIfNotExists()
278+
{
279+
$blueprint = new Blueprint('test');
280+
$blueprint->enablePostgisIfNotExists();
281+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
282+
283+
$this->assertCount(1, $statements);;
284+
$this->assertStringContainsString('CREATE EXTENSION IF NOT EXISTS postgis', $statements[0]);
285+
}
286+
277287
public function testDisablePostgis()
278288
{
279289
$blueprint = new Blueprint('test');
@@ -284,6 +294,16 @@ public function testDisablePostgis()
284294
$this->assertStringContainsString('DROP EXTENSION postgis', $statements[0]);
285295
}
286296

297+
public function testDisablePostgisIfExists()
298+
{
299+
$blueprint = new Blueprint('test');
300+
$blueprint->disablePostgisIfExists();
301+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
302+
303+
$this->assertCount(1, $statements);;
304+
$this->assertStringContainsString('DROP EXTENSION IF EXISTS postgis', $statements[0]);
305+
}
306+
287307
/**
288308
* @return Connection
289309
*/

0 commit comments

Comments
 (0)