Dropped dbname variable and set QUOTED_IDENTIFIER to ON (#911)

This commit is contained in:
Jenny Tam 2019-01-04 12:53:35 -08:00 committed by GitHub
parent 4efb54e45a
commit 5801edd5c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 54 additions and 104 deletions

View file

@ -1,6 +1,3 @@
USE $(dbname)
GO
IF EXISTS (SELECT * FROM sys.objects IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[168256]') AND type in (N'U')) WHERE object_id = OBJECT_ID(N'[dbo].[168256]') AND type in (N'U'))

View file

@ -1,11 +1,8 @@
USE $(dbname)
GO
IF EXISTS (SELECT * FROM sys.objects IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[tracks]') AND type in (N'U')) WHERE object_id = OBJECT_ID(N'[dbo].[tracks]') AND type in (N'U'))
BEGIN BEGIN
ALTER TABLE $(dbname)..[tracks] DROP CONSTRAINT [FK__tracks__asin__7F60ED59] ALTER TABLE [tracks] DROP CONSTRAINT [FK__tracks__asin__7F60ED59]
END END
GO GO

View file

@ -3,10 +3,8 @@
import os import os
import sys import sys
import subprocess
import platform import platform
import argparse import argparse
from subprocess import Popen, PIPE
from exec_sql_scripts import * from exec_sql_scripts import *
if __name__ == '__main__': if __name__ == '__main__':
@ -25,8 +23,9 @@ if __name__ == '__main__':
sys.exit(1) sys.exit(1)
conn_options = ' -S ' + server + ' -U ' + uid + ' -P ' + pwd + ' ' conn_options = ' -S ' + server + ' -U ' + uid + ' -P ' + pwd + ' '
executeSQLscript( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'drop_db.sql'), conn_options, args.DBNAME) sql_script = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'drop_db.sql');
manageTestDB(sql_script, conn_options, args.DBNAME)
# if Windows, remove self signed certificate using ps command # if Windows, remove self signed certificate using ps command
if platform.system() == 'Windows': if platform.system() == 'Windows':

View file

@ -1,13 +1,5 @@
USE [master] IF EXISTS (SELECT name FROM sys.databases WHERE name = 'TEST_DB' ) DROP DATABASE TEST_DB
GO
CREATE DATABASE TEST_DB
IF EXISTS (SELECT name FROM sys.databases WHERE name = '$(dbname)' ) GO
BEGIN
DROP DATABASE $(dbname)
END
CREATE DATABASE $(dbname)
GO

View file

@ -1,8 +1 @@
USE [master] IF EXISTS (SELECT name FROM sys.databases WHERE name = 'TEST_DB' ) DROP DATABASE TEST_DB
GO
IF EXISTS (SELECT name FROM sys.databases WHERE name = '$(dbname)' )
BEGIN
DROP DATABASE $(dbname)
END

View file

@ -1,10 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# contains helper methods # contains helper methods
import os import os
import sys
import subprocess import subprocess
import platform
import argparse
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
def executeCommmand(inst_command): def executeCommmand(inst_command):
@ -15,29 +12,17 @@ def executeCommmand(inst_command):
print (oo) print (oo)
def executeSQLscript(sqlfile, conn_options, dbname): def executeSQLscript(sqlfile, conn_options, dbname):
if platform.system() == 'Windows': inst_command = 'sqlcmd -I ' + conn_options + ' -i ' + sqlfile + ' -d ' + dbname
executeSQLscriptWindows(sqlfile, conn_options, dbname)
elif platform.system() == 'Linux' or platform.system() == 'Darwin':
executeSQLscriptUnix(sqlfile, conn_options, dbname)
def executeSQLscriptWindows(sqlfile, conn_options, dbname):
inst_command = 'sqlcmd ' + conn_options + ' -i ' + sqlfile + ' -v dbname =' + dbname
executeCommmand(inst_command) executeCommmand(inst_command)
def executeSQLscriptUnix(sqlfile, conn_options, dbname): def manageTestDB(sqlfile, conn_options, dbname):
# This is a workaround because sqlcmd in Unix does not support -v option for variables. tmp_sql_file = 'test_db_tmp.sql'
# It inserts setvar dbname into the beginning of a temp .sql file if os.path.exists(tmp_sql_file):
tmpFileName = sqlfile[0:-4] + '_tmp.sql' os.remove(tmp_sql_file)
redirect_string = '(echo :setvar dbname {0}) > {2}; cat {1} >> {2}; ' with open(sqlfile, 'r') as infile:
sqlcmd = 'sqlcmd ' + conn_options + ' -i ' + tmpFileName script = infile.read().replace('TEST_DB', dbname)
with open(tmp_sql_file, 'w') as outfile:
outfile.write(script)
# Execute a simple query via sqlcmd: without this step, the next step fails in travis CI executeSQLscript(tmp_sql_file, conn_options, 'master')
simple_cmd = 'sqlcmd ' + conn_options + ' -Q \"select @@Version\" ' os.remove(tmp_sql_file)
executeCommmand(simple_cmd)
# inst_command = redirect_string.format(dbname, sqlfile, tmpFileName) + sqlcmd
inst_command = redirect_string.format(dbname, sqlfile, tmpFileName)
executeCommmand(inst_command)
executeCommmand(sqlcmd)
os.remove(tmpFileName)

View file

@ -1,21 +1,15 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# py setup_dbs.py -dbname <DBNAME> -azure <yes or no> # py setup_dbs.py -dbname <DBNAME> -azure <yes or no>
# OR # OR
# py setup_dbs.py -dbname <DBNAME> # py setup_dbs.py -dbname <DBNAME>
import os import os
import sys import sys
import subprocess
import platform import platform
import argparse import argparse
from subprocess import Popen, PIPE
from exec_sql_scripts import * from exec_sql_scripts import *
def setupTestDatabase(conn_options, dbname, azure): def setupTestDatabase(conn_options, dbname, azure):
sqlFiles = ['test_types.sql', '168256.sql', 'cd_info.sql', 'tracks.sql'] sqlFiles = ['test_types.sql', '168256.sql', 'cd_info.sql', 'tracks.sql']
# for Azure, must specify the database for the sql scripts to work
if (azure.lower() == 'yes'):
conn_options += ' -d ' + dbname
for sqlFile in sqlFiles: for sqlFile in sqlFiles:
executeSQLscript(sqlFile, conn_options, dbname) executeSQLscript(sqlFile, conn_options, dbname)
@ -28,29 +22,29 @@ def populateTables(conn_options, dbname):
executeBulkCopy(conn_options, dbname, '168256', '168256') executeBulkCopy(conn_options, dbname, '168256', '168256')
def executeBulkCopy(conn_options, dbname, tblname, datafile): def executeBulkCopy(conn_options, dbname, tblname, datafile):
redirect_string = 'bcp {0}..[{1}] in {2}.dat -f {2}.fmt ' redirect_string = 'bcp {0}..{1} in {2}.dat -f {2}.fmt -q'
inst_command = redirect_string.format(dbname, tblname, datafile) + conn_options inst_command = redirect_string.format(dbname, tblname, datafile) + conn_options
executeCommmand(inst_command) executeCommmand(inst_command)
def setupAE(conn_options, dbname): def setupAE(conn_options, dbname):
if (platform.system() == 'Windows'): if (platform.system() == 'Windows'):
# import self signed certificate # import self signed certificate
inst_command = "certutil -user -p '' -importPFX My PHPcert.pfx NoRoot" inst_command = "certutil -user -p '' -importPFX My PHPcert.pfx NoRoot"
executeCommmand(inst_command) executeCommmand(inst_command)
# create Column Master Key and Column Encryption Key # create Column Master Key and Column Encryption Key
script_command = 'sqlcmd ' + conn_options + ' -i ae_keys.sql -d ' + dbname script_command = 'sqlcmd -I ' + conn_options + ' -i ae_keys.sql -d ' + dbname
executeCommmand(script_command) executeCommmand(script_command)
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-dbname', '--DBNAME', required=True) parser.add_argument('-dbname', '--DBNAME', required=True)
parser.add_argument('-azure', '--AZURE', required=False, default='no') parser.add_argument('-azure', '--AZURE', required=False, default='no')
args = parser.parse_args() args = parser.parse_args()
try: try:
server = os.environ['TEST_PHP_SQL_SERVER'] server = os.environ['TEST_PHP_SQL_SERVER']
uid = os.environ['TEST_PHP_SQL_UID'] uid = os.environ['TEST_PHP_SQL_UID']
pwd = os.environ['TEST_PHP_SQL_PWD'] pwd = os.environ['TEST_PHP_SQL_PWD']
except : except :
print("TEST_PHP_SQL_SERVER environment variable must be set to the name of the server to use") print("TEST_PHP_SQL_SERVER environment variable must be set to the name of the server to use")
print("TEST_PHP_SQL_UID environment variable must be set to the name of the user to authenticate with") print("TEST_PHP_SQL_UID environment variable must be set to the name of the user to authenticate with")
@ -59,18 +53,17 @@ if __name__ == '__main__':
current_working_dir=os.getcwd() current_working_dir=os.getcwd()
os.chdir(os.path.dirname(os.path.realpath(__file__))) os.chdir(os.path.dirname(os.path.realpath(__file__)))
conn_options = ' -S ' + server + ' -U ' + uid + ' -P ' + pwd + ' ' conn_options = ' -S ' + server + ' -U ' + uid + ' -P ' + pwd + ' '
# In Azure, assume an empty test database has been created using Azure portal # In Azure, assume an empty test database has been created using Azure portal
if (args.AZURE.lower() == 'no'): if (args.AZURE.lower() == 'no'):
executeSQLscript('create_db.sql', conn_options, args.DBNAME) manageTestDB('create_db.sql', conn_options, args.DBNAME)
# create tables in the new database # create tables in the new database
setupTestDatabase(conn_options, args.DBNAME, args.AZURE) setupTestDatabase(conn_options, args.DBNAME, args.AZURE)
# populate these tables # populate these tables
populateTables(conn_options, args.DBNAME) populateTables(conn_options, args.DBNAME)
# setup AE (certificate, column master key and column encryption key) # setup AE (certificate, column master key and column encryption key)
setupAE(conn_options, args.DBNAME) setupAE(conn_options, args.DBNAME)
os.chdir(current_working_dir) os.chdir(current_working_dir)

View file

@ -1,28 +1,25 @@
USE $(dbname)
GO
CREATE TABLE [test_types] ([bigint_type] BIGINT null, CREATE TABLE [test_types] ([bigint_type] BIGINT null,
[int_type] INT null, [int_type] INT null,
[smallint_type] SMALLINT null, [smallint_type] SMALLINT null,
[tinyint_type] TINYINT null, [tinyint_type] TINYINT null,
[bit_type] BIT null, [bit_type] BIT null,
[decimal_type] DECIMAL(38,0) null, [decimal_type] DECIMAL(38,0) null,
[money_type] MONEY null, [money_type] MONEY null,
[smallmoney_type] SMALLMONEY null, [smallmoney_type] SMALLMONEY null,
[float_type] FLOAT(53) null, [float_type] FLOAT(53) null,
[real_type] REAL null, [real_type] REAL null,
[datetime_type] DATETIME null, [datetime_type] DATETIME null,
[smalldatetime_type] SMALLDATETIME null ); [smalldatetime_type] SMALLDATETIME null );
GO GO
-- maximum test -- maximum test
INSERT INTO $(dbname)..[test_types] (bigint_type, int_type, smallint_type, tinyint_type, bit_type, decimal_type, datetime_type, money_type, smallmoney_type, float_type, real_type) INSERT INTO [test_types] (bigint_type, int_type, smallint_type, tinyint_type, bit_type, decimal_type, datetime_type, money_type, smallmoney_type, float_type, real_type)
VALUES (9223372036854775807, 2147483647, 32767, 255, 1, 9999999999999999999999999999999999999, '12/12/1968 16:20', 922337203685477.5807, 214748.3647, 1.79E+308, 1.18E-38 ) VALUES (9223372036854775807, 2147483647, 32767, 255, 1, 9999999999999999999999999999999999999, '12/12/1968 16:20', 922337203685477.5807, 214748.3647, 1.79E+308, 1.18E-38 )
-- minimum test -- minimum test
INSERT INTO $(dbname)..[test_types] (bigint_type, int_type, smallint_type, tinyint_type, bit_type, decimal_type, datetime_type, money_type, smallmoney_type, float_type, real_type) INSERT INTO [test_types] (bigint_type, int_type, smallint_type, tinyint_type, bit_type, decimal_type, datetime_type, money_type, smallmoney_type, float_type, real_type)
VALUES (-9223372036854775808, -2147483648, -32768, 0, 0, -10000000000000000000000000000000000001,'12/12/1968 16:20', -922337203685477.5808, -214748.3648, -1.79E+308, -1.18E-38 ) VALUES (-9223372036854775808, -2147483648, -32768, 0, 0, -10000000000000000000000000000000000001,'12/12/1968 16:20', -922337203685477.5808, -214748.3648, -1.79E+308, -1.18E-38 )
-- zero test -- zero test
INSERT INTO $(dbname)..[test_types] (bigint_type, int_type, smallint_type, tinyint_type, bit_type, decimal_type, datetime_type, money_type, smallmoney_type, float_type, real_type) INSERT INTO [test_types] (bigint_type, int_type, smallint_type, tinyint_type, bit_type, decimal_type, datetime_type, money_type, smallmoney_type, float_type, real_type)
VALUES (0, 0, 0, 0, 0, 0, '12/12/1968 16:20', 0, 0, 0, 0) VALUES (0, 0, 0, 0, 0, 0, '12/12/1968 16:20', 0, 0, 0, 0)
GO GO

View file

@ -1,6 +1,3 @@
USE $(dbname)
GO
IF EXISTS (SELECT * FROM sys.objects IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[tracks]') AND type in (N'U')) WHERE object_id = OBJECT_ID(N'[dbo].[tracks]') AND type in (N'U'))