@@ -582,55 +582,69 @@ public static void EnsureDatabaseAndTokenCacheTableExist(string serverConnection
582
582
{
583
583
connection . Open ( ) ;
584
584
585
- // Check if database exists
586
- string checkDatabaseQuery = $ "SELECT database_id FROM sys.databases WHERE name = '{ databaseName } '";
587
- using ( SqlCommand command = new ( checkDatabaseQuery , connection ) )
585
+ // Check if database exists and create it if it does not
586
+ if ( DatabaseExists ( connection , databaseName ) )
588
587
{
589
- object result = command . ExecuteScalar ( ) ;
590
- if ( result == null )
591
- {
592
- // Create database if it doesn't exist
593
- string createDatabaseQuery = $ "CREATE DATABASE { databaseName } ";
594
- using SqlCommand createCommand = new SqlCommand ( createDatabaseQuery , connection ) ;
595
- createCommand . ExecuteNonQuery ( ) ;
596
- output . WriteLine ( "Database created." ) ;
597
- }
598
- else
599
- {
600
- output . WriteLine ( "Database already exists." ) ;
601
- }
588
+ output . WriteLine ( "Database already exists." ) ;
589
+ }
590
+ else
591
+ {
592
+ CreateDatabase ( connection , databaseName ) ;
593
+ output . WriteLine ( "Database created." ) ;
602
594
}
603
595
604
596
// Switch to the database
605
597
connection . ChangeDatabase ( databaseName ) ;
606
598
607
- // Check if table exists
608
- string checkTableQuery = $ "SELECT object_id('{ tableName } ', 'U')";
609
- using ( SqlCommand command = new SqlCommand ( checkTableQuery , connection ) )
599
+ // Check if table exists and create it if it does not
600
+ if ( TableExists ( connection , tableName ) )
601
+ {
602
+ output . WriteLine ( "Table already exists." ) ;
603
+ }
604
+ else
610
605
{
611
- object result = command . ExecuteScalar ( ) ;
612
- if ( result . GetType ( ) == typeof ( DBNull ) )
613
- {
614
- // Create table if it doesn't exist
615
- string createCacheTableQuery = $@ "
616
- CREATE TABLE [dbo].[{ tableName } ] (
617
- [Id] NVARCHAR(449) NOT NULL PRIMARY KEY,
618
- [Value] VARBINARY(MAX) NOT NULL,
619
- [ExpiresAtTime] DATETIMEOFFSET NOT NULL,
620
- [SlidingExpirationInSeconds] BIGINT NULL,
621
- [AbsoluteExpiration] DATETIMEOFFSET NULL
622
- )" ;
623
- using SqlCommand createCommand = new SqlCommand ( createCacheTableQuery , connection ) ;
624
- createCommand . ExecuteNonQuery ( ) ;
625
- output . WriteLine ( "Table created." ) ;
626
- }
627
- else
628
- {
629
- output . WriteLine ( "Table already exists." ) ;
630
- }
606
+ CreateTokenCacheTable ( connection , tableName ) ;
607
+ output . WriteLine ( "Table created." ) ;
631
608
}
632
609
}
633
610
}
611
+
612
+ private static bool DatabaseExists ( SqlConnection connection , string databaseName )
613
+ {
614
+ string checkDatabaseQuery = $ "SELECT database_id FROM sys.databases WHERE name = '{ databaseName } '";
615
+ using SqlCommand command = new SqlCommand ( checkDatabaseQuery , connection ) ;
616
+ object result = command . ExecuteScalar ( ) ;
617
+ return result != null ;
618
+ }
619
+
620
+ private static void CreateDatabase ( SqlConnection connection , string databaseName )
621
+ {
622
+ string createDatabaseQuery = $ "CREATE DATABASE { databaseName } ";
623
+ using SqlCommand createCommand = new SqlCommand ( createDatabaseQuery , connection ) ;
624
+ createCommand . ExecuteNonQuery ( ) ;
625
+ }
626
+
627
+ private static bool TableExists ( SqlConnection connection , string tableName )
628
+ {
629
+ string checkTableQuery = $ "SELECT object_id('{ tableName } ', 'U')";
630
+ using SqlCommand command = new SqlCommand ( checkTableQuery , connection ) ;
631
+ object result = command . ExecuteScalar ( ) ;
632
+ return result . GetType ( ) != typeof ( DBNull ) ;
633
+ }
634
+
635
+ private static void CreateTokenCacheTable ( SqlConnection connection , string tableName )
636
+ {
637
+ string createCacheTableQuery = $@ "
638
+ CREATE TABLE [dbo].[{ tableName } ] (
639
+ [Id] NVARCHAR(449) NOT NULL PRIMARY KEY,
640
+ [Value] VARBINARY(MAX) NOT NULL,
641
+ [ExpiresAtTime] DATETIMEOFFSET NOT NULL,
642
+ [SlidingExpirationInSeconds] BIGINT NULL,
643
+ [AbsoluteExpiration] DATETIMEOFFSET NULL
644
+ )" ;
645
+ using SqlCommand createCommand = new SqlCommand ( createCacheTableQuery , connection ) ;
646
+ createCommand . ExecuteNonQuery ( ) ;
647
+ }
634
648
}
635
649
636
650
/// <summary>
0 commit comments