Skip to content

Commit

Permalink
Schema: limit text columns used in keys
Browse files Browse the repository at this point in the history
Otherwise it's impossible to create an equivalent schema in MySQL.
MySQL requires its keys to have a length.
  • Loading branch information
Al2Klimov committed Jul 15, 2024
1 parent 37fb724 commit c67ed39
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
4 changes: 4 additions & 0 deletions internal/channel/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,9 @@ func ValidateType(t string) error {
return fmt.Errorf("type contains invalid chars, may only contain a-zA-Z0-9, %q given", t)
}

if len(t) > 255 {
return fmt.Errorf("type is too long, at most 255 chars allowed, %d given", len(t))
}

return nil
}
14 changes: 14 additions & 0 deletions internal/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ func (e *Event) Validate() error {
return fmt.Errorf("invalid event: tags must not be empty")
}

for tag := range e.Tags {
if len(tag) > 255 {
return fmt.Errorf("invalid event: tag %q is too long, at most 255 chars allowed, %d given", tag, len(tag))
}
}

for tag := range e.ExtraTags {
if len(tag) > 255 {
return fmt.Errorf(
"invalid event: extra tag %q is too long, at most 255 chars allowed, %d given", tag, len(tag),
)
}
}

if e.SourceId == 0 {
return fmt.Errorf("invalid event: source ID must not be empty")
}
Expand Down
14 changes: 8 additions & 6 deletions schema/pgsql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ CREATE OR REPLACE FUNCTION anynonarrayliketext(anynonarray, text)
CREATE OPERATOR ~~ (LEFTARG=anynonarray, RIGHTARG=text, PROCEDURE=anynonarrayliketext);

CREATE TABLE available_channel_type (
type text NOT NULL,
type varchar(255) NOT NULL,
name text NOT NULL,
version text NOT NULL,
author text NOT NULL,
Expand All @@ -43,7 +43,7 @@ CREATE TABLE available_channel_type (
CREATE TABLE channel (
id bigserial,
name citext NOT NULL,
type text NOT NULL, -- 'email', 'sms', ...
type varchar(255) NOT NULL, -- 'email', 'sms', ...
config text, -- JSON with channel-specific attributes
-- for now type determines the implementation, in the future, this will need a reference to a concrete
-- implementation to allow multiple implementations of a sms channel for example, probably even user-provided ones
Expand Down Expand Up @@ -71,6 +71,7 @@ CREATE TABLE contact (
-- As the username is unique, it must be NULLed for deletion via "deleted = 'y'"
CONSTRAINT uk_contact_username UNIQUE (username),

CONSTRAINT ck_contact_username_up_to_254_chars CHECK (length(username) <= 254),
CONSTRAINT fk_contact_channel FOREIGN KEY (default_channel_id) REFERENCES channel(id)
);

Expand All @@ -79,7 +80,7 @@ CREATE INDEX idx_contact_changed_at ON contact(changed_at);
CREATE TABLE contact_address (
id bigserial,
contact_id bigint NOT NULL,
type text NOT NULL, -- 'phone', 'email', ...
type varchar(255) NOT NULL, -- 'phone', 'email', ...
address text NOT NULL, -- phone number, email address, ...

changed_at bigint NOT NULL,
Expand Down Expand Up @@ -285,7 +286,7 @@ CREATE TABLE object (

CREATE TABLE object_id_tag (
object_id bytea NOT NULL,
tag text NOT NULL,
tag varchar(255) NOT NULL,
value text NOT NULL,

CONSTRAINT pk_object_id_tag PRIMARY KEY (object_id, tag),
Expand All @@ -294,7 +295,7 @@ CREATE TABLE object_id_tag (

CREATE TABLE object_extra_tag (
object_id bytea NOT NULL,
tag text NOT NULL,
tag varchar(255) NOT NULL,
value text NOT NULL,

CONSTRAINT pk_object_extra_tag PRIMARY KEY (object_id, tag),
Expand Down Expand Up @@ -495,7 +496,8 @@ CREATE TABLE browser_session (
user_agent varchar(4096) NOT NULL,
authenticated_at bigint NOT NULL,

CONSTRAINT pk_browser_session PRIMARY KEY (php_session_id)
CONSTRAINT pk_browser_session PRIMARY KEY (php_session_id),
CONSTRAINT ck_browser_session_username_up_to_254_chars CHECK (length(username) <= 254)
);

CREATE INDEX idx_browser_session_authenticated_at ON browser_session (authenticated_at DESC);
Expand Down
7 changes: 7 additions & 0 deletions schema/pgsql/upgrades/034.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE available_channel_type ALTER COLUMN type TYPE varchar(255);
ALTER TABLE channel ALTER COLUMN type TYPE varchar(255);
ALTER TABLE contact ADD CONSTRAINT ck_contact_username_up_to_254_chars CHECK (length(username) <= 254);
ALTER TABLE contact_address ALTER COLUMN type TYPE varchar(255);
ALTER TABLE object_id_tag ALTER COLUMN tag TYPE varchar(255);
ALTER TABLE object_extra_tag ALTER COLUMN tag TYPE varchar(255);
ALTER TABLE browser_session ADD CONSTRAINT ck_browser_session_username_up_to_254_chars CHECK (length(username) <= 254);

0 comments on commit c67ed39

Please sign in to comment.