Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
USER_ID=<user id of system user>
GROUP_ID=<group id of system user>

ORCID_SANDBOX_AUTHENTICATION=<true or false; true=>use the Sandbox Orcid, false=>use the Production Orcid. Defaults to false.>

# Authentication variables
OAUTH_CLIENT_ID=<oauth app client id>
OAUTH_CLIENT_SECRET=<ouath_client_secret goes here>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.breedinginsight.db.migration;

import lombok.extern.slf4j.Slf4j;
import org.breedinginsight.daos.UserDAO;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
import org.jooq.DSLContext;

import javax.inject.Inject;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

@Slf4j
public class V1_31_0__Set_Dev_Admin_Email extends BaseJavaMigration {

@Inject
private DSLContext dsl;
@Inject
private UserDAO userDAO;

final private String ORCID_SANDBOX_AUTHENTICATION = "orcid-sandbox-authentication";

public void migrate(Context context) throws Exception {
Map<String, String> placeholders = context.getConfiguration().getPlaceholders();
boolean isOrcidSandboxAuthentication = Boolean.parseBoolean( placeholders.get(ORCID_SANDBOX_AUTHENTICATION) );
updateDevAdminUser(context, isOrcidSandboxAuthentication);
//Must delete user "Chris Tucker" before adding the new Constraint (User "Chris Tucker" is added by migration V0.5.2__populate-user-data.sql.
// It would violate the new constraint)
// PS -- We miss Chris Tucker.
deleteUserChris(context);
addConstraint(context);
}

private void updateDevAdminUser(Context context, boolean isOrcidSandboxAuthentication) throws SQLException {
String biDevAdminUserEmail = isOrcidSandboxAuthentication ?
"bidevteam@mailinator.com" : "bidevteam@cornell.edu";
try (Statement update = context.getConnection().createStatement()) {
String sql = "UPDATE bi_user SET email='" + biDevAdminUserEmail + "' WHERE NULLIF(email,'') IS NULL AND bi_user.name='BI-DEV Admin'";
log.debug(sql);
update.executeUpdate(sql);
}
}

private void deleteUserChris(Context context) throws SQLException {
try (Statement delete = context.getConnection().createStatement()) {
String sql = "DELETE FROM bi_user WHERE name = 'Chris Tucker' AND email IS NULL";
log.debug(sql);
delete.executeUpdate(sql);
}
}

private void addConstraint(Context context) throws SQLException {
final String CONSTRAINT_NAME = "email_orcid";
// First, drop the constraint if it already exist.
try (Statement altTable = context.getConnection().createStatement()) {
String sql = "ALTER TABLE bi_user DROP CONSTRAINT IF EXISTS "+ CONSTRAINT_NAME;
log.debug(sql);
altTable.executeUpdate(sql);
}

// Add new constraint
try (Statement altTable = context.getConnection().createStatement()) {
String sql = "ALTER TABLE bi_user\n" +
"ADD CONSTRAINT " +CONSTRAINT_NAME+ " CHECK ( (email IS NOT NULL ) OR (orcid IS NULL) ) ;";
log.debug(sql);
altTable.executeUpdate(sql);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ flyway:
placeholders:
default-url: ${brapi.server.default-url}
brapi-reference-source: ${brapi.server.reference-source}
orcid-sandbox-authentication: ${ORCID_SANDBOX_AUTHENTICATION:false}
out-of-order: true
jooq:
datasources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void getDataResponseMetadataFilterSuccess() {

// Check our page numbers weren't altered by the filtered
assertEquals(1, data.getAsJsonPrimitive("totalPages").getAsInt(), "Default total pages is incorrect");
assertEquals(38, data.getAsJsonPrimitive("totalCount").getAsInt(), "Default total count is incorrect");
assertEquals(37, data.getAsJsonPrimitive("totalCount").getAsInt(), "Default total count is incorrect");
assertEquals(50, data.getAsJsonPrimitive("pageSize").getAsInt(), "Default page size is incorrect");
assertEquals(1, data.getAsJsonPrimitive("currentPage").getAsInt(), "Default current page is incorrect");
}
Expand Down