Git: closing a git branch

I had spent some time trying to find a way of closing a git branch, once changes are accepted to master one.

After trying few different approaches I had decided to adopt an approach described in this answer on stackoverflow.com:


git checkout feature-branch
git pull
git checkout release-branch
git pull
git merge --no-ff feature-branch
git push
git tag -a branch-feature-branch -m "Merge feature-branch into release-branch"
git push origin branch-feature-branch
git branch -d feature-branch
git push origin :feature-branch

Github: how to update forked repository.

While working on Github with forked repositories, most of the time there is a need to update the forked one when changes were made to to original repo. As this answer on stackoverflow.com explains, this is one of the ways to achieve so:

# Add the remote, call it "upstream":

git remote add upstream git://github.com/whoever/whatever.git

# Fetch all the branches of that remote into remote-tracking branches,
# such as upstream/master:

git fetch upstream

# Make sure that you're on your master branch:

git checkout master

# Rewrite your master branch so that any commits of yours that
# aren't already in upstream/master are replayed on top of that
# other branch:

git rebase upstream/master

If you don’t want to rewrite the history of your master branch, (for example because other people may have cloned it) then you should replace the last command with

git merge upstream/master

After doing so, push made to GitHub will need to be a “forced” push (the -f switch).

“Upserts” in Postgres.

Today I had a task, requiring me to either update the existing records in the Postgres database or insert new one if none was existing. That concept is commonly known as upsert (or merge). As there are many things which are worth to consider or at least be aware of when doing something like that, this answer on stackoverflow.com provides a rather convenient way of achieveing that one in Postgres 9.1+:

WITH new_values (id, field1, field2) AS (
  VALUES 
     (1, 'A', 'X'),
     (2, 'B', 'Y'),
     (3, 'C', 'Z')
 
),
upsert AS
( 
    UPDATE mytable m 
        SET field1 = nv.field1,
            field2 = nv.field2
    FROM new_values nv
    WHERE m.id = nv.id
    RETURNING m.*
)
INSERT INTO mytable (id, field1, field2)
SELECT id, field1, field2
FROM new_values
WHERE NOT EXISTS (SELECT 1 
                  FROM upsert up 
                  WHERE up.id = new_values.id)

Linux: sort directories by size using standard tools.

Having 120GB SSD drive in my laptop as the only drive is a really interesting experience – now I’m finally managing my folders, removing old files, cleaning the folders and dusting all the odd corners.

While doing so, the following command gives a really nice overview which folders are occupying most space:

du -h --max-depth=1 | sort -h -r | less

As it might look complicated for somebody who doesn’t have any linux experience, it’s quite easy to understand. All it does, is using three separate tools to present you the information which you require, piping data from one to the other.

Firstly, let’s use builtin command du, to show sizes of directories in current dir:

du -h --max-depth=1

-h presents the information in human readable format (22G instead of 22836892). --max-depth=1 prints the total only if given directory is at max 1 level below the current one (so, we see all folders in current dir, but not their children).

Everything then is piped to sort.

| sort -h -r

Here, -h allows to sort by human readable numbers produced by previous command and -r is just reversing the sorting (showing largest folders first).

And then, output of this command is piped to less, which allows to easily browse through it, if there is more folders present in the output than lines in the terminal.

| less

T-SQL: Dropping all connections to the database.

While looking for a way to drop all connections to SQL Server 2008 database, I’ve found this solution on StackOverflow:

USE Master
GO
 
DECLARE @dbname sysname
 
SET @dbname = 'name of database you want to drop connections from'
 
DECLARE @spid INT
SELECT @spid = MIN(spid) FROM master.dbo.sysprocesses
WHERE dbid = db_id(@dbname)
While @spid IS NOT NULL
BEGIN
        EXECUTE ('Kill ' + @spid)
        SELECT @spid = MIN(spid) FROM master.dbo.sysprocesses
        WHERE dbid = db_id(@dbname) AND spid > @spid
END

T-SQL: always think in transactions.

Today I was interviewing a possible candidate to join my team and after the usual batch of questions was finished, I’ve asked him if it was ok for me to progress with practical test, asking him to write few queries on an unknown database.

I wanted to start with something simple – basic SELECT query. After describing the internals of the database, stating that there is a table called Vehicles from which I would like to see all information, this is the query which he had written in Management Studio:

;BEGIN tran
 
SELECT top 100 * FROM Vehicles
 
ROLLBACK

As you might think that this is just basic knowledge, you would be surprised how many people which I’ve interviewed are not aware of those. There are three things which he did correctly:

  1. Least important of three items as this is more a personal opinion – he terminated all the previous statements by a semi-colon – simple thing, yet a rather good habit to have, especially if you are not separating queries into distinct files.
  2. He started new transaction, which was rolled back once the query is finished. For simple queries it might not be that important, but if you are planning to do more advanced tasks, that’s the only way to assure integrity of data in the database (and I cannot stress how important it is).
  3. He only selected first 100 rows, not knowing how big the table was. All previous candidates were selecting everything, which, of course might be enough in some cases to make some servers crawl.

T-SQL: show all objects in database.

I know that there was an object created in a database – either procedure or function. In total there was about 8000 objects existing there already. I wasn’t sure about the name and knowing only that it might contained ‘MFM’ as a part of it. The following snippet lists all objects from SQL Server database, which, after adding specific WHERE clauses was enough for me to limit the search to only a handful of objects:

 
SELECT 
  o.*, 
  s.name AS schema_name, 
  isnull(po.name, ps.name) AS object_owner, 
  isnull(po.type_desc, ps.type_desc) AS owner_type 
FROM sys.all_objects o 
INNER JOIN sys.schemas s ON o.schema_id = s.schema_id 
LEFT OUTER JOIN sys.database_principals po ON o.principal_id = po.principal_id 
LEFT OUTER JOIN sys.database_principals ps ON s.principal_id = ps.principal_ID

Python + Redis: TypeError: an integer is required.

Today, while trying to write a simple python script to work with Redis (using the redis-py client), I had stumbled across a silly, yet time consuming error.

I had written RedisConnector class, which is working as a kind of a wrapper around the official redis-py client:

import redis
 
class RedisConnector(object):
 
   def __init__(self, host='localhost', port='6379'):
      super(RedisConnector, self).__init__()
      self._host = host
      self._port = port
      self.r = False
      self.connect()
 
   def connect(self):
      self.r = redis.StrictRedis(self.host, self.port, db=0)
      if self.r != False:
         return True
      else:
         return False
As it appears to be ok, there is one and very confusing error there, which will block you from doing any communication with redis: if only you would read the documentation, you would know that self._port needs to be an integer, not a string, otherwise you will be getting this error:

TypeError: an integer is required
Now, I must admit that this message is telling you what is wrong, sadly – I think that it might have been a bit more verbose about where exactly there is an issue here.
The correct version of this snippet looks like this:

import redis
 
class RedisConnector(object):
 
   def __init__(self, host='localhost', port=6379):
      super(RedisConnector, self).__init__()
      self._host = host
      self._port = port
      self.r = False
      self.connect()
 
   def connect(self):
      self.r = redis.StrictRedis(self.host, self.port, db=0)
      if self.r != False:
         return True
      else:
         return False
Simple, yet stupid mistake which could have been avoided if only I would read the documentation.