Module: ActiveRecordTasks

Extended by:
ActiveRecordTasks
Included in:
ActiveRecordTasks
Defined in:
lib/conjoin/tasks/migrate.rb

Instance Method Summary collapse

Instance Method Details

#createObject



9
10
11
12
13
# File 'lib/conjoin/tasks/migrate.rb', line 9

def create
  silence_activerecord do
    ActiveRecord::Tasks::DatabaseTasks.create(config)
  end
end

#create_migration(migration_name, version = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/conjoin/tasks/migrate.rb', line 50

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 change\n        end\n      end\n    MIGRATION\n  end\nend\n".strip_heredoc

#dropObject



15
16
17
18
19
# File 'lib/conjoin/tasks/migrate.rb', line 15

def drop
  silence_activerecord do
    ActiveRecord::Tasks::DatabaseTasks.drop(config)
  end
end

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



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/conjoin/tasks/migrate.rb', line 82

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



104
105
106
# File 'lib/conjoin/tasks/migrate.rb', line 104

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

#migrate(version = nil) ⇒ Object



68
69
70
71
72
73
# File 'lib/conjoin/tasks/migrate.rb', line 68

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

#purgeObject



96
97
98
99
100
101
102
# File 'lib/conjoin/tasks/migrate.rb', line 96

def purge
  if config
    ActiveRecord::Tasks::DatabaseTasks.purge(config)
  else
    raise ActiveRecord::ConnectionNotEstablished
  end
end

#rollback(step = nil) ⇒ Object



75
76
77
78
79
80
# File 'lib/conjoin/tasks/migrate.rb', line 75

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

#seed(file = false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/conjoin/tasks/migrate.rb', line 21

def seed file = false
  if ENV['RACK_ENV'] == 'production'
    continue = ask("Running seeds could override data, proceed y/N? ", String)
  else
    continue = 'y'
  end

  if %w(yes y Y).include? continue
    silence_activerecord do
      load("db/seeds.rb")
      if not file
        Conjoin::Seeds.run
      else
        load("db/seeds/#{file}.rb")
      end
    end
  else
    say 'Aborting!'
  end
end

#setupObject



42
43
44
45
46
47
48
# File 'lib/conjoin/tasks/migrate.rb', line 42

def setup
  silence_activerecord do
    create()
    load_schema()
    seed()
  end
end