Skip to content

Commit

Permalink
fix(jdbc): cast properly state
Browse files Browse the repository at this point in the history
  • Loading branch information
tchiotludo committed Jun 22, 2022
1 parent ae5007c commit e33c3b7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ protected void find() {
ArrayListTotal<Execution> executions = executionRepository.find(Pageable.from(1, 10), null, null, null, null, null, null);
assertThat(executions.getTotal(), is(28L));
assertThat(executions.size(), is(10));

executions = executionRepository.find(Pageable.from(1, 10), null, null, null, null, null, List.of(State.Type.RUNNING, State.Type.FAILED));
assertThat(executions.getTotal(), is(8L));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package io.kestra.repository.postgres;

import io.kestra.core.models.executions.Execution;
import io.kestra.core.models.flows.State;
import io.kestra.core.repositories.ExecutionRepositoryInterface;
import io.kestra.jdbc.repository.AbstractJdbcExecutionRepository;
import io.kestra.jdbc.runner.AbstractJdbcExecutorStateStorage;
import io.micronaut.context.ApplicationContext;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import org.jooq.Condition;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Singleton
@PostgresRepositoryEnabled
Expand All @@ -19,6 +24,18 @@ public PostgresExecutionRepository(ApplicationContext applicationContext, Abstra
super(new PostgresRepository<>(Execution.class, applicationContext), executorStateStorage);
}

@Override
protected Condition statesFilter(List<State.Type> state) {
return DSL.or(state
.stream()
.map(Enum::name)
.map(s -> DSL.field("state_current")
.eq(DSL.field("CAST(? AS state_type)", SQLDataType.VARCHAR(50).getArrayType(), s)
))
.collect(Collectors.toList())
);
}

@Override
protected Condition findCondition(String query) {
return this.jdbcRepository.fullTextCondition(Collections.singletonList("fulltext"), query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import io.kestra.core.repositories.ArrayListTotal;
import io.kestra.core.repositories.ExecutionRepositoryInterface;
import io.kestra.core.runners.Executor;
import io.kestra.jdbc.runner.AbstractJdbcExecutorStateStorage;
import io.kestra.core.runners.ExecutorState;
import io.kestra.jdbc.runner.AbstractJdbcExecutorStateStorage;
import io.kestra.jdbc.runner.JdbcIndexerInterface;
import io.micronaut.data.model.Pageable;
import jakarta.inject.Singleton;
Expand Down Expand Up @@ -60,6 +60,11 @@ public Optional<Execution> findById(String id) {

abstract protected Condition findCondition(String query);

protected Condition statesFilter(List<State.Type> state) {
return field("state_current")
.in(state.stream().map(Enum::name).collect(Collectors.toList()));
}

public ArrayListTotal<Execution> find(
Pageable pageable,
@Nullable String query,
Expand Down Expand Up @@ -98,8 +103,7 @@ public ArrayListTotal<Execution> find(
}

if (state != null) {
select = select.and(field("state_current")
.in(state.stream().map(Enum::name).collect(Collectors.toList())));
select = select.and(this.statesFilter(state));
}

if (query != null) {
Expand Down

0 comments on commit e33c3b7

Please sign in to comment.