Commit ebdaeb78 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch 'ab/drop-tables-rake-task' into 'master'

Rake task gitlab:db:drop_tables to also drop views

See merge request gitlab-org/gitlab!43745
parents 629b6600 2a291cd8
...@@ -35,6 +35,11 @@ namespace :gitlab do ...@@ -35,6 +35,11 @@ namespace :gitlab do
# Truncate schema_migrations to ensure migrations re-run # Truncate schema_migrations to ensure migrations re-run
connection.execute('TRUNCATE schema_migrations') if connection.table_exists? 'schema_migrations' connection.execute('TRUNCATE schema_migrations') if connection.table_exists? 'schema_migrations'
# Drop any views
connection.views.each do |view|
connection.execute("DROP VIEW IF EXISTS #{connection.quote_table_name(view)} CASCADE")
end
# Drop tables with cascade to avoid dependent table errors # Drop tables with cascade to avoid dependent table errors
# PG: http://www.postgresql.org/docs/current/static/ddl-depend.html # PG: http://www.postgresql.org/docs/current/static/ddl-depend.html
# Add `IF EXISTS` because cascade could have already deleted a table. # Add `IF EXISTS` because cascade could have already deleted a table.
......
...@@ -164,6 +164,49 @@ RSpec.describe 'gitlab:db namespace rake task' do ...@@ -164,6 +164,49 @@ RSpec.describe 'gitlab:db namespace rake task' do
end end
end end
describe 'drop_tables' do
subject { run_rake_task('gitlab:db:drop_tables') }
let(:tables) { %w(one two) }
let(:views) { %w(three four) }
let(:connection) { ActiveRecord::Base.connection }
before do
allow(connection).to receive(:execute).and_return(nil)
allow(connection).to receive(:tables).and_return(tables)
allow(connection).to receive(:views).and_return(views)
end
it 'drops all tables, except schema_migrations' do
expect(connection).to receive(:execute).with('DROP TABLE IF EXISTS "one" CASCADE')
expect(connection).to receive(:execute).with('DROP TABLE IF EXISTS "two" CASCADE')
subject
end
it 'drops all views' do
expect(connection).to receive(:execute).with('DROP VIEW IF EXISTS "three" CASCADE')
expect(connection).to receive(:execute).with('DROP VIEW IF EXISTS "four" CASCADE')
subject
end
it 'truncates schema_migrations table' do
expect(connection).to receive(:execute).with('TRUNCATE schema_migrations')
subject
end
it 'drops extra schemas' do
Gitlab::Database::EXTRA_SCHEMAS.each do |schema|
expect(connection).to receive(:execute).with("DROP SCHEMA IF EXISTS \"#{schema}\"")
end
subject
end
end
describe 'reindex' do describe 'reindex' do
let(:reindex) { double('reindex') } let(:reindex) { double('reindex') }
let(:indexes) { double('indexes') } let(:indexes) { double('indexes') }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment