mack-active_record 0.5.5 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. data/lib/database.rb +118 -0
  2. data/lib/genosaurus_helpers.rb +40 -0
  3. data/lib/helpers/orm_helpers.rb +15 -15
  4. data/lib/mack-active_record.rb +16 -14
  5. data/lib/migration_generator/migration_generator.rb +1 -1
  6. data/lib/model_column.rb +55 -0
  7. data/lib/model_generator/manifest.yml +9 -1
  8. data/lib/model_generator/model_generator.rb +5 -1
  9. data/lib/model_generator/templates/rspec.rb.template +9 -0
  10. data/lib/scaffold_generator/manifest.yml +9 -2
  11. data/lib/scaffold_generator/scaffold_generator.rb +6 -5
  12. data/lib/scaffold_generator/templates/app/controllers/controller.rb.template +8 -7
  13. data/lib/scaffold_generator/templates/spec.rb.template +47 -0
  14. data/lib/tasks/db_create_drop_tasks.rake +39 -57
  15. data/lib/tasks/db_migration_tasks.rake +9 -74
  16. metadata +15 -45
  17. data/test/database.yml +0 -3
  18. data/test/fixtures/add_users_migration.rb.fixture +0 -9
  19. data/test/fixtures/album.rb.fixture +0 -3
  20. data/test/fixtures/album_unit_test.rb.fixture +0 -9
  21. data/test/fixtures/create_users_migration.rb.fixture +0 -12
  22. data/test/fixtures/routes.rb.fixture +0 -3
  23. data/test/fixtures/zoo.rb.fixture +0 -3
  24. data/test/fixtures/zoo_no_cols/edit.html.erb.fixture +0 -11
  25. data/test/fixtures/zoo_no_cols/index.html.erb.fixture +0 -20
  26. data/test/fixtures/zoo_no_cols/new.html.erb.fixture +0 -11
  27. data/test/fixtures/zoo_no_cols/show.html.erb.fixture +0 -6
  28. data/test/fixtures/zoo_with_cols/edit.html.erb.fixture +0 -19
  29. data/test/fixtures/zoo_with_cols/index.html.erb.fixture +0 -26
  30. data/test/fixtures/zoo_with_cols/new.html.erb.fixture +0 -19
  31. data/test/fixtures/zoo_with_cols/show.html.erb.fixture +0 -22
  32. data/test/fixtures/zoo_with_cols/zoos_controller.rb.fixture +0 -50
  33. data/test/generators/migration_generator_test.rb +0 -71
  34. data/test/generators/model_generator_test.rb +0 -37
  35. data/test/generators/scaffold_generator_test.rb +0 -61
  36. data/test/lib/user.rb +0 -3
  37. data/test/tasks/db_migration_tasks_test.rb +0 -57
  38. data/test/test_helper.rb +0 -77
data/lib/database.rb ADDED
@@ -0,0 +1,118 @@
1
+ #
2
+ # AR db create/drop.
3
+ # Currently it supports 3 adapters: sqlite3, postgresql, and mysql
4
+ #
5
+ # ds - July 2008
6
+ #
7
+
8
+ module Mack
9
+ module Database
10
+
11
+ module Migrator
12
+ def self.version
13
+ ActiveRecord::Migrator.current_version
14
+ end
15
+
16
+ def self.migrate
17
+ ActiveRecord::Migrator.up(File.join(Mack.root, "db", "migrations"))
18
+ end
19
+
20
+ def self.rollback(step = 1)
21
+ step = (ENV["STEP"] || step).to_i
22
+ cur_version = version.to_i
23
+ target_version = cur_version - step
24
+ target_version = 0 if target_version < 0
25
+
26
+ ActiveRecord::Migrator.down(File.join(Mack.root, "db", "migrations"), target_version)
27
+ end
28
+
29
+ end
30
+
31
+ def self.db_settings(env)
32
+ dbs = YAML::load(ERB.new(IO.read(File.join(Mack.root, "config", "database.yml"))).result)
33
+ dbs = dbs[env]
34
+ dbs.symbolize_keys!
35
+ return dbs
36
+ end
37
+
38
+ def self.establish_connection(env)
39
+ dbs = db_settings(env)
40
+ ActiveRecord::Base.establish_connection(dbs)
41
+ end
42
+
43
+ # Perform db create or drop
44
+ #
45
+ # By default the mode is drop then create, but caller will be able to
46
+ # call this routine with a specific action (:drop, :create, or :drop_and_create)
47
+ #
48
+ def self.drop_or_create_database(env, mode = :drop_and_create)
49
+ dbs = db_settings(env)
50
+ case dbs[:adapter]
51
+ when "mysql"
52
+ establish_mysql_connection
53
+ drop_mysql_db(env, dbs) if mode == :drop or mode == :drop_and_create
54
+ create_mysql_db(env, dbs) if mode == :create or mode == :drop_and_create
55
+
56
+ when "postgresql"
57
+ ENV['PGHOST'] = dbs[:host] if dbs[:host]
58
+ ENV['PGPORT'] = dbs[:port].to_s if dbs[:port]
59
+ ENV['PGPASSWORD'] = dbs[:password].to_s if dbs[:password]
60
+
61
+ ActiveRecord::Base.clear_active_connections!
62
+ drop_postgresql_db(env, dbs) if mode == :drop or mode == :drop_and_create
63
+ create_postgresql_db(env, dbs) if mode == :create or mode == :drop_and_create
64
+
65
+ when "sqlite3"
66
+ ActiveRecord::Base.clear_active_connections!
67
+ FileUtils.rm_rf(dbs[:database]) if mode == :drop or mode == :drop_and_create
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ def self.drop_postgresql_db(env, dbs)
74
+ begin
75
+ puts "Dropping (PostgreSQL): #{dbs[:database]}"
76
+ `dropdb -U "#{dbs[:username]}" #{dbs[:database]}`
77
+ rescue Exception => e
78
+ puts e
79
+ end
80
+ end
81
+
82
+ def self.create_postgresql_db(env, dbs)
83
+ begin
84
+ enc_option = "-E #{dbs[:encoding]}" if dbs[:encoding]
85
+ puts "Creating (PostgreSQL): #{dbs[:database]}"
86
+ `createdb #{enc_option} -U "#{dbs[:username]}" #{dbs[:database]}`
87
+ rescue Exception => e
88
+ puts e
89
+ end
90
+ end
91
+
92
+ def self.establish_mysql_connection
93
+ # connect to mysql meta database
94
+ ActiveRecord::Base.establish_connection(
95
+ :adapter => "mysql",
96
+ :host => "localhost",
97
+ :database => "mysql",
98
+ :username => ENV["DB_USERNAME"] || "root",
99
+ :password => ENV["DB_PASSWORD"] || ""
100
+ )
101
+ end
102
+
103
+ def self.create_mysql_db(env, dbs)
104
+ if dbs[:collation]
105
+ puts "Dropping (MySQL): #{dbs[:database]}"
106
+ ActiveRecord::Base.connection.execute "CREATE DATABASE `#{dbs[:database]}` DEFAULT CHARACTER SET `#{dbs[:charset] || 'utf8'}` COLLATE `#{dbs[:collation]}`"
107
+ else
108
+ puts "Creating (MySQL): #{dbs[:database]}"
109
+ ActiveRecord::Base.connection.execute "CREATE DATABASE `#{dbs[:database]}` DEFAULT CHARACTER SET `#{dbs[:charset] || 'utf8'}`"
110
+ end
111
+ end
112
+
113
+ def self.drop_mysql_db(env, dbs)
114
+ puts "Dropping (MySQL): #{dbs[:database]}"
115
+ ActiveRecord::Base.connection.execute "DROP DATABASE IF EXISTS `#{dbs[:database]}`"
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,40 @@
1
+ module Mack
2
+ module Genosaurus # :nodoc:
3
+ module ActiveRecord # :nodoc:
4
+ module Helpers
5
+
6
+ def columns(name = param(:name))
7
+ ivar_cache("form_columns") do
8
+ cs = []
9
+ cols = (param(:cols) || param(:columns))
10
+ if cols
11
+ cols.split(",").each do |x|
12
+ cs << Mack::Genosaurus::ActiveRecord::ModelColumn.new(name, x)
13
+ end
14
+ end
15
+ cs
16
+ end
17
+ end
18
+
19
+ def db_directory
20
+ File.join(Mack.root, "db")
21
+ end
22
+
23
+ def migrations_directory
24
+ File.join(db_directory, "migrations")
25
+ end
26
+
27
+ def next_migration_number
28
+ last = Dir.glob(File.join(migrations_directory, "*.rb")).last
29
+ if last
30
+ return File.basename(last).match(/^\d+/).to_s.succ
31
+ end
32
+ return "001"
33
+ end
34
+
35
+ ::Genosaurus.send(:include, self)
36
+
37
+ end # Helpers
38
+ end # ActiveRecord
39
+ end # Genosaurus
40
+ end # Mack
@@ -6,25 +6,25 @@ end
6
6
 
