RestoreData.ps1 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. $tables = @("ProductType","CurrencyMaster", "Cards", "Transaction", "Transaction_Int", "Payment_Token")
  2. $initCmd = @("ALTER TABLE AlexisPTSTest.dbo.AUTH_IMPLIED_LIMIT NOCHECK CONSTRAINT all; ")
  3. $initCmd += "ALTER TABLE AlexisPTSTest.dbo.Payment_Token NOCHECK CONSTRAINT all; "
  4. $initCmd += "ALTER TABLE AlexisPTSTest.dbo.TxnAccumulator NOCHECK CONSTRAINT all; "
  5. $initCmd += "ALTER TABLE AlexisPTSTest.dbo.BNK_Banking_ModulrDetails NOCHECK CONSTRAINT all; "
  6. $initCmd += "ALTER TABLE AlexisPTSTest.dbo.BNK_BankAccountNumbers NOCHECK CONSTRAINT all; "
  7. $initCmd += "ALTER TABLE AlexisPTSTest.dbo.AccumulatorProductLimit NOCHECK CONSTRAINT all; "
  8. $initCmd += "ALTER DATABASE AlexisPTSTest SET RECOVERY SIMPLE; "
  9. $finalCmd = @()
  10. for ($i=0; $i -lt $tables.length; $i++) {
  11. $table = $tables[$i]
  12. $initCmd += "DECLARE @Deleted_Rows INT; SET @Deleted_Rows = 1; WHILE (@Deleted_Rows > 0) BEGIN DELETE TOP (100000) FROM AlexisPTSTest.dbo.[${table}]; SET @Deleted_Rows = @@ROWCOUNT; END "
  13. $initCmd += "SET IDENTITY_INSERT AlexisPTSTest.dbo.[${table}] ON; "
  14. $finalCmd += "SET IDENTITY_INSERT AlexisPTSTest.dbo.[${table}] OFF; "
  15. }
  16. $finalCmd +=
  17. "SET IDENTITY_INSERT AlexisPTSTest.dbo.[Transaction] ON;
  18. DELETE FROM AlexisPTSTest.dbo.[Transaction_Int] WHERE EXISTS (SELECT 1 FROM AlexisPTSTest.dbo.[Transaction] t WHERE t.Id = AlexisPTSTest.dbo.[Transaction_Int].Id);
  19. INSERT INTO AlexisPTSTest.dbo.[Transaction] ([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])
  20. SELECT * FROM AlexisPTSTest.dbo.[Transaction_Int] WHERE transDate > '2020-01-01';
  21. DELETE FROM AlexisPTSTest.dbo.[Transaction_Int] WHERE transDate > '2020-01-01';
  22. SET IDENTITY_INSERT AlexisPTSTest.dbo.[Transaction] OFF;"
  23. $finalCmd +=
  24. "USE AlexisPTSTest;
  25. DBCC SHRINKFILE (Alexis, 1);
  26. DBCC SHRINKFILE (Alexis_log, 1);
  27. ALTER DATABASE AlexisPTSTest SET RECOVERY FULL;"
  28. for ($i=0; $i -lt $initCmd.Count; $i++) {
  29. $cmd = $initCmd[$i]
  30. Write-Host $cmd
  31. sqlcmd -S localhost -U gpspl -P yW2S6KXdY8 -Q "${cmd}"
  32. }
  33. for ($i=0; $i -lt $tables.length; $i++) {
  34. $table = $tables[$i]
  35. $bcpIn = "AlexisPTSTest.dbo.[${table}] in ${table}.bcp -f ${table}.fmt -e ${table}.log -E -b 50000 -S localhost -U gpspl -P yW2S6KXdY8"
  36. Start-Process bcp -ArgumentList $bcpIn -Wait
  37. }
  38. for ($i=0; $i -lt $finalCmd.Count; $i++) {
  39. $cmd = $finalCmd[$i]
  40. Write-Host $cmd
  41. sqlcmd -S localhost -U gpspl -P yW2S6KXdY8 -Q "${cmd}"
  42. }