Permalink
Browse files

First version of the skeleton, currently only tested on Postgresql

  • Loading branch information...
1 parent 5167223 commit 3c78ee86bd57fa3c8c9b8b4c69909e43885ebf2d @SiGe SiGe committed Nov 16, 2012
Showing with 119 additions and 0 deletions.
  1. +7 −0 Gemfile
  2. +104 −0 Rakefile
  3. +8 −0 config/database.yml
View
@@ -0,0 +1,7 @@
+source :rubygems
+
+# TODO: Change it to whatever database you want to use
+gem 'pg'
+
+# Active record and active support to lazy to add them
+gem 'rails'
View
104 Rakefile
@@ -0,0 +1,104 @@
+require 'yaml'
+require 'logger'
+require 'active_record'
+
+namespace :db do
+ # Creates the database according to the engine used
+ # Currently only supports, sqlite, mysql and postresql
+ # Copied from http://exposinggotchas.blogspot.com/2011/02/activerecord-migrations-without-rails.html
+ def create_database config
+ begin
+ if config['adapter'] =~ /sqlite/
+ if File.exist?(config['database'])
+ $stderr.puts "#{config['database']} already exists"
+ else
+ begin
+ # Create the SQLite database
+ ActiveRecord::Base.establish_connection(config)
+ ActiveRecord::Base.connection
+ rescue
+ $stderr.puts $!, *($!.backtrace)
+ $stderr.puts "Couldn't create database for #{config.inspect}"
+ end
+ end
+ return # Skip the else clause of begin/rescue
+ else
+ ActiveRecord::Base.establish_connection(config)
+ ActiveRecord::Base.connection
+ end
+ rescue
+ case config['adapter']
+ when 'mysql'
+ @charset = ENV['CHARSET'] || 'utf8'
+ @collation = ENV['COLLATION'] || 'utf8_general_ci'
+ begin
+ ActiveRecord::Base.establish_connection(config.merge('database' => nil))
+ ActiveRecord::Base.connection.create_database(config['database'], :charset => (config['charset'] || @charset),
+ :collation => (config['collation'] || @collation))
+ ActiveRecord::Base.establish_connection(config)
+ rescue
+ $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset},
+ collation: #{config['collation'] || @collation}"
+ end
+ when 'postgresql'
+ @encoding = config[:encoding] || ENV['CHARSET'] || 'utf8'
+ begin
+ ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
+ ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding))
+ ActiveRecord::Base.establish_connection(config)
+ rescue
+ $stderr.puts $!, *($!.backtrace)
+ $stderr.puts "Couldn't create database for #{config.inspect}"
+ end
+ end
+ else
+ $stderr.puts "#{config['database']} already exists"
+ end
+ end
+
+ task :environment do
+ MIGRATIONS_DIR = 'db/migrate'
+ end
+
+ task :configuration => :environment do
+ @config = YAML.load_file('config/database.yml')
+ end
+
+ task :configure_connection => :configuration do
+ ActiveRecord::Base.establish_connection @config
+ ActiveRecord::Base.logger = Logger.new STDOUT if @config['logger']
+ end
+
+ desc 'Create the database from config/database.yml'
+ task :create => :configure_connection do
+ create_database @config
+ end
+
+ desc 'Drops the database'
+ task :drop => :configuration do
+ if @config['adapter'] == 'postgresql'
+ ActiveRecord::Base.establish_connection(@config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
+ ActiveRecord::Base.connection.drop_database @config['database']
+ else
+ ActiveRecord::Base.establish_connection @config
+ ActiveRecord::Base.connection.drop_database @config['database']
+ end
+ end
+
+ desc 'Migrate the database (options: VERSION=x, VERBOSE=false).'
+ task :migrate => :configure_connection do
+ ActiveRecord::Migration.verbose = true
+ ActiveRecord::Migrator.migrate MIGRATIONS_DIR, ENV['VERSION'] ? ENV['VERSION'].to_i : nil
+ end
+
+ desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'
+ task :rollback => :configure_connection do
+ step = ENV['STEP'] ? ENV['STEP'].to_i : 1
+ ActiveRecord::Migrator.rollback MIGRATIONS_DIR, step
+ end
+
+ desc "Retrieves the current schema version number"
+ task :version => :configure_connection do
+ puts "Current version: #{ActiveRecord::Migrator.current_version}"
+ end
+end
View
@@ -0,0 +1,8 @@
+adapter: postgresql
+username: omid
+password: # Your password
+host: localhost
+pool: 5
+database: ar-skel
+encoding: utf8
+logger: true

0 comments on commit 3c78ee8

Please sign in to comment.