summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/migration_helpers/v2_spec.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2023-05-17 16:05:49 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2023-05-17 16:05:49 +0000
commit43a25d93ebdabea52f99b05e15b06250cd8f07d7 (patch)
treedceebdc68925362117480a5d672bcff122fb625b /spec/lib/gitlab/database/migration_helpers/v2_spec.rb
parent20c84b99005abd1c82101dfeff264ac50d2df211 (diff)
downloadgitlab-ce-0f94cf6ca9d272d8e0fda4a7a597866cf3dc1fc0.tar.gz
Add latest changes from gitlab-org/gitlab@16-0-stable-eev16.0.0-rc4216-0-stable
Diffstat (limited to 'spec/lib/gitlab/database/migration_helpers/v2_spec.rb')
-rw-r--r--spec/lib/gitlab/database/migration_helpers/v2_spec.rb79
1 files changed, 79 insertions, 0 deletions
diff --git a/spec/lib/gitlab/database/migration_helpers/v2_spec.rb b/spec/lib/gitlab/database/migration_helpers/v2_spec.rb
index 0d75094a2fd..8b653e2d89d 100644
--- a/spec/lib/gitlab/database/migration_helpers/v2_spec.rb
+++ b/spec/lib/gitlab/database/migration_helpers/v2_spec.rb
@@ -416,4 +416,83 @@ RSpec.describe Gitlab::Database::MigrationHelpers::V2 do
end
end
end
+
+ describe '#truncate_tables!' do
+ before do
+ ApplicationRecord.connection.execute(<<~SQL)
+ CREATE TABLE _test_gitlab_main_table (id serial primary key);
+ CREATE TABLE _test_gitlab_main_table2 (id serial primary key);
+
+ INSERT INTO _test_gitlab_main_table DEFAULT VALUES;
+ INSERT INTO _test_gitlab_main_table2 DEFAULT VALUES;
+ SQL
+
+ Ci::ApplicationRecord.connection.execute(<<~SQL)
+ CREATE TABLE _test_gitlab_ci_table (id serial primary key);
+ SQL
+ end
+
+ it 'truncates the table' do
+ expect(migration).to receive(:execute).with('TRUNCATE TABLE "_test_gitlab_main_table"').and_call_original
+
+ expect { migration.truncate_tables!('_test_gitlab_main_table') }
+ .to change { ApplicationRecord.connection.select_value('SELECT count(1) from _test_gitlab_main_table') }.to(0)
+ end
+
+ it 'truncates multiple tables' do
+ expect(migration).to receive(:execute).with('TRUNCATE TABLE "_test_gitlab_main_table", "_test_gitlab_main_table2"').and_call_original
+
+ expect { migration.truncate_tables!('_test_gitlab_main_table', '_test_gitlab_main_table2') }
+ .to change { ApplicationRecord.connection.select_value('SELECT count(1) from _test_gitlab_main_table') }.to(0)
+ .and change { ApplicationRecord.connection.select_value('SELECT count(1) from _test_gitlab_main_table2') }.to(0)
+ end
+
+ it 'raises an ArgumentError if truncating multiple gitlab_schema' do
+ expect do
+ migration.truncate_tables!('_test_gitlab_main_table', '_test_gitlab_ci_table')
+ end.to raise_error(ArgumentError, /one `gitlab_schema`/)
+ end
+
+ context 'with multiple databases' do
+ before do
+ skip_if_shared_database(:ci)
+ end
+
+ context 'for ci database' do
+ before do
+ migration.instance_variable_set :@connection, Ci::ApplicationRecord.connection
+ end
+
+ it 'skips the TRUNCATE statement tables not in schema for connection' do
+ expect(migration).not_to receive(:execute)
+
+ migration.truncate_tables!('_test_gitlab_main_table')
+ end
+ end
+
+ context 'for main database' do
+ before do
+ migration.instance_variable_set :@connection, ApplicationRecord.connection
+ end
+
+ it 'executes a TRUNCATE statement' do
+ expect(migration).to receive(:execute).with('TRUNCATE TABLE "_test_gitlab_main_table"')
+
+ migration.truncate_tables!('_test_gitlab_main_table')
+ end
+ end
+ end
+
+ context 'with single database' do
+ before do
+ skip_if_database_exists(:ci)
+ end
+
+ it 'executes a TRUNCATE statement' do
+ expect(migration).to receive(:execute).with('TRUNCATE TABLE "_test_gitlab_main_table"')
+
+ migration.truncate_tables!('_test_gitlab_main_table')
+ end
+ end
+ end
end