Skip to content

Commit a6d328c

Browse files
committed
formatting
2 parents abf1e2c + 23547e3 commit a6d328c

11 files changed

+203
-4
lines changed

src/Illuminate/Database/Console/Migrations/FreshCommand.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ public function handle()
3535
return;
3636
}
3737

38-
$this->dropAllTables(
39-
$database = $this->input->getOption('database')
40-
);
38+
$database = $this->input->getOption('database');
39+
40+
if ($this->option('drop-views')) {
41+
$this->dropAllViews($database);
42+
43+
$this->info('Dropped all views successfully.');
44+
}
45+
46+
$this->dropAllTables($database);
4147

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

@@ -65,6 +71,19 @@ protected function dropAllTables($database)
6571
->dropAllTables();
6672
}
6773

74+
/**
75+
* Drop all of the database views.
76+
*
77+
* @param string $database
78+
* @return void
79+
*/
80+
protected function dropAllViews($database)
81+
{
82+
$this->laravel['db']->connection($database)
83+
->getSchemaBuilder()
84+
->dropAllViews();
85+
}
86+
6887
/**
6988
* Determine if the developer has requested database seeding.
7089
*
@@ -100,6 +119,8 @@ protected function getOptions()
100119
return [
101120
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
102121

122+
['drop-views', null, InputOption::VALUE_NONE, 'Drop all tables and views.'],
123+
103124
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
104125

105126
['path', null, InputOption::VALUE_OPTIONAL, 'The path to the migrations files to be executed.'],

src/Illuminate/Database/Schema/Builder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,18 @@ public function dropAllTables()
203203
throw new LogicException('This database driver does not support dropping all tables.');
204204
}
205205

206+
/**
207+
* Drop all views from the database.
208+
*
209+
* @return void
210+
*
211+
* @throws \LogicException
212+
*/
213+
public function dropAllViews()
214+
{
215+
throw new LogicException('This database driver does not support dropping all views.');
216+
}
217+
206218
/**
207219
* Rename a table on the schema.
208220
*

src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,17 @@ public function compileDropAllTables($tables)
369369
return 'drop table '.implode(',', $this->wrapArray($tables));
370370
}
371371

372+
/**
373+
* Compile the SQL needed to drop all views.
374+
*
375+
* @param array $views
376+
* @return string
377+
*/
378+
public function compileDropAllViews($views)
379+
{
380+
return 'drop view '.implode(',', $this->wrapArray($views));
381+
}
382+
372383
/**
373384
* Compile the SQL needed to retrieve all table names.
374385
*
@@ -379,6 +390,16 @@ public function compileGetAllTables()
379390
return 'SHOW FULL TABLES WHERE table_type = \'BASE TABLE\'';
380391
}
381392

393+
/**
394+
* Compile the SQL needed to retrieve all view names.
395+
*
396+
* @return string
397+
*/
398+
public function compileGetAllViews()
399+
{
400+
return 'SHOW FULL TABLES WHERE table_type = \'VIEW\'';
401+
}
402+
382403
/**
383404
* Compile the command to enable foreign key constraints.
384405
*

src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@ public function compileDropAllTables($tables)
205205
return 'drop table "'.implode('","', $tables).'" cascade';
206206
}
207207

208+
/**
209+
* Compile the SQL needed to drop all views.
210+
*
211+
* @param string $views
212+
* @return string
213+
*/
214+
public function compileDropAllViews($views)
215+
{
216+
return 'drop view "'.implode('","', $views).'" cascade';
217+
}
218+
208219
/**
209220
* Compile the SQL needed to retrieve all table names.
210221
*
@@ -216,6 +227,17 @@ public function compileGetAllTables($schema)
216227
return "select tablename from pg_catalog.pg_tables where schemaname = '{$schema}'";
217228
}
218229

230+
/**
231+
* Compile the SQL needed to retrieve all view names.
232+
*
233+
* @param string $schema
234+
* @return string
235+
*/
236+
public function compileGetAllViews($schema)
237+
{
238+
return "select viewname from pg_catalog.pg_views where schemaname = '{$schema}'";
239+
}
240+
219241
/**
220242
* Compile a drop column command.
221243
*

src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,16 @@ public function compileDropAllTables()
231231
return "delete from sqlite_master where type in ('table', 'index', 'trigger')";
232232
}
233233

234+
/**
235+
* Compile the SQL needed to drop all views.
236+
*
237+
* @return string
238+
*/
239+
public function compileDropAllViews()
240+
{
241+
return "delete from sqlite_master where type in ('view')";
242+
}
243+
234244
/**
235245
* Compile a drop column command.
236246
*

src/Illuminate/Database/Schema/MySqlBuilder.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,30 @@ public function dropAllTables()
6464
$this->enableForeignKeyConstraints();
6565
}
6666

67+
/**
68+
* Drop all views from the database.
69+
*
70+
* @return void
71+
*/
72+
public function dropAllViews()
73+
{
74+
$views = [];
75+
76+
foreach ($this->getAllViews() as $row) {
77+
$row = (array) $row;
78+
79+
$views[] = reset($row);
80+
}
81+
82+
if (empty($views)) {
83+
return;
84+
}
85+
86+
$this->connection->statement(
87+
$this->grammar->compileDropAllViews($views)
88+
);
89+
}
90+
6791
/**
6892
* Get all of the table names for the database.
6993
*
@@ -75,4 +99,16 @@ protected function getAllTables()
7599
$this->grammar->compileGetAllTables()
76100
);
77101
}
102+
103+
/**
104+
* Get all of the view names for the database.
105+
*
106+
* @return array
107+
*/
108+
protected function getAllViews()
109+
{
110+
return $this->connection->select(
111+
$this->grammar->compileGetAllViews()
112+
);
113+
}
78114
}

