Skip to content
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
21 changes: 21 additions & 0 deletions src/Illuminate/Database/Console/Migrations/FreshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public function handle()

$this->info('Dropped all tables successfully.');

if ($this->option('drop-types')) {
$this->dropAllTypes($database);

$this->info('Dropped all types successfully.');
}

$this->call('migrate', array_filter([
'--database' => $database,
'--path' => $this->input->getOption('path'),
Expand Down Expand Up @@ -86,6 +92,19 @@ protected function dropAllViews($database)
->dropAllViews();
}

/**
* Drop all of the database types.
*
* @param string $database
* @return void
*/
protected function dropAllTypes($database)
{
$this->laravel['db']->connection($database)
->getSchemaBuilder()
->dropAllTypes();
}

/**
* Determine if the developer has requested database seeding.
*
Expand Down Expand Up @@ -123,6 +142,8 @@ protected function getOptions()

['drop-views', null, InputOption::VALUE_NONE, 'Drop all tables and views'],

['drop-types', null, InputOption::VALUE_NONE, 'Drop all tables and types (Postgres only)'],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Feedback here may be to change this to PostgreSQL


['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],

['path', null, InputOption::VALUE_OPTIONAL, 'The path to the migrations files to be executed'],
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ public function dropAllViews()
throw new LogicException('This database driver does not support dropping all views.');
}

/**
* Drop all types from the database.
*
* @return void
*
* @throws \LogicException
*/
public function dropAllTypes()
{
throw new LogicException('This database driver does not support dropping all types');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other exceptions in this class end in a period

}

/**
* Rename a table on the schema.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ public function compileDropAllViews($views)
return 'drop view "'.implode('","', $views).'" cascade';
}

/**
* Compile the SQL needed to drop all types.
*
* @param array $types
* @return string
*/
public function compileDropAllTypes($types)
{
return 'drop type "'.implode('","', $types).'" cascade';
}

/**
* Compile the SQL needed to retrieve all table names.
*
Expand All @@ -241,6 +252,16 @@ public function compileGetAllViews($schema)
return "select viewname from pg_catalog.pg_views where schemaname = '{$schema}'";
}

/**
* Compile the SQL needed to retrieve all type names.
*
* @return string
*/
public function compileGetAllTypes()
{
return "select distinct pg_type.typname from pg_type inner join pg_enum on pg_enum.enumtypid = pg_type.oid";
}

/**
* Compile a drop column command.
*
Expand Down
34 changes: 34 additions & 0 deletions src/Illuminate/Database/Schema/PostgresBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ public function dropAllViews()
);
}

/**
* Drop all types from the database.
*/
public function dropAllTypes()
{
$types = [];

foreach ($this->getAllTypes() as $row) {
$row = (array) $row;

$types[] = reset($row);
}

if (empty($types)) {
return;
}

$this->connection->statement(
$this->grammar->compileDropAllTypes($types)
);
}

/**
* Get all of the table names for the database.
*
Expand All @@ -99,6 +121,18 @@ protected function getAllViews()
);
}

/**
* Get all of the type names for the database.
*
* @return array
*/
protected function getAllTypes()
{
return $this->connection->select(
$this->grammar->compileGetAllTypes()
);
}

/**
* Get the column listing for a given table.
*
Expand Down
18 changes: 15 additions & 3 deletions src/Illuminate/Foundation/Testing/RefreshDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ protected function refreshInMemoryDatabase()
protected function refreshTestDatabase()
{
if (! RefreshDatabaseState::$migrated) {
$this->artisan('migrate:fresh', $this->shouldDropViews() ? [
'--drop-views' => true,
] : []);
$this->artisan('migrate:fresh', [
'--drop-views' => $this->shouldDropViews(),
'--drop-types' => $this->shouldDropTypes(),
]);

$this->app[Kernel::class]->setArtisan(null);

Expand Down Expand Up @@ -114,4 +115,15 @@ protected function shouldDropViews()
return property_exists($this, 'dropViews')
? $this->dropViews : false;
}

/**
* Determine if types should be dropped when refreshing the database.
*
* @return bool
*/
protected function shouldDropTypes()
{
return property_exists($this, 'dropTypes')
? $this->dropTypes : false;
}
}
7 changes: 7 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,13 @@ public function testDropAllViewsEscapesTableNames()
$this->assertEquals('drop view "alpha","beta","gamma" cascade', $statement);
}

public function testDropAllTypesEscapesTableNames()
{
$statement = $this->getGrammar()->compileDropAllTypes(['alpha', 'beta', 'gamma']);

$this->assertEquals('drop type "alpha","beta","gamma" cascade', $statement);
}

protected function getConnection()
{
return m::mock(Connection::class);
Expand Down