2021-02-05 07:09:31 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-03-15 05:39:35 -04:00
|
|
|
task dev: ["dev:setup"]
|
|
|
|
|
2013-08-26 06:20:45 -04:00
|
|
|
namespace :dev do
|
2020-01-23 01:08:32 -05:00
|
|
|
desc "GitLab | Dev | Setup developer environment (db, fixtures)"
|
2017-02-22 13:18:40 -05:00
|
|
|
task setup: :environment do
|
2013-08-26 06:20:45 -04:00
|
|
|
ENV['force'] = 'yes'
|
2014-03-15 05:39:35 -04:00
|
|
|
Rake::Task["gitlab:setup"].invoke
|
2019-11-15 07:06:12 -05:00
|
|
|
|
2022-02-23 07:13:44 -05:00
|
|
|
Gitlab::Database::EachDatabase.each_database_connection do |connection|
|
|
|
|
# Make sure DB statistics are up to date.
|
2022-03-28 20:09:12 -04:00
|
|
|
# gitlab:setup task can insert quite a bit of data, especially with MASS_INSERT=1
|
|
|
|
# so ANALYZE can take more than default 15s statement timeout. This being a dev task,
|
|
|
|
# we disable the statement timeout for ANALYZE to run and enable it back afterwards.
|
|
|
|
connection.execute('SET statement_timeout TO 0')
|
2022-02-23 07:13:44 -05:00
|
|
|
connection.execute('ANALYZE')
|
2022-03-28 20:09:12 -04:00
|
|
|
connection.execute('RESET statement_timeout')
|
2022-02-23 07:13:44 -05:00
|
|
|
end
|
2019-11-15 07:06:12 -05:00
|
|
|
|
2013-08-26 06:20:45 -04:00
|
|
|
Rake::Task["gitlab:shell:setup"].invoke
|
|
|
|
end
|
2017-12-25 05:20:50 -05:00
|
|
|
|
2020-01-23 01:08:32 -05:00
|
|
|
desc "GitLab | Dev | Eager load application"
|
2017-12-25 05:20:50 -05:00
|
|
|
task load: :environment do
|
2019-02-06 07:41:17 -05:00
|
|
|
Rails.configuration.eager_load = true
|
2017-12-25 05:20:50 -05:00
|
|
|
Rails.application.eager_load!
|
|
|
|
end
|
2022-03-28 23:08:22 -04:00
|
|
|
|
2022-04-06 11:08:23 -04:00
|
|
|
# If there are any clients connected to the DB, PostgreSQL won't let
|
|
|
|
# you drop the database. It's possible that Sidekiq, Puma, or
|
|
|
|
# some other client will be hanging onto a connection, preventing
|
|
|
|
# the DROP DATABASE from working. To workaround this problem, this
|
|
|
|
# method terminates all the connections so that a subsequent DROP
|
|
|
|
# will work.
|
|
|
|
desc "Used to drop all connections in development"
|
|
|
|
task :terminate_all_connections do
|
|
|
|
# In production, we might want to prevent ourselves from shooting
|
|
|
|
# ourselves in the foot, so let's only do this in a test or
|
|
|
|
# development environment.
|
|
|
|
unless Rails.env.production?
|
|
|
|
cmd = <<~SQL
|
|
|
|
SELECT pg_terminate_backend(pg_stat_activity.pid)
|
|
|
|
FROM pg_stat_activity
|
|
|
|
WHERE datname = current_database()
|
|
|
|
AND pid <> pg_backend_pid();
|
|
|
|
SQL
|
|
|
|
|
|
|
|
Gitlab::Database::EachDatabase.each_database_connection(include_shared: false) do |connection|
|
|
|
|
connection.execute(cmd)
|
|
|
|
rescue ActiveRecord::NoDatabaseError
|
|
|
|
end
|
2022-06-26 20:08:19 -04:00
|
|
|
|
|
|
|
# Clear connections opened by this rake task too
|
|
|
|
ActiveRecord::Base.clear_all_connections! # rubocop:disable Database/MultipleDatabases
|
2022-04-06 11:08:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-28 23:08:22 -04:00
|
|
|
databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
|
|
|
|
|
|
|
|
namespace :copy_db do
|
2022-03-31 08:08:17 -04:00
|
|
|
ALLOWED_DATABASES = %w[ci].freeze
|
|
|
|
|
2022-04-19 23:08:31 -04:00
|
|
|
defined_copy_db_tasks = []
|
|
|
|
|
2022-03-28 23:08:22 -04:00
|
|
|
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
2022-03-31 08:08:17 -04:00
|
|
|
next unless ALLOWED_DATABASES.include?(name)
|
2022-03-28 23:08:22 -04:00
|
|
|
|
2022-04-19 23:08:31 -04:00
|
|
|
defined_copy_db_tasks << name
|
|
|
|
|
2022-03-28 23:08:22 -04:00
|
|
|
desc "Copies the #{name} database from the main database"
|
|
|
|
task name => :environment do
|
2022-04-06 11:08:23 -04:00
|
|
|
Rake::Task["dev:terminate_all_connections"].invoke
|
|
|
|
|
2022-03-28 23:08:22 -04:00
|
|
|
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
|
|
|
|
|
|
|
|
ApplicationRecord.connection.create_database(db_config.database, template: ApplicationRecord.connection_db_config.database)
|
|
|
|
rescue ActiveRecord::DatabaseAlreadyExists
|
|
|
|
warn "Database '#{db_config.database}' already exists"
|
|
|
|
end
|
|
|
|
end
|
2022-04-19 23:08:31 -04:00
|
|
|
|
|
|
|
ALLOWED_DATABASES.each do |name|
|
|
|
|
next if defined_copy_db_tasks.include?(name)
|
|
|
|
|
|
|
|
# :nocov: we cannot mock ActiveRecord::Tasks::DatabaseTasks in time
|
|
|
|
# Workaround for GDK issue, see
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab-development-kit/-/issues/1464
|
|
|
|
desc "No-op task"
|
|
|
|
task name
|
|
|
|
# :nocov:
|
|
|
|
end
|
2022-03-28 23:08:22 -04:00
|
|
|
end
|
2013-08-26 06:20:45 -04:00
|
|
|
end
|