padrino-gen 0.12.0 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +1 -1
  3. data/lib/padrino-gen.rb +0 -1
  4. data/lib/padrino-gen/generators/app.rb +22 -14
  5. data/lib/padrino-gen/generators/app/app.rb.tt +0 -1
  6. data/lib/padrino-gen/generators/cli.rb +1 -2
  7. data/lib/padrino-gen/generators/components/actions.rb +1 -1
  8. data/lib/padrino-gen/generators/components/mocks/rr.rb +2 -2
  9. data/lib/padrino-gen/generators/components/orms/activerecord.rb +3 -0
  10. data/lib/padrino-gen/generators/components/orms/datamapper.rb +21 -0
  11. data/lib/padrino-gen/generators/components/orms/dynamoid.rb +67 -0
  12. data/lib/padrino-gen/generators/components/orms/minirecord.rb +3 -0
  13. data/lib/padrino-gen/generators/components/tests/cucumber.rb +1 -1
  14. data/lib/padrino-gen/generators/components/tests/minitest.rb +1 -1
  15. data/lib/padrino-gen/generators/components/tests/rspec.rb +4 -4
  16. data/lib/padrino-gen/generators/model.rb +73 -29
  17. data/lib/padrino-gen/generators/project.rb +7 -3
  18. data/lib/padrino-gen/generators/project/config/apps.rb.tt +0 -1
  19. data/lib/padrino-gen/generators/templates/Gemfile.tt +1 -1
  20. data/lib/padrino-gen/padrino-tasks/activerecord.rb +43 -39
  21. data/lib/padrino-gen/padrino-tasks/database.rb +2 -1
  22. data/lib/padrino-gen/padrino-tasks/sql-helpers.rb +24 -16
  23. data/test/helper.rb +4 -3
  24. data/test/test_app_generator.rb +13 -13
  25. data/test/test_cli.rb +4 -4
  26. data/test/test_component_generator.rb +18 -18
  27. data/test/test_controller_generator.rb +26 -26
  28. data/test/test_generator.rb +2 -3
  29. data/test/test_mailer_generator.rb +10 -10
  30. data/test/test_migration_generator.rb +42 -27
  31. data/test/test_model_generator.rb +95 -74
  32. data/test/test_plugin_generator.rb +23 -23
  33. data/test/test_project_generator.rb +100 -83
  34. data/test/test_task_generator.rb +7 -7
  35. metadata +9 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 529163bdc909b9c5aad2faa4ba1dcf647290eef3
4
- data.tar.gz: 3fd83aca799e92e14220d0447f8a0145e279aade
3
+ metadata.gz: a7628694455061d5f6906099df64d2570594b629
4
+ data.tar.gz: 30c14c0393ea4289e1aa41d06d579646e1f864b7
5
5
  SHA512:
6
- metadata.gz: 49e3c1b34292d216122cd51ce66d23359b38df3409966063f5db03ace99ea3dea3a077edc1503c6cffdb2055046625b1b47aa53acadf5913aa581ecd6891ef0a
7
- data.tar.gz: 3aa0eea49ffdb6dc2b020d1a551423d68e86c71c2b942d799327ce0df3f9abb49d7eec7ecbc1843d66cc2cd696c600ffbdace4d45ec47680e10172abb2e80c52
6
+ metadata.gz: c230053d26d0c623f1f03b5ef036f7f0b0315f14bdc3b3abeb8d8cfc327584407d27b3d04fa590c5fdaf4110d2a71d309ddf9ca7697c91dbe600a4a7c3991bc2
7
+ data.tar.gz: 226607ee34f61de01dd944de3dd97245c70c2b9b033e978e663c4983edfa05e95cd936667dfa20791324198ad6025c801448a0c664943741832f69c232d5f9d0
data/README.rdoc CHANGED
@@ -43,7 +43,7 @@ renderer:: slim (default), erb, erubis, liquid, haml
43
43
  stylesheet:: none (default), less, compass, sass, scss
44
44
  mock:: none (default), mocha, rr
45
45
  script:: none (default), jquery, prototype, mootools, rightjs, extcore, dojo
46
- orm:: none (default), datamapper, mongomapper, mongoid, activerecord, minirecord, sequel, couchrest, ohm, mongomatic, ripple
46
+ orm:: none (default), datamapper, mongomapper, mongoid, activerecord, minirecord, sequel, couchrest, ohm, mongomatic, ripple, dynamoid
47
47
 
