Module: Sinatra::ActiveRecordTasks

Extended by:
ActiveRecordTasks
Included in:
ActiveRecordTasks
Defined in:
lib/sinatra/activerecord/rake.rb

Instance Method Summary collapse

Instance Method Details

#create_migration(migration_name, version = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sinatra/activerecord/rake.rb', line 9

def create_migration(migration_name, version = nil)
  raise "No NAME specified. Example usage: `rake db:create_migration NAME=create_users`" if migration_name.nil?

  migration_number = version || Time.now.utc.strftime("%Y%m%d%H%M%S")
  migration_file = File.join(migrations_dir, "#{migration_number}_#{migration_name}.rb")
  migration_class = migration_name.split("_").map(&:capitalize).join

  FileUtils.mkdir_p(migrations_dir)
  File.open(migration_file, 'w') do |file|
    file.write "      class \#{migration_class} < ActiveRecord::Migration\n        def up\n        end\n\n        def down\n        end\n      end\n    MIGRATION\n  end\nend\n".strip_heredoc

#dump_schema(file_name = 'db/schema.rb') ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sinatra/activerecord/rake.rb', line 44

def dump_schema(file_name = 'db/schema.rb')
  silence_activerecord do
    ActiveRecord::Migration.suppress_messages do
      # Create file
      out = File.new(file_name, 'w')

      # Load schema
      ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, out)

      out.close
    end
  end
end

#load_schema(file_name = 'db/schema.rb') ⇒ Object



58
59
60
# File 'lib/sinatra/activerecord/rake.rb', line 58

def load_schema(file_name = 'db/schema.rb')
  load(file_name)
end

#migrate(version = nil) ⇒ Object



30
31
32
33
34
35
# File 'lib/sinatra/activerecord/rake.rb', line 30

def migrate(version = nil)
  silence_activerecord do
    migration_version = version ? version.to_i : version
    ActiveRecord::Migrator.migrate(migrations_dir, migration_version)
  end
end

#rollback(step = nil) ⇒ Object



37
38
39
40
41
42
# File 'lib/sinatra/activerecord/rake.rb', line 37

def rollback(step = nil)
  silence_activerecord do
    migration_step = step ? step.to_i : 1
    ActiveRecord::Migrator.rollback(migrations_dir, migration_step)
  end
end