Skip to content
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

Lps 107396 #85277

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d50c100
LPS-107396 If we don't add null or not null we don't have to include …
achaparro Jan 21, 2020
7de22dc
LPS-107396 Add nullable clause in those databases which allow it
achaparro Jan 21, 2020
f64d257
LPS-107396 Add it in two commands for Postgres
achaparro Jan 21, 2020
8b197d4
LPS-107396 Implementation for DB2. DB2 does not allow to execute two …
achaparro Jan 25, 2020
372806c
LPS-107396 Postgres does not allow to change the type and the nullabl…
achaparro Jan 25, 2020
d54452f
LPS-107396 Oracle throws an error if we try to change the nullable co…
achaparro Jan 25, 2020
750d7f5
LPS-107396 SQLServer returns NVARCHAR for Varchar, string o text types
achaparro Jan 25, 2020
7ac5a2e
LPS-107396 I think we do not need this anymore
achaparro Jan 25, 2020
9c09da7
LPS-107396 Take into account the case when nullable is blank
achaparro Jan 28, 2020
770990d
LPS-107396 New integration tests
achaparro Jan 31, 2020
7f897f3
LPS-107396 Null is always the last word when we have 5 words. In this…
achaparro Jan 31, 2020
de60548
LPS-107396 Now we need to validate if the nullable condition has been…
achaparro Jan 31, 2020
d036ad9
LPS-107396 New unit tests for reword with every database and DBInspector
achaparro Jan 31, 2020
98615dd
LPS-107396 Use another column instead of name since alter table colum…
achaparro Jan 31, 2020
66eba13
LPS-107396 Use CharPool
shuyangzhou Jan 31, 2020
5822a7a
LPS-107396 Less String chopping
shuyangzhou Jan 31, 2020
0a30ddb
LPS-107396 Fix test class name
shuyangzhou Jan 31, 2020
c18eee5
LPS-107396 Implement a more generic logic for DB2
achaparro Feb 22, 2020
2533612
LPS-107396 Throw SQLException where buildSQL is used
achaparro Feb 22, 2020
b182892
LPS-107396 SQLException is allowed now
shuyangzhou Feb 25, 2020
7a77119
LPS-107396 Proper assert
shuyangzhou Feb 25, 2020
4180b47
LPS-107396 Make it test friendly
shuyangzhou Feb 25, 2020
7f376d8
LPS-107396 Avoid powermock
shuyangzhou Feb 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -215,7 +215,7 @@ public void testHasTableLowerCase() throws Exception {

@Test
public void testHasTableNonexisting() throws Exception {
Assert.assertTrue(!_dbInspector.hasTable(_TABLE_NAME_NONEXISTING));
Assert.assertFalse(_dbInspector.hasTable(_TABLE_NAME_NONEXISTING));
}

@Test
Expand All @@ -228,6 +228,17 @@ public void testHasTableUpperCase() throws Exception {
_dbInspector.hasTable(StringUtil.toUpperCase(_TABLE_NAME)));
}

@Test
public void testIsNotNullColumnNullable() throws Exception {
Assert.assertTrue(_dbInspector.isNullable(_TABLE_NAME, "nilColumn"));
}

@Test
public void testIsNullableColumnNullable() throws Exception {
Assert.assertFalse(
_dbInspector.isNullable(_TABLE_NAME, "notNilColumn"));
}

private static final String _COLUMN_NAME = "id";

private static final String _COLUMN_NAME_NONEXISTING = "nonexistingColumn";
Expand Down
@@ -0,0 +1,134 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/

package com.liferay.portal.dao.db.test;

import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;
import com.liferay.petra.string.StringBundler;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.dao.db.DB;
import com.liferay.portal.kernel.dao.db.DBInspector;
import com.liferay.portal.kernel.dao.db.DBManagerUtil;
import com.liferay.portal.kernel.dao.jdbc.DataAccess;
import com.liferay.portal.kernel.test.rule.AggregateTestRule;
import com.liferay.portal.test.rule.LiferayIntegrationTestRule;