7
7
  module Mack
8
8
  module ViewHelpers
9
- module OrmHelpers
9
+ module ActiveRecordHelpers
10
10
  DEFAULT_PARTIAL = %{
11
- <div>
12
- <div class="errorExplanation" id="errorExplanation">
13
- <h2><%= pluralize_word(errors.size, "error") %> occured.</h2>
14
- <ul>
15
- <% for error in errors %>
16
- <li><%= error %></li>
17
- <% end %>
18
- </ul>
19
- </div>
20
- </div>
11
+ <div>
12
+ <div class="errorExplanation" id="errorExplanation">
13
+ <h2><%= pluralize_word(errors.size, "error") %> occured.</h2>
14
+ <ul>
15
+ <% for error in errors %>
16
+ <li><%= error %></li>
17
+ <% end %>
18
+ </ul>
19
+ </div>
20
+ </div>
21
21
  }
22
22
 
23
23
  def error_messages_for(object_names = [], view_partial = nil)
24
24
  object_names = [object_names]
25
25
  object_names.flatten!
26
26
  app_errors = []
27
- object_names.each do |name|
27
+ object_names.each do |name|
28
28
  object = instance_variable_get("@#{name}")
29
29
  if object
30
30
  object.errors.each do |key, value|
@@ -55,13 +55,13 @@ module Mack
55
55
  end
56
56
  end
57
57
  end
58
- File.join(Mack::Configuration.views_directory, "application", "_error_messages.html.erb")
59
58
  unless app_errors.empty?
59
+ app_errors.uniq!
60
60
  if view_partial.nil?
61
- if File.exist?(File.join(Mack::Configuration.views_directory, "application", "_error_messages.html.erb"))
61
+ if File.exist?(File.join(Mack.root, "app", "views", "application", "_error_messages.html.erb"))
62
62
  render(:partial, "application/error_messages", :locals => {:errors => app_errors})
63
63
  else
64
- render(:text, DEFAULT_PARTIAL, :locals => {:errors => app_errors})
64
+ render(:inline, DEFAULT_PARTIAL, :locals => {:errors => app_errors})
65
65
  end
66
66
  else
67
67
  render(:partial, view_partial, :locals => {:errors => app_errors})
@@ -1,23 +1,25 @@
1
1
  require 'rubygems'
2
2
  require 'genosaurus'
3
- require 'erubis'
4
- begin
5
- require 'mack-orm_common'
6
- rescue Exception => e
7
- puts e
8
- end
9
3
 
10
4
  require 'activerecord'
11
5
 
12
- dbs = Mack::Configuration.database_configurations
13
-
14
- unless dbs.nil?
15
- ActiveRecord::Base.establish_connection(dbs[Mack::Configuration.env])
16
- class SchemaInfo < ActiveRecord::Base # :nodoc:
17
- set_table_name 'schema_info'
18
- end
6
+ module ActiveRecord # :nodoc:
19
7
  end
8
+
9
+ fl = File.join(File.dirname(__FILE__))
10
+
11
+ require File.join(fl, "database")
12
+ require File.join(fl, "helpers", "orm_helpers")
13
+ require File.join(fl, "model_column")
14
+ require File.join(fl, "genosaurus_helpers")
15
+
16
+ # [:migration, :model, :scaffold].each do |gen|
17
+ # require File.join(fl, "#{gen}_generator", "#{gen}_generator")
18
+ # end
20
19
  [:helpers, :migration_generator, :model_generator, :scaffold_generator].each do |folder|
