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

feat: Dev database with backup and restore scripts #141

Merged
merged 9 commits into from
Nov 15, 2024

Conversation

DafyddLlyr
Copy link
Contributor

@DafyddLlyr DafyddLlyr commented Nov 4, 2024

What does this PR do?

  • Sets up a local development database
  • Creates a backup script (make a copy of the prod db)
  • Creates a restore script (populate local db)

Comments

This PR sets up a local database, which has a copy of prod data. This means we can make local database changes, test them out, and then deploy those changes to the production database.

We're using Docker to containerise the local database, as well as the "dbtools" service which runs the backup and restore scripts.

In order for this to work, we'll need to use .env.local and .env.production. Both of these files have been added to the Fairhold vault in 1Password - let me know if you have issues accessing these files.

How do I test this out?

You'll need to have do install docker, configure env vars (see above) and then checkout this branch.

From here you should be able to take the following steps -

  • Backup the prod db to your local disk (npm run backup)
  • Populate your local database (npm run restore)
  • Run the app and see it working as expected (npm run dev)
  • Checkout Prisma studio for access to the local data (npm run prisma:studio)

Please have a crack at this and we'll talk through all of this on the call Wednesday 👍

Architecture

flowchart LR
    classDef todo stroke-dasharray: 5 5

    subgraph "fairhold-data"
        S1[(Source 1)] <---> Script
        S2[(Source 2)] <---> Script
        S3[(Source 3)] <---> Script
    end

    subgraph "DB tools"
        LD[(Local disk)] --> UpdateData
        LD <--> Backup
        LD --> Restore
    end

    subgraph "Local development"
        NextLocal[Next.js App]
        PrismaLocal[Prisma]
        LocalDB[(Database)]
        PrismaLocal <--> NextLocal
        LocalDB <--> PrismaLocal
    end
    
    subgraph "CI/CD"
        GitHub["GitHub actions"]:::todo
    end

    subgraph "Production (Vercel)"
        NextProd[Next.js App]
        PrismaProd[Prisma]
        ProdDB[(Database)]
        PrismaProd <--> NextProd
        ProdDB <--> PrismaProd
    end

    Script -- Writes to --> LD
    Restore -- .env.local --> LocalDB
    Backup <-- .env.production --> ProdDB
    UpdateData:::todo -- .env.production --> ProdDB
    
    PrismaLocal -- "Migration files" --> GitHub
    GitHub -- "Merge to main" --> ProdDB
Loading

Next steps...

  • Setup Prisma migrations

Copy link

vercel bot commented Nov 4, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
fairhold-dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 5, 2024 11:49am

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DafyddLlyr DafyddLlyr changed the title dp/backup restore feat: Dev database with backup and restore scripts Nov 4, 2024
Copy link
Collaborator

@zz-hh-aa zz-hh-aa left a comment

Choose a reason for hiding this comment

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

Super exciting, thanks Daf!

Everything works for me locally, though I did run into some errors when running the scripts--I did have to update the end of line sequences from CRLF to LF. Do you know if there's a more OS-agnostic way of handling this?

@DafyddLlyr
Copy link
Contributor Author

Ah good catch @zz-hh-aa - we can fix this with a .gitattributes files. I'll open a PR to handle this 👍

@zz-hh-aa
Copy link
Collaborator

zz-hh-aa commented Nov 5, 2024

Worked great--thanks Daf 🙌

@gabrielegranello
Copy link
Collaborator

Hi @DafyddLlyr I could make it work locally fairly easily, thanks for setting this up!

@DafyddLlyr DafyddLlyr merged commit 3b184d8 into main Nov 15, 2024
5 checks passed
@DafyddLlyr DafyddLlyr deleted the dp/backup-restore branch November 15, 2024 16:48
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.

3 participants