summaryrefslogtreecommitdiff
path: root/config/initializers/3_extra_migrations.rb
diff options
context:
space:
mode:
authorDouwe Maan <douwe@selenight.nl>2017-03-16 16:01:14 -0600
committerDouwe Maan <douwe@selenight.nl>2017-03-16 16:01:14 -0600
commit8825c2fc89506443c4416178e2040c26c3a299f6 (patch)
treee55dc2cf66a71719d3724cca2681ec5c8215e5c2 /config/initializers/3_extra_migrations.rb
parent92da8e98e8845852c01dfc005aa948604820991d (diff)
downloadgitlab-ce-release-migrations.tar.gz
POC for zero downtime migrations without having to manually manage what migration goes into what releaserelease-migrations
Diffstat (limited to 'config/initializers/3_extra_migrations.rb')
-rw-r--r--config/initializers/3_extra_migrations.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/config/initializers/3_extra_migrations.rb b/config/initializers/3_extra_migrations.rb
new file mode 100644
index 00000000000..e2d55349425
--- /dev/null
+++ b/config/initializers/3_extra_migrations.rb
@@ -0,0 +1,29 @@
+current_version = Gitlab::VersionInfo.parse(Gitlab::VERSION)
+
+past_version_paths = Dir.glob(Rails.root.join('db', 'release_migrations', '*').to_s).select do |path|
+ version_string = path.match(%r{db/release_migrations/((\d+)\.(\d+)\.(\d+))})[1]
+ version = Gitlab::VersionInfo.parse(version_string)
+ version <= current_version
+end
+
+migrate_paths = past_version_paths.map { |path| File.join(path, 'migrate') }
+
+Rails.application.config.paths['db/migrate'].concat(migrate_paths)
+
+# Rails memoizes migrations at certain points where it won't read the above
+# path just yet. As such we must also update the following list of paths.
+ActiveRecord::Migrator.migrations_paths.concat(migrate_paths)
+
+# Post deployment migrations are included by default. This file must be loaded
+# before other initializers as Rails may otherwise memoize a list of migrations
+# excluding the post deployment migrations.
+unless ENV['SKIP_POST_DEPLOYMENT_MIGRATIONS']
+ post_migrate_paths = past_version_paths.map { |path| File.join(path, 'post_migrate') }
+ post_migrate_paths << Rails.root.join('db', 'post_migrate').to_s
+
+ Rails.application.config.paths['db/migrate'].concat(post_migrate_paths)
+
+ # Rails memoizes migrations at certain points where it won't read the above
+ # path just yet. As such we must also update the following list of paths.
+ ActiveRecord::Migrator.migrations_paths.concat(post_migrate_paths)
+end