15
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
|
# File 'lib/sequent/rake/migration_tasks.rb', line 15
def register_tasks!
namespace :sequent do
desc " Rake task that runs before all sequent rake tasks. Hook applications can use to for instance run other rake tasks.\n EOS\n task :init\n\n namespace :db do\n desc 'Creates the database and initializes the event_store schema for the current env'\n task create: ['sequent:init'] do\n ensure_rack_env_set!\n\n db_config = Sequent::Support::Database.read_config(@env)\n Sequent::Support::Database.create!(db_config)\n\n create_event_store(db_config)\n end\n\n desc 'Drops the database for the current env'\n task :drop, [:production] => ['sequent:init'] do |_t, args|\n ensure_rack_env_set!\n\n if @env == 'production' && args[:production] != 'yes_drop_production'\n fail <<~OES\n Wont drop db in production unless you whitelist the environment as follows: rake sequent:db:drop[yes_drop_production]\n OES\n end\n\n db_config = Sequent::Support::Database.read_config(@env)\n Sequent::Support::Database.drop!(db_config)\n end\n\n desc 'Creates the view schema for the current env'\n task create_view_schema: ['sequent:init'] do\n ensure_rack_env_set!\n\n db_config = Sequent::Support::Database.read_config(@env)\n Sequent::Support::Database.establish_connection(db_config)\n Sequent::Migrations::ViewSchema.new(db_config: db_config).create_view_schema_if_not_exists\n end\n\n desc 'Creates the event_store schema for the current env'\n task create_event_store: ['sequent:init'] do\n ensure_rack_env_set!\n db_config = Sequent::Support::Database.read_config(@env)\n create_event_store(db_config)\n end\n\n # rubocop:disable Lint/NestedMethodDefinition\n def create_event_store(db_config)\n event_store_schema = Sequent.configuration.event_store_schema_name\n sequent_schema = File.join(Sequent.configuration.database_schema_directory, \"\#{event_store_schema}.rb\")\n unless File.exist?(sequent_schema)\n fail \"File \#{sequent_schema} does not exist. Check your Sequent configuration.\"\n end\n\n Sequent::Support::Database.establish_connection(db_config)\n if Sequent::Support::Database.schema_exists?(event_store_schema)\n fail \"Schema \#{event_store_schema} already exists in the database\"\n end\n\n Sequent::Support::Database.create_schema(event_store_schema)\n Sequent::Support::Database.with_schema_search_path(event_store_schema, db_config, @env) do\n load(sequent_schema)\n end\n end\n # rubocop:enable Lint/NestedMethodDefinition\n end\n\n namespace :migrate do\n desc <<~EOS\n Rake task that runs before all migrate rake tasks. Hook applications can use to for instance run other rake tasks.\n EOS\n task :init\n\n desc 'Prints the current version in the database'\n task current_version: ['sequent:init', :init] do\n ensure_rack_env_set!\n\n Sequent::Support::Database.connect!(@env)\n\n puts \"Current version in the database is: \#{Sequent::Migrations::ViewSchema::Versions.maximum(:version)}\"\n end\n\n desc <<~EOS\n Migrates the Projectors while the app is running. Call +sequent:migrate:offline+ after this successfully completed.\n EOS\n task online: ['sequent:init', :init] do\n ensure_rack_env_set!\n\n db_config = Sequent::Support::Database.read_config(@env)\n view_schema = Sequent::Migrations::ViewSchema.new(db_config: db_config)\n\n view_schema.migrate_online\n end\n\n desc <<~EOS\n Migrates the events inserted while +online+ was running. It is expected +sequent:migrate:online+ ran first.\n EOS\n task offline: ['sequent:init', :init] do\n ensure_rack_env_set!\n\n db_config = Sequent::Support::Database.read_config(@env)\n view_schema = Sequent::Migrations::ViewSchema.new(db_config: db_config)\n\n view_schema.migrate_offline\n end\n end\n\n namespace :snapshots do\n desc <<~EOS\n Rake task that runs before all snapshots rake tasks. Hook applications can use to for instance run other rake tasks.\n EOS\n task :init\n\n task :set_snapshot_threshold, %i[aggregate_type threshold] => ['sequent:init', :init] do |_t, args|\n aggregate_type = args['aggregate_type']\n threshold = args['threshold']\n\n unless aggregate_type\n fail ArgumentError,\n 'usage rake sequent:snapshots:set_snapshot_threshold[AggregegateType,threshold]'\n end\n unless threshold\n fail ArgumentError,\n 'usage rake sequent:snapshots:set_snapshot_threshold[AggregegateType,threshold]'\n end\n\n execute <<~EOS\n UPDATE \#{Sequent.configuration.stream_record_class} SET snapshot_threshold = \#{threshold.to_i} WHERE aggregate_type = '\#{aggregate_type}'\n EOS\n end\n\n task delete_all: ['sequent:init', :init] do\n result = Sequent::ApplicationRecord\n .connection\n .execute(<<~EOS)\n DELETE FROM \#{Sequent.configuration.event_record_class.table_name} WHERE event_type = 'Sequent::Core::SnapshotEvent'\n EOS\n Sequent.logger.info \"Deleted \#{result.cmd_tuples} aggregate snapshots from the event store\"\n end\n end\n end\nend\n"
|