WHAT'S NEW?
Loading...

Start coding? Choose the language you want!

Start coding? Choose the language you want! Brief with all the main languages.


Git: Perforce is our new diff / merge tool

If you are one of those noob Git users like me probably you faced this problem already. My Git does not provide a merge tool by default. I used to work with TortoiseSVN merge tool but as long as I work more time with Git, I have the need to try more commands and learn more about this amazing tool.

Let's start with the premise you have Git already installed in your computer. First of all, we'll execute a command to know which info can we get about the merge tools we have in our computer. Run this in your Git console: git mergetool --tool-help



As you can see in the previous screenshot, I already have some tools to configure Git and start merging code by using vim or tortoise. Tortoise is already there because I use it as a GUI but now we'll configure perforce to give it a go. First of all you need to download the Perforce tool from its web using the link close to P4Merge: Visual Merge Tool. After install the app, go back to the git console and run the following commands to setup Perforce as default merge tool in Git:


git config --global merge.tool p4merge


git config --global mergetool.p4merge.cmd 'p4merge.exe \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"'

As you can see in the following image now we are able to see p4merge is configured already:



Unfortunately, if we want to run the merge tool in our solution by using git mergetool we won't see it yet. You need to make a small change within your .gitconfig file. Open the file (C:\Program Files (x86)\Git\etc) and add the following lines at the end of it:

[merge]
   tool = p4merge
[mergetool]
   keepBackup = false
   prompt = false

After this step I faced an issue from p4merge telling me it was not able to open my cs classes and compare them. A workaround is to execute the two commands I posted in lines above. That solved the problem I was able to work with p4!!!


Looking forward to your comments and feedback!


SQL restore last backup from different server

A lot of times, we have the need to execute a scheduled task to perform different tasks in our databases like backups, restore db, extract or import data. There are a lot of different tools which are really powerful but for the most of the small tasks we might use the SQL console.

A few days ago I had to perform some tasks over a database throw different servers and I decided to do it using a sqlcmd process in a batch file. These are the steps I wanted to execute:

  • Copy the last backup created in server A to server B.
  • Restore the backup overwriting the database in server B.
  • Extract all the images from one table in an FTP server



Copy last backup


At this stage wee need to share the folder where the .bak files are generated in the production database (server A). That folder contains the las N bakups created each week but we just need to get the last one created.

In the following script lines we take a list with all the .bak files and copy the last one over the network. Until the copy process is not finished our scrip does not continue executing the following lines:

@echo off

:Variables
SET DatabaseBackupPath=\\DatabasePath\

echo %DATE% %TIME%: Starting scheduled task >> "log.txt"
echo %DATE% %TIME%: Copy prod database backup >> "log.txt"

FOR /F "delims=|" %%I IN ('DIR "%DatabaseBackupPath%\*.bak" /B /O:D') DO SET NewestFile=%%I
copy "%DatabaseBackupPath%\%NewestFile%" "E:\Production DB copy\" /z

Notice the "echo ... >> "log.txt" ", which is saving all the actions we take in a text file file to get a summary of the tasks performed. In every echo command we are using the %DATE%%TIME% which gets the current date and time from the server and print it in the log.txt file.

Restore backup


Now the backup is already in our test server (server B) and we are free to perform a restoration task using the database backup just copied. The following lines are consecutive to the previous example and they will replace the database with the bakup from production:

echo %DATE% %TIME%: Restore Sys Database >> "log.txt"

:sqlcmd -S DbServerName -U UserName -P Password -o "DBlog.txt" -d master -Q ^
:"RESTORE DATABASE DbName ^
:FROM DISK = N'E:\Production DB copy\%NewestFile%' WITH REPLACE"

Notice about the "WITH REPLACE" parameter which allow us to overwrite the database if there is already created.

Extract all the images


Finally to extract all the images (or any other column you need to extract). First, I used SQL Server Data Tools, which it's a really powerful tool provided from Microsoft to handle all the extra stuff you want to do with your databases. I followed the tutorial from Daniel Calbimonte which comes really good explained with images of all the steps:


Once your package is properly created you just need to create a new job in your SQL Server Agent to execute it every time you need to update the images. I faced some problems when I did this step, because the password to connect to the database is saved inside of the package and you need a way to secure it. In the following tutorial you can see an explanation of how to solve it:

http://zarez.net/?p=155

Finally, you'll just need to create a call to sql to execute the job throw our great bash console application:

echo %DATE% %TIME%: Execute 'Export images delta' job >> "log.txt"
osql -S "SQL11CTMD" -U UserName -P UserPassword -d master -Q"exec msdb.dbo.sp_start_job 'Export images delta'"



--
References