From 13e4c9ce251e773048e3e86fc9cafdb04b2fa11e Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sat, 30 Mar 2024 18:29:27 +0330 Subject: [PATCH 01/22] get default schema name on sqlsrv --- .../Database/Schema/Grammars/SqlServerGrammar.php | 10 ++++++++++ .../Database/Schema/SqlServerBuilder.php | 14 +++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php b/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php index a661094b2e84..631e57876f84 100755 --- a/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php @@ -66,6 +66,16 @@ public function compileDropDatabaseIfExists($name) ); } + /** + * Compile the query to determine the name of the default schema of the caller. + * + * @return string + */ + public function compileDefaultSchema() + { + return 'select schema_name()'; + } + /** * Compile the query to determine the tables. * diff --git a/src/Illuminate/Database/Schema/SqlServerBuilder.php b/src/Illuminate/Database/Schema/SqlServerBuilder.php index 876e8b39be46..2833b5098aba 100644 --- a/src/Illuminate/Database/Schema/SqlServerBuilder.php +++ b/src/Illuminate/Database/Schema/SqlServerBuilder.php @@ -42,6 +42,7 @@ public function hasTable($table) { [$schema, $table] = $this->parseSchemaAndTable($table); + $schema ??= $this->getDefaultSchema(); $table = $this->connection->getTablePrefix().$table; foreach ($this->getTables() as $value) { @@ -64,6 +65,7 @@ public function hasView($view) { [$schema, $view] = $this->parseSchemaAndTable($view); + $schema ??= $this->getDefaultSchema(); $view = $this->connection->getTablePrefix().$view; foreach ($this->getViews() as $value) { @@ -151,6 +153,16 @@ public function getForeignKeys($table) ); } + /** + * Get the default schema for the connection. + * + * @return string + */ + protected function getDefaultSchema() + { + return $this->connection->scalar($this->grammar->compileDefaultSchema()); + } + /** * Parse the database object reference and extract the schema and table. * @@ -159,7 +171,7 @@ public function getForeignKeys($table) */ protected function parseSchemaAndTable($reference) { - $parts = array_pad(explode('.', $reference, 2), -2, 'dbo'); + $parts = array_pad(explode('.', $reference, 2), -2, null); if (str_contains($parts[1], '.')) { $database = $parts[0]; From f17ab18118f23c7412555f37f07cbf8a7dec170a Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sat, 30 Mar 2024 18:41:13 +0330 Subject: [PATCH 02/22] force re-run tests From 6a919d336320d78e1133ca51e4bbf4f6eaf240d9 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sat, 30 Mar 2024 19:15:45 +0330 Subject: [PATCH 03/22] add test --- .../Database/SchemaBuilderSchemaNameTest.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index 2d6d9512ed87..1c8591af381d 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -425,6 +425,37 @@ public function testAutoIncrementStartingValue($connection) }); } + #[DataProvider('connectionProvider')] + public function testHasTable($connection) + { + if ($this->driver !== 'sqlsrv') { + $this->markTestSkipped('Test requires a SQL Server connection.'); + } + + $connection = DB::connection($connection); + $schema = $connection->getSchemaBuilder(); + + $this->assertEquals('dbo', $connection->scalar('select schema_name()')); + + var_dump($connection->scalar('select current_user')); + + $connection->statement('alter user SA with default_schema=my_schema'); + + $this->assertEquals('my_schema', $connection->scalar('select schema_name()')); + + $schema->create('table', function (Blueprint $table) { + $table->id(); + }); + + $this->assertTrue($schema->hasTable('table')); + $this->assertTrue($schema->hasTable('my_schema.table')); + $this->assertFalse($schema->hasTable('dbo.table')); + + $connection->statement('alter user SA with default_schema=dbo'); + + $this->assertEquals('dbo', $connection->scalar('select schema_name()')); + } + public static function connectionProvider(): array { return [ From 98f86ad4e18f00e7303670f97d7499c1f7698128 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sat, 30 Mar 2024 19:35:47 +0330 Subject: [PATCH 04/22] wip --- .../Database/SchemaBuilderSchemaNameTest.php | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index 1c8591af381d..219f497b7338 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -433,27 +433,8 @@ public function testHasTable($connection) } $connection = DB::connection($connection); - $schema = $connection->getSchemaBuilder(); - - $this->assertEquals('dbo', $connection->scalar('select schema_name()')); var_dump($connection->scalar('select current_user')); - - $connection->statement('alter user SA with default_schema=my_schema'); - - $this->assertEquals('my_schema', $connection->scalar('select schema_name()')); - - $schema->create('table', function (Blueprint $table) { - $table->id(); - }); - - $this->assertTrue($schema->hasTable('table')); - $this->assertTrue($schema->hasTable('my_schema.table')); - $this->assertFalse($schema->hasTable('dbo.table')); - - $connection->statement('alter user SA with default_schema=dbo'); - - $this->assertEquals('dbo', $connection->scalar('select schema_name()')); } public static function connectionProvider(): array From 66cbdae1c63bf2cf830f2df478ac80774192e346 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sat, 30 Mar 2024 19:37:49 +0330 Subject: [PATCH 05/22] wip --- .../Database/SchemaBuilderSchemaNameTest.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index 219f497b7338..4827e37230f0 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -433,8 +433,25 @@ public function testHasTable($connection) } $connection = DB::connection($connection); + $schema = $connection->getSchemaBuilder(); + + $this->assertEquals('dbo', $connection->scalar('select schema_name()')); + + $connection->statement('alter user dbo with default_schema=my_schema'); + + $this->assertEquals('my_schema', $connection->scalar('select schema_name()')); + + $schema->create('table', function (Blueprint $table) { + $table->id(); + }); + + $this->assertTrue($schema->hasTable('table')); + $this->assertTrue($schema->hasTable('my_schema.table')); + $this->assertFalse($schema->hasTable('dbo.table')); + + $connection->statement('alter user dbo with default_schema=dbo'); - var_dump($connection->scalar('select current_user')); + $this->assertEquals('dbo', $connection->scalar('select schema_name()')); } public static function connectionProvider(): array From dac08631a94a71ecadf8134bdc44511ec51dcfad Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sat, 30 Mar 2024 19:41:25 +0330 Subject: [PATCH 06/22] wip --- tests/Integration/Database/SchemaBuilderSchemaNameTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index 4827e37230f0..bb89dde203e5 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -435,6 +435,8 @@ public function testHasTable($connection) $connection = DB::connection($connection); $schema = $connection->getSchemaBuilder(); + var_dump($connection->select('SELECT * FROM sysusers')); + $this->assertEquals('dbo', $connection->scalar('select schema_name()')); $connection->statement('alter user dbo with default_schema=my_schema'); From 9ac7a180118bbce2da04a84b32095dd0303143f2 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sat, 30 Mar 2024 19:49:30 +0330 Subject: [PATCH 07/22] wip --- tests/Integration/Database/SchemaBuilderSchemaNameTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index bb89dde203e5..f3be426ebe65 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -435,7 +435,7 @@ public function testHasTable($connection) $connection = DB::connection($connection); $schema = $connection->getSchemaBuilder(); - var_dump($connection->select('SELECT * FROM sysusers')); + var_dump($connection->select('SELECT * FROM syslogins')); $this->assertEquals('dbo', $connection->scalar('select schema_name()')); From f6a68ac046b65698012e1b5d3fe444f11a92ba38 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sat, 30 Mar 2024 19:57:16 +0330 Subject: [PATCH 08/22] revert adding test --- .../Database/SchemaBuilderSchemaNameTest.php | 31 ------------------- 1 file changed, 31 deletions(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index f3be426ebe65..2d6d9512ed87 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -425,37 +425,6 @@ public function testAutoIncrementStartingValue($connection) }); } - #[DataProvider('connectionProvider')] - public function testHasTable($connection) - { - if ($this->driver !== 'sqlsrv') { - $this->markTestSkipped('Test requires a SQL Server connection.'); - } - - $connection = DB::connection($connection); - $schema = $connection->getSchemaBuilder(); - - var_dump($connection->select('SELECT * FROM syslogins')); - - $this->assertEquals('dbo', $connection->scalar('select schema_name()')); - - $connection->statement('alter user dbo with default_schema=my_schema'); - - $this->assertEquals('my_schema', $connection->scalar('select schema_name()')); - - $schema->create('table', function (Blueprint $table) { - $table->id(); - }); - - $this->assertTrue($schema->hasTable('table')); - $this->assertTrue($schema->hasTable('my_schema.table')); - $this->assertFalse($schema->hasTable('dbo.table')); - - $connection->statement('alter user dbo with default_schema=dbo'); - - $this->assertEquals('dbo', $connection->scalar('select schema_name()')); - } - public static function connectionProvider(): array { return [ From 350cdf90d8a0f77c1a9debd0fb54939f9210ceea Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sun, 31 Mar 2024 16:12:42 +0330 Subject: [PATCH 09/22] wip --- .../Database/SchemaBuilderSchemaNameTest.php | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index 2d6d9512ed87..4d1acbfd9c0a 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -425,6 +425,40 @@ public function testAutoIncrementStartingValue($connection) }); } + #[DataProvider('connectionProvider')] + public function testHasTable($connection) + { + if ($this->driver !== 'sqlsrv') { + $this->markTestSkipped('Test requires a SQL Server connection.'); + } + $connection = DB::connection($connection); + $schema = $connection->getSchemaBuilder(); + + $connection->statement("create login [sample] with password = 'sample'"); + $connection->statement("create user [sample] for login [sample]"); + $connection->statement("alter user [sample] with default_schema = my_schema"); + // GRANT CREATE TABLE TO [sample]; + // GRANT SELECT ON SCHEMA::role TO [sample]; + // GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES, ALTER ON SCHEMA::sample TO [sample]; + // GRANT CREATE TABLE TO [sample]; + // GRANT ALTER ON SCHEMA::sample TO [sample]; + + config([ + 'database.connections.sqlsrv.username' => 'sample', + 'database.connections.sqlsrv.password' => 'sample', + ]); + + $this->assertEquals('my_schema', $connection->scalar('select schema_name()')); + + $schema->create('table', function (Blueprint $table) { + $table->id(); + }); + + $this->assertTrue($schema->hasTable('table')); + $this->assertTrue($schema->hasTable('my_schema.table')); + $this->assertFalse($schema->hasTable('dbo.table')); + } + public static function connectionProvider(): array { return [ From 3eb32a289d34d7b5eb9bba9ca89901c3ae4414b3 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sun, 31 Mar 2024 16:18:23 +0330 Subject: [PATCH 10/22] wip --- .../Integration/Database/SchemaBuilderSchemaNameTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index 4d1acbfd9c0a..677958bf3f8e 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -434,9 +434,9 @@ public function testHasTable($connection) $connection = DB::connection($connection); $schema = $connection->getSchemaBuilder(); - $connection->statement("create login [sample] with password = 'sample'"); - $connection->statement("create user [sample] for login [sample]"); - $connection->statement("alter user [sample] with default_schema = my_schema"); + $connection->statement("create login [sample] with password = 'password'"); + $connection->statement('create user [sample] for login [sample]'); + $connection->statement('alter user [sample] with default_schema = my_schema'); // GRANT CREATE TABLE TO [sample]; // GRANT SELECT ON SCHEMA::role TO [sample]; // GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES, ALTER ON SCHEMA::sample TO [sample]; @@ -445,7 +445,7 @@ public function testHasTable($connection) config([ 'database.connections.sqlsrv.username' => 'sample', - 'database.connections.sqlsrv.password' => 'sample', + 'database.connections.sqlsrv.password' => 'password', ]); $this->assertEquals('my_schema', $connection->scalar('select schema_name()')); From 43dccfc5a411c4ba470b474430575b8a973b4d96 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sun, 31 Mar 2024 16:21:06 +0330 Subject: [PATCH 11/22] wip --- tests/Integration/Database/SchemaBuilderSchemaNameTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index 677958bf3f8e..70b30f43331f 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -434,7 +434,7 @@ public function testHasTable($connection) $connection = DB::connection($connection); $schema = $connection->getSchemaBuilder(); - $connection->statement("create login [sample] with password = 'password'"); + $connection->statement("create login [sample] with password = 'Passw0rd'"); $connection->statement('create user [sample] for login [sample]'); $connection->statement('alter user [sample] with default_schema = my_schema'); // GRANT CREATE TABLE TO [sample]; @@ -445,7 +445,7 @@ public function testHasTable($connection) config([ 'database.connections.sqlsrv.username' => 'sample', - 'database.connections.sqlsrv.password' => 'password', + 'database.connections.sqlsrv.password' => 'Passw0rd', ]); $this->assertEquals('my_schema', $connection->scalar('select schema_name()')); From 14d3698f5271832cd9ce76d4f48664a2610595e1 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sun, 31 Mar 2024 16:28:54 +0330 Subject: [PATCH 12/22] wip --- .../Database/SchemaBuilderSchemaNameTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index 70b30f43331f..c5d9661f8176 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -434,17 +434,17 @@ public function testHasTable($connection) $connection = DB::connection($connection); $schema = $connection->getSchemaBuilder(); - $connection->statement("create login [sample] with password = 'Passw0rd'"); - $connection->statement('create user [sample] for login [sample]'); - $connection->statement('alter user [sample] with default_schema = my_schema'); - // GRANT CREATE TABLE TO [sample]; - // GRANT SELECT ON SCHEMA::role TO [sample]; - // GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES, ALTER ON SCHEMA::sample TO [sample]; - // GRANT CREATE TABLE TO [sample]; - // GRANT ALTER ON SCHEMA::sample TO [sample]; + $connection->statement("create login my_user with password = 'Passw0rd'"); + $connection->statement('create user my_user for login my_user'); + $connection->statement('alter user my_user with default_schema = my_schema'); + // GRANT CREATE TABLE TO [my_user]; + // GRANT SELECT ON SCHEMA::role TO [my_user]; + // GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES, ALTER ON SCHEMA::my_schema TO [my_user]; + // GRANT CREATE TABLE TO [my_user]; + // GRANT ALTER ON SCHEMA::my_schema TO [my_user]; config([ - 'database.connections.sqlsrv.username' => 'sample', + 'database.connections.sqlsrv.username' => 'my_user', 'database.connections.sqlsrv.password' => 'Passw0rd', ]); From 5f848e6795ccd51573015a0e1f9ac2f4126d6d7b Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sun, 31 Mar 2024 16:42:06 +0330 Subject: [PATCH 13/22] wip --- .../Database/SchemaBuilderSchemaNameTest.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index c5d9661f8176..25751f5c11ff 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -431,12 +431,13 @@ public function testHasTable($connection) if ($this->driver !== 'sqlsrv') { $this->markTestSkipped('Test requires a SQL Server connection.'); } - $connection = DB::connection($connection); - $schema = $connection->getSchemaBuilder(); - $connection->statement("create login my_user with password = 'Passw0rd'"); - $connection->statement('create user my_user for login my_user'); - $connection->statement('alter user my_user with default_schema = my_schema'); + $db = DB::connection($connection); + $schema = $db->getSchemaBuilder(); + + $db->statement("create login my_user with password = 'Passw0rd'"); + $db->statement('create user my_user for login my_user'); + $db->statement('alter user my_user with default_schema = my_schema'); // GRANT CREATE TABLE TO [my_user]; // GRANT SELECT ON SCHEMA::role TO [my_user]; // GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES, ALTER ON SCHEMA::my_schema TO [my_user]; @@ -444,11 +445,11 @@ public function testHasTable($connection) // GRANT ALTER ON SCHEMA::my_schema TO [my_user]; config([ - 'database.connections.sqlsrv.username' => 'my_user', - 'database.connections.sqlsrv.password' => 'Passw0rd', + 'database.connections.'.$connection.'.username' => 'my_user', + 'database.connections.'.$connection.'.password' => 'Passw0rd', ]); - $this->assertEquals('my_schema', $connection->scalar('select schema_name()')); + $this->assertEquals('my_schema', $db->scalar('select schema_name()')); $schema->create('table', function (Blueprint $table) { $table->id(); From 8e9d4ba5540ce327e00b60d585d96e90d9646c87 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sun, 31 Mar 2024 16:49:40 +0330 Subject: [PATCH 14/22] wip --- tests/Integration/Database/SchemaBuilderSchemaNameTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index 25751f5c11ff..8d6ef060db0a 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -437,7 +437,7 @@ public function testHasTable($connection) $db->statement("create login my_user with password = 'Passw0rd'"); $db->statement('create user my_user for login my_user'); - $db->statement('alter user my_user with default_schema = my_schema'); + $db->statement("alter user my_user with default_schema = my_schema execute as user='my_user'"); // GRANT CREATE TABLE TO [my_user]; // GRANT SELECT ON SCHEMA::role TO [my_user]; // GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES, ALTER ON SCHEMA::my_schema TO [my_user]; From 21b6d4db17f77fafc6ad6c20b26ba471889b207a Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sun, 31 Mar 2024 16:56:01 +0330 Subject: [PATCH 15/22] wip --- tests/Integration/Database/SchemaBuilderSchemaNameTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index 8d6ef060db0a..f50f87112077 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -435,10 +435,10 @@ public function testHasTable($connection) $db = DB::connection($connection); $schema = $db->getSchemaBuilder(); - $db->statement("create login my_user with password = 'Passw0rd'"); - $db->statement('create user my_user for login my_user'); + $db->statement("if suser_id(N'my_user') is null create login my_user with password = 'Passw0rd'"); + $db->statement("if suser_id(N'my_user') is null create user my_user for login my_user"); + $db->statement('grant create table to my_user'); $db->statement("alter user my_user with default_schema = my_schema execute as user='my_user'"); - // GRANT CREATE TABLE TO [my_user]; // GRANT SELECT ON SCHEMA::role TO [my_user]; // GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES, ALTER ON SCHEMA::my_schema TO [my_user]; // GRANT CREATE TABLE TO [my_user]; From 7ee641fb4e7a5360cf6bc94466e6be506bcad5af Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sun, 31 Mar 2024 16:58:52 +0330 Subject: [PATCH 16/22] wip --- tests/Integration/Database/SchemaBuilderSchemaNameTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index f50f87112077..ea06e9d14396 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -435,8 +435,8 @@ public function testHasTable($connection) $db = DB::connection($connection); $schema = $db->getSchemaBuilder(); - $db->statement("if suser_id(N'my_user') is null create login my_user with password = 'Passw0rd'"); - $db->statement("if suser_id(N'my_user') is null create user my_user for login my_user"); + $db->statement("create login my_user with password = 'Passw0rd'"); + $db->statement("create user my_user for login my_user"); $db->statement('grant create table to my_user'); $db->statement("alter user my_user with default_schema = my_schema execute as user='my_user'"); // GRANT SELECT ON SCHEMA::role TO [my_user]; From 73f58a036b4f427b37aa0f6d7126d56861826673 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sun, 31 Mar 2024 17:07:38 +0330 Subject: [PATCH 17/22] wip --- .../Integration/Database/SchemaBuilderSchemaNameTest.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index ea06e9d14396..ce559bb3dd0f 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -435,9 +435,12 @@ public function testHasTable($connection) $db = DB::connection($connection); $schema = $db->getSchemaBuilder(); - $db->statement("create login my_user with password = 'Passw0rd'"); - $db->statement("create user my_user for login my_user"); - $db->statement('grant create table to my_user'); + try { + $db->statement("create login my_user with password = 'Passw0rd'"); + $db->statement("create user my_user for login my_user"); + } catch(\Illuminate\Database\QueryException $e) {} + // $db->statement('grant select, insert, update, delete, REFERENCES, ALTER ON SCHEMA::my_schema TO [my_user]'); + $db->statement('grant create table on SCHEMA::my_schema to my_user'); $db->statement("alter user my_user with default_schema = my_schema execute as user='my_user'"); // GRANT SELECT ON SCHEMA::role TO [my_user]; // GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES, ALTER ON SCHEMA::my_schema TO [my_user]; From 436a7d0a58ada496bad3a5983b86477a5c4e7502 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sun, 31 Mar 2024 17:13:04 +0330 Subject: [PATCH 18/22] wip --- .../Database/SchemaBuilderSchemaNameTest.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index ce559bb3dd0f..0234641569f1 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -437,13 +437,14 @@ public function testHasTable($connection) try { $db->statement("create login my_user with password = 'Passw0rd'"); - $db->statement("create user my_user for login my_user"); - } catch(\Illuminate\Database\QueryException $e) {} - // $db->statement('grant select, insert, update, delete, REFERENCES, ALTER ON SCHEMA::my_schema TO [my_user]'); - $db->statement('grant create table on SCHEMA::my_schema to my_user'); + $db->statement('create user my_user for login my_user'); + } catch(\Illuminate\Database\QueryException $e) { + // + } $db->statement("alter user my_user with default_schema = my_schema execute as user='my_user'"); + $db->statement('grant create table to my_user'); // GRANT SELECT ON SCHEMA::role TO [my_user]; - // GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES, ALTER ON SCHEMA::my_schema TO [my_user]; + $db->statement('grant select, insert, update, delete, references, alter on SCHEMA::my_schema to my_user'); // GRANT CREATE TABLE TO [my_user]; // GRANT ALTER ON SCHEMA::my_schema TO [my_user]; From b4d5c733b7a79082ba50ab2dedd6997d26cfbde4 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sun, 31 Mar 2024 17:17:35 +0330 Subject: [PATCH 19/22] wip --- tests/Integration/Database/SchemaBuilderSchemaNameTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index 0234641569f1..99339d43d4e5 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -443,10 +443,10 @@ public function testHasTable($connection) } $db->statement("alter user my_user with default_schema = my_schema execute as user='my_user'"); $db->statement('grant create table to my_user'); - // GRANT SELECT ON SCHEMA::role TO [my_user]; + // $db->statement('grant select on SCHEMA::role to my_user'); $db->statement('grant select, insert, update, delete, references, alter on SCHEMA::my_schema to my_user'); - // GRANT CREATE TABLE TO [my_user]; - // GRANT ALTER ON SCHEMA::my_schema TO [my_user]; + $db->statement('grant create table to my_user'); + $db->statement('grant alter on SCHEMA::my_schema to my_user'); config([ 'database.connections.'.$connection.'.username' => 'my_user', From 9dcb12fc3e69d7c571e2e7504e4f01faae0e9a46 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sun, 31 Mar 2024 17:37:06 +0330 Subject: [PATCH 20/22] wip --- tests/Integration/Database/SchemaBuilderSchemaNameTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index 99339d43d4e5..c8d2bb6d7d37 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -441,12 +441,13 @@ public function testHasTable($connection) } catch(\Illuminate\Database\QueryException $e) { // } - $db->statement("alter user my_user with default_schema = my_schema execute as user='my_user'"); + $db->statement('grant create table to my_user'); // $db->statement('grant select on SCHEMA::role to my_user'); $db->statement('grant select, insert, update, delete, references, alter on SCHEMA::my_schema to my_user'); - $db->statement('grant create table to my_user'); + // $db->statement('grant create table to my_user'); $db->statement('grant alter on SCHEMA::my_schema to my_user'); + $db->statement("alter user my_user with default_schema = my_schema execute as user='my_user'"); config([ 'database.connections.'.$connection.'.username' => 'my_user', From 25dfffd9590f7d4315a5977b8a6787853d7e3f76 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sun, 31 Mar 2024 17:39:46 +0330 Subject: [PATCH 21/22] wip --- tests/Integration/Database/SchemaBuilderSchemaNameTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php index c8d2bb6d7d37..f548422317db 100644 --- a/tests/Integration/Database/SchemaBuilderSchemaNameTest.php +++ b/tests/Integration/Database/SchemaBuilderSchemaNameTest.php @@ -443,9 +443,6 @@ public function testHasTable($connection) } $db->statement('grant create table to my_user'); - // $db->statement('grant select on SCHEMA::role to my_user'); - $db->statement('grant select, insert, update, delete, references, alter on SCHEMA::my_schema to my_user'); - // $db->statement('grant create table to my_user'); $db->statement('grant alter on SCHEMA::my_schema to my_user'); $db->statement("alter user my_user with default_schema = my_schema execute as user='my_user'"); From 2064a04ab3985219bd6ae61b7736aa42e7c478bb Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 31 Mar 2024 19:29:22 -0500 Subject: [PATCH 22/22] formatting --- .../Schema/Grammars/SqlServerGrammar.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php b/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php index 631e57876f84..d15a32f3cbe7 100755 --- a/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php @@ -37,6 +37,16 @@ class SqlServerGrammar extends Grammar */ protected $fluentCommands = ['Default']; + /** + * Compile a query to determine the name of the default schema. + * + * @return string + */ + public function compileDefaultSchema() + { + return 'select schema_name()'; + } + /** * Compile a create database command. * @@ -66,16 +76,6 @@ public function compileDropDatabaseIfExists($name) ); } - /** - * Compile the query to determine the name of the default schema of the caller. - * - * @return string - */ - public function compileDefaultSchema() - { - return 'select schema_name()'; - } - /** * Compile the query to determine the tables. *