Module: ActiveRecordTasks

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

Instance Method Summary collapse

Instance Method Details

#createObject



27
28
29
30
31
# File 'lib/conjoin/tasks/migrate.rb', line 27

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

#create_migration(migration_name, version = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/conjoin/tasks/migrate.rb', line 68

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 <<-MIGRATION.strip_heredoc
      class #{migration_class} < ActiveRecord::Migration
        def change
        end
      end
    MIGRATION
  end
end

#dropObject



33
34
35
36
37
# File 'lib/conjoin/tasks/migrate.rb', line 33

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

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



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/conjoin/tasks/migrate.rb', line 100

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



122
123
124
# File 'lib/conjoin/tasks/migrate.rb', line 122

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

#migrate(version = nil) ⇒ Object



86
87
88
89
90
91
# File 'lib/conjoin/tasks/migrate.rb', line 86

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

#purgeObject



114
115
116
117
118
119
120
# File 'lib/conjoin/tasks/migrate.rb', line 114

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

#rollback(step = nil) ⇒ Object



93
94
95
96
97
98
# File 'lib/conjoin/tasks/migrate.rb', line 93

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/conjoin/tasks/migrate.rb', line 39

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



60
61
62
63
64
65
66
# File 'lib/conjoin/tasks/migrate.rb', line 60

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