Skip to content

Commit

Permalink
refactored migrations to a single file to keep table creation in the …
Browse files Browse the repository at this point in the history
…proper order as well as migrations where this library is used
  • Loading branch information
Jef committed Mar 15, 2020
1 parent 8f52e5b commit 1411091
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 166 deletions.
21 changes: 13 additions & 8 deletions docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,9 @@ Composer
Service Provider
----------------

Add ``Czechbox\LaravelPlans\LaravelPlansServiceProvider::class`` to your application service providers file: ``config/app.php``.
This used to be manually added, however, given this package is not targeted at older versions, that's no longer the cas. We use autodiscovery specified in the package ``composer.json`` file.

.. code-block:: php

'providers' => [
/**
* Third Party Service Providers...
*/
Czechbox\LaravelPlans\LaravelPlansServiceProvider::class,
]

Config File and Migrations
--------------------------
Expand All @@ -33,6 +26,18 @@ Publish package config file and migrations with the following command:
php artisan vendor:publish --provider="Czechbox\LaravelPlans\LaravelPlansServiceProvider"
Depending on your use case, you may want to adjust the published migrations. If your User model id is a 'uuid' rather than 'increments', modify the ``XXXX_XX-XX_XXXXXX_create_plan_subscriptions_table.php`` file as below.

.. code-block:: php
Schema::create('plan_subscriptions', function (Blueprint $table) {
$table->increments('id');
$table->uuidMorphs('suscribable');
Then run migrations:

.. code-block:: bash
Expand Down
6 changes: 2 additions & 4 deletions src/LaravelPlans/LaravelPlansServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ public function boot()
$timestamp = date('Y_m_d_His', time());
$this->publishes([
// using templates to publish the migrations
__DIR__ . '/../database/migrations/_create_plan_features_table.phpt' => database_path("/migrations/{$timestamp}_create_plan_features_table.php"),
__DIR__ . '/../database/migrations/_create_plan_subscription_usages_table.phpt' => database_path("/migrations/{$timestamp}_create_plan_subscription_usages_table.php"),
__DIR__ . '/../database/migrations/_create_plan_subscriptions_table.phpt' => database_path("/migrations/{$timestamp}_create_plan_subscriptions_table.php"),
__DIR__ . '/../database/migrations/_create_plans_table.phpt' => database_path("/migrations/{$timestamp}_create_plans_table.php"),
__DIR__ . '/../database/migrations/_create_laravelplans_tables.phpt' => database_path("/migrations/{$timestamp}_create_laravelplans_tables.phpt"),


], 'migrations');

Expand Down
90 changes: 90 additions & 0 deletions src/database/migrations/_create_laravelplans_tables.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

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

class CreatePlansTables extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// plans table
Schema::create('plans', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('description')->nullable();
$table->decimal('price', 7, 2)->default('0.00');
$table->string('interval')->default('month');
$table->smallInteger('interval_count')->default(1);
$table->smallInteger('trial_period_days')->nullable();
$table->smallInteger('sort_order')->nullable();
$table->timestamps();
});

// plan features table
Schema::create('plan_features', function (Blueprint $table) {
$table->increments('id');
$table->integer('plan_id')->unsigned();
$table->string('code');
$table->string('value');
$table->smallInteger('sort_order')->nullable();
$table->timestamps();

$table->unique(['plan_id', 'code']);
$table->foreign('plan_id')->references('id')->on('plans')->onDelete('cascade');
});

// plan subscriptions table
Schema::create('plan_subscriptions', function (Blueprint $table) {
$table->increments('id');

$table->morphs('subscribable'); //this accomplishes what the following commented out lines did previously.
// If your User model id is a 'uuid' rather than 'increments', change to '$table->uuidMorphs('suscribable');'
//
// $table->integer('subscribable_id')->unsigned()->index();
// $table->string('subscribable_type')->index();

$table->integer('plan_id')->unsigned();
$table->string('name');
$table->boolean('canceled_immediately')->nullable();
$table->timestamp('trial_ends_at')->nullable();
$table->timestamp('starts_at')->nullable();
$table->timestamp('ends_at')->nullable();
$table->timestamp('canceled_at')->nullable();
$table->timestamps();

$table->foreign('plan_id')->references('id')->on('plans')->onDelete('cascade');
});

// plan subscriptions usage table
Schema::create('plan_subscription_usages', function (Blueprint $table) {
$table->increments('id');
$table->integer('subscription_id')->unsigned();
$table->string('code');
$table->smallInteger('used')->unsigned();
$table->timestamp('valid_until')->nullable();
$table->timestamps();

$table->unique(['subscription_id', 'code']);
$table->foreign('subscription_id')->references('id')->on('plan_subscriptions')->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('plan_subscription_usages');
Schema::dropIfExists('plan_subscriptions');
Schema::dropIfExists('plan_features');
Schema::dropIfExists('plans');
}
}
37 changes: 0 additions & 37 deletions src/database/migrations/_create_plan_features_table.phpt

This file was deleted.

This file was deleted.

43 changes: 0 additions & 43 deletions src/database/migrations/_create_plan_subscriptions_table.phpt

This file was deleted.

37 changes: 0 additions & 37 deletions src/database/migrations/_create_plans_table.phpt

This file was deleted.

0 comments on commit 1411091

Please sign in to comment.