padrino-gen 0.6.7 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.7
1
+ 0.7.0
@@ -10,11 +10,15 @@ module Padrino
10
10
  # You can use other adapters like:
11
11
  #
12
12
  # ActiveRecord::Base.configurations[:development] = {
13
- # :adapter => "mysql",
14
- # :host => "localhost",
15
- # :username => "myuser",
16
- # :password => "mypass",
17
- # :database => "somedatabase"
13
+ # :adapter => 'mysql',
14
+ # :encoding => 'utf8',
15
+ # :reconnect => false,
16
+ # :database => 'your_database',
17
+ # :pool => 5,
18
+ # :username => 'root',
19
+ # :password => '',
20
+ # :host => 'localhost',
21
+ # :socket => '/tmp/mysql.sock'
18
22
  # )
19
23
  #
20
24
  ActiveRecord::Base.configurations[:development] = {
@@ -52,27 +56,9 @@ module Padrino
52
56
  ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Padrino.env])
53
57
  AR
54
58
 
55
- RAKE = (<<-RAKE).gsub(/^ {10}/, '')
56
- require 'sinatra/base'
57
- require 'active_record'
58
-
59
- namespace :db do
60
- desc "Migrate the database"
61
- task(:migrate) do
62
- load File.dirname(__FILE__) + '/config/boot.rb'
63
- APP_CLASS.new
64
- ActiveRecord::Base.logger = Logger.new(STDOUT)
65
- ActiveRecord::Migration.verbose = true
66
- ActiveRecord::Migrator.migrate( File.dirname(__FILE__) + "/db/migrate")
67
- end
68
- end
69
- RAKE
70
-
71
-
72
59
  def setup_orm
73
60
  require_dependencies 'active_record'
74
61
  create_file("config/database.rb", AR)
75
- create_file("Rakefile", RAKE.gsub(/APP_CLASS/, @class_name))
76
62
  empty_directory('app/models')
77
63
  end
78
64
 
@@ -34,9 +34,9 @@ module Padrino
34
34
  self.destination_root = options[:root]
35
35
  if in_app_root?
36
36
  @app_name = fetch_app_name(options[:root])
37
- @actions = controller_actions(fields)
37
+ @actions = controller_actions(fields)
38
+ @controller = name
38
39
  self.behavior = :revoke if options[:destroy]
39
- # inject_into_file destination_root("config/urls.rb"), controller_routes(name,fields), :after => "urls do\n"
40
40
  template "templates/controller.rb.tt", destination_root("app/controllers", "#{name}.rb")
41
41
  template "templates/helper.rb.tt", destination_root("app/helpers", "#{name}_helper.rb")
42
42
  empty_directory destination_root("app/views/#{name}")
@@ -1,11 +1,17 @@
1
- <%= @app_name %>.controllers do
1
+ <%= @app_name %>.controllers :<%= @name %> do
2
2
  # get :index, :map => "/foo/bar" do
3
3
  # session[:foo] = "bar"
4
4
  # render 'index'
5
5
  # end
6
-
7
- # get :sample, :map => "/sample/url" do
8
- # "Hello world!"
6
+
7
+ # get :sample, :map => "/sample/url", :respond_to => [:any, :js] do
8
+ # case content_type
9
+ # when :js then ...
10
+ # else ...
11
+ # end
12
+
13
+ # get :foo, :with => :id do
14
+ # "Im foo/#{params[:id]}"
9
15
  # end
10
16
 
11
17
  # get "/example" do
@@ -33,6 +33,7 @@ module Padrino
33
33
  Dir[File.dirname(__FILE__) + '/generators/{components}/**/*.rb'].each { |lib| require lib }
34
34
 
35
35
  begin
36
+ ENV['PADRINO_LOG_LEVEL'] ||= "test"
36
37
  if options[:root]
37
38
  require File.join(options[:root], 'config/boot.rb') if File.exist?(File.join(options[:root], 'config/boot.rb'))
38
39
  else
@@ -126,7 +126,6 @@ if defined?(ActiveRecord)
126
126
  end
127
127
  end
128
128
 
129
-
130
129
  desc "Migrate the database through scripts in db/migrate and update db/schema.rb by invoking ar:schema:dump. Target specific version with VERSION=x. Turn off output with VERBOSE=false."
131
130
  task :migrate => :environment do
132
131
  ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
