Translate

January 8, 2024

 

Steps to migrate a MySQL database to PostgreSQL?

PostgreSQL vs. MySQL is an important decision when it comes to choosing an open-source relational database management system. Both PostgreSQL and MySQL are time-proven solutions

PostgreSQL is The world’s most advanced open source database, and MySql is popular for ease of development.

First thing WHY your project need to switch the database, consider below before you decide. 

Is current DB using any store procs, functions, triggers, TEMP tables TINYINT(1), Unsigned integer, HASH, KEY, scheduled events etc.

Once you decided to migrate existing MySql to PostgresQL then Export data from MySQL using tools like pgLoader or custom scripts. Modify the SQL dump to match the PostgreSQL syntax. You need to adjust data types, column lengths, and function, procedure calls to take advantage of pgSQL features. watch out for triggers / task scheduler etc.

Use the migration tool to transfer the data from the MySQL database to the PostgreSQL database.

Once data is migrated without any errors then you need to change your apps libraries , db connections, modify SQL queries in app code(Need to change syntax for some queries probably not all queries.)

DO Rigorous testing. Once you are confident then try adding and use advance feature of PostgreSQL LIKE FULL OUTER JOIN, Full-text search, CASCADE option to drop table’s dependent objects e.g., tables and views, Analytic functions, Data types array, hstore, and user-defined type, Boolean type, IP address data type, CHECK constraint, Partial indexes, Bitmap indexes etc.

DB optimization is endless process, you can add features step by step, Rigorous testing is essential for every step. 

IMPORTANT : USE BACKUP AND RECOVERY FEATURES FOR EVERY STEP YOU PERFORM SO THAT YOU CAN COME BACK TO STABLE STATE IF SOMETHING WENT WRONG.

November 19, 2015

Types of Caching in Rails

A few types of caching to know :
  • Page Caching: Save entire pages of an application without hitting the stack by returning cached pre-rendered pages. Good in applications without authentication and other highly dynamic aspects.
  • Fragment Caching: Allows fragments of view logic, for example partials or other bits of HTML that are independent from other parts, to be wrapped in a cache block and served out of the cache store when the next request comes in.
  • Action Caching: Works like Page Caching, except the incoming web request hits the Rails stack. This means that before filters (like authentication or other restrictions) can be run on it before the cache is served.
  • Rails.cache: All cached content except cached pages are stored in the Rails.cache.
  • HTTP Caching: HTTP caching occurs when the browser stores local copies of web resources for faster retrieval the next time the resource is required. It uses HTTP headers to determine if the browser can use a locally stored version of the response or if it needs to request a fresh copy from the server.

September 15, 2015

Uploading Multiple Images using CarrierWave

How to upload multiple image by selecting "ctrl + selction key" at same time.

CarrierWave
This gem provides a simple and extremely flexible way to upload files from Ruby applications.
This gem has convenient support for multiple file upload fields.

Following are the steps:
For Complete Source code click here

In Rails, add it to your Gemfile:


gem 'carrierwave'
 
bundle install
 
rails generate uploader photo

Create post scaffold

rails generate scaffold post title:string

Create post_attachment scaffold

post_attachment post_id:integer photo:string
 
rake db:migrate

In post.rb

class Post < ActiveRecord::Base
  has_many :post_attachments
  accepts_nested_attributes_for :post_attachments
end

In post_attachment.rb

class PostAttachment < ActiveRecord::Base
   mount_uploader :photo, PhotoUploader
   belongs_to :post
end

In post_controller.rb

def show
  @post_attachments = @post.post_attachments.all
end

 def new
   @post = Post.new
   @post_attachment = @post.post_attachments.build
 end
 
 def create
   @post = Post.new(post_params)
   respond_to do |format|
     if @post.save
       params[:post_attachments]['photo'].each do |a|
         @post_attachment = @post.post_attachments.create!(:photo => a)
       end
      format.html { redirect_to @post, notice: 'Post was successfully created.' }
     else
      format.html { render action: 'new' }
    end
   end 
 end
 
 private
  def post_params
    params.require(:post).permit(:title, post_attachments_attributes: [:id, :post_id, :photo])
  end

In views/posts/_form.html.haml

 = form_for(@post, :html => { :multipart => true }) do |f|
  .field
    = f.label :title
    = f.text_field :title
  = f.fields_for :post_attachments do |p|
    .field
      = p.label :photo
      = p.file_field :photo, :multiple => true, name: "post_attachments[photo][]"
  .actions
    = f.submit
 

