Skip to content

Commit

Permalink
fix(db): use text constraints, not limited size data types (#757)
Browse files Browse the repository at this point in the history
* fix(db): use text constraints, not limited size data types

* test(rule): add test for maximum rule name length
  • Loading branch information
andrewazores authored Jan 10, 2025
1 parent 4960a47 commit e8ca7fe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/main/resources/db/migration/V4.0.0__cryostat.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
maxAge bigint not null,
maxSize bigint not null,
metadata jsonb,
name varchar(255),
name text check (char_length(name) < 64),
remoteId bigint not null,
startTime bigint not null,
state smallint check (state between 0 and 4),
Expand All @@ -40,48 +40,48 @@
create table DiscoveryNode (
id bigint not null,
labels jsonb,
name varchar(255) not null,
nodeType varchar(255) not null,
name text not null check (char_length(name) < 255),
nodeType text not null check (char_length(nodeType) < 255),
parentNode bigint,
primary key (id)
);

create table DiscoveryPlugin (
id uuid not null,
builtin boolean not null,
callback varchar(255) unique,
callback text unique,
credential_id bigint unique,
realm_id bigint not null unique,
primary key (id)
);

create table MatchExpression (
id bigint not null,
script varchar(255) not null,
script text not null check (char_length(script) < 1024),
primary key (id)
);

create table Rule (
id bigint not null,
archivalPeriodSeconds integer not null,
description varchar(255),
description text check (char_length(description) < 1024),
enabled boolean not null,
eventSpecifier varchar(255) not null,
eventSpecifier text not null check (char_length(eventSpecifier) < 255),
initialDelaySeconds integer not null,
maxAgeSeconds integer not null,
maxSizeBytes integer not null,
name varchar(255) unique,
name text unique check (char_length(name) < 255),
preservedArchives integer not null,
matchExpression bigint unique,
primary key (id)
);

create table Target (
id bigint not null,
alias varchar(255),
alias text check (char_length(alias) < 255),
annotations jsonb,
connectUrl bytea unique,
jvmId varchar(255),
jvmId text check (char_length(jvmId) < 255),
labels jsonb,
discoveryNode bigint unique,
primary key (id)
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/io/cryostat/rules/RulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.*;

import java.util.Arrays;

import io.cryostat.AbstractTransactionalTestBase;

import io.quarkus.test.common.http.TestHTTPEndpoint;
Expand All @@ -30,6 +32,8 @@
import jakarta.transaction.Transactional;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito;

@QuarkusTest
Expand Down Expand Up @@ -244,6 +248,20 @@ public void testCreateThrowsWhenBodyNull() {
.statusCode(400);
}

@ParameterizedTest
@ValueSource(ints = {1, 16, 64, 128, 256})
public void testCreateThrowsWhenNameTooLong(int len) {
char[] c = new char[len];
Arrays.fill(c, 'a');
rule.put("name", new String(c));
final int limit = 255;
given().body(rule.toString())
.contentType(ContentType.JSON)
.post()
.then()
.statusCode(len <= limit ? 201 : 400);
}

@Test
public void testCreateThrowsWhenMandatoryFieldsUnspecified() {
var badRule = new JsonObject();
Expand Down

0 comments on commit e8ca7fe

Please sign in to comment.