48
48
  In addition, you can generate projects based on existing templates:
49
49
 
data/lib/padrino-gen.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'padrino-core/support_lite'
2
1
  require 'padrino-core/tasks'
3
2
  require 'padrino-gen/command'
4
3
  require 'active_support/ordered_hash'
@@ -35,25 +35,33 @@ module Padrino
35
35
  if in_app_root?
36
36
  @project_name = options[:namespace].underscore.camelize
37
37
  @project_name = fetch_project_name(@app_folder) if @project_name.empty?
38
- if already_exists?(@app_name, @project_name)
38
+
39
+ if options[:destroy]
40
+ self.behavior = :revoke
41
+ else
39
42
  say "#{@app_name} already exists."
40
43
  say "Please, change the name."
41
44
  return
45
+ end if already_exists?(@app_name, @project_name)
46
+
47
+ lowercase_app_folder = @app_folder.downcase
48
+ app_skeleton(lowercase_app_folder, options[:tiny])
49
+ empty_directory destination_root("public/#{lowercase_app_folder}")
50
+
51
+ mount_command = "\nPadrino.mount('#{@project_name}::#{@app_name}', :app_file => Padrino.root('#{lowercase_app_folder}/app.rb')).to('/#{lowercase_app_folder}')\n"
52
+ if File.read(destination_root('config/apps.rb')).match(/^Padrino.mount.*\.to\('\/'\)$/)
53
+ inject_into_file destination_root('config/apps.rb'), mount_command, :before => /^Padrino.mount.*\.to\('\/'\)$/
42
54
  else
43
- lowercase_app_folder = @app_folder.downcase
44
- self.behavior = :revoke if options[:destroy]
45
- app_skeleton(lowercase_app_folder, options[:tiny])
46
- empty_directory destination_root("public/#{lowercase_app_folder}")
47
- inject_into_file destination_root('config/apps.rb'), "\nPadrino.mount('#{@project_name}::#{@app_name}', :app_file => Padrino.root('#{lowercase_app_folder}/app.rb')).to('/#{lowercase_app_folder}')\n", :before => /^Padrino.mount.*\.to\('\/'\)$/
48
-
49
- return if self.behavior == :revoke
50
- say
51
- say '=' * 65, :green
52
- say "Your #{@app_name} application has been installed."
53
- say '=' * 65, :green
54
- say "This application has been mounted to /#{@app_name.downcase}"
55
- say "You can configure a different path by editing 'config/apps.rb'"
55
+ append_file destination_root('config/apps.rb'), mount_command
56
56
  end
57
+
58
+ return if self.behavior == :revoke
59
+ say
60
+ say '=' * 65, :green
61
+ say "Your #{@app_name} application has been installed."
62
+ say '=' * 65, :green
63
+ say "This application has been mounted to /#{@app_name.downcase}"
64
+ say "You can configure a different path by editing 'config/apps.rb'"
57
65
  else
58
66
  say 'You are not at the root of a Padrino application! (config/boot.rb not found)'
59
67
  end
@@ -1,6 +1,5 @@
1
1
  module <%= @project_name %>
2
2
  class <%= @app_name %> < Padrino::Application
3
- register Padrino::Rendering
4
3
  register Padrino::Mailer
5
4
  register Padrino::Helpers
6
5
 
@@ -25,8 +25,7 @@ module Padrino
25
25
  if File.exist?(boot)
26
26
  require File.expand_path(boot)
27
27
  else
28
- # If we are outside app we need to load support_lite
29
- require 'padrino-core/support_lite' unless defined?(SupportLite)
28
+ require 'padrino-support'
30
29
  end
31
30
  rescue StandardError => e
32
31
  puts "=> Problem loading #{boot}"
@@ -67,7 +67,7 @@ module Padrino
67
67
  else
68
68
  return if migration_exist?(filename)
69
69
  change_format = options[:change_format]
70
- migration_scan = filename.underscore.camelize.scan(/(Add|Remove)(?:.*?)(?:To|From)(.*?)$/).flatten
70
+ migration_scan = filename.underscore.camelize.scan(/(Add|Remove).*?(?:(?:To|From).+?)*(?:To|From)((?:To|From)?.*?)$/).flatten
71
71
  direction, table_name = migration_scan[0].downcase, migration_scan[1].downcase.pluralize if migration_scan.any?
72
72
  tuples = direction ? columns.map { |value| value.split(":") } : []
