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

Commit 1726acd

Browse files
authored
Merge pull request #157 from studnitz/add-publishable-migrations
Add publishable migration
2 parents 9c477ab + 5be106f commit 1726acd

File tree

3 files changed

+42
-32
lines changed

3 files changed

+42
-32
lines changed

README.md

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,45 +54,21 @@ To start, ensure you have PostGIS enabled in your database - you can do this in
5454

5555
### Enable PostGIS via a Laravel migration
5656

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

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

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-
}
63+
And then you run the migrations:
8064

81-
/**
82-
* Reverse the migrations.
83-
*
84-
* @return void
85-
*/
86-
public function down()
87-
{
88-
Schema::disablePostgisIfExists();
89-
}
90-
}
65+
```sh
66+
php artisan migrate
9167
```
9268

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

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.
71+
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.
9672

9773
### Enable PostGIS manually
9874

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Support\Facades\Schema;
5+
6+
class EnablePostgis extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::enablePostgisIfNotExists();
16+
}
17+
18+
/**
19+
* Reverse the migrations.
20+
*
21+
* @return void
22+
*/
23+
public function down()
24+
{
25+
Schema::disablePostgisIfExists();
26+
}
27+
}

src/DatabaseServiceProvider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ public function boot()
1313
// Load the config
1414
$config_path = __DIR__ . '/../config/postgis.php';
1515
$this->publishes([$config_path => config_path('postgis.php')], 'postgis');
16+
17+
if (!class_exists('EnablePostgis')) {
18+
$this->publishes([
19+
__DIR__ . '/../database/migrations/enable_postgis.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_enable_postgis.php'),
20+
], 'migrations');
21+
}
22+
1623
$this->mergeConfigFrom($config_path, 'postgis');
1724
}
1825

0 commit comments

Comments
 (0)