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

Bugfix/sql syntax #880

Merged
merged 2 commits into from
Nov 24, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
create table jobrunr_job_counters
CREATE TABLE jobrunr_job_counters
(
name NCHAR(36) PRIMARY KEY,
amount int NOT NULL
Expand All @@ -17,7 +17,7 @@ VALUES ('FAILED', 0);
INSERT INTO jobrunr_job_counters (name, amount)
VALUES ('SUCCEEDED', 0);

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select count(*) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
drop view jobrunr_jobs_stats;
DROP VIEW jobrunr_jobs_stats;

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select count(*) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ VALUES ('succeeded-jobs-counter-cluster', 'succeeded-jobs-counter', 'cluster',
DROP VIEW jobrunr_jobs_stats;
DROP TABLE jobrunr_job_counters;

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select count(*) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DROP VIEW jobrunr_jobs_stats;

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select count(*) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
create table jobrunr_job_counters
CREATE TABLE jobrunr_job_counters
(
name nvarchar(36) NOT NULL,
amount int NOT NULL,
Expand All @@ -18,7 +18,7 @@ VALUES ('FAILED', 0);
INSERT INTO jobrunr_job_counters (name, amount)
VALUES ('SUCCEEDED', 0);

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select count(*) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
drop view jobrunr_jobs_stats;
DROP VIEW jobrunr_jobs_stats;

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select count(*) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ VALUES ('succeeded-jobs-counter-cluster', 'succeeded-jobs-counter', 'cluster',
DROP VIEW jobrunr_jobs_stats;
DROP TABLE jobrunr_job_counters;

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select count(*) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ VALUES ('succeeded-jobs-counter-cluster', 'succeeded-jobs-counter', 'cluster',
DROP VIEW jobrunr_jobs_stats;
DROP TABLE jobrunr_job_counters;

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select count(*) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DROP VIEW jobrunr_jobs_stats;

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select count(*) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand All @@ -9,7 +9,7 @@ select count(*)
(select count(*) from jobrunr_jobs jobs where jobs.state = 'PROCESSING') as processing,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'FAILED') as failed,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'SUCCEEDED') as succeeded,
(select cast(cast(`value` as char(10)) as decimal(10, 0))
(select cast(cast(`value` as char(10)) as decimal(10,0))
from jobrunr_metadata jm
where jm.id = 'succeeded-jobs-counter-cluster') as allTimeSucceeded,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'DELETED') as deleted,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ VALUES ('succeeded-jobs-counter-cluster', 'succeeded-jobs-counter', 'cluster',
DROP VIEW jobrunr_jobs_stats;
DROP TABLE jobrunr_job_counters;

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select count(*) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ VALUES ('succeeded-jobs-counter-cluster', 'succeeded-jobs-counter', 'cluster',
DROP VIEW jobrunr_jobs_stats;
DROP TABLE jobrunr_job_counters;

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select count(*) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
create table jobrunr_job_counters
CREATE TABLE jobrunr_job_counters
(
name nvarchar2(36) NOT NULL,
amount int NOT NULL,
Expand All @@ -18,7 +18,7 @@ VALUES ('FAILED', 0);
INSERT INTO jobrunr_job_counters (name, amount)
VALUES ('SUCCEEDED', 0);

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select (select count(*) from jobrunr_jobs jobs) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
drop view jobrunr_jobs_stats;
DROP VIEW jobrunr_jobs_stats;

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select (select count(*) from jobrunr_jobs jobs) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ VALUES ('succeeded-jobs-counter-cluster', 'succeeded-jobs-counter', 'cluster',
DROP VIEW jobrunr_jobs_stats;
DROP TABLE jobrunr_job_counters;

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select (select count(*) from jobrunr_jobs jobs) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DROP VIEW jobrunr_jobs_stats;

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select count(*) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ VALUES ('succeeded-jobs-counter-cluster', 'succeeded-jobs-counter', 'cluster',
DROP VIEW jobrunr_jobs_stats;
DROP TABLE jobrunr_job_counters;

create view jobrunr_jobs_stats
CREATE VIEW jobrunr_jobs_stats
as
select count(*) as total,
(select count(*) from jobrunr_jobs jobs where jobs.state = 'AWAITING') as awaiting,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ void testIndexesAreCreatedInSchemaForAnsiDatabase() throws SQLException {

assertThat(getAllExecutedStatements())
.areAtLeastOne(stringContaining("CREATE TABLE SOME_SCHEMA.SOME_PREFIX_jobrunr_jobs"))
.areAtLeastOne(stringContaining("CREATE INDEX SOME_PREFIX_jobrunr_state_idx ON SOME_SCHEMA.SOME_PREFIX_jobrunr_jobs (state)"));
.areAtLeastOne(stringContaining("CREATE INDEX SOME_PREFIX_jobrunr_state_idx ON SOME_SCHEMA.SOME_PREFIX_jobrunr_jobs (state)"))
.areAtLeastOne(stringContaining("DROP INDEX SOME_PREFIX_jobrunr_job_updated_at_idx ON SOME_SCHEMA.SOME_PREFIX_jobrunr_jobs"));
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
import org.h2.jdbcx.JdbcDataSource;
import org.jobrunr.JobRunrException;
import org.jobrunr.configuration.JobRunr;
import org.jobrunr.storage.sql.SqlStorageProvider;
import org.jobrunr.storage.sql.common.migrations.DefaultSqlMigrationProvider;
import org.jobrunr.storage.sql.common.migrations.SqlMigration;
import org.jobrunr.storage.sql.h2.H2StorageProvider;
import org.jobrunr.storage.sql.mariadb.MariaDbStorageProvider;
import org.jobrunr.storage.sql.mysql.MySqlStorageProvider;
import org.jobrunr.storage.sql.postgres.PostgresStorageProvider;
import org.jobrunr.storage.sql.sqlite.SqLiteStorageProvider;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand All @@ -17,7 +22,9 @@
import java.nio.file.Paths;
import java.sql.Connection;
import java.util.List;
import java.util.stream.Stream;

import static java.util.Comparator.comparing;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
Expand All @@ -33,6 +40,15 @@ void setupJobStorageProvider() throws IOException {
Files.deleteIfExists(Paths.get(SQLITE_DB1));
}

@Test
void noSmallSQLKeywordsInSqlFilesSoTablePrefixStatementUpdaterWorksCorrectly() {
migrationsFor(H2StorageProvider.class).forEach(this::assertNoLowerCaseSQLKeywords);
migrationsFor(MariaDbStorageProvider.class).forEach(this::assertNoLowerCaseSQLKeywords);
migrationsFor(MySqlStorageProvider.class).forEach(this::assertNoLowerCaseSQLKeywords);
migrationsFor(PostgresStorageProvider.class).forEach(this::assertNoLowerCaseSQLKeywords);
migrationsFor(SqLiteStorageProvider.class).forEach(this::assertNoLowerCaseSQLKeywords);
}

@Test
void testSqlLiteMigrationsUsingMainMethod() {
assertThatCode(() -> DatabaseCreator.main(new String[]{"jdbc:sqlite:" + SQLITE_DB1, "", ""})).doesNotThrowAnyException();
Expand Down Expand Up @@ -114,12 +130,28 @@ private static SQLiteDataSource createDataSource(String url) {
}

private void insertExtraMigrationInDB(DataSource dataSource, DatabaseCreator databaseCreator) {
try(Connection connection = dataSource.getConnection()) {
try (Connection connection = dataSource.getConnection()) {
SqlMigration migration = mock(SqlMigration.class);
when(migration.getFileName()).thenReturn("v014__improve_job_stats.sql");
databaseCreator.updateMigrationsTable(connection, migration);
} catch (Exception e) {
throw new RuntimeException(e);
}
}


private Stream<SqlMigration> migrationsFor(Class<? extends SqlStorageProvider> sqlStorageProviderClass) {
return new DatabaseMigrationsProvider(sqlStorageProviderClass).getMigrations().sorted(comparing(SqlMigration::getFileName));
}

private void assertNoLowerCaseSQLKeywords(SqlMigration sqlMigration) {
try {
String migrationSql = sqlMigration.getMigrationSql();
assertThat(migrationSql)
.describedAs("Migration " + sqlMigration.getFileName() + " contains lowercase SQL Keyword")
.doesNotContain("create ", "unique", "index", "drop", " on ", " view", " replace");
} catch (IOException e) {
throw new RuntimeException("Could not load SQL file", e);
}
}
}