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',
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Tasks

Returns a new instance of Tasks.



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

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#display_deprecation_warningObject



26
27
28
# File 'lib/sequent/rake/tasks.rb', line 26

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

#register!Object



30
31
32
33
34
35
# File 'lib/sequent/rake/tasks.rb', line 30

def register!
  display_deprecation_warning

  register_db_tasks!
  register_view_schema_tasks!
end

#register_db_tasks!Object



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
72
73
74
# File 'lib/sequent/rake/tasks.rb', line 37

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))
      ActiveRecord::Base.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



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

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