Friday, March 27, 2015

PostgreSQL: perl like join

db=# select array_to_string(array['a', 'b', 'c'], '-');                                                                     
 array_to_string 
-----------------
 a-b-c
(1 row)

PostgreSQL: date to epoch

select extract(epoch from localtimestamp(0));

Thursday, March 26, 2015

PostgreSQL: cursor example

create or replace function test_cursor() returns void as
$$
declare
    _my_cursor(p_co_id text) for
        select * from crm_db.ht_co_addon where co_id = p_co_id;
    _idx int := 0;
    _row record;
begin
    -- Example 1
    for _row in _my_cursor('000000000666') loop
        _row.co_addon_id := _idx;
        _idx := _idx + 1;
    end loop;

    -- Example 2
    open _my_cursor('000000000666')
    loop
        fetch _my_cursor into _row
        exit when not found
        raise notice 'co_addon_id=%', _row.co_addon_id
    end loop
    close _my_cursor
end;
$$ language plpgsql security definer;

PostgreSQL: hstore or a perl like hash table

create or replace function test_hstore() returns void as
$$
declare
    _my_cursor(p_co_id text) for
        select * from crm_db.ht_co_addon where co_id = p_co_id;
    _row record;
    _addon_id2co_addon_id_h hstore;
    _sms_ao text := 'SMS_AO';
begin
    for _row in _my_cursor('000000000666') loop
        if _addon_id2co_addon_id_h is null then
            _addon_id2co_addon_id_h := (_row.addon_id||'=>'||_row.co_addon_id)::hstore;
        else
            _addon_id2co_addon_id_h := _addon_id2co_addon_id_h || (_row.addon_id||'=>'||_row.co_addon_id)::hstore;
        end if;
    end loop;

    raise notice 'SMS_AO: %', _addon_id2co_addon_id_h->_sms_ao;

    for _row in select * from each(_addon_id2co_addon_id_h) loop
        raise notice 'h: %=>%', _row.key, _row.value;
    end loop;
end;
$$ language plpgsql security definer;

Friday, March 20, 2015

Linux: remove files older than x days

E.g. Remove files older than 90 days
find . -mtime +90 -exec rm {} \;

Thursday, March 19, 2015

Git merge master into feature branch

Source: http://stackoverflow.com/questions/16955980/git-merge-master-into-feature-branch

You should be able to rebase your branch on master:
git checkout feature1
git rebase master
Manage all conflicts that arise. When you get to the commits with the bugfixes (already in master), git will say that there were no changes and that maybe they were already applied. You then continue the rebase (while skipping the commits already in master) with
git rebase --skip
If you perform a git log on your feature branch, you'll see the bugfix commit appear only once, and in the master portion.

git: change remote repository

Context: I have used bitbucket as a remote git repository so far, and now I want to switch to a corporate git server.

Following the instructions detailled here: http://www.smashingmagazine.com/2014/05/19/moving-git-repository-new-server/
Here are the steps eventually run:
git fetch origin
git remote add new-origin ssh://john.smith@192.168.2.3/volume1/git-server/is.git
git pull new-origin master
git push --all new-origin
git push --tags new-origin
git remote rm origin
git remote rename new-origin origin

Tuesday, March 17, 2015

Git: Sync remote repository w/ local

From http://stackoverflow.com/questions/6373277/git-sync-local-repo-with-remote-one
cd $HOME/src
git pull -p
NB: git pull runs git fetch with the given parameters and calls git merge to merge the retrieved branch heads into the current branch - git-scm.com/docs/git-pull

-p, --prune After fetching, remove any remote-tracking branches which no longer exist on the remote

Thursday, March 12, 2015

SSH with keep alive option

ssh -o "ServerAliveInterval 600" yapit

Disable Strict SSH Checking on Source Peer

By appending the following to your ~/.ssh/config file you can disable SSH prompts which ask to add new hosts to the “known hosts file.” Note that the ${DESTPEERHOST} should be the name of the host machine running the destination peer:

Host ${DESTPEERHOST}
CheckHostIP no
StrictHostKeyChecking no
BatchMode yes

SSH to tondu as omnitest without password

  • As gallinar, in .ssh/config, comment “Batchmode yes” so as to temporarily get a password question when sshing:
 #BatchMode yes
  • As gallinar, in .ssh/config, add the following lines in order to log to tondu as omnitest:
 Host tndu
 HostName tondu
 User omnitest
  • As gallinar, if not already done, generate a public key:
 ssh-keygen -t rsa
  • As gallinar, copy the public key to the target:
 scp ~/.ssh/id_rsa.pub tondu:/tmp
  • As omnitest, on tondu:
 cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys
  • As gallinar, in .ssh/config, uncomment “Batchmode yes”, and remove /tmp/id_rsa.pub on tondu.

Configured ssh on host for no password but still asked

Check $HOME/.ssh and $HOME rights. If writeable for group or world, a password will be asked.
> chmod 700 $HOME/.ssh 
> chmod 600 $HOME/.ssh/authorized_keys

Thursday, March 5, 2015

Virtualbox: Shrinking VM Disk Images

Source: http://snippets.khromov.se/shrinking-a-virtualbox-linux-image-with-zerofree/
  • Download the SystemRescueCd Live Image
  • Mount it as a CD on your virtual machine and boot into the live distro
  • Find all available partition and LVM (volume groups) with:
  • fdisk -l
  • After we find our partition run zerofree to zero out the free space. In our example we will run it on the LVM root partition
  • zerofree -v /dev/mapper/vg_rheldev-lv_root
  • Finally, boot down your virtual machine, and run VBoxManage on the host to resize the .vdi file
  • C:\>"c:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd "c:
    \Users\jerome\VirtualBox VMs\Monster\Mobiquithings.vdi" --compact
With this method, my VM shrunk from 200 GiB to 26 GiB

NB: The other method mentioned on the net replacing zerofree with dd like in
dd -if /dev/zero -of /bigemptyfile -bs 4096; rm /bigemptyfile
was not satisfactory for me.