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

Commit

Permalink
Merge pull request #152 from gbuckingham89/postgis-existence
Browse files Browse the repository at this point in the history
  • Loading branch information
mstaack authored Jan 29, 2021
2 parents 1e9b896 + f343b5d commit 9c477ab
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 4 deletions.
50 changes: 48 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,57 @@ If you are using Laravel < 5.5, you also need to add the DatabaseServiceProvider
```
## Usage

First of all, make sure to enable postgis.
To start, ensure you have PostGIS enabled in your database - you can do this in a Laravel migration or manually via SQL.

### Enable PostGIS via a Laravel migration

Create a new migration file by running

php artisan make:migration enable_postgis

Update the newly created migration file to call the `enablePostgisIfNotExists()` and `disablePostgisIfExists()` methods on the `Schema` facade. For example:

```PHP
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Schema;

class EnablePostgis extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::enablePostgisIfNotExists();
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::disablePostgisIfExists();
}
}
```

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.

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.

### Enable PostGIS manually

Use an SQL client to connect to your database and run the following command:

CREATE EXTENSION postgis;

To verify that postgis is enabled
To verify that PostGIS is enabled you can run:

SELECT postgis_full_version();

Expand Down
23 changes: 23 additions & 0 deletions src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,37 @@ public function enablePostgis()
return $this->addCommand('enablePostgis');
}

/**
* Enable postgis on this database.
* Will create the extension in the database if it doesn't already exist.
*
* @return \Illuminate\Support\Fluent
*/
public function enablePostgisIfNotExists()
{
return $this->addCommand('enablePostgisIfNotExists');
}

/**
* Disable postgis on this database.
* WIll drop the extension in the database.
*
* @return \Illuminate\Support\Fluent
*/
public function disablePostgis()
{
return $this->addCommand('disablePostgis');
}

/**
* Disable postgis on this database.
* WIll drop the extension in the database if it exists.
*
* @return \Illuminate\Support\Fluent
*/
public function disablePostgisIfExists()
{
return $this->addCommand('disablePostgisIfExists');
}

}
32 changes: 30 additions & 2 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function createBlueprint($table, Closure $callback = null)
}

/**
* Enable foreign key constraints.
* Enable postgis on this database.
* Will create the extension in the database.
*
* @return bool
*/
Expand All @@ -29,7 +30,21 @@ public function enablePostgis()
}

/**
* Disable foreign key constraints.
* Enable postgis on this database.
* Will create the extension in the database if it doesn't already exist.
*
* @return bool
*/
public function enablePostgisIfNotExists()
{
return $this->connection->statement(
$this->grammar->compileEnablePostgisIfNotExists()
);
}

/**
* Disable postgis on this database.
* WIll drop the extension in the database.
*
* @return bool
*/
Expand All @@ -39,4 +54,17 @@ public function disablePostgis()
$this->grammar->compileDisablePostgis()
);
}

/**
* Disable postgis on this database.
* WIll drop the extension in the database if it exists.
*
* @return bool
*/
public function disablePostgisIfExists()
{
return $this->connection->statement(
$this->grammar->compileDisablePostgisIfExists()
);
}
}
20 changes: 20 additions & 0 deletions src/Schema/Grammars/PostgisGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ public function compileEnablePostgis()
return 'CREATE EXTENSION postgis';
}

/**
* Adds a statement to create the postgis extension, if it doesn't already exist
*
* @return string
*/
public function compileEnablePostgisIfNotExists()
{
return 'CREATE EXTENSION IF NOT EXISTS postgis';
}

/**
* Adds a statement to drop the postgis extension
*
Expand All @@ -143,6 +153,16 @@ public function compileDisablePostgis()
return 'DROP EXTENSION postgis';
}

/**
* Adds a statement to drop the postgis extension, if it exists
*
* @return string
*/
public function compileDisablePostgisIfExists()
{
return 'DROP EXTENSION IF EXISTS postgis';
}

/**
* Adds a statement to add a geometry column
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Schema/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ public function testEnablePostgis()
$this->blueprint->enablePostgis();
}

public function testEnablePostgisIfNotExists()
{
$this->blueprint
->shouldReceive('addCommand')
->with('enablePostgis', []);

$this->blueprint->enablePostgisIfNotExists();
}

public function testDisablePostgis()
{
$this->blueprint
Expand All @@ -98,4 +107,13 @@ public function testDisablePostgis()

$this->blueprint->disablePostgis();
}

public function testDisablePostgisIfExists()
{
$this->blueprint
->shouldReceive('addCommand')
->with('disablePostgis', []);

$this->blueprint->disablePostgisIfExists();
}
}
20 changes: 20 additions & 0 deletions tests/Schema/Grammars/PostgisGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,16 @@ public function testEnablePostgis()
$this->assertStringContainsString('CREATE EXTENSION postgis', $statements[0]);
}

public function testEnablePostgisIfNotExists()
{
$blueprint = new Blueprint('test');
$blueprint->enablePostgisIfNotExists();
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);;
$this->assertStringContainsString('CREATE EXTENSION IF NOT EXISTS postgis', $statements[0]);
}

public function testDisablePostgis()
{
$blueprint = new Blueprint('test');
Expand All @@ -284,6 +294,16 @@ public function testDisablePostgis()
$this->assertStringContainsString('DROP EXTENSION postgis', $statements[0]);
}

public function testDisablePostgisIfExists()
{
$blueprint = new Blueprint('test');
$blueprint->disablePostgisIfExists();
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);;
$this->assertStringContainsString('DROP EXTENSION IF EXISTS postgis', $statements[0]);
}

/**
* @return Connection
*/
Expand Down

0 comments on commit 9c477ab

Please sign in to comment.