import java.sql.Connection;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* @author Alberto Chaparro
*/
@RunWith(Arquillian.class)
public class DBTest {

@ClassRule
@Rule
public static final AggregateTestRule aggregateTestRule =
new LiferayIntegrationTestRule();

@Before
public void setUp() throws Exception {
_connection = DataAccess.getConnection();

_dbInspector = new DBInspector(_connection);

_db = DBManagerUtil.getDB();

_db.runSQL(
StringBundler.concat(
"create table ", _TABLE_NAME, "(id LONG not null primary key, ",
"notNilColumn VARCHAR(75) not null, nilColumn VARCHAR(75) ",
"null)"));
}

@After
public void tearDown() throws Exception {
_db.runSQL("drop table " + _TABLE_NAME);

DataAccess.cleanUp(_connection);
}

@Test
public void testAlterColumnTypeAlterSize() throws Exception {
_db.runSQL(_getAlterColumType("notNilColumn", "VARCHAR(200) not null"));

Assert.assertTrue(
_dbInspector.hasColumnType(
_TABLE_NAME, "notNilColumn", "VARCHAR(200) not null"));
}

@Test
public void testAlterColumnTypeChangeToNotNull() throws Exception {
_db.runSQL(_getAlterColumType("nilColumn", "VARCHAR(75) not null"));

Assert.assertTrue(
_dbInspector.hasColumnType(
_TABLE_NAME, "nilColumn", "VARCHAR(75) not null"));
}

@Test
public void testAlterColumnTypeChangeToNull() throws Exception {
_db.runSQL(_getAlterColumType("notNilColumn", "VARCHAR(75) null"));

Assert.assertTrue(
_dbInspector.hasColumnType(
_TABLE_NAME, "notNilColumn", "VARCHAR(75) null"));
}

@Test
public void testAlterColumnTypeNoChangesNotNull() throws Exception {
_db.runSQL(_getAlterColumType("notNilColumn", "VARCHAR(75) not null"));

Assert.assertTrue(
_dbInspector.hasColumnType(
_TABLE_NAME, "notNilColumn", "VARCHAR(75) not null"));
}

@Test
public void testAlterColumnTypeNoChangesNull() throws Exception {
_db.runSQL(_getAlterColumType("nilColumn", "VARCHAR(75) null"));

Assert.assertTrue(
_dbInspector.hasColumnType(
_TABLE_NAME, "nilColumn", "VARCHAR(75) null"));
}

private String _getAlterColumType(String columnName, String newType) {
StringBundler sb = new StringBundler(6);

sb.append("alter_column_type ");
sb.append(_TABLE_NAME);
sb.append(StringPool.SPACE);
sb.append(columnName);
sb.append(StringPool.SPACE);
sb.append(newType);

return sb.toString();
}

private static final String _TABLE_NAME = "DBTest";

private Connection _connection;
private DB _db;
private DBInspector _dbInspector;

}
Expand Up @@ -21,6 +21,7 @@
import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;

import java.io.IOException;

Expand All @@ -47,7 +48,7 @@ public DB2DB(int majorVersion, int minorVersion) {
}

