viernes, 3 de junio de 2011

How-To copy column values from a table to another in MySQL

Imagine table A and B like:

SELECT * FROM A WHERE (...) LIMIT 3
id, a, x, c
__________
2, 5, 1, 6
7, 2, 6, 1
9, 8, 2, 2

SELECT * FROM B WHERE (...) LIMIT 3
id, x, b, c
__________
1, NULL, 3, 3
5, NULL, 0, 4
3, NULL, 2, 7

You want to copy x column values from A (i.e. 1,6,2) to B (all NULL).

Solution:

SELECT @i:=0;
SELECT @j:=0;
UPDATE B
JOIN (
SELECT @i:=@i+1 AS tid, B.id FROM B WHERE (...) LIMIT 3
) Bt ON (Bt.id=B.id)
JOIN (
SELECT @j:=@j+1 AS tid, A.id from A WHERE (...) LIMIT 3
) At ON (At.tid=Bt.tid)
SET B.x = At.x;

lunes, 23 de mayo de 2011

Linux command line python pretty print

cat somexmlfile.xml | python -c "import sys;import lxml.etree as etree;x = etree.parse(sys.stdin);print etree.tostring(x, pretty_print = True)"

alias pp='python -mjson.tool'
cat somexmlfile.json | pp

lunes, 28 de marzo de 2011

How to open port 80 for out connections to a specific user

sudo iptables -A ufw-user-output -p tcp --dport 80 -m owner \
--uid-owner pepito -j ACCEPT

jueves, 17 de marzo de 2011

Apache RewriteRule to Forbid (403) unknown URL requests

Write as the last rule:

# Everything else is forbidden
RewriteRule (.*) - [F,L]

jueves, 24 de febrero de 2011

martes, 15 de febrero de 2011

PAM authentication in PHP


sudo aptitude install php5-auth-pam
sudo cp /usr/share/doc/php5-auth-pam/examples/php /etc/pam.d/php


PHP example:

$error = 0;
if(pam_auth("zappedy", "XXXX", &$error))
echo "bien";
else
echo "mal $error";


Ref:
http://www.dokuwiki.org/auth:pam

miércoles, 19 de enero de 2011

MySQL slow queries

sudo mysqld_safe --timezone=+0:00 --long_query_time=1 --log-slow-queries=/tmp/slow.log --log-queries-not-using-indexes

Sort by Rows_examined:

grep Query_time /tmp/slow.log |sort -n -k9
# Query_time: 0.007019 Lock_time: 0.000098 Rows_sent: 0 Rows_examined: 65
# Query_time: 0.001595 Lock_time: 0.000042 Rows_sent: 0 Rows_examined: 66
# Query_time: 0.002316 Lock_time: 0.000101 Rows_sent: 0 Rows_examined: 72
# Query_time: 0.003357 Lock_time: 0.000380 Rows_sent: 0 Rows_examined: 83
# Query_time: 0.003342 Lock_time: 0.000403 Rows_sent: 0 Rows_examined: 116
# Query_time: 0.004729 Lock_time: 0.000319 Rows_sent: 0 Rows_examined: 116
# Query_time: 0.002406 Lock_time: 0.000043 Rows_sent: 0 Rows_examined: 205

jueves, 13 de enero de 2011

Unit Tests' value in TDD

Hierarchy of tests:
  • Acceptance: Does the whole system work?
  • Integration: Does our code work against code we can't change?
  • Unit: Do our objects do the right thing, are they convenient to work with?

Unit Tests' value in TDD:
  • Write loosely coupled components
  • Detect errors while the context is fresh in our mind
  • Add an executable description of what the code does (documentation)
  • Focus: discouraging unnecessary features
  • Rythm: tests tell what to do next
  • Trust: tests give a reason to trust you
ref:
- Extreme Programming Explained
- Growing Object-Oriented Software, Guided by Tests