21
20
  Dir.glob(File.join(File.dirname(__FILE__), folder.to_s, "**/*.rb")).each {|f| require f}
22
21
  end
23
- # Dir.glob(File.join(File.dirname(__FILE__), "tasks", "**/*.rake")).each {|f| load f}
22
+
23
+ ActiveRecord::Base.logger = Mack.logger
24
+
25
+ Mack::Database.establish_connection(Mack.env)
@@ -33,7 +33,7 @@ class MigrationGenerator < Genosaurus
33
33
 
34
34
  require_param :name
35
35
 
36
- def setup
36
+ def setup # :nodoc:
37
37
  @table_name = param(:name).underscore.plural.gsub("create_", "")
38
38
  @migration_name = "#{next_migration_number}_#{param(:name).underscore}"
39
39
  end
@@ -0,0 +1,55 @@
1
+ module Mack
2
+ module Genosaurus
3
+ module ActiveRecord
4
+ # Used to represent a 'column' from the param cols or columns for generators.
5
+ class ModelColumn
6
+
7
+ # The name of the column.
8
+ attr_accessor :column_name
9
+ # The type of the column. Ie. string, integer, datetime, etc...
10
+ attr_accessor :column_type
11
+ # The name of the model associated with the column. Ie. user, post, etc...
12
+ attr_accessor :model_name
13
+
14
+ # Takes in the model_name (user, post, etc...) and the column (username:string, body:text, etc...)
15
+ def initialize(model_name, column_unsplit)
16
+ self.model_name = model_name.singular.underscore
17
+ cols = column_unsplit.split(":")
18
+ self.column_name = cols.first#.underscore
19
+ self.column_type = cols.last#.underscore
20
+ end
21
+
22
+ # Examples:
23
+ # Mack::Generator::ColumnObject.new("user", "username:string").form_element_name # => "user[username]"
24
+ # Mack::Generator::ColumnObject.new("Post", "body:text").form_element_name # => "post[body]"
25
+ def form_element_name
26
+ "#{self.model_name}[#{self.column_name}]"
27
+ end
28
+
29
+ # Examples:
30
+ # Mack::Generator::ColumnObject.new("user", "username:string").form_element_id # => "user_username"
31
+ # Mack::Generator::ColumnObject.new("Post", "body:text").form_element_id # => "post_body"
32
+ def form_element_id
33
+ "#{self.model_name}_#{self.column_name}"
34
+ end
35
+
36
+ # Generates the appropriate HTML form field for the type of column represented.
37
+ #
38
+ # Examples:
39
+ # Mack::Generator::ColumnObject.new("user", "username:string").form_field
40
+ # => "<input type="text" name="user[username]" id="user_username" size="30" value="<%= user.username %>" />"
41
+ # Mack::Generator::ColumnObject.new("Post", "body:text").form_field
42
+ # => "<textarea name="post[body]" id="post_id"><%= post.body %></textarea>"
43
+ def form_field
44
+ case self.column_type
45
+ when "text"
46
+ %{<textarea name="#{self.form_element_name}" id="#{self.form_element_id}" cols="60" rows="20"><%= @#{self.model_name}.#{self.column_name} %></textarea>}
47
+ else
48
+ %{<input type="text" name="#{self.form_element_name}" id="#{self.form_element_id}" size="30" value="<%= @#{self.model_name}.#{self.column_name} %>" />}
49
+ end
50
+ end
51
+
52
+ end # ModelColumn
53
+ end # ActiveRecord
54
+ end # Generator
55
+ end # Mack
@@ -5,7 +5,15 @@ model_template:
5
5
  type: file
6
6
  template_path: <%= File.join(templates_directory_path, "model.rb.template") %>
7
7
  output_path: <%= File.join("app", "models", "#{param(:name).singular.underscore}.rb") %>
8
+
9
+ <% if testing_framework == "test_case" %>
8
10
  test_template:
9
11
  type: file
10
12
  template_path: <%= File.join(templates_directory_path, "test.rb.template") %>
11
- output_path: <%= File.join("test", "unit", "#{param(:name).singular.underscore}_test.rb") %>
13
+ output_path: <%= File.join("test", "unit", "#{param(:name).singular.underscore}_test.rb") %>
14
+ <% elsif testing_framework == "rspec" %>
15
+ test_template:
16
+ type: file
17
+ template_path: <%= File.join(templates_directory_path, "rspec.rb.template") %>
18
+ output_path: <%= File.join("test", "unit", "#{param(:name).singular.underscore}_spec.rb") %>
19
+ <% end %>
@@ -39,8 +39,12 @@ class ModelGenerator < Genosaurus
39
39
 
40
40
  require_param :name
41
41
 
42
- def after_generate
42
+ def after_generate # :nodoc:
43
43
  MigrationGenerator.run(@options.merge({"name" => "create_#{param(:name).plural}"}))
44
44
  end
45
45
 
46
+ def testing_framework # :nodoc:
47
+ app_config.mack.testing_framework
48
+ end
49
+
46
50
  end
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(__FILE__), "..", "spec_helper.rb")
2
+
3
+ describe "<%= param(:name).singular.camelcase %>" do
4
+
5
+ it "should not blow up" do
6
+ true
7
+ end
8
+
9
+ end
@@ -18,7 +18,14 @@ show_template:
18
18
  type: file