@@ -252,6 +251,16 @@ if defined?(ActiveRecord)
252
251
  raise %{#{file} doesn't exist yet. Run "rake ar:migrate" to create it then try again. If you do not intend to use a database, you should instead alter #{Padrino.root}/config/boot.rb to limit the frameworks that will be loaded}
253
252
  end
254
253
  end
254
+
255
+ desc "Create migration from schema.rb"
256
+ task :to_migration => :environment do
257
+ AutoMigrations.schema_to_migration
258
+ end
259
+
260
+ desc "Create migration from schema.rb and reset migrations log"
261
+ task :to_migration_with_reset => :environment do
262
+ AutoMigrations.schema_to_migration(true)
263
+ end
255
264
  end
256
265
 
257
266
  namespace :structure do
@@ -292,6 +301,13 @@ if defined?(ActiveRecord)
292
301
  end
293
302
  end
294
303
 
304
+ namespace :auto do
305
+ desc "Use schema.rb to auto-upgrade"
306
+ task :upgrade => :environment do
307
+ AutoMigrations.run
308
+ end
309
+ end
310
+
295
311
  end
296
312
 
297
313
  def drop_database(config)
@@ -323,4 +339,191 @@ if defined?(ActiveRecord)
323
339
  def firebird_db_string(config)
324
340
  FireRuby::Database.db_string_for(config.symbolize_keys)
325
341
  end
342
+
343
+ ##
344
+ # Padrino plugin for automating migrations
345
+ #
346
+ # Thanks to:: PJ Hyett
347
+ # Original Repo:: http://github.com/pjhyett/auto_migrations
348
+ #
349
+ module AutoMigrations
350
+
351
+ def self.run
352
+ # Turn off schema_info code for auto-migration
353
+ class << ActiveRecord::Schema
354
+ alias :old_define :define
355
+ attr_accessor :version
356
+ def define(info={}, &block) @version = Time.now.utc.strftime("%Y%m%d%H%M%S"); instance_eval(&block) end
357
+ end
358
+
359
+ load(Padrino.root('db', 'schema.rb'))
360
+ ActiveRecord::Migration.drop_unused_tables
361
+ ActiveRecord::Migration.drop_unused_indexes
362
+ ActiveRecord::Migration.update_schema_version(ActiveRecord::Schema.version) if ActiveRecord::Schema.version
363
+
364
+ class << ActiveRecord::Schema
365
+ alias :define :old_define
366
+ end
367
+ end
368
+
369
+ def self.schema_to_migration(with_reset = false)
370
+ schema_in = File.read(Padrino.root("db", "schema.rb"))
371
+ schema_in.gsub!(/#(.)+\n/, '')
372
+ schema_in.sub!(/ActiveRecord::Schema.define(.+)do[ ]?\n/, '')
373
+ schema_in.gsub!(/^/, ' ')
374
+ schema = "class InitialSchema < ActiveRecord::Migration\n def self.up\n"
375
+ schema += " # We're resetting the migrations database...\n" +
376
+ " drop_table :schema_migrations\n" +
377
+ " initialize_schema_migrations_table\n\n" if with_reset
378
+ schema += schema_in
379
+ schema << "\n def self.down\n"
380
+ schema << (ActiveRecord::Base.connection.tables - %w(schema_info schema_migrations)).map do |table|
381
+ " drop_table :#{table}\n"
382
+ end.join
383
+ schema << " end\nend\n"
384
+ migration_file = Padrino.root("db", "migrate", "001_initial_schema.rb")
385
+ File.open(migration_file, "w") { |f| f << schema }
386
+ puts "Migration created at db/migrate/001_initial_schema.rb"
387
+ end
388
+
389
+ def self.included(base)
390
+ base.extend ClassMethods
391
+ class << base
392
+ cattr_accessor :tables_in_schema, :indexes_in_schema
393
+ self.tables_in_schema, self.indexes_in_schema = [], []
394
+ alias_method_chain :method_missing, :auto_migration
395
+ end
396
+ end
397
+
398
+ module ClassMethods
399
+
400
+ def method_missing_with_auto_migration(method, *args, &block)
401
+ case method
402
+ when :create_table
403
+ auto_create_table(method, *args, &block)
404
+ when :add_index
405
+ auto_add_index(method, *args, &block)
406
+ else
407
+ method_missing_without_auto_migration(method, *args, &block)
408
+ end
409
+ end
410
+
411
+ def auto_create_table(method, *args, &block)
412
+ table_name = args.shift.to_s
413
+ options = args.pop || {}
414
+
415
+ (self.tables_in_schema ||= []) << table_name
416
+
417
+ # Table doesn't exist, create it
418
+ unless ActiveRecord::Base.connection.tables.include?(table_name)
419
+ return method_missing_without_auto_migration(method, *[table_name, options], &block)
420
+ end
421
+
422
+ # Grab database columns
423
+ fields_in_db = ActiveRecord::Base.connection.columns(table_name).inject({}) do |hash, column|
424
+ hash[column.name] = column
425
+ hash
426
+ end
427
+
428
+ # Grab schema columns (lifted from active_record/connection_adapters/abstract/schema_statements.rb)
429
+ table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new(ActiveRecord::Base.connection)
430
+ primary_key = options[:primary_key] || "id"
431
+ table_definition.primary_key(primary_key) unless options[:id] == false
432
+ yield table_definition
433
+ fields_in_schema = table_definition.columns.inject({}) do |hash, column|
434
+ hash[column.name.to_s] = column
435
+ hash
436
+ end
437
+
438
+ # Add fields to db new to schema
439
+ (fields_in_schema.keys - fields_in_db.keys).each do |field|
440
+ column = fields_in_schema[field]
441
+ options = {:limit => column.limit, :precision => column.precision, :scale => column.scale}
442
+ options[:default] = column.default if !column.default.nil?
443
+ options[:null] = column.null if !column.null.nil?
444
+ add_column table_name, column.name, column.type.to_sym, options
445
+ end
446
+
447
+ # Remove fields from db no longer in schema
448
+ (fields_in_db.keys - fields_in_schema.keys & fields_in_db.keys).each do |field|
449
+ column = fields_in_db[field]
450
+ remove_column table_name, column.name
451
+ end
452
+
453
+ (fields_in_schema.keys & fields_in_db.keys).each do |field|
454
+ if field != primary_key #ActiveRecord::Base.get_primary_key(table_name)
455
+ changed = false # flag
456
+ new_type = fields_in_schema[field].type.to_sym
457
+ new_attr = {}
458
+
459
+ # First, check if the field type changed
460
+ if fields_in_schema[field].type.to_sym != fields_in_db[field].type.to_sym
461
+ changed = true
462
+ end
463
+
464
+ # Special catch for precision/scale, since *both* must be specified together
465
+ # Always include them in the attr struct, but they'll only get applied if changed = true
466
+ new_attr[:precision] = fields_in_schema[field][:precision]
467
+ new_attr[:scale] = fields_in_schema[field][:scale]
468
+
469
+ # Next, iterate through our extended attributes, looking for any differences
470
+ # This catches stuff like :null, :precision, etc
471
+ fields_in_schema[field].each_pair do |att,value|
472
+ next if att == :type or att == :base or att == :name # special cases
473
+ if !value.nil? && value != fields_in_db[field].send(att)
474
+ new_attr[att] = value
475
+ changed = true
476
+ end
477
+ end
478
+
479
+ # Change the column if applicable
480
+ change_column table_name, field, new_type, new_attr if changed
481
+ end
482
+ end
483
+ end
484
+
485
+ def auto_add_index(method, *args, &block)
486
+ table_name = args.shift.to_s
487
+ fields = Array(args.shift).map(&:to_s)
488
+ options = args.shift
489
+
490
+ index_name = options[:name] if options
491
+ index_name ||= ActiveRecord::Base.connection.index_name(table_name, :column => fields)
492
+
493
+ (self.indexes_in_schema ||= []) << index_name
494
+
495
+ unless ActiveRecord::Base.connection.indexes(table_name).detect { |i| i.name == index_name }
496
+ method_missing_without_auto_migration(method, *[table_name, fields, options], &block)
497
+ end
498
+ end
499
+
500
+ def drop_unused_tables
501
+ (ActiveRecord::Base.connection.tables - tables_in_schema - %w(schema_info schema_migrations)).each do |table|
502
+ drop_table table
503
+ end
504
+ end
505
+
506
+ def drop_unused_indexes
507
+ tables_in_schema.each do |table_name|
508
+ indexes_in_db = ActiveRecord::Base.connection.indexes(table_name).map(&:name)
509
+ (indexes_in_db - indexes_in_schema & indexes_in_db).each do |index_name|
510
+ remove_index table_name, :name => index_name
511
+ end
512
+ end
513
+ end
514
+
515
+ def update_schema_version(version)
516
+ ActiveRecord::Base.connection.update("INSERT INTO schema_migrations VALUES ('#{version}')")
517
+
518
+ schema_file = Padrino.root("db", "schema.rb")
519
+ schema = File.read(schema_file)
520
+ schema.sub!(/:version => \d+/, ":version => #{version}")
521
+ File.open(schema_file, "w") { |f| f << schema }
522
+ end
523
+
524
+ end
525
+
526
+ end
527
+
528
+ ActiveRecord::Migration.send :include, AutoMigrations
326
529
  end
data/padrino-gen.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{padrino-gen}
8
- s.version = "0.6.7"
8
+ s.version = "0.7.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
12
- s.date = %q{2010-01-26}
12
+ s.date = %q{2010-01-31}
13
13
  s.default_executable = %q{padrino-gen}
14
14
  s.description = %q{Generators for easily creating and building padrino applications from the console}
15
15
  s.email = %q{nesquena@gmail.com}
@@ -98,7 +98,7 @@ Gem::Specification.new do |s|
98
98
 
99
99
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
100
100
  s.add_runtime_dependency(%q<sinatra>, [">= 0.9.2"])
101
- s.add_runtime_dependency(%q<padrino-core>, ["= 0.6.7"])
101
+ s.add_runtime_dependency(%q<padrino-core>, ["= 0.7.0"])
102
102
  s.add_runtime_dependency(%q<thor>, [">= 0.11.8"])
103
103
  s.add_runtime_dependency(%q<bundler>, [">= 0.5.0"])
104
104
  s.add_development_dependency(%q<haml>, [">= 2.2.1"])
@@ -109,7 +109,7 @@ Gem::Specification.new do |s|
109
109
  s.add_development_dependency(%q<fakeweb>, [">= 1.2.3"])
110
110
  else
111
111
  s.add_dependency(%q<sinatra>, [">= 0.9.2"])
112
- s.add_dependency(%q<padrino-core>, ["= 0.6.7"])
112
+ s.add_dependency(%q<padrino-core>, ["= 0.7.0"])
113
113
  s.add_dependency(%q<thor>, [">= 0.11.8"])
114
114
  s.add_dependency(%q<bundler>, [">= 0.5.0"])
115
115
  s.add_dependency(%q<haml>, [">= 2.2.1"])
@@ -121,7 +121,7 @@ Gem::Specification.new do |s|
121
121
  end
122
122
  else
123
123
  s.add_dependency(%q<sinatra>, [">= 0.9.2"])
124
- s.add_dependency(%q<padrino-core>, ["= 0.6.7"])
124
+ s.add_dependency(%q<padrino-core>, ["= 0.7.0"])
125
125
  s.add_dependency(%q<thor>, [">= 0.11.8"])
126
126
  s.add_dependency(%q<bundler>, [">= 0.5.0"])
127
127
  s.add_dependency(%q<haml>, [">= 2.2.1"])
@@ -106,7 +106,6 @@ class TestAppGenerator < Test::Unit::TestCase
106
106
  buffer = silence_logger { @app.start(['sample_app', '--root=/tmp', '--orm=activerecord', '--script=none']) }
107
107
  assert_match /Applying.*?activerecord.*?orm/, buffer
108
108
  assert_match_in_file(/gem 'active_record'/, '/tmp/sample_app/Gemfile')
109
- assert_match_in_file(/Migrate the database/, '/tmp/sample_app/Rakefile')
110
109
  assert_match_in_file(/ActiveRecord::Base.establish_connection/, '/tmp/sample_app/config/database.rb')
111
110
  assert_dir_exists('/tmp/sample_app/app/models')
112
111
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-gen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.7
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Padrino Team
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2010-01-26 00:00:00 +01:00
15
+ date: 2010-01-31 00:00:00 +01:00
16
16
  default_executable: padrino-gen
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
@@ -33,7 +33,7 @@ dependencies:
33
33
  requirements:
34
34
  - - "="
35
35
  - !ruby/object:Gem::Version
36
- version: 0.6.7
36
+ version: 0.7.0
37
37
  version:
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: thor