In views/posts/show.html.haml

%p#notice= notice
%p
  %strong Title:
  = @post.title
- @post_attachments.each do |p|
  = image_tag p.photo_url
  = link_to "Edit Attachment", edit_post_attachment_path(p)
= link_to 'Edit', edit_post_path(@post)
|
\#{link_to 'Back', posts_path}
 
Update form to edit an attachment views/post_attachments/_form.html.haml

= image_tag @post_attachment.photo
= form_for(@post_attachment) do |f|
  .field
    = f.label :photo
    %br/
    = f.file_field :photo
  .actions
    = f.submit
 
 Now, update method in post_attachment_controller.rb

 def update
    respond_to do |format|
      if @post_attachment.update(post_attachment_params)
        format.html { redirect_to @post_attachment.post, notice: 'Post attachment was successfully updated.' }
      end
    end
  end


For Complete Source code click here

June 4, 2014

Apple launches new Swift programming language for MAC OS X and iOS

Apple developers now have a new tool in their toolbox.

Switft, a new programming language that can power all of its devices. Swift is the successor to Objective-C, the venerable language that Apple has used to build apps for the Mac and iOS. Swift includes full support for Cocoa and Cocoa Touch, so developers can build apps for the iPad and iPhone.

Swift uses the same LLVM compiler that Apple uses for Objective-C, so developers can run Swift, Objective-C,  and C code all in the same program, and brings a number of improvements.

Swift seems to get rid of Objective C's reliance on defined pointers; instead, the compiler infers the variable type, just as many scripting languages do. At the same time, it provides modern features similar to those found in C++ and Java, like well-defined namespaces, generics, and operator overloading. From the few fragments of code shown during the demo, Swift appears to rely heavily on the dot-notation that Apple introduced in an earlier iteration of Objective C.

Apple showed off a couple of cases where implementing the same algorithm in Swift provided a speedup of about 1.3X compared to the same code implemented in Objective C. It also showed off a Swift "playground," where code is compiled as it's typed and the output is displayed in a separate pane of the editing window. The goal here is to allow developers to test code fragments without having to recompile an entire complex project.

The Swift Programming Language, This book is available for download with iBooks on your Mac or iOS device, and with iTunes on your computer. Books can be read with iBooks on your Mac or iOS device. Download link

May 15, 2014

What happens when you run Rails migration?

Rails migration allows the creation of the table and provides the functionality that can be performed on a table with the following commands:

create_table(name, options)
drop_table(name)
rename_table(old_name, new_name)
add_column(table_name, column_name, type, options)
rename_column(table_name, column_name, new_column_name)
change_column(table_name, column_name, type, options)
remove_column(table_name, column_name)
add_index(table_name, column_name, index_type)
remove_index(table_name, column_name)


-Rails Migration also allows the use of pre-defined data type in the application as it supports all the data types. The data types consist of string, integer, float, etc.

-Rails Migration allows the users to use the valid column options like limit (:limit=> "50"), default (:default => "hello"), and null (:null => false implies NOT NULL)

February 5, 2014

What is the difference between Symbol and String in Ruby?

The symbol in Ruby on rails act the same way as the string but the difference is in their behaviors that are opposite to each other.

The difference remains in the object_id, memory and process time for both of them when used together at one time.

Strings are considered as mutable objects. Whereas, symbols, belongs to the category of immutable.

Strings objects are mutable so that it takes only the assignments to change the object information. Whereas, information of, immutable objects gets overwritten.

String objects are written like
irb(main):001:0> "string object apple".object_id #=>70278982314860
or
irb(main):002:0> "string object
apple".to_sym.object_id #=> 456148

Symbols are used to show the values for the actions like equality or non-equality to test the symbols faster then the string values.

January 14, 2014

Difference between Argument and Parameter in ruby on rails?

A parameter represents a value that the method expects you to pass when you call it.
An argument represents the value you pass to a method parameter when you call the method. The calling code supplies the arguments when it calls the method.

In simple words, parameters appear in method definitions; arguments appear in method calls.

For example, in below method, variables param1 and param2 are the parameters

def foo_method(param1, param2):
    .......
end

while calling the method, arg1 and arg2 are the arguments
 
foo_method(arg1, arg2)