Friday, December 6, 2013

Mysql commands

Login to MySQL

mysql --user=xxx --password=xxx --database=xxx

Dump a database

mysqldump --user=xxx --password=xxx dbname

Dump a single table with data

mysqldump --user=xxx --password=xxx dbname tablename

Dump a single table with only some of the data using --where

mysqldump --user=xxx --password=xxx dbname tablename --where="db='blog3'"

Dump a single table with data in tab separated format (for import to XL)

mysqldump --user=xxx --password=xxx dbname tablename -T path
The -T path (example: -T /tmp) tells mysqldump where to create a tablename.sql file with the table definition, and a tablename.txt file with the tab separated data from the table.

Dump a single table structure without the data

mysqldump --user=xxx --password=xxx --no-data dbname tablename

Grant all privileges on a database to a user, be sure to flush privileges after to make the security change effective immediately

GRANT ALL ON db1.* TO 'username'@'localhost';
FLUSH PRIVILEGES;

Tuesday, August 20, 2013

Getting Rails variables into external JavaScript files

It is often convenient to keep JavaScript code in external .js files and pull them into a web page using this HTML command:

<script src="external.js"></script>

In a normal rails .erb or .haml view, you can't use a rails variable value inside the external JavaScript file. The trick is to set the value in an HTML meta tag, then use JavaScript to read the meta tag after the DOM is ready. For example, in the .erb view file, use:

<meta name='railsvariable' content='<%= @railsvar %>' /%>

Then, in the JavaScript file, using the Dojo query function:

var railsvar = query('meta[name="railsvariable"]')[0].content;

In raw JavaScript, it would be this:

var railsvar = document.getElementsByTagName('meta').item(property='railsvariable').getAttribute('content');

Tuesday, May 7, 2013

Haml comments

I've had to look this up about three times which means I should add it to the blog.

This technique was found at Makandra Cards.

To comment out code such that haml does not process it (it generates no HTML):

-# = link_to 'foo'