dacpacentrypoint.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. sqlcmd="/opt/mssql-tools/bin/sqlcmd"
  2. if [ -z "$1" ]; then
  3. echo "Usage: docker-compose run dbutils function"
  4. echo " where function = 'gps-databases' for loading Databases. Requires second parameter with databases and optionally a branch"
  5. echo " gps.database.dataaccesslayer GPS.Database.DataAccessLayer for Data Access Layer and optionally a branch"
  6. echo " 'seed' 'Database' for seeding data"
  7. echo " 'bash' for shell"
  8. echo " shrink db to shrink database"
  9. elif [ "$1" = "bash" ]; then
  10. bash
  11. elif [ "$1" = "args" ]; then
  12. echo "Got to args"
  13. sleep 10
  14. $sqlcmd -S "${HOST}" -U sa -P $SA_PASSWORD -Q "
  15. IF NOT EXISTS (select loginname from master.dbo.syslogins where name = 'gpspl')
  16. BEGIN
  17. CREATE LOGIN gpspl WITH PASSWORD='${SA_PASSWORD}', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF;
  18. ALTER SERVER ROLE sysadmin ADD MEMBER gpspl;
  19. END;
  20. IF NOT EXISTS (select loginname from master.dbo.syslogins where name = 'usrSPCaller')
  21. BEGIN
  22. CREATE LOGIN usrSPCaller WITH PASSWORD=N'1qazxsw23EDC';
  23. END; "
  24. for args in ${!DACPAC*}
  25. do
  26. arg=${!args}
  27. bash /entrypoint.sh $arg
  28. done
  29. elif [ "$1" = "gps-databases" ] || [ "$1" = "gps.clearing" ] || [ "$1" = "gps.database.dataaccesslayer" ] || [ "$1" = "gps.databases.alexissecure" ] ; then
  30. if [ -z "$2" ]; then
  31. echo "Usage: docker-compose run dbutils function"
  32. echo " where function = 'gps-databases' for loading Databases. Requires second parameter with databases and optionally a branch"
  33. echo " gps.database.dataaccesslayer GPS.Database.DataAccessLayer for Data Access Layer and optionally a branch"
  34. echo " 'seed' 'Database' for seeding data"
  35. echo " 'bash' for shell"
  36. exit 1
  37. fi
  38. echo "Performing $2 migration"
  39. branch="develop"
  40. if [ ! -z $3 ]; then
  41. branch=$3
  42. fi
  43. if [ ! -d $1 ]; then
  44. git clone git@bitbucket.org:globalprocessing/$1.git --single-branch --branch $branch
  45. fi
  46. DBs="$2"
  47. for db in ${DBs//,/ }
  48. do
  49. if [ "$1" = "gps.clearing" ]; then
  50. cd /root/$1/Databases/$db
  51. else
  52. cd /root/$1/$db
  53. fi
  54. bash /tmp/altersqlproj.sh
  55. sqlproj=`basename *$db*.sqlproj .sqlproj`
  56. echo "Building dacpac for $sqlproj"
  57. dotnet build $sqlproj.sqlproj /p:NetCoreBuild=true /p:NETCoreTargetsPath="/root/.azuredatastudio/extensions/microsoft.sql-database-projects-0.12.0/BuildDirectory" /p:Configuration=Release /p:OutputPath=/dacpac /m:2 /clp:ErrorsOnly
  58. tdn=$db
  59. if [ "$db" = "Alexis" ] || [ "$db" = "GPS.Database.DataAccessLayer.Tier1" ] || [ "$db" = "NGA_Processing_Alexis" ]; then
  60. tdn="AlexisPTSTest"
  61. elif [ "$db" = "AlexisToken" ] ; then
  62. tdn="AlexisPTSTokenTest"
  63. elif [ "$db" = "GPS.Database.DataAccessLayer.Tier2" ] ; then
  64. tdn="Tier2"
  65. fi
  66. mkdir /tmp/$db
  67. pr=""
  68. if [ -f PublishProfiles/${db}AWS.publish.xml ]; then
  69. pr="/pr:PublishProfiles/${db}AWS.publish.xml"
  70. fi
  71. dacpac=/dacpac/$sqlproj.dacpac
  72. /opt/sqlpackage/sqlpackage /a:Script /tsn:"${HOST}" /tdn:$tdn /tu:sa /tp:$SA_PASSWORD /sf:$dacpac /op:/tmp/$db/$db.sql $pr
  73. sed -i -e 's/^:on error exit/:on error ignore/' -e 's/\[$(LoggingServer)\].//' -e 's/\[$(SupplementalData)\].//' /tmp/$db/$db.sql
  74. # unzip -d /tmp/$db $dacpac predeploy.sql
  75. # if [ -f /tmp/$db/predeploy.sql ]; then
  76. # $sqlcmd -S "${HOST}" -U sa -P $SA_PASSWORD -d $tdn -i /tmp/$db/predeploy.sql
  77. # fi
  78. $sqlcmd -S "${HOST}" -U gpspl -P $SA_PASSWORD -i /tmp/$db/$db.sql
  79. # unzip -d /tmp/$db $dacpac postdeploy.sql
  80. # if [ -f /tmp/$db/postdeploy.sql ]; then
  81. # $sqlcmd -S "${HOST}" -U sa -P $SA_PASSWORD -d $tdn -i /tmp/$db/postdeploy.sql
  82. # fi
  83. rm -rf /tmp/$db
  84. if [ "$db" = "AlexisToken" ] ; then
  85. $sqlcmd -S "${HOST}" -U gpspl -P $SA_PASSWORD -d AlexisPTSTokenTest -Q "
  86. ALTER Procedure [dbo].[PR_GetKeyData] (@Path Int)
  87. AS
  88. BEGIN
  89. DECLARE @file_contents NVARCHAR(4000)
  90. If @Path = 1
  91. SELECT @file_contents = N'+UAdKPLjoSG0lHPnBLVOZoCycNc2gBI/MY/M2HGI1qK5Q1hTJ5enLJKhKBrU8/Xa'
  92. Else If @Path = 2
  93. Begin
  94. SELECT @file_contents = N'17CC97140E479DF48CF3FEF88ADE4F8E'
  95. End
  96. SELECT @file_contents Data
  97. End"
  98. $sqlcmd -S "${HOST}" -U gpspl -P $SA_PASSWORD -d AlexisPTSTokenTest -Q "
  99. ALTER Procedure [dbo].[PR_GetEncryKey]
  100. @filecontents varchar(200) OUTPUT
  101. AS
  102. BEGIN
  103. SELECT @filecontents = N'17CC97140E479DF48CF3FEF88ADE4F8E'
  104. End"
  105. fi
  106. done
  107. elif [ "$1" = "seed" ]; then
  108. for file in /SeedData/$2/*.sql
  109. do
  110. echo "Processing $file file.."
  111. $sqlcmd -S "${HOST}" -U gpspl -P $SA_PASSWORD -d $2 -i $file
  112. done
  113. elif [ "$1" = "bcpdb" ]; then
  114. AlexisTables="ProductType,CurrencyMaster,Cards,Transaction,Transaction_Int"
  115. bcpdb=$2
  116. if [ $# -gt 1 ]; then
  117. AlexisTables=$3
  118. fi
  119. bcpdir=$BCPDIR
  120. for table in ${AlexisTables//,/ }
  121. do
  122. if [ -d "/bcp" ]; then
  123. bcpdir=/bcp
  124. else
  125. /opt/mssql-tools/bin/bcp $bcpdb.dbo.[${table}] format nul -f $bcpdir/${table}.fmt -n -S $DATA_SERVER -U gpspl -P $SA_PASSWORD
  126. /opt/mssql-tools/bin/bcp $bcpdb.dbo.[${table}] out $bcpdir/${table}.bcp -n -S $DATA_SERVER -U gpspl -P $SA_PASSWORD
  127. fi
  128. echo "Bulk Copying $bcpdir/${table}.bcp file.."
  129. /opt/mssql-tools/bin/bcp $bcpdb.dbo.[${table}] in $bcpdir/${table}.bcp -f $bcpdir/${table}.fmt -E -b 50000 -S "${HOST}" -U gpspl -P $SA_PASSWORD
  130. done
  131. elif [ "$1" = "bcp" ]; then
  132. AlexisTables="ProductType,CurrencyMaster,Cards,Transaction,Transaction_Int"
  133. bcpdb="AlexisPTSTest"
  134. if [ $# -gt 1 ]; then
  135. AlexisTables=$2
  136. fi
  137. for table in ${AlexisTables//,/ }
  138. do
  139. /opt/mssql-tools/bin/bcp AlexisPTSTest.dbo.[${table}] format nul -f /tmp/${table}.fmt -n -S $DATA_SERVER -U gpspl -P yW2S6KXdY8
  140. /opt/mssql-tools/bin/bcp AlexisPTSTest.dbo.[${table}] out /tmp/${table}.bcp -n -S $DATA_SERVER -U gpspl -P yW2S6KXdY8
  141. /opt/mssql-tools/bin/bcp AlexisPTSTest.dbo.[${table}] in /tmp/${table}.bcp -f /tmp/${table}.fmt -E -b 50000 -S "${HOST}" -U gpspl -P $SA_PASSWORD
  142. done
  143. /opt/mssql-tools/bin/sqlcmd -S "${HOST}" -U gpspl -P $SA_PASSWORD -Q "
  144. ALTER TABLE AlexisPTSTest.dbo.Transaction_Int NOCHECK CONSTRAINT FK_Transaction__payment_token_id;
  145. UPDATE AlexisPTSTest.dbo.[Transaction]
  146. SET TransDate = DATEADD(year, 1, TransDate);
  147. SET IDENTITY_INSERT AlexisPTSTest.dbo.[Transaction_Int] ON;
  148. INSERT INTO AlexisPTSTest.dbo.[Transaction_Int] ([ID], [PAN], [SysDate], [TransDate], [TransCcy], [BillAmt], [Description], [OldStatus], [NewStatus], [TransactionType], [RecordID], [TransactionStatus], [TransAmt], [BillNat_Conv], [Actual_Balance], [Blocked_Amount], [flCredit], [ProcessingCode], [AuthMethod], [LoadSRC], [LoadType], [OrigStan], [Note], [Amount_other], [Fixed_Fee], [Rate_Fee], [Fx_Padding], [Mcc_Padding], [AuthPresDiff], [txnCountry], [FeeID], [Trans_Link], [SettlementAmt], [Additional_Data], [BillCcy], [SettleCcy], [TxnTypeProcCode], [Account_From], [Account_To], [traceid_lifecycle], [payment_token_id])
  149. SELECT * FROM AlexisPTSTest.dbo.[Transaction] WHERE transDate < '2020-01-01';
  150. SET IDENTITY_INSERT AlexisPTSTest.dbo.[Transaction_Int] OFF;
  151. DELETE FROM AlexisPTSTest.dbo.[Transaction] WHERE transDate < '2020-01-01';
  152. USE AlexisPTSTest
  153. ALTER DATABASE AlexisPTSTest SET RECOVERY SIMPLE
  154. DBCC SHRINKFILE (AlexisPTSTest, 1);
  155. DBCC SHRINKFILE (AlexisPTSTest_Log, 1);
  156. ALTER DATABASE AlexisPTSTest SET RECOVERY FULL;"
  157. elif [ "$1" = "bcpdir" ]; then
  158. for dir in /bcp/*/
  159. do
  160. dbname=`basename $dir`
  161. for file in /bcp/$dbname/*.bcp
  162. do
  163. table=`basename $file .bcp`
  164. echo "Processing $table table.."
  165. /opt/mssql-tools/bin/bcp $dbname.${table} in /bcp/$dbname/${table}.bcp -f /bcp/$dbname/${table}.fmt -q -E -b 500000 -S "${HOST}" -U gpspl -P $SA_PASSWORD
  166. done
  167. done
  168. elif [ "$1" = "shrink" ]; then
  169. /opt/mssql-tools/bin/sqlcmd -S "${HOST}" -U gpspl -P $SA_PASSWORD -Q "
  170. USE ${2}
  171. ALTER DATABASE ${2} SET RECOVERY SIMPLE
  172. DBCC SHRINKFILE (${2}, 1);
  173. DBCC SHRINKFILE (${2}_Log, 1);
  174. ALTER DATABASE ${2} SET RECOVERY FULL;"
  175. else
  176. echo "Usage: docker-compose run dbutils function"
  177. echo " where function = 'gps-databases' for loading Databases. Requires second parameter with databases and optionally a branch"
  178. echo " gps.database.dataaccesslayer GPS.Database.DataAccessLayer for Data Access Layer and optionally a branch"
  179. echo " 'seed' 'Database' for seeding data"
  180. echo " 'bash' for shell"
  181. echo " shrink db to shrink database"
  182. fi