Skip to content

Commit

Permalink
[9.x] Add getAllTables support for SQLite and SQLServer schema builde…
Browse files Browse the repository at this point in the history
…rs (#41896)

* add sqlite and sqlserver table column getters

* formatting

Co-authored-by: Jonathan Havens <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored Apr 12, 2022
1 parent fab6107 commit 827893f
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,26 @@ public function compileDropAllViews()
return "delete from sqlite_master where type in ('view')";
}

/**
* Compile the SQL needed to retrieve all table names.
*
* @return string
*/
public function compileGetAllTables()
{
return 'select type, name from sqlite_master where type = \'table\' and name not like \'sqlite_%\'';
}

/**
* Compile the SQL needed to retrieve all view names.
*
* @return string
*/
public function compileGetAllViews()
{
return 'select type, name from sqlite_master where type = \'view\'';
}

/**
* Compile the SQL needed to rebuild the database.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,27 @@ public function compileDropAllViews()
EXEC sp_executesql @sql;";
}

/**
* Compile the SQL needed to retrieve all table names.
*
* @return string
*/
public function compileGetAllTables()
{
return "select name, type from sys.tables where type = 'U'";
}

/**
* Compile the SQL needed to retrieve all view names.
*
* @return string
*/
public function compileGetAllViews()
{
return "select name, type from sys.objects where type = 'V'";
}


/**
* Create the column definition for a char type.
*
Expand Down
24 changes: 24 additions & 0 deletions src/Illuminate/Database/Schema/SQLiteBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ public function dropAllViews()
$this->connection->select($this->grammar->compileRebuild());
}

/**
* Get all of the table names for the database.
*
* @return array
*/
public function getAllTables()
{
return $this->connection->select(
$this->grammar->compileGetAllTables()
);
}

/**
* Get all of the view names for the database.
*
* @return array
*/
public function getAllViews()
{
return $this->connection->select(
$this->grammar->compileGetAllViews()
);
}

/**
* Empty the database file.
*
Expand Down
26 changes: 25 additions & 1 deletion src/Illuminate/Database/Schema/SqlServerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function dropDatabaseIfExists($name)
);
}

/**
/**
* Drop all tables from the database.
*
* @return void
Expand All @@ -51,4 +51,28 @@ public function dropAllViews()
{
$this->connection->statement($this->grammar->compileDropAllViews());
}

/**
* Drop all tables from the database.
*
* @return array
*/
public function getAllTables()
{
return $this->connection->select(
$this->grammar->compileGetAllTables()
);
}

/**
* Get all of the view names for the database.
*
* @return array
*/
public function getAllViews()
{
return $this->connection->select(
$this->grammar->compileGetAllViews()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Illuminate\Tests\Integration\Database\SqlServer;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use stdClass;

class DatabaseSqlServerSchemaBuilderTest extends SqlServerTestCase
{
protected function getEnvironmentSetUp($app)
{
if (getenv('DB_CONNECTION') !== 'sqlsrv') {
$this->markTestSkipped('Test requires a SQLServer connection.');
}

$this->driver = 'sqlsrv';
}

protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
{
Schema::create('users', function (Blueprint $table) {
$table->integer('id');
$table->string('name');
$table->string('age');
$table->enum('color', ['red', 'blue']);
});
}

protected function destroyDatabaseMigrations()
{
Schema::drop('users');
}

public function testGetAllTablesAndColumnListing()
{
$tables = Schema::getAllTables();

$this->assertCount(2, $tables);
$tableProperties = array_values((array) $tables[0]);
$this->assertEquals(['migrations', 'U '], $tableProperties);

$this->assertInstanceOf(stdClass::class, $tables[1]);

$tableProperties = array_values((array) $tables[1]);
$this->assertEquals(['users', 'U '], $tableProperties);

$columns = Schema::getColumnListing('users');

foreach (['id', 'name', 'age', 'color'] as $column) {
$this->assertContains($column, $columns);
}

Schema::create('posts', function (Blueprint $table) {
$table->integer('id');
$table->string('title');
});
$tables = Schema::getAllTables();
$this->assertCount(3, $tables);
Schema::drop('posts');
}

public function testGetAllViews()
{
DB::connection('sqlsrv')->statement(<<<SQL
CREATE VIEW users_view
AS
SELECT name,age from users;
SQL);

$tableView = Schema::getAllViews();

$this->assertCount(1, $tableView);
$this->assertInstanceOf(stdClass::class, $obj = array_values($tableView)[0]);
$this->assertEquals('users_view', $obj->name);
$this->assertEquals('V ', $obj->type);

DB::connection('sqlsrv')->statement(<<<SQL
DROP VIEW IF EXISTS users_view;
SQL);

$this->assertEmpty(Schema::getAllViews());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Illuminate\Tests\Integration\Database\Sqlite;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;
use stdClass;

class DatabaseSqliteSchemaBuilderTest extends DatabaseTestCase
{
protected function getEnvironmentSetUp($app)
{
if (getenv('DB_CONNECTION') !== 'testing') {
$this->markTestSkipped('Test requires a Sqlite connection.');
}

$app['config']->set('database.default', 'conn1');

$app['config']->set('database.connections.conn1', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}

protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
{
Schema::create('users', function (Blueprint $table) {
$table->integer('id');
$table->string('name');
$table->string('age');
$table->enum('color', ['red', 'blue']);
});
}

protected function destroyDatabaseMigrations()
{
Schema::drop('users');
}

public function testGetAllTablesAndColumnListing()
{
$tables = Schema::getAllTables();

$this->assertCount(2, $tables);
$tableProperties = array_values((array) $tables[0]);
$this->assertEquals(['table', 'migrations'], $tableProperties);

$this->assertInstanceOf(stdClass::class, $tables[1]);

$tableProperties = array_values((array) $tables[1]);
$this->assertEquals(['table', 'users'], $tableProperties);

$columns = Schema::getColumnListing('users');

foreach (['id', 'name', 'age', 'color'] as $column) {
$this->assertContains($column, $columns);
}

Schema::create('posts', function (Blueprint $table) {
$table->integer('id');
$table->string('title');
});
$tables = Schema::getAllTables();
$this->assertCount(3, $tables);
Schema::drop('posts');
}

public function testGetAllViews()
{
DB::connection('conn1')->statement(<<<SQL
CREATE VIEW users_view
AS
SELECT name,age from users;
SQL);

$tableView = Schema::getAllViews();

$this->assertCount(1, $tableView);
$this->assertInstanceOf(stdClass::class, $obj = array_values($tableView)[0]);
$this->assertEquals('users_view', $obj->name);
$this->assertEquals('view', $obj->type);

DB::connection('conn1')->statement(<<<SQL
DROP VIEW IF EXISTS users_view;
SQL);

$this->assertEmpty(Schema::getAllViews());
}
}

0 comments on commit 827893f

Please sign in to comment.