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 #157 from studnitz/add-publishable-migrations
Browse files Browse the repository at this point in the history
Add publishable migration
  • Loading branch information
mstaack authored Jan 12, 2022
2 parents 9c477ab + 5be106f commit 1726acd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
40 changes: 8 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,45 +54,21 @@ To start, ensure you have PostGIS enabled in your database - you can do this in

### Enable PostGIS via a Laravel migration

Create a new migration file by running
You need to publish the migration to easily enable PostGIS:

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;
```sh
php artisan vendor:publish --provider="MStaack\LaravelPostgis\DatabaseServiceProvider" --tag="migrations"
```

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

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

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.
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

Expand Down
27 changes: 27 additions & 0 deletions database/migrations/enable_postgis.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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();
}
}
7 changes: 7 additions & 0 deletions src/DatabaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public function boot()
// Load the config
$config_path = __DIR__ . '/../config/postgis.php';
$this->publishes([$config_path => config_path('postgis.php')], 'postgis');

if (!class_exists('EnablePostgis')) {
$this->publishes([
__DIR__ . '/../database/migrations/enable_postgis.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_enable_postgis.php'),
], 'migrations');
}

$this->mergeConfigFrom($config_path, 'postgis');
}

Expand Down

0 comments on commit 1726acd

Please sign in to comment.