Archive for the ‘rails’ Category

DbCharmer 1.7.0 Release: Rails 3.0 Support and Forced Slave Reads

Сентябрь 1st, 2011

This week, after 3 months in the works, we’ve finally released version 1.7.0 of DbCharmer ruby gem – Rails plugin that significantly extends ActiveRecord’s ability to work with multiple databases and/or database servers by adding features like multiple databases support, master/slave topologies support, sharding, etc.

New features in this release:

  • Rails 3.0 support. We’ve worked really hard to bring all the features we supported in Rails 2.X to the new version of Rails and now I’m proud that we’ve implemented them all and the implementation looks much cleaner and more universal (all kinds of relations in rails 3 work in exactly the same way and we do not need to implement connection switching for all kinds of weird corner-cases in ActiveRecord).
  • Forced Slave Reads functionality. Now we could have models with slaves that are not used by default, but could be turned on globally (per-controller, per-action or in a block). This is a new feature that brings our master/slave routing capabilities to a really new level – we could now use it for a really mission-critical models on demand and not be afraid of breaking major functionality of our applications by switching them to slave reads.
  • Lots of changes were made in the structure of our code and tests to make sure it would be much easier for new developers to understand DbCharmer internals and make changes in its code.

Along with the new release we’ve got a brand new web site. You can find much better, cleaner and, most importantly, correct documentation for the library on the web site. We’ll be adding more examples, will try to add more in-depth explanation of our core functions, etc.

If you have any questions about the release, feel free to ask them in our new mailing list: DbCharmer Users Group.

For more updates in our releases, you can follow @DbCharmer on Twitter.



PlanetMySQL Voting: Vote UP / Vote DOWN

On Password Strength

Август 11th, 2011

XKCD (as usual) makes a very good point – this time about password strength, and I reckon it’s something app developers need to consider urgently. Geeks can debate the exact amount of entropy, but that’s not really the issue: insisting on mixed upper/lower and/or non-alpha and/or numerical components to a user password does not really improve security, and definitely makes life more difficult for users.

So basically, the functions that do a “is this a strong password” should seriously reconsider their approach, particularly if they’re used to have the app decide whether to accept the password as “good enough” at all.


PlanetMySQL Voting: Vote UP / Vote DOWN

Encoding and JRuby on Rails

Март 1st, 2011

Studing JRuby and Rails, I faced some encoding problems. The solution was very easy. First you must have to create your database using utf8 character set. In case you are using MySQL, just do the following:

CREATE DATABASE `yourdatabasename` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

or

ALTER DATABASE yourdatabasename CHARACTER SET utf8 COLLATE utf8_general_ci;

If you already have a database with records, read this post from @akitaonrails.

In my case, I’m using the activerecord-jdbc-adapter gem, and connecting to the database through the MySQL JDBC Connector.

At this time, I just have downloaded the JDBC driver (.jar file), and copied to $JRUBY_HOME/lib folder (if you are using RVM, find the JRuby installation folder using the rvm debug or rvm info command).

Lastly, I just have set the following parameters into connection string (in database.yml file):

jdbc:mysql://localhost:3306/yourdatabasename?characterSetResults=UTF-8&characterEncoding=UTF-8&useUnicode=yes

Maybe this will be useful to someone.
See ya!

 




PlanetMySQL Voting: Vote UP / Vote DOWN

TOTD #150: Collection of GlassFish, NetBeans, JPA, JSF, JAX-WS, EJB, Jersey, MySQL, Rails, Eclipse, and OSGi tips

Ноябрь 18th, 2010

This is the 150th tip published on this blog so decided to make it a collection of all the previous ones. Here is a tag cloud (created from wordle.net/create) from title of all the tips:

As expected GlassFish is the most prominent topic. And then there are several entries on NetBeans, JRuby/Rails, several Java EE 6 technologies like JPA, JAX-WS, JAX-RS, EJB, and JSF, and more entries on Eclipse, OSGi and some other tecnhologies too. Here is a complete collection of all the tips published so far:

Just for fun, here is another tag cloud:

You can access all the tips here. And keep those suggestions coming!

Technorati: totd glassfish netbeans jpa jsf jaxws jersey mysql rails osgi eclipse


PlanetMySQL Voting: Vote UP / Vote DOWN

DbCharmer – Rails Can Scale!

Апрель 17th, 2010

Back in November 2009 I was working on a project to port Scribd.com code base to Rails 2.2 and noticed that some old plugins we were using in 2.1 were abandoned by their authors. Some of them were just removed from the code base, but one needed a replacement – that was an old plugin called acts_as_readonlyable that helped us to distribute our queries among a cluster of MySQL slaves. There were some alternatives but we didn’t like them for one or another reasons so we’ve decided to go with creating our own ActiveRecord plugin, that would help us scale our databases out. That’s the story behind the first release of DbCharmer.

Today, six months after the first release of the gem and we’ve moved it to gemcutter (which is now the official gems hosting) and we’re already at version 1.6.11. The gem was downloaded more than 2000 times. There are (at least) 10+ large users that rely on this gem to scale their products out. And (this is the most exciting) we’ve added tons of new features to the product.

Here are the main features added since the first release:

  • Much better multi-database migrations support including default migrations connection changing.
  • We’ve added ActiveRecord associations preload support that makes it possible to move eager loading queries to the same connection where your finder queries go to.
  • We’ve improved ActiveRecord’s query logging feature and now you can see what connections your queries executed on (and yes, all those improvements are colorized :-) ).
  • We’ve added an ability to temporary remap any ActiveRecord connections to any other connections for a block of code (really useful when you need to make sure all your queries would go to some non-default slave and you do not want to mess with all your models).
  • The most interesting change: we’ve implemented some basic sharding functionality in ActiveRecord which currently is being used in production in our application.

