-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from bheemreddy181/timestampwithouttimezone_lint
Timestamp_without_Timezone lint
- Loading branch information
Showing
27 changed files
with
850 additions
and
221 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
...java/io/github/mbarre/schemacrawler/tool/linter/LinterTimeStampWithOutTimeZoneColumn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/test/db/liquibase/LinterTimeStampWithOutTimeZoneColumn/db.changelog.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
.../io/github/mbarre/schemacrawler/tool/linter/LinterTimeStampWithOutTimeZoneColumnTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.