2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-14 23:51:52 +08:00
|
|
|
# we should set the locale before the migration
|
|
|
|
task 'set_locale' do
|
2017-11-16 23:58:09 +08:00
|
|
|
begin
|
|
|
|
I18n.locale = (SiteSetting.default_locale || :en) rescue :en
|
|
|
|
rescue I18n::InvalidLocale
|
|
|
|
I18n.locale = :en
|
|
|
|
end
|
2015-02-14 23:51:52 +08:00
|
|
|
end
|
|
|
|
|
2019-03-21 09:50:16 +08:00
|
|
|
module MultisiteTestHelpers
|
|
|
|
def self.load_multisite?
|
|
|
|
Rails.env.test? && !ENV["RAILS_DB"] && !ENV["SKIP_MULTISITE"]
|
2017-08-31 12:06:56 +08:00
|
|
|
end
|
2019-06-14 13:44:07 +08:00
|
|
|
|
|
|
|
def self.create_multisite?
|
|
|
|
(ENV["RAILS_ENV"] == "test" || !ENV["RAILS_ENV"]) && !ENV["RAILS_DB"] && !ENV["SKIP_MULTISITE"]
|
|
|
|
end
|
2017-08-31 12:06:56 +08:00
|
|
|
end
|
|
|
|
|
2019-03-21 09:50:16 +08:00
|
|
|
task 'db:environment:set' => [:load_config] do |_, args|
|
|
|
|
if MultisiteTestHelpers.load_multisite?
|
|
|
|
system("RAILS_ENV=test RAILS_DB=discourse_test_multisite rake db:environment:set")
|
2017-08-07 21:05:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-14 12:20:48 +08:00
|
|
|
task 'db:force_skip_persist' do
|
|
|
|
GlobalSetting.skip_db = true
|
|
|
|
GlobalSetting.skip_redis = true
|
|
|
|
end
|
|
|
|
|
2019-06-14 13:05:29 +08:00
|
|
|
task 'db:create' => [:load_config] do |_, args|
|
2019-06-14 13:44:07 +08:00
|
|
|
if MultisiteTestHelpers.create_multisite?
|
2019-12-20 00:52:07 +08:00
|
|
|
unless system("RAILS_ENV=test RAILS_DB=discourse_test_multisite rake db:create")
|
|
|
|
|
|
|
|
STDERR.puts "-" * 80
|
|
|
|
STDERR.puts "ERROR: Could not create multisite DB. A common cause of this is a plugin"
|
|
|
|
STDERR.puts "checking the column structure when initializing, which raises an error."
|
|
|
|
STDERR.puts "-" * 80
|
|
|
|
raise "Could not initialize discourse_test_multisite"
|
|
|
|
end
|
2017-08-07 21:05:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-14 12:20:48 +08:00
|
|
|
begin
|
|
|
|
reqs = Rake::Task['db:create'].prerequisites.map(&:to_sym)
|
|
|
|
Rake::Task['db:create'].clear_prerequisites
|
|
|
|
Rake::Task['db:create'].enhance(["db:force_skip_persist"] + reqs)
|
|
|
|
end
|
|
|
|
|
2019-03-21 09:50:16 +08:00
|
|
|
task 'db:drop' => [:load_config] do |_, args|
|
2019-06-14 13:44:07 +08:00
|
|
|
if MultisiteTestHelpers.create_multisite?
|
|
|
|
system("RAILS_DB=discourse_test_multisite RAILS_ENV=test rake db:drop")
|
2019-03-21 09:50:16 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-13 07:17:54 +08:00
|
|
|
begin
|
|
|
|
Rake::Task["db:migrate"].clear
|
2019-11-16 05:37:47 +08:00
|
|
|
Rake::Task["db:rollback"].clear
|
|
|
|
end
|
|
|
|
|
|
|
|
task 'db:rollback' => ['environment', 'set_locale'] do |_, args|
|
|
|
|
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
|
|
|
|
ActiveRecord::Base.connection.migration_context.rollback(step)
|
|
|
|
Rake::Task['db:_dump'].invoke
|
2019-09-13 07:17:54 +08:00
|
|
|
end
|
|
|
|
|
2019-03-21 09:50:16 +08:00
|
|
|
# we need to run seed_fu every time we run rake db:migrate
|
2019-12-17 03:46:33 +08:00
|
|
|
task 'db:migrate' => ['load_config', 'environment', 'set_locale'] do |_, args|
|
2019-09-13 07:17:54 +08:00
|
|
|
ActiveRecord::Tasks::DatabaseTasks.migrate
|
|
|
|
|
|
|
|
Rake::Task['db:_dump'].invoke
|
|
|
|
|
2017-11-17 03:42:38 +08:00
|
|
|
SeedFu.seed(DiscoursePluginRegistry.seed_paths)
|
2017-08-07 21:05:13 +08:00
|
|
|
|
2019-04-30 18:50:31 +08:00
|
|
|
unless Discourse.skip_post_deployment_migrations?
|
2019-05-01 21:44:45 +08:00
|
|
|
puts
|
|
|
|
print "Optimizing site icons... "
|
|
|
|
SiteIconManager.ensure_optimized!
|
|
|
|
puts "Done"
|
2019-04-30 18:50:31 +08:00
|
|
|
end
|
|
|
|
|
2019-03-21 09:50:16 +08:00
|
|
|
if MultisiteTestHelpers.load_multisite?
|
|
|
|
system("RAILS_DB=discourse_test_multisite rake db:migrate")
|
2017-08-07 21:05:13 +08:00
|
|
|
end
|
2013-04-08 07:56:42 +08:00
|
|
|
end
|
2013-10-25 07:31:33 +08:00
|
|
|
|
|
|
|
task 'test:prepare' => 'environment' do
|
2014-07-29 00:40:00 +08:00
|
|
|
I18n.locale = SiteSetting.default_locale rescue :en
|
2017-11-17 03:42:38 +08:00
|
|
|
SeedFu.seed(DiscoursePluginRegistry.seed_paths)
|
2013-10-25 07:31:33 +08:00
|
|
|
end
|
2014-02-01 03:16:15 +08:00
|
|
|
|
2014-09-11 10:53:21 +08:00
|
|
|
task 'db:api_test_seed' => 'environment' do
|
|
|
|
puts "Loading test data for discourse_api"
|
|
|
|
load Rails.root + 'db/api_test_seeds.rb'
|
|
|
|
end
|
|
|
|
|
2017-08-25 22:29:04 +08:00
|
|
|
def print_table(array)
|
|
|
|
width = array[0].keys.map { |k| k.to_s.length }
|
|
|
|
cols = array[0].keys.length
|
|
|
|
|
|
|
|
array.each do |row|
|
|
|
|
row.each_with_index do |(_, val), i|
|
|
|
|
width[i] = [width[i].to_i, val.to_s.length].max
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
array[0].keys.each_with_index do |col, i|
|
|
|
|
print col.to_s.ljust(width[i], ' ')
|
|
|
|
if i == cols - 1
|
|
|
|
puts
|
|
|
|
else
|
|
|
|
print ' | '
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "-" * (width.sum + width.length)
|
|
|
|
|
|
|
|
array.each do |row|
|
|
|
|
row.each_with_index do |(_, val), i|
|
|
|
|
print val.to_s.ljust(width[i], ' ')
|
|
|
|
if i == cols - 1
|
|
|
|
puts
|
|
|
|
else
|
|
|
|
print ' | '
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Statistics about database'
|
|
|
|
task 'db:stats' => 'environment' do
|
|
|
|
|
|
|
|
sql = <<~SQL
|
|
|
|
select table_name,
|
|
|
|
(
|
|
|
|
select reltuples::bigint
|
|
|
|
from pg_class
|
|
|
|
where oid = ('public.' || table_name)::regclass
|
|
|
|
) AS row_estimate,
|
2019-08-02 15:25:30 +08:00
|
|
|
pg_size_pretty(pg_table_size(quote_ident(table_name))) table_size,
|
|
|
|
pg_size_pretty(pg_indexes_size(quote_ident(table_name))) index_size,
|
|
|
|
pg_size_pretty(pg_total_relation_size(quote_ident(table_name))) total_size
|
2017-08-25 22:29:04 +08:00
|
|
|
from information_schema.tables
|
|
|
|
where table_schema = 'public'
|
2019-08-02 15:25:30 +08:00
|
|
|
order by pg_total_relation_size(quote_ident(table_name)) DESC
|
2017-08-25 22:29:04 +08:00
|
|
|
SQL
|
|
|
|
|
|
|
|
puts
|
2018-06-19 14:13:14 +08:00
|
|
|
print_table(DB.query_hash(sql))
|
2017-08-25 22:29:04 +08:00
|
|
|
end
|
|
|
|
|
2014-02-01 03:16:15 +08:00
|
|
|
desc 'Rebuild indexes'
|
|
|
|
task 'db:rebuild_indexes' => 'environment' do
|
|
|
|
if Import::backup_tables_count > 0
|
|
|
|
raise "Backup from a previous import exists. Drop them before running this job with rake import:remove_backup, or move them to another schema."
|
|
|
|
end
|
|
|
|
|
2014-02-13 12:38:28 +08:00
|
|
|
Discourse.enable_readonly_mode
|
|
|
|
|
2014-02-01 03:16:15 +08:00
|
|
|
backup_schema = Jobs::Importer::BACKUP_SCHEMA
|
2018-06-19 14:13:14 +08:00
|
|
|
table_names = DB.query_single("select table_name from information_schema.tables where table_schema = 'public'")
|
2014-02-01 03:16:15 +08:00
|
|
|
|
|
|
|
begin
|
|
|
|
# Move all tables to the backup schema:
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec("DROP SCHEMA IF EXISTS #{backup_schema} CASCADE")
|
|
|
|
DB.exec("CREATE SCHEMA #{backup_schema}")
|
2014-02-01 03:16:15 +08:00
|
|
|
table_names.each do |table_name|
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec("ALTER TABLE public.#{table_name} SET SCHEMA #{backup_schema}")
|
2014-02-01 03:16:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Create a new empty db
|
|
|
|
Rake::Task["db:migrate"].invoke
|
|
|
|
|
|
|
|
# Fetch index definitions from the new db
|
|
|
|
index_definitions = {}
|
|
|
|
table_names.each do |table_name|
|
2018-06-19 14:13:14 +08:00
|
|
|
index_definitions[table_name] = DB.query_single("SELECT indexdef FROM pg_indexes WHERE tablename = '#{table_name}' and schemaname = 'public';")
|
2014-02-01 03:16:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Drop the new tables
|
|
|
|
table_names.each do |table_name|
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec("DROP TABLE public.#{table_name}")
|
2014-02-01 03:16:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Move the old tables back to the public schema
|
|
|
|
table_names.each do |table_name|
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec("ALTER TABLE #{backup_schema}.#{table_name} SET SCHEMA public")
|
2014-02-01 03:16:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Drop their indexes
|
2018-06-19 14:13:14 +08:00
|
|
|
index_names = DB.query_single("SELECT indexname FROM pg_indexes WHERE schemaname = 'public' AND tablename IN ('#{table_names.join("', '")}')")
|
2014-02-01 03:16:15 +08:00
|
|
|
index_names.each do |index_name|
|
|
|
|
begin
|
|
|
|
puts index_name
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec("DROP INDEX public.#{index_name}")
|
2016-04-08 02:32:31 +08:00
|
|
|
rescue ActiveRecord::StatementInvalid
|
2014-02-01 03:16:15 +08:00
|
|
|
# It's this:
|
|
|
|
# PG::Error: ERROR: cannot drop index category_users_pkey because constraint category_users_pkey on table category_users requires it
|
|
|
|
# HINT: You can drop constraint category_users_pkey on table category_users instead.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create the indexes
|
|
|
|
table_names.each do |table_name|
|
|
|
|
index_definitions[table_name].each do |index_def|
|
|
|
|
begin
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec(index_def)
|
2016-04-08 02:32:31 +08:00
|
|
|
rescue ActiveRecord::StatementInvalid
|
2014-02-01 03:16:15 +08:00
|
|
|
# Trying to recreate a primary key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
# Can we roll this back?
|
|
|
|
raise
|
|
|
|
ensure
|
2014-02-13 12:38:28 +08:00
|
|
|
Discourse.disable_readonly_mode
|
2014-02-01 03:16:15 +08:00
|
|
|
end
|
2014-02-13 12:38:28 +08:00
|
|
|
end
|