Skip to content

Commit

Permalink
Merge pull request #180 from bheemreddy181/timestampwithouttimezone_lint
Browse files Browse the repository at this point in the history
Timestamp_without_Timezone lint
  • Loading branch information
mbarre authored Feb 26, 2019
2 parents 17bf48b + f312949 commit de95837
Show file tree
Hide file tree
Showing 27 changed files with 850 additions and 221 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package io.github.mbarre.schemacrawler.tool.linter;

import java.sql.Connection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import io.github.mbarre.schemacrawler.utils.LintUtils;

/*
* #%L
* Additional SchemaCrawler Lints
* %%
* Copyright (C) 2015 - 2016 github
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import schemacrawler.schema.Column;
import schemacrawler.schema.Table;
import schemacrawler.tools.lint.BaseLinter;
import schemacrawler.tools.lint.LintSeverity;

/**
* Linter to check if the column has any TimeStamp data type columns if so suggest to use TimeStamp with TimeZones - PostgreSQL reserved
* lint
*
* @author Bheem
* @since 1.0.0
*/
public class LinterTimeStampWithOutTimeZoneColumn extends BaseLinter {
private static final Logger LOGGER = Logger.getLogger(LinterTimeStampWithOutTimeZoneColumn.class.getName());

/**
* The lint that test if column with TimeStamp data type alone used without TimeZone
*
*/
public LinterTimeStampWithOutTimeZoneColumn() {
super();
setSeverity(LintSeverity.critical);
}

/**
* Get the description
*
* @return the description
*/

public String getDescription() {
return "Timestamp without time zone (timestamp) is not a permitted data type. Use timestamp with time zone (timestamptz)";
}

/**
* Get the summary
*
* @return the summary
*/
@Override
public String getSummary() {
return "Use timeStamptz instead of just TimeStamp";
}

/**
* The lint that does the job
*
* @param table
* table
* @param connection
* connection
*/
@Override
protected void lint(final Table table, final Connection connection) {
List<Column> columns = getColumns(table);
for (Column column : columns) {
LOGGER.log(Level.INFO, "Checking {0}...", column.getFullName());
if (LintUtils.isSqlTypeTimeStampBased(column.getColumnDataType().getJavaSqlType().getVendorTypeNumber())) {
addLint(table, getDescription(), column.getFullName());
}
}
}

}
10 changes: 10 additions & 0 deletions src/main/java/io/github/mbarre/schemacrawler/utils/LintUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ public static final boolean isSqlTypeDateBased(int javaSqlType) {
|| javaSqlType == Types.TIMESTAMP
|| javaSqlType == Types.TIMESTAMP_WITH_TIMEZONE;
}

/**
* Tells whether a column is TimeStamp like type or not.
* @param javaSqlType the dataType
* @return is the sqlType is TimeStamp based or not
*/
public static final boolean isSqlTypeTimeStampBased(int javaSqlType) {
return javaSqlType == Types.TIMESTAMP;
}


