Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop Table #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,42 @@ php artisan make:migration:schema create_dogs_table --schema="name:string"

You'll get a migration, populated with the schema...but you'll also get an Eloquent model at `app/Dog.php`. Naturally, you can opt out of this by adding the `--model=false` flag/option.

If you wish to drop a table from the DB you can use the "drop" keyword.
```php
<?php

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

class DropOnionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::drop('onions');
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::create('onions', function (Blueprint $table) {
$table->increments('id');
$table->string('origin');
});
}
}
```
Don't forget to submit your columns as well for the down method!
Also pay attention that if you try to drop a table which has a foreign key attached, your migration will fail at runtime!

#### Foreign Constraints

There's also a secret bit of sugar for when you need to generate foreign constraints. Imagine that you have a posts table, where each post belongs to a user. Let's try:
Expand Down
3 changes: 2 additions & 1 deletion src/Migrations/NameParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ private function normalizeActionName($action)
return 'create';
case 'delete':
case 'destroy':
case 'drop':
return 'remove';
case 'drop':
return 'drop';
case 'add':
case 'append':
case 'update':
Expand Down
11 changes: 11 additions & 0 deletions src/Migrations/SyntaxBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ private function createSchemaForUpMethod($schema, $meta)
return $this->insert($fields)->into($this->getChangeSchemaWrapper());
}

if ($meta['action'] == 'drop') {
return sprintf("Schema::drop('%s');", $meta['table']);
}

// Otherwise, we have no idea how to proceed.
throw new GeneratorException;
}
Expand Down Expand Up @@ -90,6 +94,13 @@ private function createSchemaForDownMethod($schema, $meta)
return $this->insert($fields)->into($this->getChangeSchemaWrapper());
}

// If the user removed a table, then for
// the down method, we should add it back in.
if ($meta['action'] == 'drop') {
$fields = $this->constructSchema($schema);
return $this->insert($fields)->into($this->getCreateSchemaWrapper());
}

// Otherwise, we have no idea how to proceed.
throw new GeneratorException;
}
Expand Down