-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CHE-4372; add unique constraing for factory name + user #4521
Changes from 7 commits
580a0f4
4087afa
2944199
9cc84e9
a13ee62
86c457b
fe24361
03702cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2017 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.api.factory.server; | ||
|
||
import org.eclipse.che.api.factory.server.model.impl.FactoryImpl; | ||
import org.eclipse.che.api.factory.server.spi.FactoryDao; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Captor; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.testng.MockitoTestNGListener; | ||
import org.testng.annotations.Listeners; | ||
import org.testng.annotations.Test; | ||
|
||
import static com.google.common.base.Strings.isNullOrEmpty; | ||
import static org.mockito.Mockito.verify; | ||
import static org.testng.Assert.assertFalse; | ||
|
||
/** | ||
* @author Max Shaposhnik (mshaposhnik@codenvy.com) on 3/20/17. | ||
*/ | ||
@Listeners(value = {MockitoTestNGListener.class}) | ||
public class FactoryManagerTest { | ||
|
||
@Mock | ||
private FactoryDao factoryDao; | ||
|
||
@InjectMocks | ||
private FactoryManager factoryManager; | ||
|
||
@Captor | ||
private ArgumentCaptor<FactoryImpl> factoryCaptor; | ||
|
||
@Test | ||
public void shouldGenerateNameOnFactoryCreation() throws Exception { | ||
final FactoryImpl factory = FactoryImpl.builder().generateId().build(); | ||
factoryManager.saveFactory(factory); | ||
verify(factoryDao).create(factoryCaptor.capture()); | ||
assertFalse(isNullOrEmpty(factoryCaptor.getValue().getName())); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-- | ||
-- [2012] - [2017] Codenvy, S.A. | ||
-- All Rights Reserved. | ||
-- | ||
-- NOTICE: All information contained herein is, and remains | ||
-- the property of Codenvy S.A. and its suppliers, | ||
-- if any. The intellectual and technical concepts contained | ||
-- herein are proprietary to Codenvy S.A. | ||
-- and its suppliers and may be covered by U.S. and Foreign Patents, | ||
-- patents in process, and are protected by trade secret or copyright law. | ||
-- Dissemination of this information or reproduction of this material | ||
-- is strictly forbidden unless prior written permission is obtained | ||
-- from Codenvy S.A.. | ||
-- | ||
|
||
CREATE UNIQUE INDEX index_che_factory_name_user_id ON che_factory (user_id, name); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
-- | ||
-- [2012] - [2017] Codenvy, S.A. | ||
-- All Rights Reserved. | ||
-- | ||
-- NOTICE: All information contained herein is, and remains | ||
-- the property of Codenvy S.A. and its suppliers, | ||
-- if any. The intellectual and technical concepts contained | ||
-- herein are proprietary to Codenvy S.A. | ||
-- and its suppliers and may be covered by U.S. and Foreign Patents, | ||
-- patents in process, and are protected by trade secret or copyright law. | ||
-- Dissemination of this information or reproduction of this material | ||
-- is strictly forbidden unless prior written permission is obtained | ||
-- from Codenvy S.A.. | ||
-- | ||
|
||
|
||
-- Generate names where they are not set. | ||
UPDATE che_factory | ||
SET name = concat('f', right(id, 9)) WHERE name IS NULL OR name = ''; | ||
|
||
-- Make names unique for the same user, e.g if there is more than one factory with same name and user, | ||
-- leave first one and rename others. | ||
WITH dupes AS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do this migration on codenvy tables before we move factory to che? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I can move naming uniques into 1.1 in codenvy, then 1.2 will do the migration, and costraint will be just in |
||
( SELECT id, user_id, name | ||
FROM che_factory | ||
WHERE (name,user_id) | ||
IN (SELECT name, user_id | ||
FROM che_factory | ||
GROUP BY name, user_id | ||
HAVING count(*) > 1) | ||
), | ||
uniques AS | ||
( WITH q as | ||
( SELECT *, row_number() | ||
OVER (PARTITION BY name,user_id ORDER BY name,user_id) | ||
AS rn FROM che_factory | ||
) | ||
SELECT id FROM q WHERE rn = 1 | ||
) | ||
UPDATE che_factory | ||
SET name = concat(che_factory.name, '-', right(dupes.id, 9)) | ||
FROM dupes, uniques | ||
WHERE dupes.id = che_factory.id AND NOT EXISTS (SELECT id FROM uniques WHERE che_factory.id = uniques.id); | ||
|
||
-- Create composite unique index. | ||
CREATE UNIQUE INDEX index_che_factory_name_user_id ON che_factory (user_id, name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script name does not match the content