public static Set<Long> generateSample(int sampleSize, Long totalRows){
Set<Long> sampleIndex = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ io.github.mbarre.schemacrawler.tool.linter.LinterForbiddenPrimaryKeyType
io.github.mbarre.schemacrawler.tool.linter.LinterNoSpaceInNames
io.github.mbarre.schemacrawler.tool.linter.LinterPluralTableName
io.github.mbarre.schemacrawler.tool.linter.LinterLeftSpacePadding
io.github.mbarre.schemacrawler.tool.linter.LinterRightSpacePadding
io.github.mbarre.schemacrawler.tool.linter.LinterRightSpacePadding
io.github.mbarre.schemacrawler.tool.linter.LinterTimeStampWithOutTimeZoneColumn
7 changes: 7 additions & 0 deletions src/main/resources/schemacrawler-linter-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
</linter>
<linter id="io.github.mbarre.schemacrawler.tool.linter.LinterOrphanTable">
<run>true</run>
<severity>high</severity>
<table-exclusion-pattern><![CDATA[\w*\.((databasechangeloglock)|(databasechangelog))]]></table-exclusion-pattern>
</linter>
<linter id="io.github.mbarre.schemacrawler.tool.linter.LinterForbiddenPrimaryKeyType">
Expand All @@ -221,5 +222,11 @@
<severity>high</severity>
<table-exclusion-pattern><![CDATA[\w*\.((databasechangeloglock)|(databasechangelog))]]></table-exclusion-pattern>
</linter>
<linter
id="io.github.mbarre.schemacrawler.tool.linter.LinterTimeStampWithOutTimeZoneColumn">
<run>true</run>
<severity>critical</severity>
<table-exclusion-pattern><![CDATA[\w*\.((databasechangeloglock)|(databasechangelog))]]></table-exclusion-pattern>
</linter>

</schemacrawler-linter-configs>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
#%L
Additional SchemaCrawler Lints
%%
Copyright (C) 2015 - 2016 github
%%
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program. If not, see
<http://www.gnu.org/licenses/gpl-3.0.html>.
#L%
-->


<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">

<changeSet author="bheem" id="1516923589828-1" objectQuotingStrategy="QUOTE_ALL_OBJECTS">

<createTable tableName="test_timetsamp_type" remarks="table for lint LinterTimeStampWithOutTimeZoneColumn">
<column name="id" type="int" remarks="primary key"/>
<column name="content" type="varchar(20)"/>
<column name="created_at" type="timestamp" remarks="column timestamp type"/>
</createTable>

<addPrimaryKey columnNames="id"
constraintName="pk_timetsamp_test"
schemaName="public"
tableName="test_timetsamp_type"/>

<sql>insert into test_timetsamp_type(id, content, created_at) values (1, 'timestamp_lint', current_timestamp)</sql>

</changeSet>

</databaseChangeLog>
1 change: 1 addition & 0 deletions src/test/java/TestLintPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void testLintPlugin() throws Exception
assertTrue(registry.hasLinter("io.github.mbarre.schemacrawler.tool.linter.LinterPluralTableName"));
assertTrue(registry.hasLinter("io.github.mbarre.schemacrawler.tool.linter.LinterLeftSpacePadding"));
assertTrue(registry.hasLinter("io.github.mbarre.schemacrawler.tool.linter.LinterRightSpacePadding"));
assertTrue(registry.hasLinter("io.github.mbarre.schemacrawler.tool.linter.LinterTimeStampWithOutTimeZoneColumn"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.github.mbarre.schemacrawler.tool.linter;

/*-
* #%L
* Additional SchemaCrawler Lints
* %%
* Copyright (C) 2015 - 2019 github
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import io.github.mbarre.schemacrawler.test.utils.LintWrapper;
import io.github.mbarre.schemacrawler.test.utils.PostgreSqlDatabase;
import schemacrawler.schemacrawler.SchemaCrawlerOptions;
import schemacrawler.schemacrawler.SchemaCrawlerOptionsBuilder;
import schemacrawler.schemacrawler.SchemaInfoLevelBuilder;
import schemacrawler.tools.lint.LinterRegistry;

public class LinterTimeStampWithOutTimeZoneColumnTest extends BaseLintTest {

private static final String CHANGE_LOG_TIMESTAMP_CHECK = "src/test/db/liquibase/LinterTimeStampWithOutTimeZoneColumn/db.changelog.xml";
private static PostgreSqlDatabase database;

@BeforeClass
public static void init(){
database = new PostgreSqlDatabase();
database.setUp(CHANGE_LOG_TIMESTAMP_CHECK);
}

@Test
public void testLint() throws Exception{

final LinterRegistry registry = new LinterRegistry();
Assert.assertTrue(registry.hasLinter(LinterTimeStampWithOutTimeZoneColumn.class.getName()));

final SchemaCrawlerOptions options = SchemaCrawlerOptionsBuilder.builder().withSchemaInfoLevel(SchemaInfoLevelBuilder.standard()).toOptions();

Connection connection = DriverManager.getConnection(PostgreSqlDatabase.CONNECTION_STRING,
PostgreSqlDatabase.USER_NAME, database.getPostgresPassword());

List<LintWrapper> lints = executeToJsonAndConvertToLintList(LinterTimeStampWithOutTimeZoneColumn.class.getSimpleName(), options, connection);
Assert.assertEquals(1,lints.size());
int index = 0;
Assert.assertEquals(LinterTimeStampWithOutTimeZoneColumn.class.getName(), lints.get(index).getId());
Assert.assertEquals("public.test_timetsamp_type.created_at", lints.get(index).getValue());
Assert.assertEquals("Timestamp without time zone (timestamp) is not a permitted data type. Use timestamp with time zone (timestamptz)", lints.get(index).getDescription());
Assert.assertEquals("critical", lints.get(index).getSeverity());

}

}
Loading

0 comments on commit de95837

Please sign in to comment.