19
19
  template_path: <%= File.join(templates_directory_path, "app", "views", "show.html.erb.template") %>
20
20
  output_path: <%= File.join("app", "views", @name_plural, "show.html.erb") %>
21
- functional_teat_template:
21
+ <% if @test_framework == "test_case" -%>
22
+ functional_test_template:
22
23
  type: file
23
24
  template_path: <%= File.join(templates_directory_path, "test.rb.template") %>
24
- output_path: <%= File.join("test", "functional", "#{@name_plural}_controller_test.rb") %>
25
+ output_path: <%= File.join("test", "functional", "#{@name_plural}_controller_test.rb") %>
26
+ <% elsif @test_framework == "rspec" -%>
27
+ functional_test_template:
28
+ type: file
29
+ template_path: <%= File.join(templates_directory_path, "spec.rb.template") %>
30
+ output_path: <%= File.join("test", "functional", "#{@name_plural}_controller_spec.rb") %>
31
+ <% end -%>
@@ -6,21 +6,22 @@ class ScaffoldGenerator < Genosaurus
6
6
 
7
7
  require_param :name
8
8
 
9
- def setup
9
+ def setup # :nodoc:
10
10
  @name_singular = param(:name).singular.underscore
11
11
  @name_plural = param(:name).plural.underscore
12
12
  @name_singular_camel = @name_singular.camelcase
13
- @name_plural_camel = @name_plural.camelcase
13
+ @name_plural_camel = @name_plural.camelcase
14
+ @test_framework = app_config.mack.testing_framework
14
15
  end
15
16
 
16
- def after_generate
17
+ def after_generate # :nodoc:
17
18
  ModelGenerator.run(@options)
18
19
  update_routes_file
19
20
  end
20
21
 
21
- def update_routes_file
22
+ def update_routes_file # :nodoc:
22
23
  # update routes.rb
23
- routes = File.join(Mack::Configuration.config_directory, "routes.rb")
24
+ routes = File.join(Mack.root, "config", "routes.rb")
24
25
  rf = File.open(routes).read
25
26
  unless rf.match(".resource :#{@name_plural}")
26
27
  puts "Updating routes.rb"
@@ -1,4 +1,5 @@
1
- class <%= @name_plural_camel %>Controller < Mack::Controller::Base
1
+ class <%= @name_plural_camel %>Controller
2
+ include Mack::Controller
2
3
 
3
4
  # GET /<%= @name_plural %>
4
5
  def index
@@ -7,7 +8,7 @@ class <%= @name_plural_camel %>Controller < Mack::Controller::Base
7
8
 
8
9
  # GET /<%= @name_plural %>/1
9
10
  def show
10
- @<%= @name_singular %> = <%= @name_singular_camel %>.find(params(:id))
11
+ @<%= @name_singular %> = <%= @name_singular_camel %>.find(params[:id])
11
12
  end
12
13
 
13
14
  # GET /<%= @name_plural %>/new
