Skip to content

Commit

Permalink
Improved rake tasks for database setup
Browse files Browse the repository at this point in the history
Database will be created if it doesn't exist.
Included Postgres settings for testing but currently no tests are written.
  • Loading branch information
anlek committed May 7, 2016
1 parent a7b84f4 commit ad70c0b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 31 deletions.
90 changes: 59 additions & 31 deletions Rakefile
Expand Up @@ -4,7 +4,6 @@ Bundler::GemHelper.install_tasks
require 'cucumber/rake/task'
require 'rspec/core/rake_task'


task :cleanup_rcov_files do
rm_rf 'coverage.data'
end
Expand Down Expand Up @@ -51,64 +50,93 @@ namespace :test do
Cucumber::Rake::Task.new(:cucumber)

namespace :setup do
desc "Setup a postgresql database based on the spec/support/database.yml settings (under postgresql)"
task :postgresql do
task :environment do
require 'mongify'
require './spec/support/config_reader'
require 'yaml'
end

desc "Setup a postgresql database based on the spec/support/database.yml settings (under postgresql)"
task postgresql: :environment do
require 'active_record'
require 'pg'

::CONNECTION_CONFIG = ConfigReader.new('spec/support/database.yml')

create_pg_database(CONNECTION_CONFIG)

ActiveRecord::Base.establish_connection(CONNECTION_CONFIG.postgresql)
conn = ActiveRecord::Base.connection
conn.create_table(:users, :force => true) do |t|
t.string :first_name, :last_name
t.timestamps
end

conn.create_table(:posts, :force => true) do |t|
t.string :title
t.integer :owner_id
t.text :body
t.datetime :published_at
t.timestamps
end
build_tables(conn)

conn.create_table(:comments, :force => true) do |t|
t.text :body
t.integer :post_id
t.integer :user_id
t.timestamps
end
puts "Finished"
puts "Database Setup Finished"
end

desc "Setup a mysql database based on the spec/support/database.yml settings"
task :mysql do
require './spec/support/config_reader'
task mysql: :environment do
require 'mysql2'
require 'active_record'

::CONNECTION_CONFIG = ConfigReader.new('spec/support/database.yml')

create_mysql_database(CONNECTION_CONFIG)

ActiveRecord::Base.establish_connection(CONNECTION_CONFIG.mysql)
conn = ActiveRecord::Base.connection
conn.create_table(:users, :force => true) do |t|

build_tables(conn)

puts "Database Setup Finished"
end

#######
private
#######

def build_tables(conn)
conn.create_table(:users, force: true) do |t|
t.string :first_name, :last_name
t.timestamps
t.timestamps null: false
end

conn.create_table(:posts, :force => true) do |t|
conn.create_table(:posts, force: true) do |t|
t.string :title
t.integer :owner_id
t.text :body
t.datetime :published_at
t.timestamps
t.timestamps null: false
end

conn.create_table(:comments, :force => true) do |t|
conn.create_table(:comments, force: true) do |t|
t.text :body
t.integer :post_id
t.integer :user_id
t.timestamps
t.timestamps null: false
end
puts "Finished"
end

def create_mysql_database(config)
client = Mysql2::Client.new(host: config.mysql["host"] || "localhost",
username: config.mysql["username"] || "root",
password: config.mysql["password"])

client.query("CREATE DATABASE IF NOT EXISTS #{config.mysql["database"]}")
client.close
end

def create_pg_database(config)
client = PG.connect(host: config.postgresql["host"] || "localhost",
dbname: 'postgres',
user: config.postgresql["username"] || "root",
password: config.postgresql["password"])

missing_db = client.exec("SELECT 1 FROM pg_database where datname='#{config.postgresql["database"]}'").values.empty?
client.exec("CREATE DATABASE #{config.postgresql["database"]}") if missing_db
client.flush
client.close
end
end
end

task :default => ['test']
task default: ['test']
9 changes: 9 additions & 0 deletions spec/support/database.example
Expand Up @@ -10,6 +10,15 @@ mysql:
host: localhost
port: 3306

postgresql:
adapter: postgresql
database: mongify_test
encoding: unicode
username: root
password: passw0rd
host: localhost
port: 5432

mongo:
adapter: mongo
database: mongify_test
Expand Down

0 comments on commit ad70c0b

Please sign in to comment.