-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathevolver.rb
More file actions
146 lines (135 loc) · 3.62 KB
/
Copy pathevolver.rb
File metadata and controls
146 lines (135 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
# encoding: utf-8
require "mongoid"
require "evolver/extensions"
require "evolver/loggable"
require "evolver/migrator"
require "evolver/railtie"
require "evolver/sessions"
require "evolver/version"
require "rails/generators/evolver"
module Evolver
include Loggable
extend self
# Get an instance of the migration class from the registry with the provided
# session.
#
# @example Get the migration.
# Evolver.find("MoveSomeData", session)
#
# @param [ String ] migration The name of the migration class.
# @param [ Moped::Session ] session The current session.
#
# @return [ Migration ] The instantiated migration.
#
# @since 0.0.0
def find(migration, session)
metadata = registry.fetch(migration)
Object.const_get(migration).new(metadata[:file], session, metadata[:time])
end
# Run all migrations for all sessions that have not run yet.
#
# @example Run all the migrations.
# Evolver.migrate
#
# @return [ true ] True if all migrations succeeded.
#
# @since 0.0.0
def migrate(options = {})
load_migrations
Migrator.new(sessions, options).execute and true
end
# Load all the migrations in the application.
#
# @example Load all the migrations.
# Evolver.load_migrations
#
# @return [ Array<String> ] The migration filenames.
#
# @since 0.0.0
def load_migrations
Dir.glob("#{migrations_path}/*.rb").sort.each do |filename|
load_migration(filename)
end
end
# Load the migration for the provided filename.
#
# @example Load the migration.
# Evolver.load_migration("20120101120000_move_data.rb")
#
# @param [ String ] filename The name of the migration file.
#
# @return [ true, false ] If the require succeeded.
#
# @since 0.0.0
def load_migration(filename)
require filename
end
# Get the path where evolver's migrations are stored. This is
# db/evolver/migrations from the root of whatever framework you are using or
# the root of your standalone project.
#
# @example Get the migrations path.
# Evolver.migrations_path
#
# @return [ String ] "/path/to/my/app/db/evolver/migrations"
#
# @since 0.0.0
def migrations_path
File.join(Rails.root, "db/evolver/migrations")
end
# Get evolver's registry of migration metadata.
#
# @example Get the registry.
# Evolver.registry
#
# @return [ Hash ] The metadata registry.
#
# @since 0.0.0
def registry
@registry ||= {}
end
# Register a migration's metadata with the evolver.
#
# @example Register the migration.
# Evolver.register(MoveSomeData, "move_some_data.rb", time, sessions)
#
# @param [ Class ] migration The migration class.
# @param [ String ] file The file name of the migration.
# @param [ Time ] time The generation time of the migration.
# @param [ Array<Symbol> ] sessions The names of the sessions this applies
# to.
#
# @return [ Hash ] The migration metadata.
#
# @since 0.0.0
def register(migration, file, time, sessions)
registry.store(migration, { file: file, time: time, sessions: sessions })
end
def revert
# load_migration(last_run)
end
# Get all the sessions configured in the mongoid.yml
#
# @example Get all the sessions.
# Evolver.sessions
#
# @return [ Array<Moped::Session> ] The sessions.
#
# @since 0.0.0
def sessions
Mongoid.sessions.keys.reduce({}) do |_sessions, name|
_sessions[name.to_sym] = Mongoid.session(name)
_sessions
end
end
# Get the stats of run and pending migrations.
#
# @example Get the stats.
# Evolver.stats
#
# @since 0.0.0
def stats
load_migrations
logger.info(Migrator.new(sessions).stats)
end
end