Skip to content

Commit

Permalink
Copying sqlserver init script from xo source
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Oct 24, 2024
1 parent 07998a5 commit d499701
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions contrib/sqlserver/init.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
\set SQLSERVER_USER sa
\set SQLSERVER_PASS Adm1nP@ssw0rd
\set SQLSERVER_DB postgres
\set SQLSERVER_HOST `docker port sqlserver 1433`

\prompt NAME 'Create database user: '
\prompt -password PASS 'Password for "':NAME'": '

\connect 'sqlserver://':SQLSERVER_USER':':SQLSERVER_PASS'@':SQLSERVER_HOST'/':SQLSERVER_DB

EXEC sp_configure
'contained database authentication', 1;

Expand All @@ -18,21 +8,36 @@ DROP LOGIN :NAME;
DROP DATABASE :NAME;

CREATE DATABASE :NAME
containment=partial;

\connect 'sqlserver://':SQLSERVER_USER':':SQLSERVER_PASS'@':SQLSERVER_HOST'/':SQLSERVER_DB

CREATE LOGIN :NAME
WITH
password=:'PASS',
check_policy=off,
default_database=:NAME;

CREATE USER :NAME
FOR login :NAME
WITH default_schema=:NAME;

CREATE SCHEMA :NAME authorization :NAME;

EXEC sp_addrolemember
'db_owner', :'NAME';
CONTAINMENT=PARTIAL;

\set QNAME "''":NAME"''"

\set SQL 'CREATE LOGIN ':NAME' WITH PASSWORD=':QNAME', CHECK_POLICY=OFF, DEFAULT_DATABASE=':NAME';'
EXEC [:NAME].[dbo].[sp_executesql] N:'SQL'

\set SQL 'CREATE USER ':NAME' FOR LOGIN ':NAME' WITH DEFAULT_SCHEMA=':NAME';'
EXEC [:NAME].[dbo].[sp_executesql] N:'SQL';

\set SQL 'CREATE SCHEMA ':NAME' AUTHORIZATION ':NAME';'
EXEC [:NAME].[dbo].[sp_executesql] N:'SQL';

\set SQL 'EXEC sp_addrolemember db_owner, ':QNAME';'
EXEC [:NAME].[dbo].[sp_executesql] N:'SQL';

-- original reconnect version:
--
--\connect 'sqlserver://localhost/':NAME
--
--CREATE LOGIN :NAME
-- WITH
-- PASSWORD=:'PASS',
-- CHECK_POLICY=OFF,
-- DEFAULT_DATABASE=:NAME;
--
--CREATE USER :NAME
-- FOR LOGIN :NAME
-- WITH DEFAULT_SCHEMA=:NAME;
--
--CREATE SCHEMA :NAME AUTHORIZATION :NAME;
--
--EXEC sp_addrolemember 'db_owner', :'NAME';

0 comments on commit d499701

Please sign in to comment.