Donnerstag, 9. August 2007

SQLFairy - The SQL Translator

I needed to convert a database schema from MySQL to SQLite and found a great tool for doing such schema conversion jobs.
# install sqlt on debian sytems with:
sudo apt-get install sqlfairy
# convert from one schema to another schema with:
sqlt -f DBI --dsn dbi:mysql:dbname --db-user root -t SQLite > sqlite.sql
The above command connects to mysql and reads the mysql schema from the connection and writes the sqlite schema to sqlite.sql. You can easily change this command for other databases. If you want to know if sqlt supports your conversion you can run:
sqlt -l
Parsers:
Access
DB2
DB2-Grammar
DBI
DBI-DB2
DBI-MySQL
DBI-PostgreSQL
DBI-SQLite
DBI-Sybase
Excel
MySQL
Oracle
PostgreSQL
SQLite
Storable
Sybase
XML
XML-SQLFairy
YAML
xSV

Producers:
ClassDBI
Diagram
Dumper
GraphViz
HTML
MySQL
Oracle
POD
PostgreSQL
SQLServer
SQLite
Storable
Sybase
TT-Base
TT-Table
TTSchema
XML
XML-SQLFairy
YAML
The Parsers are needed to read the schema and the Producers are needed to write the schema.
Links
http://sqlfairy.sourceforge.net/

Labels: , , , , ,

Mittwoch, 9. Mai 2007

ri and ri_proxy

Yesterday i have written a little caching proxy script for ri. The script forwards any invocation to ri. The results are cached in a sqlite3 database. So that any subsequent calls are much faster, not fast as i wished but definitely faster.
#!/usr/bin/env ruby

require 'rubygems'

ROOT = File.dirname(__FILE__)
BASE_NAME = File.basename(__FILE__)

DB_FILE = File.join(ROOT, BASE_NAME + '.sqlite3')
DB_CONFIG = {:adapter => 'sqlite3', :timeout => 5000, :database => DB_FILE}

LOG_FILE = File.join(ROOT, BASE_NAME + '.log')

# speed up active record loading a bit
require 'active_support'
RAILS_CONNECTION_ADAPTERS = ['sqlite']

require 'active_record'
ActiveRecord::Base.logger = nil # Logger.new(LOG_FILE)
ActiveRecord::Base.establish_connection(DB_CONFIG)

class RiMethodEntry < ActiveRecord::Base
end

force = false
if !RiMethodEntry.table_exists? or force
ActiveRecord::Schema.define(:version => 1) do
create_table :ri_method_entries, :force => true do |t|
t.column :name, :string, :null => false
t.column :text, :text, :null => false
end
add_index :ri_method_entries, :name
end
end

ARGV.length > 0 or raise Exception.new("No arguments found!")
name = ARGV[0]
entry = RiMethodEntry.find_by_name(name)
if entry
puts entry.text
exit
end

args = ARGV.collect { |a| %{"#{a}"} }
text = `ri #{args.join(" ")}`
found = text.starts_with?('-')

puts text
exit unless found

RiMethodEntry.create(:name => name, :text => text)

Labels: , , ,