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

#display_deprecation_warningObject



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

def display_deprecation_warning
  warn '[DEPRECATED] Sequent::Rake::Tasks is deprecated. Please use Sequent::Rake::MigrationTasks tasks instead.'
end

#register!Object



27
28
29
30
31
32
# File 'lib/sequent/rake/tasks.rb', line 27

def register!
  display_deprecation_warning

  register_db_tasks!
  register_view_schema_tasks!
end

#register_db_tasks!Object



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
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sequent/rake/tasks.rb', line 34

def register_db_tasks!
  namespace :db do
    desc 'Create the database'
    task :create do
      display_deprecation_warning

      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
      display_deprecation_warning

      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
      display_deprecation_warning

      database.create_schema!(options.fetch(:event_store_schema))
      database.migrate(options.fetch(:migrations_path))
    end
  end
end

#register_view_schema_tasks!Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sequent/rake/tasks.rb', line 73

def register_view_schema_tasks!
  namespace :view_schema do
    desc 'Build the view schema'
    task build: :'db:establish_connection' do
      display_deprecation_warning

      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
      display_deprecation_warning

      database.drop_schema!(view_projection.schema_name)
    end
  end
end