jeremydaly / data-api-client

A "DocumentClient" for the Amazon Aurora Serverless Data API
MIT License
439 stars 61 forks source link

Question: Is there support for schema migrations? #63

Closed jgomes94 closed 3 years ago

jgomes94 commented 3 years ago

I tried to the best of my ability find documentation regarding schema migrations, however, couldn't find any information regarding it.

Does data api client support schema migrations? Is there any documentation?

Valindo commented 3 years ago

hey @jgomes94 did you find something for schema migrations, if yes please share :)

jgomes94 commented 3 years ago

Hi Valindo, in the end I didn't use it. I'm using Typeorm with data api client but I didn't use the migrations, but from what i read it is supported.

skilbjo commented 2 years ago

hey @Valindo , did you ever come up with a solution for migrations + aurora serverless? i don't really need/use an orm, but still looking for a library that will help me with migrations.

if i don't find anything, i will try writing something like this, that is either run manually in a lambda or triggered after a deploy:

migrations/

1/
  - up.sql
  - down.sql
2/
  - up.sql
  - down.sql
etc

js

for folder in migrations/
  try {
    await db.exec(folder + up.sql)
  } catch (err) {
    await db.exec(folder + down.sql)
    break // exit the for loop, do not continue processing up.sql files
  }

would be great tho if there was a library that did this, because there are probably edge cases i haven't considered, and it would be great to have the db itself hold the state of the migrations as opposed to processing them all after every deploy

skilbjo commented 2 years ago

the other complicating issue is migrations files usually have multiple sql commands inside a single file, ie

1/up.sql

create table products (
  id uuid primary key,
  name text
);

create table sales (
  date timestamp, 
  product_id uuid references products.id, 
  amount bigint
);

with data-api-client (actually i believe this is more of a postgres limitation, not data-api-client) i believe you cannot just fs.readFileSync('1/up.sql') because of the ; delimiters inside the file - only the first command will run. Either you manually split this like

const contents = fs.readFileSync(...)

contents.split(';').forEach(command => db.query(command))

or (preferably) use a library do it for you

skilbjo commented 2 years ago

Ok, for future reference/anyone else's benefit, I want to explain how I did this:

first, a correction:

the other complicating issue is migrations files usually have multiple sql commands inside a single file

this is actually not true - all commands are executed. ie await db.query('create table blah (id text); select 1+1;'), both with will run/work.

ok, so now on to how I did it

i used @slonik/migrator (which requires also these dependencies: slonik, pg) this lets you do up/down migrations in raw sql

pros:

cons:

i think now knowing this, i am 50/50 if i should have gone with a hand-rolled/use data-api-client approach instead. (the drawback there being hand-rolled approach and lack of ability to rollback migrations and/or hold migration state in the db)

best of luck to anyone in the future who is considering migrations and is using the data api. please let me know how you did it and how it worked for you, future reader