bob icon indicating copy to clipboard operation
bob copied to clipboard

Respect character_maximum_length when generating random values for string columns

Open mbezhanov opened this issue 1 year ago • 4 comments

Hi, I'm opening this PR to propose a new feature that addresses an issue I ran into while trying out the ORM generators.

Consider the following PostgreSQL schema:

CREATE TABLE pilots (
    id serial PRIMARY KEY,
    name varchar(5) NOT NULL
);

CREATE TABLE jets (
  id serial PRIMARY KEY,
  pilot_id int NOT NULL,
  first_name varchar(7) NOT NULL,
  last_name varchar(8) NOT NULL,
  FOREIGN KEY (pilot_id) REFERENCES pilots(id)
);

CREATE INDEX idx_jets_pilots ON jets (pilot_id);
CREATE INDEX idx_jets_names ON jets (first_name, last_name);

Or, alternatively, the following MySQL schema:

CREATE TABLE pilots (
    id int unsigned NOT NULL,
    name varchar(5) NOT NULL,
    primary key (id)
);

CREATE TABLE jets (
  id int unsigned NOT NULL,
  pilot_id int unsigned NOT NULL,
  first_name varchar(7) NOT NULL,
  last_name varchar(8) NOT NULL,
  primary key (id)
);

CREATE INDEX idx_jets_pilots ON jets (pilot_id);
CREATE INDEX idx_jets_names ON jets (first_name, last_name);
ALTER TABLE jets ADD CONSTRAINT fkey_pilots_1 FOREIGN KEY(pilot_id) REFERENCES pilots(id);

Then try using the following code against either the MySQL or the PostgreSQL schema:

func main() {
	f := factory.New()
	f.AddBasePilotMod(
		factory.PilotMods.RandomID(nil),
		factory.PilotMods.RandomName(nil)
	)
	p := f.NewJet()

	ctx := context.Background()
	//ex, err := bob.Open("mysql", "test:test@/test?parseTime=true")
	ex, err := bob.Open("postgres", "postgres://postgres:test@localhost/postgres?sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}
	_, err = p.CreateMany(ctx, ex, 5)
	if err != nil {
		log.Fatal(err)
	}
}

Bob fails with similar errors:

PostgreSQL:

2024/06/24 17:12:02 pq: value too long for type character varying(5)

MySQL:

2024/06/24 17:14:14 Error 1406 (22001): Data too long for column 'name' at row 1

That's because the random generator doesn't respect the specified character_maximum_length for the string columns. This PR changes this, and the random generators start producing strings truncated to the maximum possible length for each specified column. I hope that other Bob users will find this feature convenient too.

If you require any changes, just let me know :slightly_smiling_face:

mbezhanov avatar Jun 24 '24 14:06 mbezhanov

I just realized this is possibly the same issue as the one reported in https://github.com/stephenafamo/bob/issues/178.

mbezhanov avatar Jun 24 '24 14:06 mbezhanov

Thanks for putting this together, however, I'm not sure if this is the best way to implement this.

Ideally, instead of having a dedicated truncateString function strictly for string types, I'd want to expand this to be able to possibly apply to other types.

I'll give this some more thought and share over the next week 🙏🏾

stephenafamo avatar Jun 24 '24 21:06 stephenafamo

Sure, I'll be happy to rework this. Should I close this PR or leave it open for now to facilitate a discussion?

mbezhanov avatar Jun 25 '24 05:06 mbezhanov

Should I close this PR or leave it open for now to facilitate a discussion?

Let's leave it open for now

stephenafamo avatar Jun 26 '24 08:06 stephenafamo

Only took a year 😅, but I finally got round to taking a stab at this in #425.

I'll close this PR now, and once #425 is polished and all the tests are passing, I'll merge that instead.

I really appreciate the PR.

stephenafamo avatar May 22 '25 01:05 stephenafamo