src/Illuminate/Database/Schema/PostgresBuilder.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,30 @@ public function dropAllTables()
5151
);
5252
}
5353

54+
/**
55+
* Drop all views from the database.
56+
*
57+
* @return void
58+
*/
59+
public function dropAllViews()
60+
{
61+
$views = [];
62+
63+
foreach ($this->getAllViews() as $row) {
64+
$row = (array) $row;
65+
66+
$views[] = reset($row);
67+
}
68+
69+
if (empty($views)) {
70+
return;
71+
}
72+
73+
$this->connection->statement(
74+
$this->grammar->compileDropAllViews($views)
75+
);
76+
}
77+
5478
/**
5579
* Get all of the table names for the database.
5680
*
@@ -63,6 +87,18 @@ protected function getAllTables()
6387
);
6488
}
6589

90+
/**
91+
* Get all of the view names for the database.
92+
*
93+
* @return array
94+
*/
95+
protected function getAllViews()
96+
{
97+
return $this->connection->select(
98+
$this->grammar->compileGetAllViews($this->connection->getConfig('schema'))
99+
);
100+
}
101+
66102
/**
67103
* Get the column listing for a given table.
68104
*

src/Illuminate/Database/Schema/SQLiteBuilder.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ public function dropAllTables()
2222
$this->connection->select($this->grammar->compileDisableWriteableSchema());
2323
}
2424

25+
/**
26+
* Drop all views from the database.
27+
*
28+
* @return void
29+
*/
30+
public function dropAllViews()
31+
{
32+
$this->connection->select($this->grammar->compileEnableWriteableSchema());
33+
34+
$this->connection->select($this->grammar->compileDropAllViews());
35+
36+
$this->connection->select($this->grammar->compileDisableWriteableSchema());
37+
}
38+
2539
/**
2640
* Empty the database file.
2741
*

src/Illuminate/Foundation/Testing/RefreshDatabase.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ protected function refreshInMemoryDatabase()
5050
protected function refreshTestDatabase()
5151
{
5252
if (! RefreshDatabaseState::$migrated) {
53-
$this->artisan('migrate:fresh');
53+
$this->artisan('migrate:fresh', $this->shouldDropViews() ? [
54+
'--drop-views' => true,
55+
] : []);
5456

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

@@ -101,4 +103,15 @@ protected function connectionsToTransact()
101103
return property_exists($this, 'connectionsToTransact')
102104
? $this->connectionsToTransact : [null];
103105
}
106+
107+
/**
108+
* Determine if views should be dropped when refreshing the database.
109+
*
110+
* @return bool
111+
*/
112+
protected function shouldDropViews()
113+
{
114+
return property_exists($this, 'dropViews')
115+
? $this->dropViews : false;
116+
}
104117
}

tests/Database/DatabaseMySqlSchemaGrammarTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,13 @@ public function testDropAllTables()
941941
$this->assertEquals('drop table `alpha`,`beta`,`gamma`', $statement);
942942
}
943943

944+
public function testDropAllViews()
945+
{
946+
$statement = $this->getGrammar()->compileDropAllViews(['alpha', 'beta', 'gamma']);
947+
948+
$this->assertEquals('drop view `alpha`,`beta`,`gamma`', $statement);
949+
}
950+
944951
public function testGrammarsAreMacroable()
945952
{
946953
// compileReplace macro.

tests/Database/DatabasePostgresSchemaGrammarTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,13 @@ public function testDropAllTablesEscapesTableNames()
790790
$this->assertEquals('drop table "alpha","beta","gamma" cascade', $statement);
791791
}
792792

793+
public function testDropAllViewsEscapesTableNames()
794+
{
795+
$statement = $this->getGrammar()->compileDropAllViews(['alpha', 'beta', 'gamma']);
796+
797+
$this->assertEquals('drop view "alpha","beta","gamma" cascade', $statement);
798+
}
799+
793800
protected function getConnection()
794801
{
795802
return m::mock('Illuminate\Database\Connection');

0 commit comments

Comments
 (0)