From ad70c0b9065a4a95a1d2fc05147791ba2fcc2258 Mon Sep 17 00:00:00 2001 From: Andrew Kalek Date: Sat, 7 May 2016 14:45:36 -0700 Subject: [PATCH] Improved rake tasks for database setup Database will be created if it doesn't exist. Included Postgres settings for testing but currently no tests are written. --- Rakefile | 90 +++++++++++++++++++++++------------ spec/support/database.example | 9 ++++ 2 files changed, 68 insertions(+), 31 deletions(-) diff --git a/Rakefile b/Rakefile index 27a7714..b7e5ff2 100644 --- a/Rakefile +++ b/Rakefile @@ -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 @@ -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'] \ No newline at end of file +task default: ['test'] \ No newline at end of file diff --git a/spec/support/database.example b/spec/support/database.example index c20281d..c6e8a57 100644 --- a/spec/support/database.example +++ b/spec/support/database.example @@ -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