Added migration file sample (#55)

* Added migration file sample

* place id column first

* Mention provider

Co-authored-by: Samuel Stancl <samuel.stancl@gmail.com>
This commit is contained in:
Kakajan SH 2020-06-22 19:38:45 +05:00 committed by GitHub
parent 2f043afe35
commit 9949f888e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -41,38 +41,70 @@ That said, automatic tenancy will still work the same way.
```
- Add an `id` autoincrement column to `domains` table and retroactively generate the ids
(Note: You may want to make this part of a migration)
- Create migration file with `php artisan make:migration upgrade_tenancy_package`
```php
use Illuminate\Support\Facades\Schema;
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
$domains = DB::table('domains')->get();
Schema::table('domains', function (Blueprint $table) {
$table->unsignedBigInteger('id')->nullable();
});
$counter = 1;
foreach ($domains as $domain) {
DB::table('domains')
->where('domain', $domain->domain)
->update(['id' => $counter]);
$counter += 1;
}
class UpgradeTenancyPackage extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('domains', function (Blueprint $table) {
$table->dropPrimary();
});
Schema::table('domains', function (Blueprint $table) {
$table->primary('id'); // todo: here we want to make it autoincrement, not just primary
$table->increments('id')->first();
$table->unique('domain');
$table->timestamps();
});
Schema::table('tenants', function (Blueprint $table) {
$table->json('data')->nullable()->change();
$table->timestamps();
});
DB::table('domains')->update([
'created_at' => now(),
'updated_at' => now()
]);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('domains', function (Blueprint $table) {
$table->dropColumn('id');
$table->dropUnique(['domain']);
$table->dropTimestamps();
$table->primary('domain');
});
Schema::table('tenants', function (Blueprint $table) {
$table->json('data')->nullable(false)->change();
$table->dropTimestamps();
});
}
}
```
- Replace your Http Kernel with a stock version (copy it from laravel/laravel: [https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php](https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php)) and add back in any changes you made. The package now doesn't necessitate any Kernel changes, so remove all of the 2.x ones.
- Delete config, publish it & the new files using `php artisan tenancy:install`
- Delete config, publish it & the new files using `php artisan tenancy:install`. Register the new provider (see instructions on the [Installation]({{ $page->link('installation') }}) page)
- Create Tenant model, as instructed on the [Tenants]({{ $page->link('tenants') }}) page
- Update routes to use the correct middleware, see the [Routes]({{ $page->link('routes') }}) page