Posts Tagged ‘rails’

convert textpattern to radiant

Saturday, January 5th, 2008

This is a quick-n-dirty, poor-mans solution to a migration from TextPattern to Radiant CMS. It doesn’t work perfect, it is some manual work, but it did the trick for me, and that’s all I needed. Observe:

First of all, get your data from the textpattern database, and import it in the Radiant CMS database. I used a simple mysqldump command for this, and loaded it back.

db-server $ mysqldump database textpattern > dumpfile.mysql
...(copy)...
devel-server $ mysql -u root site_development < dumpfile.mysql

A small note: I quickly loaded the dump in my editor and converted all the fieldnames to lower case before loading it back in the radiant database. You might want to do this also.

Lazy as I am, I quickly altered the table name to be plural, so Rails/Radiant wouldn't complain:

mysql> rename table textpattern to textpatterns;

Then I fired up Textmate and made this little script:

class Textpattern < ActiveRecord:Base; end 
 
u = User.find :first
 
Textpattern.find(:all, :conditions => {:section => 'article'}).each do |textpattern|
  p = Page.new({
    "virtual"=>false, 
    "class_name"=>'Page', 
    "slug"=>textpattern.url_title.downcase.strip.gsub(/\s+/,'-'), 
    "updated_at"=>textpattern.lastmod, 
    "title"=>textpattern.title, 
    "created_by"=>u, 
    "breadcrumb"=>textpattern.title, 
    "lock_version"=>0, 
    "enable_comments"=>0, 
    "updated_by"=>u, 
    "comments_count"=>0, 
    "published_at"=>textpattern.posted, 
    "status_id"=>100, 
    "position"=>1, 
    "layout_id"=>nil, 
    "parent_id"=>87, 
    "created_at"=>textpattern.lastmod}) do |newpage|
      newpage.parts.create({"name"=>"body", "filter_id"=>"Textile", "content"=>textpattern.body})  
      newpage.parts.create({"name"=>"summary", "filter_id"=>"Textile", "content"=>textpattern.excerpt}) 
  end
  p.save!
end

The first line will add a model for the Textpattern table we added, so we can use the ActiveRecord magic to access this table. The second line will load the first user it can find (I only had one, so no problems there).

From then on, we just get all the Textpatterns articles we want (a specific section in this case), and with each of these, I create a new page in radiant, and add pageparts to them, with good defaults.

Once done (and bugfixed), I simply copy-pasted this in a script/console session in my radiant-project. Done.

The defaults were stolen from a page I quickly created and published in Radiant, under the correct section. You need to pay attention to the “parent_id” for the Page-creation (get it from the database, or from the url when hovering it in the admin section), and the “name” for the PagePart creation sections.

Last work is to drop the added table from your database.

As I told you, quick-n-dirty, and nothing I’m really proud of, but I needed it fast, and I’ve published it here as it might help other people.

all-number passwords in database.yml

Friday, November 30th, 2007

A small posting in english, as it might help a lot of people.

When using Ruby on Rails, if you keep getting a “could not connect to database” while you are 100% sure the username and password you entered in your database.yml is correct, and you can’t find what is causing it… observe

Database.yml:

production:
  username: dontcare
  password: 0123456
  host: localhost
  ...

When we connect to the database using the RoR console, we get the following:

$ RAILS_ENV=production script/console 
Loading production environment.
>> ActiveRecord::Base.establish_connection
=> #<activerecord ::Base::ConnectionSpecification:0x40b0c0a4 @adapter_method="mysql_connection", 
@config={:host=>"localhost", :username=>"dontcare", :database=>"foobar", 
:adapter=>"mysql", :password=>42798}>
</activerecord>

(wrapped the output so it would be readable)

Take a close look to the last field in the output, the password has changed from “0123456″ to “42798″. This is, however, expected behavior, as a leading zero is interpreted as the sign the number following the zero is a octal representation of a number. This has even nothing to do with rails, this is default ruby:

$ irb
irb(main):001:0> a = 0123
=> 83

The solution is to quote your password:

production:
  username: dontcare
  password: '0123456'
  host: localhost
  ...

By the way, remember these two lines, they help you test the database connection; you can even omit the first one, but then you won’t see the actual connection parameters, which helped me discover the cause of this problem.

ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.select_all('SHOW TABLES')

Just start a console, and use these to test your database.