73
73
  tuples.map! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] }
@@ -1,8 +1,8 @@
1
1
  def setup_mock
2
- require_dependencies 'rr', :group => 'test'
2
+ require_dependencies 'rr', :require => false, :group => 'test'
3
3
  case options[:test].to_s
4
4
  when 'rspec'
5
- inject_into_file 'spec/spec_helper.rb', " conf.mock_with :rr\n", :after => "RSpec.configure do |conf|\n"
5
+ inject_into_file 'spec/spec_helper.rb', "require 'rr'\n", :after => "\"/../config/boot\")\n"
6
6
  when 'riot'
7
7
  inject_into_file "test/test_config.rb","require 'riot/rr'\n", :after => "\"/../config/boot\")\n"
8
8
  when 'minitest'
@@ -53,6 +53,9 @@ ActiveSupport.escape_html_entities_in_json = false
53
53
 
54
54
  # Now we can establish connection with our db.
55
55
  ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Padrino.env])
56
+
57
+ # Timestamps are in the utc by default.
58
+ ActiveRecord::Base.default_timezone = :utc
56
59
  AR
57
60
 
58
61
  MYSQL = (<<-MYSQL) unless defined?(MYSQL)
@@ -9,6 +9,27 @@ DM = (<<-DM) unless defined?(DM)
9
9
  # # A Sqlite3 connection
10
10
  # DataMapper.setup(:default, "sqlite3://" + Padrino.root('db', "development.db"))
11
11
  #
12
+ # # Setup DataMapper using config/database.yml
13
+ # DataMapper.setup(:default, YAML.load_file(Padrino.root('config/database.yml'))[RACK_ENV])
14
+ #
15
+ # config/database.yml file:
16
+ #
17
+ # ---
18
+ # development: &defaults
19
+ # adapter: mysql
20
+ # database: example_development
21
+ # username: user
22
+ # password: Pa55w0rd
23
+ # host: 127.0.0.1
24
+ #
25
+ # test:
26
+ # <<: *defaults
27
+ # database: example_test
28
+ #
29
+ # production:
30
+ # <<: *defaults
31
+ # database: example_production
32
+ #
12
33
 
13
34
  DataMapper.logger = logger
14
35
  DataMapper::Property::String.length(255)
@@ -0,0 +1,67 @@
1
+ DYNAMOID = (<<-DYNAMOID) unless defined?(DYNAMOID)
2
+
3
+ AWS.config({
4
+ :access_key_id => ENV['AWS_ACCESS_KEY'],
5
+ :secret_access_key => ENV['AWS_SECRET_KEY'],
6
+ :dynamo_db_endpoint => 'dynamodb.ap-southeast-1.amazonaws.com'
7
+ })
8
+
9
+ Dynamoid.configure do |config|
10
+ config.adapter = 'aws_sdk' # This adapter establishes a connection to the DynamoDB servers using Amazon's own AWS gem.
11
+ config.read_capacity = 100 # Read capacity for your tables
12
+ config.write_capacity = 20 # Write capacity for your tables
13
+ end
14
+
15
+ # If you use mock in testing [for example in case of using fake_dynamo],
16
+ # the way is as following:
17
+ #
18
+ # - install fake_dynamo
19
+ # gem install fake_dynamo --version 0.1.3
20
+ # - run
21
+ # fake_dynamo --port 4567
22
+ #
23
+ # And then setting for AWS.config is as following:
24
+ #
25
+ # AWS.config({
26
+ # :access_key_id => 'xxx', # everything is ok
27
+ # :secret_access_key => 'xxx', # everything is ok
28
+ # :dynamo_db_endpoint => 'localhost', # fake_dynamo runs hostname
29
+ # :dynamo_db_port => 4567, # fake_dynamo listens port
30
+ # :use_ssl => false # fake_dynamo don't speak ssl
31
+ # })
32
+ #
33
+ # Additional information on https://github.com/ananthakumaran/fake_dynamo
34
+ DYNAMOID
35
+
36
+ def setup_orm
37
+ require_dependencies 'aws-sdk'
38
+ require_dependencies 'dynamoid', :version => '~>0.7.1'
39
+ create_file("config/database.rb", DYNAMOID.gsub(/!NAME!/, @project_name.underscore))
40
+ end
41
+
42
+ DYNAMOID_MODEL = (<<-MODEL) unless defined?(DYNAMOID_MODEL)
43
+ class !NAME!
44
+ include Dynamoid::Document
45
+
46
+ !FIELDS!
47
+
48
+ end
49
+ MODEL
50
+
51
+ # options => { :fields => ["title:string", "body:string"], :app => 'app' }
52
+ def create_model_file(name, options={})
53
+ model_path = destination_root(options[:app], 'models', "#{name.to_s.underscore}.rb")
54
+ field_tuples = options[:fields].map { |value| value.split(":") }
55
+ column_declarations = field_tuples.map { |field, kind| "field :#{field}, :#{kind}" }.join("\n ")
56
+ model_contents = DYNAMOID_MODEL.gsub(/!NAME!/, name.to_s.underscore.camelize)
57
+ model_contents.gsub!(/!FIELDS!/, column_declarations)
58
+ create_file(model_path, model_contents)
59
+ end
60
+
61
+ def create_model_migration(filename, name, fields)
62
+ # NO MIGRATION NEEDED
63
+ end
64
+
65
+ def create_migration_file(migration_name, name, columns)
66
+ # NO MIGRATION NEEDED
67
+ end
@@ -53,6 +53,9 @@ ActiveSupport.escape_html_entities_in_json = false
53
53
 
