Skip to content

Commit 89000db

Browse files
Merge pull request #54 from max-ieremenko/feature/53
#53: postgres: run scripts as a non-superuser
2 parents 7e1ca35 + 03039b6 commit 89000db

15 files changed

Lines changed: 135 additions & 42 deletions

File tree

Build/scripts/Start-Pgsql.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function Start-Pgsql {
88

99
$builder = New-Object -TypeName Npgsql.NpgsqlConnectionStringBuilder
1010
$builder["Database"] = "sqldatabasetest"
11-
$builder["Username"] = "postgres"
11+
$builder["Username"] = "adminuser"
1212
$builder["Password"] = "qwerty"
1313
$builder["Timeout"] = 5
1414

Examples/ExecuteScriptsFolder/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Predefined variables
110110
Opening a connection
111111
========================
112112

113-
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.
113+
If the database specified in the connection string does not exist, execution will be terminated with the appropriate error.
114114

115115
MSSQL Server script example
116116
=============================

Examples/MigrationStepsFolder/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Predefined variables
9898
Opening a connection
9999
========================
100100
101-
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.
101+
If the database specified in the connection string does not exist, execution will be terminated with the appropriate error.
102102
103103
Migration MSSQL Server .sql step example
104104
=============================

Sources/Docker/pgsql.create-database.sql

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
CREATE DATABASE sqldatabasetest;
1+
CREATE ROLE adminuser WITH
2+
LOGIN
3+
NOSUPERUSER
4+
INHERIT
5+
CREATEDB
6+
CREATEROLE
7+
NOREPLICATION
8+
PASSWORD 'qwerty';
9+
10+
SET ROLE adminuser;
11+
12+
CREATE DATABASE sqldatabasetest;
213

314
\connect sqldatabasetest;
415

16+
SET ROLE adminuser;
17+
518
CREATE EXTENSION citext;
619

720
CREATE TABLE public.version
@@ -10,6 +23,12 @@ CREATE TABLE public.version
1023
,version varchar(20) NOT NULL
1124
);
1225

26+
CREATE TABLE public.version2
27+
(
28+
module_name public.citext NOT NULL
29+
,version varchar(20) NOT NULL
30+
);
31+
1332
ALTER TABLE public.version
1433
ADD CONSTRAINT pk_version PRIMARY KEY (module_name);
1534

@@ -24,4 +43,4 @@ CREATE TYPE public.inventory_item AS (
2443
name text,
2544
supplier_id integer,
2645
price numeric
27-
);
46+
);

Sources/SqlDatabase.Adapter.PgSql.Test/app.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
<configuration>
33
<connectionStrings>
44
<add name="pgsql"
5-
connectionString="Host=localhost;Username=postgres;Password=qwerty;Database=sqldatabasetest;" />
5+
connectionString="Host=localhost;Username=adminuser;Password=qwerty;Database=sqldatabasetest;" />
66
</connectionStrings>
77
</configuration>

Sources/SqlDatabase.Adapter.PgSql/PgSqlDatabaseAdapter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public PgSqlDatabaseAdapter(
2525
Pooling = false
2626
};
2727

28-
DatabaseName = builder.Database;
28+
DatabaseName = builder.Database!;
2929
_connectionString = builder.ToString();
3030

31-
builder.Database = null;
31+
builder.Database = "postgres"; // The master will always be set to postgres database
3232
_connectionStringMaster = builder.ToString();
3333

3434
_onConnectionNotice = OnConnectionNotice;
@@ -72,4 +72,4 @@ private void OnConnectionNotice(object sender, NpgsqlNoticeEventArgs e)
7272
{
7373
_log.Info($"{e.Notice.Severity}: {e.Notice.MessageText}");
7474
}
75-
}
75+
}

Sources/SqlDatabase.Test/Commands/DatabaseCreateCommandTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void BeforeEachTest()
2727

2828
_database = new Mock<IDatabase>(MockBehavior.Strict);
2929
_database.SetupGet(d => d.Adapter).Returns(adapter.Object);
30-
_database.Setup(d => d.GetServerVersion()).Returns("sql server 1.0");
30+
_database.Setup(d => d.GetServerVersion(true)).Returns("sql server 1.0");
3131

3232
_scriptSequence = new Mock<ICreateScriptSequence>(MockBehavior.Strict);
3333

@@ -80,8 +80,8 @@ public void ExecuteSequence()
8080
.Setup(f => f.InitializeEnvironment(_log.Object, sequence));
8181

8282
_database
83-
.Setup(d => d.Execute(step1.Object))
84-
.Callback(() => _database.Setup(d => d.Execute(step2.Object)));
83+
.Setup(d => d.ExecuteWithDatabaseCheck(step1.Object))
84+
.Callback(() => _database.Setup(d => d.ExecuteWithDatabaseCheck(step2.Object)));
8585

8686
_scriptSequence
8787
.Setup(s => s.BuildSequence())
@@ -109,7 +109,7 @@ public void StopExecutionOnError()
109109
.Setup(f => f.InitializeEnvironment(_log.Object, sequence));
110110

111111
_database
112-
.Setup(d => d.Execute(step1.Object))
112+
.Setup(d => d.ExecuteWithDatabaseCheck(step1.Object))
113113
.Throws<InvalidOperationException>();
114114

115115
_scriptSequence

Sources/SqlDatabase.Test/Commands/DatabaseExecuteCommandTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void BeforeEachTest()
2626

2727
_database = new Mock<IDatabase>(MockBehavior.Strict);
2828
_database.SetupGet(d => d.Adapter).Returns(adapter.Object);
29-
_database.Setup(d => d.GetServerVersion()).Returns("sql server 1.0");
29+
_database.Setup(d => d.GetServerVersion(false)).Returns("sql server 1.0");
3030

3131
_scriptSequence = new Mock<ICreateScriptSequence>(MockBehavior.Strict);
3232

Sources/SqlDatabase.Test/Commands/DatabaseExportCommandTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void BeforeEachTest()
3131

3232
_database = new Mock<IDatabase>(MockBehavior.Strict);
3333
_database.SetupGet(d => d.Adapter).Returns(adapter.Object);
34-
_database.Setup(d => d.GetServerVersion()).Returns("sql server 1.0");
34+
_database.Setup(d => d.GetServerVersion(false)).Returns("sql server 1.0");
3535

3636
_scriptSequence = new Mock<ICreateScriptSequence>(MockBehavior.Strict);
3737

Sources/SqlDatabase.Test/Commands/DatabaseUpgradeCommandTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void BeforeEachTest()
2626

2727
_database = new Mock<IDatabase>(MockBehavior.Strict);
2828
_database.SetupGet(d => d.Adapter).Returns(adapter.Object);
29-
_database.Setup(d => d.GetServerVersion()).Returns("sql server 1.0");
29+
_database.Setup(d => d.GetServerVersion(false)).Returns("sql server 1.0");
3030

3131
_scriptSequence = new Mock<IUpgradeScriptSequence>(MockBehavior.Strict);
3232

0 commit comments

Comments
 (0)