Author : beriba | 11/07/2013

This post should have landed here long time ago. It haven’t because I didn’t have enough time to prepare it.

Some time ago I was testing Windows Azure combined with PHP. There were some tools and tutorials around the web. Tutorials were not really up to date. They were saying that development could be done both on windows or linux. And it’s almost true… almost. If you’ll combine it with continuous deployment from git repository, making your „hello world” is just a pleasure. But if you want to do something more, let’s say, add some database to your application, it’s getting tricky. You can develop it only on windows because sqlsrv drivers are out of date and they’re not working on linux. And there are more tricky moments.

One of them shows up when you want to get messages from the queue. Everything is normal when you want to peek 2, 10 or 20 messages. But when you want to peek more than 31 messages there is an unexpected fail. Below code is an example which triggers this error.

$message_options = new PeekMessagesOptions();
$message_options->setNumberOfMessages(32);

An error is saying what happened but that’s all.

400: Fail: Code: 400 Value: One of the query parameters specified in the 
request URI is outside the permissible range. details (if any): 
ďťżOutOfRangeQueryParameterValueOne of the query parameters specified in 
the request URI is outside the permissible range. 
RequestId:2e5ed254-51cf-42f4-9efe-3dc26e5ea902 
Time:2013-05-24T09:55:56.6088493Znumofmessages33132.

I’m a developer for a long time and I don’t expect any error to tell me every detail. And I’m aware that every technology has it’s limitations but let’s face it – possibility to peek just 31 messages? In cloud? Not good. Instead of saying the same thing twice, there could be some explanation in error message.
I still don’t know why this limit is so low but I don’t want to know. I hope this will be changed.

But there is one thing that I loved about Windows Azure. It’s the best admin panel (or whatever it is called) I’ve ever used. Everything is plain, simple and beautifully animated.

Author : beriba | 09/02/2013

Have you ever had too many GIT branches? Have you ever looked for a way to remove half of them or even all? If „Yes” is an answer for any of those two questions, this tip is for you. Probably you’ll have about 3 questions about removing git branches:
1. Is that possible? Yes
2. Is it built-in? No
3. How to do that? Look below

git branch -D `git for-each-ref --format="%(refname:short)" refs/heads/bugs_\*`

A little bit of explaination. Above command will remove permanently (without asking!) any branch that starts with „bugs_” (without quotes of course). You have to put backslash before * (asterisk), ? (question mark) and probably before few more characters. If you don’t know what is „git for-each-ref” feel free to ask me in a comment.

Recently I had to check how big the directory is. But not in the obvious way. The obvious way would be

du -h /tmp

The above command will output size of the catalog in human readable way (-h param) – in kB, MB, GB and so on. This is easy. But what if you want to check the size only for certain filetype within specified directory? It’s getting a little bit harder. To do that I prepared a command, the big one. Actually it’s not one command but five commands giving output to each other. It goes like this:

find /tmp -name '*.zip' 
    | xargs du 
    | awk '{sum[$3]+= $1;}END{for (s in sum){print sum[s], s;}}' 
    | sort -k2 
    | column -t

First command looks for all *.zip files in /tmp directory. Of course you can specify any rule which is acceptable by find command (search for „man find” in your favourite search engine) especially another filetype. Second command takes all found files and returns their sizes in bytes. Third command sums all of those sizes using awk (read more about it, it’s really powerful). Then, there is sorting by second column (indexes in -k argument goes from 1, not from 0) and after that there’s some pretty-printing. All you need to change (path and file type) is in the first command. If you need more explanation or some help, please leave a comment.

Enjoy 🙂

Author : beriba | 05/13/2013

By default vim editor doesn’t show line numbers. To change that behaviour you just have to do few very simple steps:

  • create file .vimrc in your home directory
  • touch ~/.vimrc
  • add below line to the above file
  • :set number

Save the file and voila. Any time you open vim, you will see line numbers.

I’ve prepared something very useful for you today. Setting a default value. For any relational databases it is very common practice to set some default value if user doesn’t specify any value. For example if we have boolean field and we don’t set default value, in the records we will find one of those values: true, false, null. And there is a question – what does null mean for that column? Is it the same thing as false? Or should we handle nulls in some other way? If you don’t mean to have 3 types of values you should set the default value. This practice is also useful when in the beginning this field will always have the same value an it will be changed any time later.
Database is just one of the layers. When you are using a framework like Symfony 2 you have other layer which is ORM. In this particular case it is Doctrine 2. You should also set the default value there. You can do it by adding a row