54
54
  # Now we can establish connection with our db.
55
55
  ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Padrino.env])
56
+
57
+ # Timestamps are in the utc by default.
58
+ ActiveRecord::Base.default_timezone = :utc
56
59
  MR
57
60
 
58
61
  MYSQL = (<<-MYSQL) unless defined?(MYSQL)
@@ -46,7 +46,7 @@ When /^I press '(.*)'$/ do |name|
46
46
  end
47
47
 
48
48
  Then /^I should see '(.*)'$/ do |text|
49
- response_body.should contain(/#\{text}/m)
49
+ page.should have_content(text)
50
50
  end
51
51
  TEST
52
52
 
@@ -64,7 +64,7 @@ TEST
64
64
 
65
65
  def setup_test
66
66
  require_dependencies 'rack-test', :require => 'rack/test', :group => 'test'
67
- require_dependencies 'minitest', :version => "~>2.6.0", :require => 'minitest/autorun', :group => 'test'
67
+ require_dependencies 'minitest', :require => 'minitest/autorun', :group => 'test'
68
68
  insert_test_suite_setup MINITEST_SETUP
69
69
  create_file destination_root("test/test.rake"), MINITEST_RAKE
70
70
  end
@@ -39,10 +39,10 @@ RSPEC_RAKE = (<<-TEST).gsub(/^ {12}/, '') unless defined?(RSPEC_RAKE)
39
39
  begin
40
40
  require 'rspec/core/rake_task'
41
41
 
42
- spec_tasks = Dir['spec/*/'].inject([]) do |result, d|
43
- result << File.basename(d) unless Dir["\#{d}*"].empty?
44
- result
45
- end
42
+ spec_tasks = Dir['spec/*/'].inject([]) do |result, d|
43
+ result << File.basename(d) unless Dir["\#{d}*"].empty?
44
+ result
45
+ end
46
46
 
47
47
  spec_tasks.each do |folder|
48
48
  RSpec::Core::RakeTask.new("spec:\#{folder}") do |t|
@@ -28,37 +28,81 @@ module Padrino
28
28
  # Execute the model generation.
29
29
  #
30
30
  def create_model
31
+ app = options[:app]
32
+ return unless valid_model_for?(app)
33
+
34
+ include_component_module_for(:test)
35
+ migration_name = "create_#{name.pluralize.underscore}"
36
+ apply_default_fields fields
37
+ create_model_file(name, :fields => fields, :app => app)
38
+ generate_model_test(name) if test?
39
+ create_model_migration(migration_name, name, fields) unless options[:skip_migration]
40
+ end
41
+
42
+ private
43
+
44
+ ##
45
+ # Validate model characteristics
46
+ # Alert if the model name is being used
47
+ #
48
+ def valid_model_for?(app)
31
49
  self.destination_root = options[:root]
32
- if in_app_root?
33
- app = options[:app]
34
- check_app_existence(app)
35
- camel_name = name.to_s.underscore.camelize
36
- @project_name = ""
37
- @project_name = fetch_project_name
38
- if already_exists?(camel_name, @project_name)
39
- say "#{camel_name} already exists."
40
- say "Please, change the name."
41
- return
42
- else
43
- self.behavior = :revoke if options[:destroy]
44
- if invalids = invalid_fields(fields)
45
- say 'Invalid field name:', :red
46
- say " #{invalids.join(", ")}"
47
- return
48
- end
49
- unless include_component_module_for(:orm)
50
- say "<= You need an ORM adapter for run this generator. Sorry!"
51
- raise SystemExit
52
- end
53
- include_component_module_for(:test)
54
- migration_name = "create_#{name.pluralize.underscore}"
55
- apply_default_fields fields
56
- create_model_file(name, :fields => fields, :app => app)
57
- generate_model_test(name) if test?
58
- create_model_migration(migration_name, name, fields) unless options[:skip_migration]
59
- end
50
+ return false unless correct_path?
51
+
52
+ check_app_existence(app)
53
+
54
+ if options[:destroy]
55
+ self.behavior = :revoke
60
56
  else
61
- say 'You are not at the root of a Padrino application! (config/boot.rb not found)'
57
+ say "#{@camel_name} already exists."
58
+ say "Please, change the name."
59
+ return false
60
+ end if model_name_already_exists?
61
+
62
+ return false if has_invalid_fields?
63
+
64
+ check_orm
65
+ end
66
+
67
+ ##
68
+ # Return false if the model name is being used
69
+ #
70
+ def model_name_already_exists?
71
+ @camel_name = name.to_s.underscore.camelize
72
+
73
+ @project_name = ""
74
+ @project_name = fetch_project_name
75
+
76
+ return false unless already_exists?(@camel_name, @project_name)
77
+ true
78
+ end
79
+
80
+ ##
81
+ # Alert if there is not an ORM Adapter
82
+ #
83
+ def check_orm
84
+ return true if include_component_module_for(:orm)
85
+
86
+ say "<= You need an ORM adapter for run this generator. Sorry!"
87
+ raise SystemExit
88
+ end
89
+
90
+ ##
91
+ # Check app path
92
+ #
93
+ def correct_path?
94
+ return true if in_app_root?
95
+ say 'You are not at the root of a Padrino application! (config/boot.rb not found)'
96
+ false
97
+ end
98
+
99
+ ##
100
+ # Check if the fields are valid
101
+ #
102
+ def has_invalid_fields?
103
+ if invalids = invalid_fields(fields)
104
+ say 'Invalid field name:', :red
105
+ say " #{invalids.join(", ")}"
62
106
  end
63
107
  end
64
108
  end
@@ -26,6 +26,7 @@ module Padrino
26
26
  class_option :root, :desc => 'The root destination', :aliases => '-r', :default => '.', :type => :string
27
27
  class_option :dev, :desc => 'Use padrino from a git checkout', :default => false, :type => :boolean
28
28
  class_option :tiny, :desc => 'Generate tiny app skeleton', :aliases => '-i', :default => false, :type => :boolean
29
+ class_option :lean, :desc => 'Generate lean project without apps', :aliases => '-l', :default => false, :type => :boolean
29
30
  class_option :adapter, :desc => 'SQL adapter for ORM (sqlite, mysql, mysql2, mysql-gem, postgres)', :aliases => '-a', :default => 'sqlite', :type => :string
30
31
  class_option :template, :desc => 'Generate project from template', :aliases => '-p', :default => nil, :type => :string
31
32
  class_option :gem, :desc => 'Generate project as a gem', :aliases => '-g', :default => false, :type => :boolean
@@ -47,16 +48,19 @@ module Padrino
47
48
  @project_name = name.gsub(/\W/, '_').underscore.camelize
48
49
  @app_name = app.gsub(/\W/, '_').underscore.camelize
49
50
  self.destination_root = File.join(options[:root], name)
50
- if options[:template] # Run the template to create project
51
+ if options[:template]
51
52
  execute_runner(:template, options[:template])
52
- else # generate project without template
53
+ else
53
54
  directory('project/', destination_root)
54
55
  empty_directory destination_root('public/images')
55
56
  empty_directory destination_root('public/javascripts')
56
57
  empty_directory destination_root('public/stylesheets')
57
58
  empty_directory destination_root('tmp')
58
59
  store_component_config('.components')
59
- app_skeleton('app', options[:tiny])
60
+ unless options[:lean]
61
+ app_skeleton('app', options[:tiny])
62
+ append_file destination_root('config/apps.rb'), "Padrino.mount('#{@project_name}::#{@app_name}', :app_file => Padrino.root('app/app.rb')).to('/')\n"
63
+ end
60
64
  template 'templates/Gemfile.tt', destination_root('Gemfile')
61
65
  template 'templates/Rakefile.tt', destination_root('Rakefile')
62
66
  if options.gem?
@@ -33,4 +33,3 @@ Padrino.configure_apps do
33
33
  end
34
34
 
35
35
  # Mounts the core application for this project
36
- Padrino.mount('<%= @project_name.to_s %>::<%= @app_name.to_s %>', :app_file => Padrino.root('app/app.rb')).to('/')
@@ -24,6 +24,6 @@ gem 'rake'
24
24
  # gem 'padrino', :github => 'padrino/padrino-framework'
25
25
 
26
26
  # Or Individual Gems
27
- <% unless options.dev? %># <% end %>%w(core gen helpers cache mailer admin).each do |g|
27
+ <% unless options.dev? %># <% end %>%w(core support gen helpers cache mailer admin).each do |g|
28
28
  <% unless options.dev? %># <% end %> gem 'padrino-' + g, <% if options.dev? %>:path => '<%= Padrino::Generators::DEV_PATH %>/padrino-' + g<% else %>'<%= Padrino.version %>'<% end %>
29
29
  <% unless options.dev? %># <% end %>end
@@ -129,7 +129,7 @@ if PadrinoTasks.load?(:activerecord, defined?(ActiveRecord))
129
129
  end
130
130
 
131
131
  namespace :migrate do
132
- desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x. Target specific version with VERSION=x.'
132
+ desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x. Target specific version with VERSION=x.'
133
133
  task :redo => :environment do
134
134
  if ENV["VERSION"]
135
135
  Rake::Task["ar:migrate:down"].invoke
@@ -144,35 +144,17 @@ if PadrinoTasks.load?(:activerecord, defined?(ActiveRecord))
144
144
  task :reset => ["ar:drop", "ar:create", "ar:migrate"]
145
145
 
146
146
  desc 'Runs the "up" for a given migration VERSION.'
147
- task :up => :environment do
148
- version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
149
- raise "VERSION is required" unless version
150
- ActiveRecord::Migrator.run(:up, "db/migrate/", version)
151
- Rake::Task["ar:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
152
- end
147
+ task(:up => :environment){ migrate_as(:up) }
153
148
 
154
149
  desc 'Runs the "down" for a given migration VERSION.'
155
- task :down => :environment do
156
- version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
157
- raise "VERSION is required" unless version
158
- ActiveRecord::Migrator.run(:down, "db/migrate/", version)
159
- Rake::Task["ar:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
160
- end
150
+ task(:down => :environment){ migrate_as(:down) }
161
151
  end
162
152
 
163
153
  desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
164
- task :rollback => :environment do
165
- step = ENV['STEP'] ? ENV['STEP'].to_i : 1
166
- ActiveRecord::Migrator.rollback('db/migrate/', step)
167
- Rake::Task["ar:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
168
- end
154
+ task(:rollback => :environment){ move_as(:rollback) }
169
155
 
170
156
  desc 'Pushes the schema to the next version. Specify the number of steps with STEP=n'
171
- task :forward => :environment do
172
- step = ENV['STEP'] ? ENV['STEP'].to_i : 1
173
- ActiveRecord::Migrator.forward('db/migrate/', step)
174
- Rake::Task["ar:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
175
- end
157
+ task(:forward => :environment){ move_as(:forward) }
176
158
 
177
159
  desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
178
160
  task :reset => [ 'ar:drop', 'ar:setup' ]
@@ -252,36 +234,37 @@ if PadrinoTasks.load?(:activerecord, defined?(ActiveRecord))
252
234
  desc "Dump the database structure to a SQL file"
253
235
  task :dump => :environment do
254
236
  abcs = ActiveRecord::Base.configurations
255
- case abcs[Padrino.env][:adapter]
237
+ config = abcs[Padrino.env]
238
+ case config[:adapter]
256
239
  when "mysql", "mysql2", 'em_mysql2', "oci", "oracle", 'jdbcmysql'
257
- ActiveRecord::Base.establish_connection(abcs[Padrino.env])
258
- File.open("#{Padrino.root}/db/#{Padrino.env}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
240
+ config = config.inject({}){|result, (key, value)| result[key.to_s] = value; result }
241
+ ActiveRecord::Tasks::DatabaseTasks.structure_dump(config, resolve_structure_sql)
259
242
  when "postgresql"
260
- ENV['PGHOST'] = abcs[Padrino.env][:host] if abcs[Padrino.env][:host]
261
- ENV['PGPORT'] = abcs[Padrino.env][:port].to_s if abcs[Padrino.env][:port]
262
- ENV['PGPASSWORD'] = abcs[Padrino.env][:password].to_s if abcs[Padrino.env][:password]
263
- search_path = abcs[Padrino.env][:schema_search_path]
243
+ ENV['PGHOST'] = config[:host] if config[:host]
244
+ ENV['PGPORT'] = config[:port].to_s if config[:port]
245
+ ENV['PGPASSWORD'] = config[:password].to_s if config[:password]
246
+ search_path = config[:schema_search_path]
264
247
  unless search_path.blank?
265
248
  search_path = search_path.split(",").map{|search_path| "--schema=#{search_path.strip}" }.join(" ")
266
249
  end
267
- `pg_dump -i -U "#{abcs[Padrino.env][:username]}" -s -x -O -f db/#{Padrino.env}_structure.sql #{search_path} #{abcs[Padrino.env][:database]}`
250
+ `pg_dump -i -U "#{config[:username]}" -s -x -O -f db/#{Padrino.env}_structure.sql #{search_path} #{config[:database]}`
268
251
  raise "Error dumping database" if $?.exitstatus == 1
269
252
  when "sqlite", "sqlite3"
270
- dbfile = abcs[Padrino.env][:database] || abcs[Padrino.env][:dbfile]
271
- `#{abcs[Padrino.env][:adapter]} #{dbfile} .schema > db/#{Padrino.env}_structure.sql`
253
+ dbfile = config[:database] || config[:dbfile]
254
+ `#{config[:adapter]} #{dbfile} .schema > db/#{Padrino.env}_structure.sql`
272
255
  when "sqlserver"
273
- `scptxfr /s #{abcs[Padrino.env][:host]} /d #{abcs[Padrino.env][:database]} /I /f db\\#{Padrino.env}_structure.sql /q /A /r`
274
- `scptxfr /s #{abcs[Padrino.env][:host]} /d #{abcs[Padrino.env][:database]} /I /F db\ /q /A /r`
256
+ `scptxfr /s #{config[:host]} /d #{config[:database]} /I /f db\\#{Padrino.env}_structure.sql /q /A /r`
257
+ `scptxfr /s #{config[:host]} /d #{config[:database]} /I /F db\ /q /A /r`
275
258
  when "firebird"
276
- set_firebird_env(abcs[Padrino.env])
277
- db_string = firebird_db_string(abcs[Padrino.env])
259
+ set_firebird_env(config)
260
+ db_string = firebird_db_string(config)
278
261
  sh "isql -a #{db_string} > #{Padrino.root}/db/#{Padrino.env}_structure.sql"
279
262
  else
280
- raise "Task not supported by '#{abcs[Padrino.env][:adapter]}'"
263
+ raise "Task not supported by '#{config[:adapter]}'"
281
264
  end
282
265
 
283
266
  if ActiveRecord::Base.connection.supports_migrations?
284
- File.open("#{Padrino.root}/db/#{Padrino.env}_structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
267
+ File.open(resolve_structure_sql, "a"){|f| f << ActiveRecord::Base.connection.dump_schema_information }
285
268
  end
286
269
  end
287
270
  end
@@ -366,6 +349,27 @@ if PadrinoTasks.load?(:activerecord, defined?(ActiveRecord))
366
349
  end
367
350
  end
368
351
 
352
+ def migrate_as(type)
353
+ version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
354
+ raise "VERSION is required" unless version
355
+ ActiveRecord::Migrator.run(type, "db/migrate/", version)
356
+ dump_schema
357
+ end
358
+
359
+ def move_as(type)
360
+ step = ENV['STEP'] ? ENV['STEP'].to_i : 1
361
+ ActiveRecord::Migrator.send(type, 'db/migrate/', step)
362
+ dump_schema
363
+ end
364
+
365
+ def dump_schema
366
+ Rake::Task["ar:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
367
+ end
368
+
369
+ def resolve_structure_sql
370
+ "#{Padrino.root}/db/#{Padrino.env}_structure.sql"
371
+ end
372
+
369
373
  task 'db:migrate' => 'ar:migrate'
370
374
  task 'db:create' => 'ar:create'
371
375
  task 'db:drop' => 'ar:drop'