@@ -17,12 +18,12 @@ class <%= @name_plural_camel %>Controller < Mack::Controller::Base
17
18
 
18
19
  # GET /<%= @name_plural %>/1/edit
19
20
  def edit
20
- @<%= @name_singular %> = <%= @name_singular_camel %>.find(params(:id))
21
+ @<%= @name_singular %> = <%= @name_singular_camel %>.find(params[:id])
21
22
  end
22
23
 
23
24
  # POST /<%= @name_plural %>
24
25
  def create
25
- @<%= @name_singular %> = <%= @name_singular_camel %>.new(params(:<%= @name_singular %>))
26
+ @<%= @name_singular %> = <%= @name_singular_camel %>.new(params[:<%= @name_singular %>])
26
27
  if @<%= @name_singular %>.save
27
28
  redirect_to(<%= @name_plural %>_show_url(:id => @<%= @name_singular %>.id))
28
29
  else
@@ -32,8 +33,8 @@ class <%= @name_plural_camel %>Controller < Mack::Controller::Base
32
33
 
33
34
  # PUT /<%= @name_plural %>/1
34
35
  def update
35
- @<%= @name_singular %> = <%= @name_singular_camel %>.find(params(:id))
36
- if @<%= @name_singular %>.update_attributes(params(:<%= @name_singular %>))
36
+ @<%= @name_singular %> = <%= @name_singular_camel %>.find(params[:id])
37
+ if @<%= @name_singular %>.update_attributes(params[:<%= @name_singular %>])
37
38
  redirect_to(<%= @name_plural %>_show_url(:id => @<%= @name_singular %>.id))
38
39
  else
39
40
  render(:action, "edit")
@@ -42,7 +43,7 @@ class <%= @name_plural_camel %>Controller < Mack::Controller::Base
42
43
 
43
44
  # DELETE /<%= @name_plural %>/1
44
45
  def delete
45
- @<%= @name_singular %> = <%= @name_singular_camel %>.find(params(:id))
46
+ @<%= @name_singular %> = <%= @name_singular_camel %>.find(params[:id])
46
47
  @<%= @name_singular %>.destroy
47
48
  redirect_to(<%= @name_plural %>_index_url)
48
49
  end
@@ -0,0 +1,47 @@
1
+ require File.join(File.dirname(__FILE__), "..", "spec_helper")
2
+
3
+ describe <%= @name_plural_camel %>Controller do
4
+
5
+ describe "index" do
6
+
7
+ it "should list <%= @name_plural %>"
8
+
9
+ end
10
+
11
+ describe "show" do
12
+
13
+ it "should show a <%= @name_singular %>"
14
+
15
+ end
16
+
17
+ describe "new" do
18
+
19
+ it "should show a form to create a new <%= @name_singular %>"
20
+
21
+ end
22
+
23
+ describe "edit" do
24
+
25
+ it "should edit a <%= @name_singular %>"
26
+
27
+ end
28
+
29
+ describe "create" do
30
+
31
+ it "should create a <%= @name_singular %>"
32
+
33
+ end
34
+
35
+ describe "update" do
36
+
37
+ it "should update a <%= @name_singular %>"
38
+
39
+ end
40
+
41
+ describe "delete" do
42
+
43
+ it "should delete a <%= @name_singular %>"
44
+
45
+ end
46
+
47
+ end
@@ -1,70 +1,52 @@
1
1
  require 'rake'
2
2
  namespace :db do
3
+
4
+ task :drop => :environment do
5
+ Mack::Database.drop_or_create_database(Mack.env, :drop)
6
+ end
7
+
8
+ namespace :drop do
9
+ desc "Drop databases for both development and test environemnt"
10
+ task :all => :environment do
11
+ Mack::Database.drop_or_create_database("development", :drop)
12
+ Mack::Database.drop_or_create_database("test", :drop)
13
+ end
14
+ end
15
+
16
+ task :create do
17
+ puts Mack.env
18
+ Mack::Database.drop_or_create_database(Mack.env, :create)
19
+ end
3
20
 
