Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate to postgres and postgREST #263

Merged
merged 29 commits into from
Sep 22, 2021
Merged

chore: migrate to postgres and postgREST #263

merged 29 commits into from
Sep 22, 2021

Conversation

hugomrdias
Copy link
Contributor

@hugomrdias hugomrdias commented Jul 22, 2021

This PR now holds the postgres migration code in the /packages/api/db.

The packages/db folder still has the old fauna db code and will be deleted in the future.

Refer to #331 for more info on the migration plan.

@hugomrdias hugomrdias requested a review from alanshaw July 22, 2021 11:16
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jul 22, 2021

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: e5a575c
Status: ✅  Deploy successful!
Preview URL: https://d8aaa9be.nft-storage.pages.dev

View logs

@hugomrdias hugomrdias linked an issue Aug 17, 2021 that may be closed by this pull request
12 tasks
@hugomrdias hugomrdias changed the title chore: migrate to fauna chore: migrate to postgres and postgREST Sep 1, 2021
Copy link
Contributor

@alanshaw alanshaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏 comments and requests so far:

  • I need to be able to review the PR and it's just too big currently.
    • Please can we have a separate PR for cf-sync against this branch?
    • Can we please remove the fauna stuff? We don't need it right?
    • I don't understand why we have a duplicated website? Can it be avoided/minimised?
  • Is a way we can just refactor the API code to use a database "client", and then pick the right one depending on the version number in the route URL?
  • I don't believe we can enforce a foreign key constraint on the dagcargo aggregate_entries table to our content table because it refers to CIDs in web3.storage also. We may have to write stored procedures for retrieving the data we need when deals info is involved.
  • Can we remove references to superbase in our code (aside from importing the client from the @superbase namespace)? As far as I can tell we're just using postgREST
  • supabase.ts please can we not mix typscript and javascript!

@hugomrdias
Copy link
Contributor Author

🙏 comments and requests so far:

  • I need to be able to review the PR and it's just too big currently.

    • Please can we have a separate PR for cf-sync against this branch?

i would ask you to just ignore all changes inside tools for now, i will tidy up asap

  • Can we please remove the fauna stuff? We don't need it right?

done

  • I don't understand why we have a duplicated website? Can it be avoided/minimised?

Some stuff is a bit different specially in the auth keys pages, because now we can simplify a bunch. But i will reduce the duplication where possible.

  • Is a way we can just refactor the API code to use a database "client", and then pick the right one depending on the version number in the route URL?

We could but it gets a bit messy, i tried this already and changed to duplication.

  • I don't believe we can enforce a foreign key constraint on the dagcargo aggregate_entries table to our content table because it refers to CIDs in web3.storage also. We may have to write stored procedures for retrieving the data we need when deals info is involved.

we should, we just need to reference to join

  • Can we remove references to superbase in our code (aside from importing the client from the @superbase namespace)? As far as I can tell we're just using postgREST

done

  • supabase.ts please can we not mix typscript and javascript!

im using .ts because the types i generate from the openapi endpoint are really hard to use with jsdocs

this.client.from<definitions['account']>('account').upsert(account)

@alanshaw
Copy link
Contributor

alanshaw commented Sep 6, 2021

Some stuff is a bit different specially in the auth keys pages, because now we can simplify a bunch. But i will reduce the duplication where possible.

We don't want differences! Just minimal changes to make this work and give me a fighting chance of reviewing the PR properly.

  • Is a way we can just refactor the API code to use a database "client", and then pick the right one depending on the version number in the route URL?

We could but it gets a bit messy, i tried this already and changed to duplication.

How come it got messy? The duplication is difficult to review because I can't tell what's new/old.

  • I don't believe we can enforce a foreign key constraint on the dagcargo aggregate_entries table to our content table because it refers to CIDs in web3.storage also. We may have to write stored procedures for retrieving the data we need when deals info is involved.

we should, we just need to reference to join

Please explain?

  • supabase.ts please can we not mix typscript and javascript!

im using .ts because the types i generate from the openapi endpoint are really hard to use with jsdocs

this.client.from<definitions['account']>('account').upsert(account)

Understood but if we want to typescript things then we need a planned effort to do that. If we let this in it'll set precedence for a mixture and we'll not be able to spend time on it because what we have will be good enough and we'll end up doing other bits peicemeal. We'll then spend an extended period with a messy mixed code base and may never finish.

I'm not saying it's undesirable, just we should do it later. Create a PR, open an issue and we'll get it in.

@hugomrdias
Copy link
Contributor Author

hugomrdias commented Sep 6, 2021

Regarding the duplication:

  • in next.js, it rely on the file system structure to build urls and going into the dynamic routes would make it difficult to use the static export
  • in the api basically the same thing, we could just setup v1 routes and point to the same files but the code would fork in multiple points or i would need to build a new abstraction for the old code to match the new db client.

the way i do this is, review in vscode with the github extension and just put the v0 and v1 files next to each other.

Regarding fdw:
we should be able to join because we are not actually enforcing anything just joining by reference (cid) so it doesnt matter if dag_cargo has cid (from web3) that we dont have.

i will remove typescript specific stuff and make everything js/jsdoc

@hugomrdias
Copy link
Contributor Author

also i feel safer when deploy to production with the duplication, but i will try to merge everything at least in the website

@hugomrdias
Copy link
Contributor Author

bah, nextjs doesnt support optional dynamic routes /login and /v1/login as [[version]]/login

alanshaw pushed a commit that referenced this pull request Sep 6, 2021
I've taken the schema from #263 with the following changes:

* Primary keys are called `id` except where we're using domain data as the primary key (currently only `cid` in content)
* Use shortened primary key definition to use `BIGSERIAL` for enhanced readability
* Added `nftstorage` schema
* Renamed `account` -> `user`
* Make `issuer` NOT the primary key on `user`, added `id` column for primary and referenced it from other tables
* Prefixed foreign keys with (complete) referenced table name where appropriate
* Renamed `cid` in the `upload` table to `source_cid` so more obvious it is the given/original/provided/source CID
* Added the `deleted_at` column to `upload` (and had to remove the `UNIQUE` constraint because of it)
* Consistent enums capitalization to match web3.storage and niftysave
* Added `upload_type` enum
* Renamed `content.size` to `content.dag_size` for consistency with web3.storage
@alanshaw alanshaw mentioned this pull request Sep 6, 2021
Copy link
Contributor

@alanshaw alanshaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opened #395 so we can collaborate on the schema. It includes the changes in the comments.

@hugomrdias
Copy link
Contributor Author

@alanshaw all changes in #395 already ported to this PR

all future changes to the schema should be easy to refactor in the db-client.js file and everything else should be caught by the type checker.

@hugomrdias hugomrdias marked this pull request as ready for review September 9, 2021 18:31
@hugomrdias hugomrdias mentioned this pull request Sep 10, 2021
19 tasks
Copy link
Contributor

@alanshaw alanshaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not finished, but submitting feedback so far.

Note for me: I got down to packages/api/src/utils/db-client-types.ts

Copy link
Contributor

@alanshaw alanshaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'm done reviewing for now.

I've ignored the cf-sync and cron changes. I believe they are coming in a different PR? Can we remove from here?

I've also not really looked at the website stuff. I'm still not sure I understand why the need to copy functionality? There should be no API changes, why can't we just build with a different API endpoint?

@alanshaw
Copy link
Contributor

@hugomrdias remember to merge this with feat:/fix: so it gets picked up by release please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Database Migration
2 participants