Class: Sequent::Rake::Tasks

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

Constant Summary collapse

DEFAULT_OPTIONS =
{
  migrations_path: 'db/migrate',
  event_store_schema: 'public'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Tasks

Returns a new instance of Tasks.



19
20
21
# File 'lib/sequent/rake/tasks.rb', line 19

def initialize(options)
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/sequent/rake/tasks.rb', line 17

def options
  @options
end

Instance Method Details

#register!Object



23
24
25
26
# File 'lib/sequent/rake/tasks.rb', line 23

def register!
  register_db_tasks!
  register_view_schema_tasks!
end

#register_db_tasks!Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sequent/rake/tasks.rb', line 28

def register_db_tasks!
  namespace :db do
    desc 'Create the database'
    task :create do
      current_environments.each do |env|
        env_db = db_config(env)
        puts "Create database #{env_db['database']}"
        Sequent::Support::Database.create!(env_db)
      end
    end

    desc 'Drop the database'
    task :drop do
      current_environments.each do |env|
        env_db = db_config(env)
        puts "Drop database #{env_db['database']}"
        Sequent::Support::Database.drop!(env_db)
      end
    end

    task :establish_connection do
      env_db = db_config(options.fetch(:environment))
      Sequent::Support::Database.establish_connection(env_db)
    end

    desc 'Migrate the database'
    task migrate: :establish_connection do
      database.create_schema!(options.fetch(:event_store_schema))
      database.migrate(options.fetch(:migrations_path))
    end
  end
end

#register_view_schema_tasks!Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sequent/rake/tasks.rb', line 61

def register_view_schema_tasks!
  namespace :view_schema do
    desc 'Build the view schema'
    task build: :'db:establish_connection' do
      if database.schema_exists?(view_projection.schema_name)
        puts "View version #{view_projection.version} already exists; no need to build it"
      else
        database.create_schema!(view_projection.schema_name)
        view_projection.build!
      end
    end

    desc 'Drop the view schema'
    task drop: :'db:establish_connection' do
      database.drop_schema!(view_projection.schema_name)
    end
  end
end