CI migrations fail due to duplicate CI projects
The migration migrate/20151210125930_migrate_ci_to_project.rb
fails due to some duplicate rows in the ci_projects
table. Output:
-- execute("UPDATE ci_runner_projects SET gl_project_id=(SELECT gitlab_id FROM ci_projects WHERE ci_projects.id = ci_runner_projects.project_id) WHERE gl_project_id IS NULL")
-> 0.0293s
-- execute("UPDATE ci_triggers SET gl_project_id=(SELECT gitlab_id FROM ci_projects WHERE ci_projects.id = ci_triggers.project_id) WHERE gl_project_id IS NULL")
-> 0.0095s
-- execute("UPDATE ci_variables SET gl_project_id=(SELECT gitlab_id FROM ci_projects WHERE ci_projects.id = ci_variables.project_id) WHERE gl_project_id IS NULL")
-> 0.0126s
-- execute("UPDATE ci_builds SET gl_project_id=(SELECT gl_project_id FROM ci_commits WHERE ci_commits.id = ci_builds.commit_id) WHERE gl_project_id IS NULL")
-> 26.1205s
-- execute("UPDATE projects SET ci_id=(SELECT ci_projects.id FROM ci_projects WHERE projects.id = ci_projects.gitlab_id) WHERE (SELECT ci_projects.id FROM ci_projects WHERE projects.id = ci_projects.gitlab_id) I
S NOT NULL")
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::CardinalityViolation: ERROR: more than one row returned by a subquery used as an expression
: UPDATE projects SET ci_id=(SELECT ci_projects.id FROM ci_projects WHERE projects.id = ci_projects.gitlab_id) WHERE (SELECT ci_projects.id FROM ci_projects WHERE projects.id = ci_projects.gitlab_id) IS NOT NULL
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activesupport-4.2.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/migration.rb:662:in `block in method_missing'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/migration.rb:631:in `block in say_with_time'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/migration.rb:631:in `say_with_time'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/migration.rb:651:in `method_missing'
/home/yorickpeterse/Projects/gitlab/gitlab-ce/db/migrate/20151210125930_migrate_ci_to_project.rb:30:in `migrate_project_column'
/home/yorickpeterse/Projects/gitlab/gitlab-ce/db/migrate/20151210125930_migrate_ci_to_project.rb:8:in `up'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/migration.rb:608:in `exec_migration'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/migration.rb:589:in `block (2 levels) in migrate'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/migration.rb:588:in `block in migrate'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/migration.rb:587:in `migrate'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/migration.rb:765:in `migrate'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/migration.rb:995:in `block in execute_migration_in_transaction'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/migration.rb:1041:in `block in ddl_transaction'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/home/yorickpeterse/.gem/ruby/2.1.7/gems/activerecord-4.2.4/lib/active_record/transactions.rb:220:in `transaction'
Query to get all the duplicate projects:
SELECT projects.id AS project_id, concat(namespaces.path, '/', projects.path) AS full_path, count(projects.id)
FROM ci_projects
LEFT JOIN projects ON projects.id = ci_projects.gitlab_id
LEFT JOIN namespaces ON namespaces.id = projects.namespace_id
GROUP BY projects.id, full_path
HAVING count(projects.id) > 1;
cc @ayufan