Skip to content

Commit e0289ba

Browse files
committed
First version of migration system
1 parent 29ec91e commit e0289ba

File tree

7 files changed

+127
-7
lines changed

7 files changed

+127
-7
lines changed

lib/fancybox2/migrations/base.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Fancybox2
2+
module Migrations
3+
class Base
4+
5+
attr_reader :name, :version
6+
7+
def initialize(name)
8+
@name = name
9+
10+
@version = Runner.extract_and_validate_version_from name
11+
end
12+
13+
def call(up_or_down = :up)
14+
send up_or_down
15+
end
16+
end
17+
end
18+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Fancybox2
2+
module Migrations
3+
module Exceptions
4+
5+
class FileNameError < StandardError
6+
def initialize(message = nil)
7+
message = message || 'One of the provided migrations file has an invalid name'
8+
super(message)
9+
end
10+
end
11+
end
12+
end
13+
end

lib/fancybox2/migrations/runner.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
module Fancybox2
2+
module Migrations
3+
class Runner
4+
include Exceptions
5+
6+
VERSION_REGEXP = /^(\d{14})/.freeze
7+
8+
class << self
9+
10+
def extract_and_validate_version_from(migration_name)
11+
version = migration_name.to_s.scan(VERSION_REGEXP).flatten.first
12+
unless version
13+
raise ArgumentError, 'migration version must be a 14 digits integer'
14+
end
15+
16+
version.to_i
17+
end
18+
end
19+
20+
attr_reader :files_path, :current_version, :migrations
21+
22+
def initialize(files_path, current_version = nil)
23+
@files_path = files_path
24+
if current_version
25+
@current_version = self.class.extract_and_validate_version_from current_version
26+
end
27+
28+
load_migrations
29+
end
30+
31+
def run(from: nil, to: nil)
32+
# Select migrations to run
33+
to_run = migrations_to_run from, to
34+
end
35+
36+
def load_migrations
37+
# Load files from files_path and create classes
38+
@migrations = Dir[File.join(File.expand_path(files_path), '**', '*.rb')].sort.map do |f_path|
39+
migration_name = File.basename(f_path)
40+
klass = Class.new(Base)
41+
klass.class_eval(File.read(f_path), f_path)
42+
klass.freeze
43+
klass.new migration_name
44+
end
45+
end
46+
47+
# Select migrations to run depending on direction
48+
# :up selects only migration with a version greater than current_version
49+
# :down selects migrations with a version lower_or_equal_to current_version
50+
def migrations_to_run(from, to)
51+
selected = []
52+
@migrations.each do |m|
53+
# Break if we already arrived to "from" migration
54+
break if (from > to) && (m.version > from)
55+
# Break if we already arrived to "to" migration
56+
break if (from < to) && (m.version > to)
57+
# Skip until we arrive to "from" migration
58+
next if (from < to) && (m.version < from)
59+
60+
if m.version <= from
61+
selected.prepend m
62+
else
63+
selected.append m
64+
end
65+
end
66+
67+
selected
68+
end
69+
end
70+
end
71+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def up
2+
puts "2020 UP"
3+
end
4+
5+
def down
6+
puts "2020 DOWN"
7+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def up
2+
puts "2021 UP"
3+
end
4+
5+
def down
6+
puts "2021 DOWN"
7+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def up
2+
puts "2022 UP"
3+
end
4+
5+
def down
6+
puts "2022 DOWN"
7+
end

lib/fancybox2/module/base.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,10 @@ def on_configs(packet = nil, &block)
8787
@on_configs = block
8888
return
8989
end
90-
begin
91-
cfg = packet.payload
92-
if cfg && cfg.is_a?(Hash) && cfg['configs']
93-
self.configs.merge! cfg['configs']
94-
end
95-
rescue JSON::ParserError
96-
logger.debug 'on_configs: failed parsing packet as JSON'
90+
91+
cfg = packet.payload
92+
if cfg && cfg.is_a?(Hash) && cfg['configs']
93+
self.configs.merge! cfg['configs']
9794
end
9895

9996
@on_configs.call(packet) if @on_configs

0 commit comments

Comments
 (0)