diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2018-03-01 19:23:34 +0300 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2018-03-01 19:23:34 +0300 |
commit | c37cbb3125d5c1542371ff72c5ec7d76e3d2261b (patch) | |
tree | de670d30851b05b817b95d5826f2ef7be25403af | |
parent | de454de9b10f0dd534884c8ffeabe3e534993349 (diff) | |
parent | 6f152fd193a4b8ab527c526bed891874f075179e (diff) |
Merge branch 'dz-namespace-id-not-null' into 'master'
Make project#namespace_id not null
Closes #26052
See merge request gitlab-org/gitlab-ce!17448
7 files changed, 47 insertions, 8 deletions
diff --git a/changelogs/unreleased/dz-namespace-id-not-null.yml b/changelogs/unreleased/dz-namespace-id-not-null.yml new file mode 100644 index 00000000000..07b32aeeb86 --- /dev/null +++ b/changelogs/unreleased/dz-namespace-id-not-null.yml @@ -0,0 +1,5 @@ +--- +title: Add NOT NULL constraint to projects.namespace_id +merge_request: 17448 +author: +type: other diff --git a/db/post_migrate/20180301084653_change_project_namespace_id_not_null.rb b/db/post_migrate/20180301084653_change_project_namespace_id_not_null.rb new file mode 100644 index 00000000000..0342372cbed --- /dev/null +++ b/db/post_migrate/20180301084653_change_project_namespace_id_not_null.rb @@ -0,0 +1,29 @@ +# See http://doc.gitlab.com/ce/development/migration_style_guide.html +# for more information on how to write migrations for GitLab. + +class ChangeProjectNamespaceIdNotNull < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + class Project < ActiveRecord::Base + self.table_name = 'projects' + include EachBatch + end + + BATCH_SIZE = 1000 + + DOWNTIME = false + + disable_ddl_transaction! + + def up + Project.where(namespace_id: nil).each_batch(of: BATCH_SIZE) do |batch| + batch.delete_all + end + + change_column_null :projects, :namespace_id, false + end + + def down + change_column_null :projects, :namespace_id, true + end +end diff --git a/db/schema.rb b/db/schema.rb index 3fb80065ff5..a6ccd9dd907 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180222043024) do +ActiveRecord::Schema.define(version: 20180301084653) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -1427,7 +1427,7 @@ ActiveRecord::Schema.define(version: 20180222043024) do t.datetime "created_at" t.datetime "updated_at" t.integer "creator_id" - t.integer "namespace_id" + t.integer "namespace_id", null: false t.datetime "last_activity_at" t.string "import_url" t.integer "visibility_level", default: 0, null: false diff --git a/spec/migrations/cleanup_namespaceless_pending_delete_projects_spec.rb b/spec/migrations/cleanup_namespaceless_pending_delete_projects_spec.rb index b47f3314926..033d0e7584d 100644 --- a/spec/migrations/cleanup_namespaceless_pending_delete_projects_spec.rb +++ b/spec/migrations/cleanup_namespaceless_pending_delete_projects_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' require Rails.root.join('db', 'post_migrate', '20170502101023_cleanup_namespaceless_pending_delete_projects.rb') -describe CleanupNamespacelessPendingDeleteProjects do +describe CleanupNamespacelessPendingDeleteProjects, :migration, schema: 20180222043024 do before do # Stub after_save callbacks that will fail when Project has no namespace allow_any_instance_of(Project).to receive(:ensure_storage_path_exists).and_return(nil) diff --git a/spec/migrations/migrate_issues_to_ghost_user_spec.rb b/spec/migrations/migrate_issues_to_ghost_user_spec.rb index ff0d44e1ed2..9220b49a736 100644 --- a/spec/migrations/migrate_issues_to_ghost_user_spec.rb +++ b/spec/migrations/migrate_issues_to_ghost_user_spec.rb @@ -8,7 +8,7 @@ describe MigrateIssuesToGhostUser, :migration do let(:users) { table(:users) } before do - project = projects.create!(name: 'gitlab') + project = projects.create!(name: 'gitlab', namespace_id: 1) user = users.create(email: 'test@example.com') issues.create(title: 'Issue 1', author_id: nil, project_id: project.id) issues.create(title: 'Issue 2', author_id: user.id, project_id: project.id) diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 56c2d7b953e..f4faec9e52a 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2499,7 +2499,8 @@ describe Project do end it 'is a no-op when there is no namespace' do - project.update_column(:namespace_id, nil) + project.namespace.delete + project.reload expect_any_instance_of(Projects::UpdatePagesConfigurationService).not_to receive(:execute) expect_any_instance_of(Gitlab::PagesTransfer).not_to receive(:rename_project) @@ -2531,7 +2532,8 @@ describe Project do it 'is a no-op on legacy projects when there is no namespace' do export_path = legacy_project.export_path - legacy_project.update_column(:namespace_id, nil) + legacy_project.namespace.delete + legacy_project.reload expect(FileUtils).not_to receive(:rm_rf).with(export_path) @@ -2543,7 +2545,8 @@ describe Project do it 'runs on hashed storage projects when there is no namespace' do export_path = project.export_path - project.update_column(:namespace_id, nil) + project.namespace.delete + legacy_project.reload allow(FileUtils).to receive(:rm_rf).and_call_original expect(FileUtils).to receive(:rm_rf).with(export_path).and_call_original diff --git a/spec/workers/namespaceless_project_destroy_worker_spec.rb b/spec/workers/namespaceless_project_destroy_worker_spec.rb index ed8cedc0079..479d9396eca 100644 --- a/spec/workers/namespaceless_project_destroy_worker_spec.rb +++ b/spec/workers/namespaceless_project_destroy_worker_spec.rb @@ -22,7 +22,9 @@ describe NamespacelessProjectDestroyWorker do end end - context 'project has no namespace' do + # Only possible with schema 20180222043024 and lower. + # Project#namespace_id has not null constraint since then + context 'project has no namespace', :migration, schema: 20180222043024 do let!(:project) do project = build(:project, namespace_id: nil) project.save(validate: false) |