Class: Sequel::RakeTasks

Inherits:
Rake::TaskLib
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/sequel_rake_tasks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RakeTasks

Returns a new instance of RakeTasks.



20
21
22
23
24
25
26
27
28
29
# File 'lib/sequel_rake_tasks.rb', line 20

def initialize(options)
  @connection        = options[:connection]
  @connection_config = options[:connection_config]
  @migrator_klass    = options[:migrator]
  @migrations_dir    = options[:migrations_dir]
  @schema_file       = options[:schema_file]
  @seed_file         = options[:seed_file]
  @structure_file    = options[:structure_file]
  define_tasks
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



12
13
14
# File 'lib/sequel_rake_tasks.rb', line 12

def connection
  @connection
end

#connection_configObject (readonly)

Returns the value of attribute connection_config.



12
13
14
# File 'lib/sequel_rake_tasks.rb', line 12

def connection_config
  @connection_config
end

#migrations_dirObject (readonly)

Returns the value of attribute migrations_dir.



12
13
14
# File 'lib/sequel_rake_tasks.rb', line 12

def migrations_dir
  @migrations_dir
end

#migrator_klassObject (readonly)

Returns the value of attribute migrator_klass.



12
13
14
# File 'lib/sequel_rake_tasks.rb', line 12

def migrator_klass
  @migrator_klass
end

#schema_fileObject (readonly)

Returns the value of attribute schema_file.



12
13
14
# File 'lib/sequel_rake_tasks.rb', line 12

def schema_file
  @schema_file
end

#seed_fileObject (readonly)

Returns the value of attribute seed_file.



12
13
14
# File 'lib/sequel_rake_tasks.rb', line 12

def seed_file
  @seed_file
end

#structure_fileObject (readonly)

Returns the value of attribute structure_file.



12
13
14
# File 'lib/sequel_rake_tasks.rb', line 12

def structure_file
  @structure_file
end

Instance Method Details

#db_commandsObject



31
32
33
# File 'lib/sequel_rake_tasks.rb', line 31

def db_commands
  @db_commands ||= MysqlDbCommands.new(connection_config)
end

#define_tasksObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sequel_rake_tasks.rb', line 39

def define_tasks
  namespace :db do

    desc 'Setup the database.'
    task :setup => [:create, :migrate]

    desc 'Reset the database.'
    task :reset => [:drop, :setup]

    desc 'Create the database.'
    task :create do |t, args|
      db_commands.create_database
    end

    desc 'Drop the database.'
    task :drop do
      db_commands.drop_database
    end

    desc 'Load the seeds'
    task :seed do
      load(seed_file)
    end

    desc 'Migrate the database.'
    task :migrate do
      migrator.run
      Rake::Task['db:schema:dump'].invoke
    end

    namespace :schema do

      desc 'Dump the current database schema as a migration.'
      task :dump do
        connection.extension :schema_dumper
        text = connection.dump_schema_migration(same_db: false)
        text = text.gsub(/ +$/, '') # remove trailing whitespace
        File.open(schema_file, 'w') { |f| f.write(text) }
      end
    end

    namespace :structure do

      desc 'Load the database structure file'
      task :load do
        raise '`structure_file` option not defined!' unless structure_file
        db_commands.load_sql_file(structure_file)
      end

    end

  end
end

#migratorObject



35
36
37
# File 'lib/sequel_rake_tasks.rb', line 35

def migrator
  @migrator ||= migrator_klass.new(connection, migrations_dir)
end