Capistrano keep_releases and releases clean up

Capistrano keeps last 5 releases by default. You can override this in your deploy.rb file

set :keep_releases, 10

This however will not clean up your releases until you run

cap deploy:cleanup

in the console. For automatically cleaning up the releases add in deploy.rb

after "deploy:update", "deploy:cleanup"

This will execute every time you make a deploy. It actually runs after deploy:update task, which is part of cap deploy.

If you want to delete all releases except the last 3 for example you can run

cap deploy:cleanup -s keep_releases=3

directly in your console.

Tagged with: , ,
Posted in Ruby On Rails

Web application deployment

In agile web application development it is a must to use deployment tool to sync your code with a staging server. I got the idea for some time from a presentation of a colleague of mine for Fabric witch is Python related deployment tool.

Working on a Rails project for the last few months I configured and currently using Capistrano – a Ruby deployment tool for RubyOnRails and many other frameworks including CakePHP with capcake.

Here is an interesting approach for deployment from the Linux console using Git witch is not language dependent. Check out the post at debuggable.

Tagged with: , , ,
Posted in Articles, Linux, PHP, Ruby On Rails

A few good GIT resources, books and tutorials

I was looking for a resources on how git works, concepts etc. and asked in the channel for that. This is the most quick and exact response I get in an IRC and I am happy to share the links of the resources I get.

  • Git from the bottom up” starts with explaining the building blocks of git and proceeds to tell you how they fit together.
  • Pro Git” is probably the best
  • Git for Computer Scientists” is a quick introduction to git internals for people who are not scared by phrases like Directed Acyclic Graph.
  • Git Concepts Simplified” explains the basic structures used by git, which is very helpful for understanding its concepts.
  • Git for Web Designers” is a quick “Who why what when how” on VCS and git
  • A book designed to teach people about Git in a real world usage model. The book follows a fictional company as they implement and learn about Git. Covers all 21 standard Git commands. Available for free
  • Version Control By Example” gives a good overview of the different VCSes available. The author will even mail you a dead-tree copy for free.
  • The git parable” provides some good reasoning behind git.

You can the list by typing

!bottomup !book !cs !gcs !designers !gitt !vcbe !parable

in the git irc channel.

Tagged with: , , ,
Posted in Articles, Books

Saving credentials for GitHub on Windows

To save credentials you use for a github repository, copy the lines below and save them in a file _netrc. Add you username and password and start githubbing.

machine github.com
 login <login>
 password <password>

Note that the file in Linux is .netrc, nut in Windows it is _netrc. Don’t ask me why :)

Tagged with: , ,
Posted in HTML

Set default Ruby version with pik

Pik is nice replacement of RVM for Windows. You can install several Ruby versions and switch between them. I was asking how to make a particular the default used, when you start the prompt. The approach on Windows is different from Linux (where you do the config in your .bashrc file). You have to change the %PATH% to point to the bin directory of the Ruby version you want to use. Here is how to view bin directories with Pik

pik list -v

Check the right path is in the %PATH% environment variable adn you are good to go.

 

Tagged with: ,
Posted in HTML

Send file for download with ajax

No matter there are sources that state downloading a file with AJAX is not possible there are many queries to do that and of course there is a way to do it.

Downloading a file with AJAX has a trick that works on a majority of browsers. One have to post parameters with AJAX, do some server process to prepare the file and then have two options:

  1. Return the data (file contents) and use it to return a file for download
  2. Save the file on the server and return a link with a parameter, that will return the file for download with the appropriate headers set.

1. The first option is OK if you handle very small files (up to 1MB). The possend some parameters to the server, file is prepared by querying a database, done some calculation etc. and then the file contents are returned. You can handle the contents as a AJAX response from the server and use them to set them as a value of a hidden input, then post again to an address where the posted parameters are returned with appropriate headers set. This works for very smaller files and is not an option, because it doubles the time for response.

2. The second option is to post the parameters to the server with AJAX, do the process of preparing the file and save it on the server. Then return only the link of an address that will return the file with download headers set. This option also has a drawback – If there are a lot of files to download, there have to be a cron job for deleting the old files.
Here is an example code in the client:

<iframe id="frm"></iframe>
<style>iframe#frm {display: none;}</style>
<script>
$("#my_form").live('ajax:success', function(event, data, status, xhr) {
    if (data.filename) {
      url = "/download_file?filename=" + data.filename
      $('#frm').src = url
      window.location = url
    } else {
      alert('No data to display')
    }
});
</script>

The server code depends on your needs, but here is an example for the download_file URL in Rails

filename = sanitize_filename params[:filename]
file = Rails.root.to_s + "/tmp/downloads/" + filename
send_file file, :filename => filename, :disposition => 'attachment', :type => 'application/x-www-form-urlencoded'
Tagged with: , , ,
Posted in Articles, Browsers, JavaScript, Ruby On Rails

Remove duplicate rows from database

This snippet is particularly for Postgres (that database I am currently using and didn’t tested it in other, but should work. At most you have to change the syntax a little). I have to remove duplicated entries for a duplicated_column

DELETE FROM table_name
WHERE id NOT IN (
    SELECT MIN(id)
    FROM table_name
    WHERE created_at > 'from_date' AND created_at < 'to_date'
    GROUP BY duplicated_column
    )
AND created_at > 'from_date' AND created_at < 'to_date';

So, change table_name with the name of your table and duplicated_column with the column where you have duplicates. Running the query from .. to date is a good idea if the duplicates are only for a given period of time and your table has a lots of records.

Tagged with: , ,
Posted in Databases, Postgres

Map tiles do not load (all pink) in Firefox using OpenLayers and own OSM server

I have set my own OSM server for providing map tiles to use in an application. For the application I use OpenLayers and came to an issue when ported the code from slippymap to my application in order to use Local tiles from the server I set.

The issue is that when switching to the Local layer I only see the pink layer of the map, but the src attribute of the image tag loads the actual tile. I tried with Firebug to remove the pink layer, but behind it there is no image there! Coping the url from src attribute of the img tag opens the tile as an image.

Tried this in Chrome and the Local layer is working in IE 7,8,9 too. The problem is only with FF and I suspected OpenLayers. The Mapnik layer is working and showing tiles on all browsers. Using HTML5 by the way.

After digging for a while I found this article that describes requesting resources from a different domain. There is a tag that allows using resources from different domain as in the case OpenLayers use images from my tile server.

Solving this issue after adding a “Access-Control-Allow-Origin” header in the response. Note you have to have mod_headers enabled in your configuration. Put that setting in <Directory>, <Location> or .htaccess file:

Header set Access-Control-Allow-Origin *

 

Tagged with: , , , , ,
Posted in Apache, Articles

Firefox tracking option

Didn’t knew there was disable tracking option and think it should be on by default…

Tagged with: , ,
Posted in Browsers

How to change collation of all tables in a database

Copy this snippet, replace database_name with the name of your database AND utf8_general_ci to any other collation you preferred. This will generate the sql commands needed for changing collation of all tables.

SELECT CONCAT('ALTER TABLE `', t.`TABLE_SCHEMA`, '`.`', t.`TABLE_NAME`, '` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;') as stmt
FROM `information_schema`.`TABLES` t
WHERE 1
AND t.`TABLE_SCHEMA` = 'database_name'
ORDER BY 1

Copy the output of the result and execute it in MySQL to change the collation.

Enjoy!

Tagged with: ,
Posted in MySQL
Sites
Categories
Archives