diff --git a/Build/README.md b/Build/README.md index eeea4807..0f575189 100644 --- a/Build/README.md +++ b/Build/README.md @@ -5,7 +5,7 @@ build.ps1 is designed to run on windows - PowerShell Desktop 5.1 - PowerShell [7.3.0](https://github.com/PowerShell/PowerShell/releases/tag/v7.3.0) for .net 7.0 and 8.0 tests - PowerShell [7.2.1](https://github.com/PowerShell/PowerShell/releases/tag/v7.2.1) for .net 6.0 tests -- Install-Module -Name [InvokeBuild](https://www.powershellgallery.com/packages/InvokeBuild/5.11.0) -RequiredVersion 5.11.0 +- Install-Module -Name [InvokeBuild](https://www.powershellgallery.com/packages/InvokeBuild/5.11.1) -RequiredVersion 5.11.1 - Install-Module -Name [ThirdPartyLibraries](https://www.powershellgallery.com/packages/ThirdPartyLibraries/3.5.1) -RequiredVersion 3.5.1 - .net framework 4.7.2+ sdk - .net 8.0 sdk diff --git a/Build/build.ps1 b/Build/build.ps1 index 39b7471b..2d0f852e 100644 --- a/Build/build.ps1 +++ b/Build/build.ps1 @@ -1,5 +1,5 @@ #Requires -Version "7.0" -#Requires -Modules @{ ModuleName="InvokeBuild"; ModuleVersion="5.11.0" } +#Requires -Modules @{ ModuleName="InvokeBuild"; ModuleVersion="5.11.1" } #Requires -Modules @{ ModuleName="ThirdPartyLibraries"; ModuleVersion="3.5.1" } [CmdletBinding()] diff --git a/Build/scripts/Start-Pgsql.ps1 b/Build/scripts/Start-Pgsql.ps1 index d1a0d01c..ceec1faf 100644 --- a/Build/scripts/Start-Pgsql.ps1 +++ b/Build/scripts/Start-Pgsql.ps1 @@ -1,14 +1,14 @@ function Start-Pgsql { param () - $npgsqldll = Join-Path $env:USERPROFILE ".nuget\packages\npgsql\4.0.11\lib\netstandard2.0\Npgsql.dll" + $npgsqldll = Join-Path $env:USERPROFILE ".nuget\packages\npgsql\4.0.16\lib\netstandard2.0\Npgsql.dll" Add-Type -Path $npgsqldll $container = Start-Container -Image sqldatabase/postgres:13.3 -ContainerPort 5432 $builder = New-Object -TypeName Npgsql.NpgsqlConnectionStringBuilder $builder["Database"] = "sqldatabasetest" - $builder["Username"] = "postgres" + $builder["Username"] = "adminuser" $builder["Password"] = "qwerty" $builder["Timeout"] = 5 diff --git a/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.11/index.json b/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.16/index.json similarity index 100% rename from Build/third-party-libraries/packages/nuget.org/npgsql/4.0.11/index.json rename to Build/third-party-libraries/packages/nuget.org/npgsql/4.0.16/index.json diff --git a/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.11/package.nuspec b/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.16/package.nuspec similarity index 91% rename from Build/third-party-libraries/packages/nuget.org/npgsql/4.0.11/package.nuspec rename to Build/third-party-libraries/packages/nuget.org/npgsql/4.0.16/package.nuspec index a2a9cd1f..4b5b8b8f 100644 --- a/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.11/package.nuspec +++ b/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.16/package.nuspec @@ -2,17 +2,15 @@ Npgsql - 4.0.11 + 4.0.16 Shay Rojansky,Yoh Deadfall,Austin Drenski,Emil Lenngren,Francisco Figueiredo Jr.,Kenji Uno - Shay Rojansky,Yoh Deadfall,Austin Drenski,Emil Lenngren,Francisco Figueiredo Jr.,Kenji Uno - false PostgreSQL https://licenses.nuget.org/PostgreSQL http://www.npgsql.org/ Npgsql is the open source .NET data provider for PostgreSQL. Copyright 2019 © The Npgsql Development Team npgsql postgresql postgres ado ado.net database sql - + diff --git a/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.11/readme.md b/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.16/readme.md similarity index 93% rename from Build/third-party-libraries/packages/nuget.org/npgsql/4.0.11/readme.md rename to Build/third-party-libraries/packages/nuget.org/npgsql/4.0.16/readme.md index e99ba1c0..f360af6d 100644 --- a/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.11/readme.md +++ b/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.16/readme.md @@ -1,4 +1,4 @@ -Npgsql [4.0.11](https://www.nuget.org/packages/Npgsql/4.0.11) +Npgsql [4.0.16](https://www.nuget.org/packages/Npgsql/4.0.16) -------------------- Used by: SqlDatabase diff --git a/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.11/remarks.md b/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.16/remarks.md similarity index 100% rename from Build/third-party-libraries/packages/nuget.org/npgsql/4.0.11/remarks.md rename to Build/third-party-libraries/packages/nuget.org/npgsql/4.0.16/remarks.md diff --git a/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.11/repository-LICENSE b/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.16/repository-LICENSE similarity index 96% rename from Build/third-party-libraries/packages/nuget.org/npgsql/4.0.11/repository-LICENSE rename to Build/third-party-libraries/packages/nuget.org/npgsql/4.0.16/repository-LICENSE index efec310c..a74ee166 100644 --- a/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.11/repository-LICENSE +++ b/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.16/repository-LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2002-2023, Npgsql +Copyright (c) 2002-2024, Npgsql Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreement diff --git a/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.11/third-party-notices.txt b/Build/third-party-libraries/packages/nuget.org/npgsql/4.0.16/third-party-notices.txt similarity index 100% rename from Build/third-party-libraries/packages/nuget.org/npgsql/4.0.11/third-party-notices.txt rename to Build/third-party-libraries/packages/nuget.org/npgsql/4.0.16/third-party-notices.txt diff --git a/Build/third-party-libraries/readme.md b/Build/third-party-libraries/readme.md index 8e5dc548..7ce9a144 100644 --- a/Build/third-party-libraries/readme.md +++ b/Build/third-party-libraries/readme.md @@ -32,7 +32,7 @@ Packages 47 |[NETStandard.Library](packages/nuget.org/netstandard.library/2.0.3)|2.0.3|[nuget.org](https://www.nuget.org/packages/NETStandard.Library/2.0.3)|[MIT](licenses/mit)|SqlDatabase| |[Newtonsoft.Json](packages/nuget.org/newtonsoft.json/13.0.1)|13.0.1|[nuget.org](https://www.nuget.org/packages/Newtonsoft.Json/13.0.1)|[MIT](licenses/mit)|SqlDatabase internal| |[Newtonsoft.Json](packages/nuget.org/newtonsoft.json/13.0.3)|13.0.3|[nuget.org](https://www.nuget.org/packages/Newtonsoft.Json/13.0.3)|[MIT](licenses/mit)|SqlDatabase internal| -|[Npgsql](packages/nuget.org/npgsql/4.0.11)|4.0.11|[nuget.org](https://www.nuget.org/packages/Npgsql/4.0.11)|[PostgreSQL](licenses/postgresql)|SqlDatabase| +|[Npgsql](packages/nuget.org/npgsql/4.0.16)|4.0.16|[nuget.org](https://www.nuget.org/packages/Npgsql/4.0.16)|[PostgreSQL](licenses/postgresql)|SqlDatabase| |[NUnit](packages/nuget.org/nunit/4.1.0)|4.1.0|[nuget.org](https://www.nuget.org/packages/NUnit/4.1.0)|[MIT](licenses/mit)|SqlDatabase internal| |[NUnit3TestAdapter](packages/nuget.org/nunit3testadapter/4.5.0)|4.5.0|[nuget.org](https://www.nuget.org/packages/NUnit3TestAdapter/4.5.0)|[MIT](licenses/mit)|SqlDatabase internal| |[PowerShellStandard.Library](packages/nuget.org/powershellstandard.library/5.1.1)|5.1.1|[nuget.org](https://www.nuget.org/packages/PowerShellStandard.Library/5.1.1)|[MIT](licenses/mit)|SqlDatabase| diff --git a/Examples/ExecuteScriptsFolder/README.md b/Examples/ExecuteScriptsFolder/README.md index 3985e677..e37fb492 100644 --- a/Examples/ExecuteScriptsFolder/README.md +++ b/Examples/ExecuteScriptsFolder/README.md @@ -110,7 +110,7 @@ Predefined variables Opening a connection ======================== -Before starting any step SqlDatabase checks if a database, provided in the connection string, exists. If database does not exists the connection will be targeted to `master` for MSSQL and `postgres` for PostgreSQL. +If the database specified in the connection string does not exist, execution will be terminated with the appropriate error. MSSQL Server script example ============================= diff --git a/Examples/MigrationStepsFolder/README.md b/Examples/MigrationStepsFolder/README.md index 33f5dc18..f14f1e26 100644 --- a/Examples/MigrationStepsFolder/README.md +++ b/Examples/MigrationStepsFolder/README.md @@ -98,7 +98,7 @@ Predefined variables Opening a connection ======================== -Before starting any step SqlDatabase checks if a database, provided in the connection string, exists. If database does not exists the connection will be targeted to `master` for MSSQL and `postgres` for PostgreSQL. +If the database specified in the connection string does not exist, execution will be terminated with the appropriate error. Migration MSSQL Server .sql step example ============================= diff --git a/Sources/Directory.Build.props b/Sources/Directory.Build.props index a16a7677..73d984d7 100644 --- a/Sources/Directory.Build.props +++ b/Sources/Directory.Build.props @@ -18,7 +18,7 @@ - 4.2.1 + 4.2.2 $(SqlDatabaseVersion) $(SqlDatabaseVersion).0 $(SqlDatabaseVersion).0 diff --git a/Sources/Directory.Packages.props b/Sources/Directory.Packages.props index 50ca7700..a86b3f2e 100644 --- a/Sources/Directory.Packages.props +++ b/Sources/Directory.Packages.props @@ -6,7 +6,7 @@ - + diff --git a/Sources/Docker/pgsql.create-database.sql b/Sources/Docker/pgsql.create-database.sql index ff741b56..30e968a6 100644 --- a/Sources/Docker/pgsql.create-database.sql +++ b/Sources/Docker/pgsql.create-database.sql @@ -1,7 +1,20 @@ -CREATE DATABASE sqldatabasetest; +CREATE ROLE adminuser WITH + LOGIN + NOSUPERUSER + INHERIT + CREATEDB + CREATEROLE + NOREPLICATION + PASSWORD 'qwerty'; + +SET ROLE adminuser; + +CREATE DATABASE sqldatabasetest; \connect sqldatabasetest; +SET ROLE adminuser; + CREATE EXTENSION citext; CREATE TABLE public.version @@ -10,6 +23,12 @@ CREATE TABLE public.version ,version varchar(20) NOT NULL ); +CREATE TABLE public.version2 +( + module_name public.citext NOT NULL + ,version varchar(20) NOT NULL +); + ALTER TABLE public.version ADD CONSTRAINT pk_version PRIMARY KEY (module_name); @@ -24,4 +43,4 @@ CREATE TYPE public.inventory_item AS ( name text, supplier_id integer, price numeric -); \ No newline at end of file +); diff --git a/Sources/SqlDatabase.Adapter.PgSql.Test/app.config b/Sources/SqlDatabase.Adapter.PgSql.Test/app.config index 022d7611..88e0184e 100644 --- a/Sources/SqlDatabase.Adapter.PgSql.Test/app.config +++ b/Sources/SqlDatabase.Adapter.PgSql.Test/app.config @@ -2,6 +2,6 @@ + connectionString="Host=localhost;Username=adminuser;Password=qwerty;Database=sqldatabasetest;" /> \ No newline at end of file diff --git a/Sources/SqlDatabase.Adapter.PgSql/PgSqlDatabaseAdapter.cs b/Sources/SqlDatabase.Adapter.PgSql/PgSqlDatabaseAdapter.cs index 3892b245..31875814 100644 --- a/Sources/SqlDatabase.Adapter.PgSql/PgSqlDatabaseAdapter.cs +++ b/Sources/SqlDatabase.Adapter.PgSql/PgSqlDatabaseAdapter.cs @@ -25,10 +25,10 @@ internal sealed class PgSqlDatabaseAdapter : IDatabaseAdapter Pooling = false }; - DatabaseName = builder.Database; + DatabaseName = builder.Database!; _connectionString = builder.ToString(); - builder.Database = null; + builder.Database = "postgres"; // The master will always be set to postgres database _connectionStringMaster = builder.ToString(); _onConnectionNotice = OnConnectionNotice; @@ -72,4 +72,4 @@ private void OnConnectionNotice(object sender, NpgsqlNoticeEventArgs e) { _log.Info($"{e.Notice.Severity}: {e.Notice.MessageText}"); } -} \ No newline at end of file +} diff --git a/Sources/SqlDatabase.Test/Commands/DatabaseCreateCommandTest.cs b/Sources/SqlDatabase.Test/Commands/DatabaseCreateCommandTest.cs index d382ba7f..49e3f016 100644 --- a/Sources/SqlDatabase.Test/Commands/DatabaseCreateCommandTest.cs +++ b/Sources/SqlDatabase.Test/Commands/DatabaseCreateCommandTest.cs @@ -27,7 +27,7 @@ public void BeforeEachTest() _database = new Mock(MockBehavior.Strict); _database.SetupGet(d => d.Adapter).Returns(adapter.Object); - _database.Setup(d => d.GetServerVersion()).Returns("sql server 1.0"); + _database.Setup(d => d.GetServerVersion(true)).Returns("sql server 1.0"); _scriptSequence = new Mock(MockBehavior.Strict); @@ -80,8 +80,8 @@ public void ExecuteSequence() .Setup(f => f.InitializeEnvironment(_log.Object, sequence)); _database - .Setup(d => d.Execute(step1.Object)) - .Callback(() => _database.Setup(d => d.Execute(step2.Object))); + .Setup(d => d.ExecuteWithDatabaseCheck(step1.Object)) + .Callback(() => _database.Setup(d => d.ExecuteWithDatabaseCheck(step2.Object))); _scriptSequence .Setup(s => s.BuildSequence()) @@ -109,7 +109,7 @@ public void StopExecutionOnError() .Setup(f => f.InitializeEnvironment(_log.Object, sequence)); _database - .Setup(d => d.Execute(step1.Object)) + .Setup(d => d.ExecuteWithDatabaseCheck(step1.Object)) .Throws(); _scriptSequence diff --git a/Sources/SqlDatabase.Test/Commands/DatabaseExecuteCommandTest.cs b/Sources/SqlDatabase.Test/Commands/DatabaseExecuteCommandTest.cs index 4fb5f50c..25410f8c 100644 --- a/Sources/SqlDatabase.Test/Commands/DatabaseExecuteCommandTest.cs +++ b/Sources/SqlDatabase.Test/Commands/DatabaseExecuteCommandTest.cs @@ -26,7 +26,7 @@ public void BeforeEachTest() _database = new Mock(MockBehavior.Strict); _database.SetupGet(d => d.Adapter).Returns(adapter.Object); - _database.Setup(d => d.GetServerVersion()).Returns("sql server 1.0"); + _database.Setup(d => d.GetServerVersion(false)).Returns("sql server 1.0"); _scriptSequence = new Mock(MockBehavior.Strict); diff --git a/Sources/SqlDatabase.Test/Commands/DatabaseExportCommandTest.cs b/Sources/SqlDatabase.Test/Commands/DatabaseExportCommandTest.cs index 080dc8fa..c794422f 100644 --- a/Sources/SqlDatabase.Test/Commands/DatabaseExportCommandTest.cs +++ b/Sources/SqlDatabase.Test/Commands/DatabaseExportCommandTest.cs @@ -31,7 +31,7 @@ public void BeforeEachTest() _database = new Mock(MockBehavior.Strict); _database.SetupGet(d => d.Adapter).Returns(adapter.Object); - _database.Setup(d => d.GetServerVersion()).Returns("sql server 1.0"); + _database.Setup(d => d.GetServerVersion(false)).Returns("sql server 1.0"); _scriptSequence = new Mock(MockBehavior.Strict); diff --git a/Sources/SqlDatabase.Test/Commands/DatabaseUpgradeCommandTest.cs b/Sources/SqlDatabase.Test/Commands/DatabaseUpgradeCommandTest.cs index bb8f760c..93adec02 100644 --- a/Sources/SqlDatabase.Test/Commands/DatabaseUpgradeCommandTest.cs +++ b/Sources/SqlDatabase.Test/Commands/DatabaseUpgradeCommandTest.cs @@ -26,7 +26,7 @@ public void BeforeEachTest() _database = new Mock(MockBehavior.Strict); _database.SetupGet(d => d.Adapter).Returns(adapter.Object); - _database.Setup(d => d.GetServerVersion()).Returns("sql server 1.0"); + _database.Setup(d => d.GetServerVersion(false)).Returns("sql server 1.0"); _scriptSequence = new Mock(MockBehavior.Strict); diff --git a/Sources/SqlDatabase.Test/Scripts/DatabaseTest.cs b/Sources/SqlDatabase.Test/Scripts/DatabaseTest.cs index 50b61b0c..935c88cc 100644 --- a/Sources/SqlDatabase.Test/Scripts/DatabaseTest.cs +++ b/Sources/SqlDatabase.Test/Scripts/DatabaseTest.cs @@ -191,13 +191,15 @@ public void GetCurrentVersionModuleNameInvalidVersion() } [Test] - public void GetServerVersion() + [TestCase(true)] + [TestCase(false)] + public void GetServerVersion(bool useMasterDatabase) { _adapter .Setup(a => a.GetServerVersionSelectScript()) .Returns("select server version"); _adapter - .Setup(a => a.CreateConnection(true)) + .Setup(a => a.CreateConnection(useMasterDatabase)) .Returns(_connection.Object); _command @@ -208,7 +210,7 @@ public void GetServerVersion() }) .Returns("server version"); - var actual = _sut.GetServerVersion(); + var actual = _sut.GetServerVersion(useMasterDatabase); actual.ShouldBe("server version"); } @@ -315,6 +317,57 @@ public void Execute(TransactionMode transaction) { _sut.Transaction = transaction; + _command + .SetupProperty(c => c.CommandTimeout, 30); + _command + .SetupProperty(c => c.Transaction); + + _connection + .Setup(c => c.BeginTransaction(IsolationLevel.ReadCommitted)) + .Returns(_transaction.Object); + + _adapter + .SetupGet(a => a.DatabaseName) + .Returns("database-name"); + _adapter + .Setup(a => a.CreateConnection(false)) + .Returns(_connection.Object); + + var script = new Mock(MockBehavior.Strict); + script + .Setup(s => s.Execute(_command.Object, It.IsNotNull(), It.IsNotNull())) + .Callback((cmd, variables, s) => + { + cmd.CommandTimeout.ShouldBe(0); + + if (transaction == TransactionMode.PerStep) + { + cmd.Transaction.ShouldBe(_transaction.Object); + } + else + { + cmd.Transaction.ShouldBeNull(); + } + + variables.GetValue("DatabaseName").ShouldBe("database-name"); + variables.GetValue("CurrentVersion").ShouldBeNullOrEmpty(); + variables.GetValue("TargetVersion").ShouldBeNullOrEmpty(); + variables.GetValue("ModuleName").ShouldBeNullOrEmpty(); + }); + + _sut.Execute(script.Object); + + script.VerifyAll(); + _command.VerifyAll(); + } + + [Test] + [TestCase(TransactionMode.None)] + [TestCase(TransactionMode.PerStep)] + public void ExecuteWithDatabaseCheck(TransactionMode transaction) + { + _sut.Transaction = transaction; + _command .SetupProperty(c => c.CommandTimeout, 30); _command @@ -364,7 +417,7 @@ public void Execute(TransactionMode transaction) variables.GetValue("ModuleName").ShouldBeNullOrEmpty(); }); - _sut.Execute(script.Object); + _sut.ExecuteWithDatabaseCheck(script.Object); script.VerifyAll(); _command.VerifyAll(); @@ -402,7 +455,7 @@ public void ExecuteDatabaseNotFound() cmd.Transaction.ShouldBeNull(); }); - _sut.Execute(script.Object); + _sut.ExecuteWithDatabaseCheck(script.Object); script.VerifyAll(); _command.VerifyAll(); diff --git a/Sources/SqlDatabase/Commands/DatabaseCommandBase.cs b/Sources/SqlDatabase/Commands/DatabaseCommandBase.cs index 299fb358..9f32705b 100644 --- a/Sources/SqlDatabase/Commands/DatabaseCommandBase.cs +++ b/Sources/SqlDatabase/Commands/DatabaseCommandBase.cs @@ -18,7 +18,7 @@ protected DatabaseCommandBase(IDatabase database, ILogger log) public void Execute() { Greet(Database.Adapter.GetUserFriendlyConnectionString()); - Log.Info(Database.GetServerVersion()); + Log.Info(GetServerVersion()); ExecuteCore(); } @@ -26,4 +26,6 @@ public void Execute() protected abstract void Greet(string databaseLocation); protected abstract void ExecuteCore(); + + protected virtual string GetServerVersion() => Database.GetServerVersion(false); } \ No newline at end of file diff --git a/Sources/SqlDatabase/Commands/DatabaseCreateCommand.cs b/Sources/SqlDatabase/Commands/DatabaseCreateCommand.cs index 9374f477..2e001dd0 100644 --- a/Sources/SqlDatabase/Commands/DatabaseCreateCommand.cs +++ b/Sources/SqlDatabase/Commands/DatabaseCreateCommand.cs @@ -27,6 +27,8 @@ protected override void Greet(string databaseLocation) Log.Info($"Create {databaseLocation}"); } + protected override string GetServerVersion() => Database.GetServerVersion(true); + protected override void ExecuteCore() { var sequences = ScriptSequence.BuildSequence(); @@ -44,7 +46,7 @@ protected override void ExecuteCore() using (Log.Indent()) { - Database.Execute(script); + Database.ExecuteWithDatabaseCheck(script); } Log.Info($"done in {timer.Elapsed}"); diff --git a/Sources/SqlDatabase/Scripts/Database.cs b/Sources/SqlDatabase/Scripts/Database.cs index 0bc782fe..99c66aac 100644 --- a/Sources/SqlDatabase/Scripts/Database.cs +++ b/Sources/SqlDatabase/Scripts/Database.cs @@ -39,9 +39,9 @@ public Version GetCurrentVersion(string? moduleName) } } - public string GetServerVersion() + public string GetServerVersion(bool useMasterDatabase) { - using (var connection = Adapter.CreateConnection(true)) + using (var connection = Adapter.CreateConnection(useMasterDatabase)) using (var command = connection.CreateCommand()) { command.CommandText = Adapter.GetServerVersionSelectScript(); @@ -78,7 +78,21 @@ public void Execute(IScript script) } else { - InvokeExecute(script); + InvokeExecute(script, false); + } + } + + public void ExecuteWithDatabaseCheck(IScript script) + { + Variables.DatabaseName = Adapter.DatabaseName; + + if (WhatIf) + { + ExecuteWhatIf(script); + } + else + { + InvokeExecute(script, !DatabaseExists()); } } @@ -172,22 +186,8 @@ private void InvokeExecuteUpgrade(IScript script, Version targetVersion) } } - private void InvokeExecute(IScript script) + private void InvokeExecute(IScript script, bool useMaster) { - bool useMaster; - - using (var connection = Adapter.CreateConnection(true)) - using (var command = connection.CreateCommand()) - { - command.CommandTimeout = 0; - connection.Open(); - - command.CommandText = Adapter.GetDatabaseExistsScript(Variables.DatabaseName!); - var value = command.ExecuteScalar(); - - useMaster = value == null || Convert.IsDBNull(value); - } - using (var connection = Adapter.CreateConnection(useMaster)) { connection.Open(); @@ -206,6 +206,21 @@ private void InvokeExecute(IScript script) } } + private bool DatabaseExists() + { + using (var connection = Adapter.CreateConnection(true)) + using (var command = connection.CreateCommand()) + { + command.CommandTimeout = 0; + connection.Open(); + + command.CommandText = Adapter.GetDatabaseExistsScript(Variables.DatabaseName!); + var value = command.ExecuteScalar(); + + return value != null && !Convert.IsDBNull(value); + } + } + private void ExecuteWhatIf(IScript script) { Log.Info("what-if mode"); diff --git a/Sources/SqlDatabase/Scripts/IDatabase.cs b/Sources/SqlDatabase/Scripts/IDatabase.cs index a095a637..278134bd 100644 --- a/Sources/SqlDatabase/Scripts/IDatabase.cs +++ b/Sources/SqlDatabase/Scripts/IDatabase.cs @@ -6,7 +6,7 @@ internal interface IDatabase { IDatabaseAdapter Adapter { get; } - string GetServerVersion(); + string GetServerVersion(bool useMasterDatabase); Version GetCurrentVersion(string? moduleName); @@ -14,5 +14,7 @@ internal interface IDatabase void Execute(IScript script); + void ExecuteWithDatabaseCheck(IScript script); + IEnumerable ExecuteReader(IScript script); } \ No newline at end of file