php-sqlsrv/test/setup/exec_sql_scripts.py
ulvii 41af6fc0a2 Database setup (#367)
* porting database setup files for functional tests

* Updating yml files to setup test databases before running the tests

* update spacing

* Renaming env variables

* Modifying cleanup_dbs.py and setup_dbs.py scripts so that they can execute database setup files

* update env variables

* Fix typo

* Update .travis.yml

* update env variables

* update travis scripts

* Checking sqlcmd setup on travis

* Checking sqlcmd setup on travis

* sqlcmd bcp fix

* sqlcmd bcp fix

* sqlcmd bcp fix

* sqlcmd bcp fix

* sqlcmd bcp fix

* sqlcmd bcp fix

* sqlcmd bcp fix

* sqlcmd bcp fix

* sqlcmd bcp fix

* Adding a test to make sure the database is setup properly

* update exec_sql_scripts.py

* update sqlsrv_num_fields.phpt

* update sqlsrv_num_fields.phpt

* update

* Fixing identation
2017-05-01 11:20:53 -07:00

37 lines
1.4 KiB
Python

#!/usr/bin/env python3
# contains helper methods
import os
import sys
import subprocess
import platform
import argparse
from subprocess import Popen, PIPE
def executeCommmand(inst_command):
proc = subprocess.Popen(inst_command , stdout=PIPE, stderr= PIPE, shell=True)
print ( inst_command )
oo,ee = proc.communicate()
print (ee)
print (oo)
def executeSQLscript(sqlfile, conn_options, dbname):
if platform.system() == 'Windows':
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)
def executeSQLscriptUnix(sqlfile, conn_options, dbname):
# This is a workaround because sqlcmd in Unix does not support -v option for variables.
# It inserts setvar dbname into the beginning of a temp .sql file
tmpFileName = sqlfile[0:-4] + '_tmp.sql'
redirect_string = '(echo :setvar dbname {0}) > {2}; cat {1} >> {2}; '
sqlcmd = 'sqlcmd ' + conn_options + ' -i ' + tmpFileName
inst_command = redirect_string.format(dbname, sqlfile, tmpFileName) + sqlcmd
executeCommmand(inst_command)
os.remove(tmpFileName)