16
17
18
19
20
21
22
23
24
25
26
27
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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/sequent/rake/migration_tasks.rb', line 16
def register_tasks!
namespace :sequent do
desc <<~EOS
Set the SEQUENT_ENV to RAILS_ENV or RACK_ENV if not already set
EOS
task :set_env_var do
ENV['SEQUENT_ENV'] ||= ENV['RAILS_ENV'] || ENV['RACK_ENV']
end
desc <<~EOS
Rake task that runs before all sequent rake tasks and after the environment is set.
Hook applications can use to for instance run other rake tasks:
Rake::Task['sequent:init'].enhance(['my_task'])
EOS
task init: :set_env_var
namespace :db do
desc 'Creates the database and initializes the event_store schema for the current env'
task create: ['sequent:init'] do
ensure_sequent_env_set!
db_config = Sequent::Support::Database.read_config(@env)
Sequent::Support::Database.create!(db_config)
Sequent::Migrations::SequentSchema.create_sequent_schema_if_not_exists(env: @env, fail_if_exists: true)
end
desc 'Drops the database for the current env'
task :drop, [:production] => ['sequent:init'] do |_t, args|
ensure_sequent_env_set!
if @env == 'production' && args[:production] != 'yes_drop_production'
fail <<~OES
Wont drop db in production unless you whitelist the environment as follows: rake sequent:db:drop[yes_drop_production]
OES
end
db_config = Sequent::Support::Database.read_config(@env)
Sequent::Support::Database.drop!(db_config)
end
desc 'Creates the view schema for the current env'
task create_view_schema: ['sequent:init'] do
ensure_sequent_env_set!
Sequent::Migrations::ViewSchema.create_view_schema_if_not_exists(env: @env)
end
desc 'Creates the event_store schema for the current env'
task create_event_store: ['sequent:init'] do
ensure_sequent_env_set!
Sequent::Migrations::SequentSchema.create_sequent_schema_if_not_exists(env: @env, fail_if_exists: true)
end
desc 'Utility tasks that can be used to guard against unsafe usage of rails db:migrate directly'
task :dont_use_db_migrate_directly do
fail <<~EOS unless ENV['SEQUENT_MIGRATION_SCHEMAS'].present?
Don't call rails db:migrate directly but wrap in your own task instead:
task :migrate_db do
ENV['SEQUENT_MIGRATION_SCHEMAS'] = 'public'
Rake::Task['db:migrate'].invoke
end
You can choose whatever name for migrate_db you like.
EOS
end
end
namespace :migrate do
desc <<~EOS
Rake task that runs before all migrate rake tasks. Hook applications can use to for instance run other rake tasks.
EOS
task :init
desc 'Prints the current version in the database'
task current_version: ['sequent:init', :init] do
ensure_sequent_env_set!
Sequent::Support::Database.connect!(@env)
puts "Current version in the database is: #{Sequent::Migrations::ViewSchema::Versions.maximum(:version)}"
end
desc <<~EOS
Migrates the Projectors while the app is running. Call +sequent:migrate:offline+ after this successfully completed.
EOS
task online: ['sequent:init', :init] do
ensure_sequent_env_set!
db_config = Sequent::Support::Database.read_config(@env)
view_schema = Sequent::Migrations::ViewSchema.new(db_config: db_config)
view_schema.migrate_online
end
desc <<~EOS
Migrates the events inserted while +online+ was running. It is expected +sequent:migrate:online+ ran first.
EOS
task offline: ['sequent:init', :init] do
ensure_sequent_env_set!
db_config = Sequent::Support::Database.read_config(@env)
view_schema = Sequent::Migrations::ViewSchema.new(db_config: db_config)
view_schema.migrate_offline
end
end
namespace :snapshots do
desc <<~EOS
Rake task that runs before all snapshots rake tasks. Hook applications can use to for instance run other rake tasks.
EOS
task :init
task :set_snapshot_threshold, %i[aggregate_type threshold] => ['sequent:init', :init] do |_t, args|
aggregate_type = args['aggregate_type']
threshold = args['threshold']
unless aggregate_type
fail ArgumentError,
'usage rake sequent:snapshots:set_snapshot_threshold[AggregegateType,threshold]'
end
unless threshold
fail ArgumentError,
'usage rake sequent:snapshots:set_snapshot_threshold[AggregegateType,threshold]'
end
execute <<~EOS
UPDATE #{Sequent.configuration.stream_record_class} SET snapshot_threshold = #{threshold.to_i} WHERE aggregate_type = '#{aggregate_type}'
EOS
end
task delete_all: ['sequent:init', :init] do
result = Sequent::ApplicationRecord
.connection
.execute(<<~EOS)
DELETE FROM #{Sequent.configuration.event_record_class.table_name} WHERE event_type = 'Sequent::Core::SnapshotEvent'
EOS
Sequent.logger.info "Deleted #{result.cmd_tuples} aggregate snapshots from the event store"
end
end
end
end
|