supabase-community / seed

Automatically seed your database with production-like dummy data based on your schema for local development and testing.
MIT License
483 stars 19 forks source link

fix: sqlite datatype generation #108

Closed avallete closed 7 months ago

avallete commented 7 months ago

Changes:

  1. Refactor some of the "transforms" code between the dialects to merge them togethers
  2. Handle booleans properly with SQLite
  3. Handle BLOB with Buffer correctly in SQLite
  4. Handle dates correctly and allow user to override model to be 1-1 with prisma values in SQLite.
  5. Add e2e tests cases with expectation over common datatypes for both postgres and sqlite.

There is some TODO. Notably we have a chicken-egg problem with dates in sqlite and prisma.

Basically:

~~1. in SQLite, dates are stored as string in ISO format so when you do something like SELECT CURRENT_TIMESTAMP The result is something like: 2024-04-10 23:07:53~~ 2. Prisma will generate a migration for sqlite with a @Datetime column looking like: createdAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP

BUT while prisma declare this in the database, it work differently:

1. Prisma use it's own convention (numeric timestamp) inside those columns, for all Date fields. 2. Prisma doesn't really rely on the sqlite database default value to generate the dates (otherwise the createdAt would take a string values)

So I made change so users using prisma with sqlite and caring about dates can override their models with something like:

{
  createdAt: ({seed}) => new Date((copycat.dateString(seed)).getTime() // will allow to have a numeric value consistent with prisma
}

What would be great is if we could do that automatically on all "Date" fields if we know the adapter used is prisma. But I couldn't find the right abstraction yet. I think I'll need @jgoux and @justinvdm on this.

Has also been solved by adding the capacity for the adapter to patch the userModels values: https://github.com/snaplet/seed/pull/111

Fixes S-1978

linear[bot] commented 7 months ago
S-1978 Better SQLite date handling

Testing sqlite database [https://github.com/remix-run/example-trellix](https://github.com/remix-run/example-trellix) We can see that the "created_at" defined by prisma like so `created_at DATETIME DEFAULT CURENT_TIMESTAMP` get seeded like a text field and seeded with a "date as string". What's expected here by the definition and `DATETIME` field is a timestamp (number of seconds). We should improve our current sqlite transformations to detect this case of usage and seed with the proper value.