2016-03-08 17:52:48 -05:00
|
|
|
namespace :gitlab do
|
|
|
|
namespace :db do
|
|
|
|
desc 'GitLab | Manually insert schema migration version'
|
|
|
|
task :mark_migration_complete, [:version] => :environment do |_, args|
|
|
|
|
unless args[:version]
|
2016-06-01 18:37:15 -04:00
|
|
|
puts "Must specify a migration version as an argument".color(:red)
|
2016-03-08 17:52:48 -05:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
version = args[:version].to_i
|
|
|
|
if version == 0
|
2016-06-01 18:37:15 -04:00
|
|
|
puts "Version '#{args[:version]}' must be a non-zero integer".color(:red)
|
2016-03-08 17:52:48 -05:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
sql = "INSERT INTO schema_migrations (version) VALUES (#{version})"
|
|
|
|
begin
|
|
|
|
ActiveRecord::Base.connection.execute(sql)
|
2016-06-01 18:37:15 -04:00
|
|
|
puts "Successfully marked '#{version}' as complete".color(:green)
|
2016-03-08 17:52:48 -05:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
2016-06-01 18:37:15 -04:00
|
|
|
puts "Migration version '#{version}' is already marked complete".color(:yellow)
|
2016-03-08 17:52:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Drop all tables'
|
|
|
|
task :drop_tables => :environment do
|
|
|
|
connection = ActiveRecord::Base.connection
|
|
|
|
tables = connection.tables
|
|
|
|
tables.delete 'schema_migrations'
|
|
|
|
# Truncate schema_migrations to ensure migrations re-run
|
|
|
|
connection.execute('TRUNCATE schema_migrations')
|
2016-05-09 13:20:18 -04:00
|
|
|
|
2016-05-03 10:29:15 -04:00
|
|
|
# Drop tables with cascade to avoid dependent table errors
|
|
|
|
# PG: http://www.postgresql.org/docs/current/static/ddl-depend.html
|
|
|
|
# MySQL: http://dev.mysql.com/doc/refman/5.7/en/drop-table.html
|
2016-05-09 13:20:18 -04:00
|
|
|
# Add `IF EXISTS` because cascade could have already deleted a table.
|
|
|
|
tables.each { |t| connection.execute("DROP TABLE IF EXISTS #{t} CASCADE") }
|
2016-03-08 17:52:48 -05:00
|
|
|
end
|
2016-05-05 19:06:34 -04:00
|
|
|
|
|
|
|
desc 'Configures the database by running migrate, or by loading the schema and seeding if needed'
|
2016-05-06 13:08:05 -04:00
|
|
|
task configure: :environment do
|
2016-05-15 10:59:49 -04:00
|
|
|
if ActiveRecord::Base.connection.tables.any?
|
2016-05-05 19:06:34 -04:00
|
|
|
Rake::Task['db:migrate'].invoke
|
|
|
|
else
|
|
|
|
Rake::Task['db:schema:load'].invoke
|
|
|
|
Rake::Task['db:seed_fu'].invoke
|
|
|
|
end
|
|
|
|
end
|
2016-03-08 17:52:48 -05:00
|
|
|
end
|
|
|
|
end
|