As you can see now DbCharmer helps you to do three major scalability tasks in your Rails projects:

  1. Master-Slave clusters to scale out your Rails models reads.
  2. Vertical sharding by moving some of your models to a separate (maybe even dedicated) servers and still keep using AR associations
  3. Horizontal sharding by slicing your models data to pieces and placing those pieces into different databases and/or servers.

So, If you didn’t check DbCharmer out yet and you’re working on some large rails project that is (or going to be) facing scalability problems, go read the docs, download/install the gem and prove them that Rails CAN scale!



PlanetMySQL Voting: Vote UP / Vote DOWN

Reusing models outside of Rails

Январь 6th, 2010

If you have done a good job of building your rails models, you may find that they are helpful for your non-rails system maintenance and such. They may even be necessary to reuse if you follow the rails model of using activerecord validations (rather that database RI) to preserve the integrity of your data.


Or you may just find yourself rewriting the same code again and again, and want all that good railsiness to make it easier to write and maintain. Personally I find myself in some instance of ./script/console as often as irb just so I can get the activesupport helper methods ( 4.days.from_now and such) that many rails developers are surprised to find are not actually a standard part of ruby.


So, the good news is it is easy to reuse rails code outside of rails.


Let's say you want to do some data manipulation (reporting, loading, scrubbing, etc) in your rails db, and want to use your models to do it. A few imports in your ruby script gets the necessary environment in place:


require 'rubygems'
require 'yaml'
require 'active_record'
require 'logger'


and a few more will load up your models (note: they're probably not in the same location as mine, unless you are also working on an app called 'seweb' in your home dir):


PROJECT_HOME = "#{ENV['HOME']}/seweb/"
require "#{PROJECT_HOME}/app/models/sales_rep.rb"
require "#{PROJECT_HOME}/app/models/organization.rb"
require "#{PROJECT_HOME}/app/models/team.rb"


Then connect to the appropriate database (note I'm connecting to the development environment - can you guess how I'd connect to 'test' or 'production'?), with rails logging enabled:


ActiveRecord::Base.logger = Logger.new( STDERR )
db_config = YAML::load( File.open("#{seweb_home}/config/database.yml";))
ActiveRecord::Base.establish_connection( db_config["development"])


And you are good! If you are using a transactional database (such as my personal favorite, MySQL with InnoDB), you can make nice transaction wrappers for your work thusly:


ActiveRecord::Base.transaction do


        rep = SalesRep.find_or_initialize_by_name( 'Kyllin D. Quota' )
        # create the component parts
        if( rep.changed? )
            rep.organization = Organization.find_or_create_by_name 'APAC'
            rep.team = Team.find_or_create_by_name 'Enterprise'
            rep.save!
        end


        rescue Exception
            raise ActiveRecord::Rollback, "Invalid record for #{rep.name}"
        end


end


Pow. You get your rails sugar, rails validations, rails logging. Are you happy? Why yes, yes you are.


PlanetMySQL Voting: Vote UP / Vote DOWN

Reusing models outside of Rails

Январь 6th, 2010

If you have done a good job of building your rails models, you may find that they are helpful for your non-rails system maintenance and such. They may even be necessary to reuse if you follow the rails model of using activerecord validations (rather that database RI) to preserve the integrity of your data.


Or you may just find yourself rewriting the same code again and again, and want all that good railsiness to make it easier to write and maintain. Personally I find myself in some instance of ./script/console as often as irb just so I can get the activesupport helper methods ( 4.days.from_now and such) that many rails developers are surprised to find are not actually a standard part of ruby.


So, the good news is it is easy to reuse rails code outside of rails.


Let's say you want to do some data manipulation (reporting, loading, scrubbing, etc) in your rails db, and want to use your models to do it. A few imports in your ruby script gets the necessary environment in place:


require 'rubygems'
require 'yaml'
require 'active_record'
require 'logger'


and a few more will load up your models (note: they're probably not in the same location as mine, unless you are also working on an app called 'seweb' in your home dir):


PROJECT_HOME = "#{ENV['HOME']}/seweb/"
require "#{PROJECT_HOME}/app/models/sales_rep.rb"
require "#{PROJECT_HOME}/app/models/organization.rb"
require "#{PROJECT_HOME}/app/models/team.rb"


Then connect to the appropriate database (note I'm connecting to the development environment - can you guess how I'd connect to 'test' or 'production'?), with rails logging enabled:


ActiveRecord::Base.logger = Logger.new( STDERR )
db_config = YAML::load( File.open("#{seweb_home}/config/database.yml";))
ActiveRecord::Base.establish_connection( db_config["development"])


And you are good! If you are using a transactional database (such as my personal favorite, MySQL with InnoDB), you can make nice transaction wrappers for your work thusly:


ActiveRecord::Base.transaction do


        rep = SalesRep.find_or_initialize_by_name( 'Kyllin D. Quota' )
        # create the component parts
        if( rep.changed? )
            rep.organization = Organization.find_or_create_by_name 'APAC'
            rep.team = Team.find_or_create_by_name 'Enterprise'
            rep.save!
        end


        rescue Exception
            raise ActiveRecord::Rollback, "Invalid record for #{rep.name}"
        end


end


Pow. You get your rails sugar, rails validations, rails logging. Are you happy? Why yes, yes you are.


PlanetMySQL Voting: Vote UP / Vote DOWN