"default" = 0

in options argument when declaring an ORM column. For example, it can look like this:

@ORM\Column(
    name="foo",
    type="decimal",
    precision=7,
    scale=2,
    options={"default" = 0}
)
Author : beriba | 03/10/2013

Vim is very powerful tool. But first you have to discover its secrets. Here is one of them – duplicating one line or many lines using just a keyboard. Yeah, that’s possible and very very useful. You just need to follow few simple steps:
1. Navigate to any place in the line you want to duplicate
2. Press yy
3. Navigate to any other line (or stay in the same line)
4. Press p to paste copied line below the line you’re in or P to paste it above

Quite simple, don’t you think? But what’s when you want to copy more than one line? Copying and pasting it line by line is not a great idea. There’s a command to do that. In step 2 instead of pressing yy you have to press 2Y. In this case the two lines will be copied – the line you’re in and the line below. After pasting, the additional blank line will be added above first copied line. You can copy as many lines as you wish. It could be 6Y, 10Y or 56Y.

Enjoy 🙂

Author : beriba | 02/24/2013

Fast and quick post today. If you need substring in C you can do it using this function. In the main() function you have example of usage. If you have comments, feel free to ask me.

#include <stdio.h>

char *substring(size_t start, size_t stop, const char *src, char *dst, size_t size)
{
   int count = stop - start;
   if ( count >= --size )
   {
      count = size;
   }
   sprintf(dst, "%.*s", count, src + start);
   return dst;
}

int main()
{
   static const char text[] = "The quick brown fox jumps over the lazy dog.";
   char a[10], b[5];
   printf("substring = \"%s\"\n", substring(4, 13, text, a, sizeof a)); /* substring = "quick bro" */
   printf("substring = \"%s\"\n", substring(4, 13, text, b, sizeof b)); /* substring = "quic" */
   return 0;
}
Author : beriba | 12/01/2012

Quick solution to a problem that took me a while to resolve. It’s very simple but I couldn’t find the solution just by asking uncle google. In my case I was trying to execute

php app/console doctrine:schema:update --dump-sql

and it responded me with big red exception

[Doctrine\DBAL\DBALException]
Unknown column type "json" requested. Any Doctrine type that you use has to
be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list
of all the known types with \Doctrine\DBAL\Types\Type::getTypeMap(). If this 
error occurs during database introspection then you might have forgot to 
register all database types for a Doctrine Type. 
Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types 
implement Type#getMappedDatabaseTypes(). If the type name is empty you might 
have a problem with the cache or forgot some mapping information.

First thing to do in that case is to install Sonata Doctrine Extensions in your symfony. There are three steps to do this:
1. Add those lines to file deps in symfony root directory:

[SonataDoctrineExtensions]
    git=https://github.com/sonata-project/sonata-doctrine-extensions.git
    target=/bundles/Sonata/DoctrineExtensions

2. Execute command:

php bin/vendors install

3. Change app/autoload.php file. Your Sonata section shoud look like this

// app/autoload.php
//...
'Sonata' => array(
                __DIR__.'/../vendor/bundles',
                __DIR__.'/../vendor/bundles/Sonata/DoctrineExtensions/src'
            ),
//...

This will install Sonata Doctrine Extensions.

Second thing to do to get rid of the main problem – check if your app/config/config.yml file look like this:

# app/config/config.yml
#...
doctrine:
    dbal:
        #...
        types: #this is about this line and line below
            json:     Sonata\Doctrine\Types\JsonType
#...

This should do the work.

Author : beriba | 10/13/2012

To install phpmyadmin just execute this command:

sudo apt-get install phpmyadmin

Then you need to go to

http://localhost/phpmyadmin


PhpMyAdmin should now work. But if the above url gives you an error:

Not Found
The requested URL /phpmyadmin was not found on this server.

you need to check two things.
1. Open file:

/etc/phpmyadmin/apache.conf

and look for alias /phpmyadmin. It should look like this

Alias /phpmyadmin /usr/share/phpmyadmin

2. Check directory:

/etc/apache2/conf.d

There should be a file phpmyadmin.conf which is a symlink to

/etc/phpmyadmin/apache.conf

If it’s not there, create it with this command:

sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf.d/phpmyadmin.conf

After this, you just need to restart the server and enjoy your phpmyadmin installation.

Author : beriba | 10/11/2012

This will be just a quick tip. If you want to change device/partition label in linux you just have to execute this command

sudo e2label <device> <label>

For example:

sudo e2label /dev/sda1 projects

Easy.