@Override
public String buildSQL(String template) throws IOException {
public String buildSQL(String template) throws IOException, SQLException {
template = replaceTemplate(template);

template = reword(template);
Expand Down Expand Up @@ -85,11 +86,6 @@ public String getRecreateSQL(String databaseName) {
return sb.toString();
}

@Override
public boolean isSupportsAlterColumnType() {
return _SUPPORTS_ALTER_COLUMN_TYPE;
}

@Override
public boolean isSupportsInlineDistinct() {
return _SUPPORTS_INLINE_DISTINCT;
Expand Down Expand Up @@ -223,7 +219,7 @@ else if (lowerCaseTemplate.startsWith(ALTER_COLUMN_TYPE)) {
}

@Override
protected String reword(String data) throws IOException {
protected String reword(String data) throws IOException, SQLException {
try (UnsyncBufferedReader unsyncBufferedReader =
new UnsyncBufferedReader(new UnsyncStringReader(data))) {

Expand All @@ -240,6 +236,35 @@ protected String reword(String data) throws IOException {
"@new-column@;",
REWORD_TEMPLATE, template);
}
else if (line.startsWith(ALTER_COLUMN_TYPE)) {
String[] template = buildColumnTypeTokens(line);

line = StringUtil.replace(
"alter table @table@ alter column @old-column@ set " +
"data type @type@;",
REWORD_TEMPLATE, template);

String nullable = template[template.length - 1];

if (!Validator.isBlank(nullable)) {
String nullableAlter;

if (nullable.equals("not null")) {
nullableAlter = StringUtil.replace(
"alter table @table@ alter column " +
"@old-column@ set not null;",
REWORD_TEMPLATE, template);
}
else {
nullableAlter = StringUtil.replace(
"alter table @table@ alter column " +
"@old-column@ drop not null;",
REWORD_TEMPLATE, template);
}

runSQL(nullableAlter);
}
}
else if (line.startsWith(ALTER_TABLE_NAME)) {
String[] template = buildTableNameTokens(line);

Expand Down Expand Up @@ -284,8 +309,6 @@ private String _removeNull(String content) {
Types.INTEGER, Types.BIGINT, Types.VARCHAR, Types.CLOB, Types.VARCHAR
};

private static final boolean _SUPPORTS_ALTER_COLUMN_TYPE = false;

private static final boolean _SUPPORTS_INLINE_DISTINCT = false;

private static final boolean _SUPPORTS_SCROLLABLE_RESULTS = false;
Expand Down
Expand Up @@ -16,13 +16,15 @@

import com.liferay.petra.string.StringBundler;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.dao.db.DBInspector;
import com.liferay.portal.kernel.dao.db.DBType;
import com.liferay.portal.kernel.dao.db.Index;
import com.liferay.portal.kernel.dao.jdbc.DataAccess;
import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;

import java.io.IOException;

Expand All @@ -49,7 +51,7 @@ public OracleDB(int majorVersion, int minorVersion) {
}

@Override
public String buildSQL(String template) throws IOException {
public String buildSQL(String template) throws IOException, SQLException {
template = replaceTemplate(template);
template = reword(template);
template = StringUtil.replace(
Expand Down Expand Up @@ -154,6 +156,16 @@ protected String[] getTemplate() {
return _ORACLE;
}

protected boolean isNullable(String tableName, String columnName)
throws SQLException {

try (Connection con = DataAccess.getConnection()) {
DBInspector dbInspector = new DBInspector(con);

return dbInspector.isNullable(tableName, columnName);
}
}

@Override
protected String replaceTemplate(String template) {

Expand Down Expand Up @@ -181,7 +193,7 @@ protected String replaceTemplate(String template) {
}

@Override
protected String reword(String data) throws IOException {
protected String reword(String data) throws IOException, SQLException {
try (UnsyncBufferedReader unsyncBufferedReader =
new UnsyncBufferedReader(new UnsyncStringReader(data))) {

Expand All @@ -201,9 +213,25 @@ protected String reword(String data) throws IOException {
else if (line.startsWith(ALTER_COLUMN_TYPE)) {
String[] template = buildColumnTypeTokens(line);

String nullable = template[template.length - 1];

if (!Validator.isBlank(nullable)) {
boolean currentNullable = isNullable(
template[0], template[1]);

if ((nullable.equals("null") && currentNullable) ||
(nullable.equals("not null") && !currentNullable)) {

nullable = StringPool.BLANK;
}
}

line = StringUtil.replace(
"alter table @table@ modify @old-column@ @type@;",
"alter table @table@ modify @old-column@ @type@ " +
nullable + ";",
REWORD_TEMPLATE, template);

line = StringUtil.replace(line, " ;", ";");
}
else if (line.startsWith(ALTER_TABLE_NAME)) {
String[] template = buildTableNameTokens(line);
Expand Down
Expand Up @@ -139,11 +139,6 @@ public String getRecreateSQL(String databaseName) {
return sb.toString();
}

@Override
public boolean isSupportsAlterColumnType() {
return _SUPPORTS_ALTER_COLUMN_TYPE;
}

@Override
public boolean isSupportsNewUuidFunction() {
return _SUPPORTS_NEW_UUID_FUNCTION;
Expand Down Expand Up @@ -181,8 +176,11 @@ else if (line.startsWith(ALTER_COLUMN_TYPE)) {
String[] template = buildColumnTypeTokens(line);

line = StringUtil.replace(
"alter table @table@ alter column @old-column@ @type@;",
"alter table @table@ alter column @old-column@ " +
"@type@ @nullable@;",
REWORD_TEMPLATE, template);

line = StringUtil.replace(line, " ;", ";");
}
else if (line.startsWith(ALTER_TABLE_NAME)) {
String[] template = buildTableNameTokens(line);
Expand Down Expand Up @@ -224,12 +222,10 @@ else if (line.contains(DROP_INDEX)) {

private static final int[] _SQL_TYPES = {
Types.LONGVARBINARY, Types.LONGVARBINARY, Types.BIT, Types.TIMESTAMP,
Types.DOUBLE, Types.INTEGER, Types.BIGINT, Types.LONGVARCHAR,
Types.LONGVARCHAR, Types.VARCHAR
Types.DOUBLE, Types.INTEGER, Types.BIGINT, Types.NVARCHAR,
Types.NVARCHAR, Types.NVARCHAR
};

private static final boolean _SUPPORTS_ALTER_COLUMN_TYPE = false;

private static final boolean _SUPPORTS_NEW_UUID_FUNCTION = true;

}
Expand Up @@ -172,8 +172,11 @@ else if (line.startsWith(ALTER_COLUMN_TYPE)) {
String[] template = buildColumnTypeTokens(line);

line = StringUtil.replace(
"alter table @table@ modify @old-column@ @type@;",
"alter table @table@ modify @old-column@ @type@ " +
"@nullable@;",
REWORD_TEMPLATE, template);

line = StringUtil.replace(line, " ;", ";");
}
else if (line.startsWith(ALTER_TABLE_NAME)) {
String[] template = buildTableNameTokens(line);
Expand Down