4
- desc "Create the database for your environment."
5
- task :create => :environment do
6
- drop_create_database
21
+ task :recreate do
22
+ puts Mack.env
23
+ Mack::Database.drop_or_create_database(Mack.env, :drop_and_create)
7
24
  end
8
25
 
9
26
  namespace :create do
10
-
11
27
  desc "Creates your Full environment. Does NOT create your production database!"
12
28
  task :all => :environment do
13
- drop_create_database("development")
14
- drop_create_database("test")
15
- ActiveRecord::Base.establish_connection(Mack::Configuration.database_configurations["development"])
29
+ abcs = YAML::load(ERB.new(IO.read(File.join(Mack.root, "config", "database.yml"))).result)
30
+ db_settings = abcs[Mack.env]
31
+
32
+ Mack::Database.drop_or_create_database("development", :create)
33
+ Mack::Database.drop_or_create_database("test", :create)
34
+ ActiveRecord::Base.establish_connection(db_settings)
16
35
  Rake::Task["db:migrate"].invoke
17
36
  end
18
-
19
37
  end
20
-
21
- end
22
-
23
- private
24
- def drop_create_database(env = Mack::Configuration.env)
25
- abcs = Mack::Configuration.database_configurations
26
- db_settings = abcs[env]
27
- case db_settings["adapter"]
28
- when "mysql"
29
- ActiveRecord::Base.establish_connection(
30
- :adapter => "mysql",
31
- :host => "localhost",
32
- :database => "mysql",
33
- :username => ENV["DB_USERNAME"] || "root",
34
- :password => ENV["DB_PASSWORD"] || ""
35
- )
36
- puts "Dropping (MySQL): #{db_settings["database"]}"
37
- ActiveRecord::Base.connection.execute "DROP DATABASE IF EXISTS `#{db_settings["database"]}`"
38
-
39
- if db_settings["collation"]
40
- puts "Dropping (MySQL): #{db_settings["database"]}"
41
- ActiveRecord::Base.connection.execute "CREATE DATABASE `#{db_settings["database"]}` DEFAULT CHARACTER SET `#{db_settings["charset"] || 'utf8'}` COLLATE `#{db_settings["collation"]}`"
42
- else
43
- puts "Creating (MySQL): #{db_settings["database"]}"
44
- ActiveRecord::Base.connection.execute "CREATE DATABASE `#{db_settings["database"]}` DEFAULT CHARACTER SET `#{db_settings["charset"] || 'utf8'}`"
45
- end
46
- when "postgresql"
47
- ENV['PGHOST'] = db_settings["host"] if db_settings["host"]
48
- ENV['PGPORT'] = db_settings["port"].to_s if db_settings["port"]
49
- ENV['PGPASSWORD'] = db_settings["password"].to_s if db_settings["password"]
50
- enc_option = "-E #{db_settings["encoding"]}" if db_settings["encoding"]
51
-
52
- ActiveRecord::Base.clear_active_connections!
53
- begin
54
- puts "Dropping (PostgreSQL): #{db_settings["database"]}"
55
- `dropdb -U "#{db_settings["username"]}" #{db_settings["database"]}`
56
- rescue Exception => e
57
- end
38
+
39
+ namespace :recreate do
40
+ desc "Creates your Full environment. Does NOT create your production database!"
41
+ task :all => :environment do
42
+ abcs = YAML::load(ERB.new(IO.read(File.join(Mack.root, "config", "database.yml"))).result)
43
+ db_settings = abcs[Mack.env]
58
44
 
59
- begin
60
- puts "Creating (PostgreSQL): #{db_settings["database"]}"
61
- `createdb #{enc_option} -U "#{db_settings["username"]}" #{db_settings["database"]}`
62
- rescue Exception => e
63
- end
64
- when 'sqlite3'
65
- ActiveRecord::Base.clear_active_connections!
66
- FileUtils.rm_rf(db_settings["database"])
67
- else
68
- raise "Task not supported by '#{db_settings["adapter"]}'"
45
+ Mack::Database.drop_or_create_database("development")
46
+ Mack::Database.drop_or_create_database("test")
47
+ ActiveRecord::Base.establish_connection(db_settings)
48
+ Rake::Task["db:migrate"].invoke
49
+ end
69
50
  end
70
- end
51
+
52
+ end