This repository demonstrates a simple Node.js Todo application that uses PostgreSQL along with automated database migrations via node-pg-migrate
. It also features automated ephemeral database branching using Neon and GitHub Actions, allowing you to safely test schema changes and operations in isolated environments triggered by pull request events.
- PostgreSQL Integration: Connects to a PostgreSQL database for data storage.
- Automated Migrations: Uses
node-pg-migrate
for managing schema changes. - Ephemeral Database Branching: Automatically creates a temporary database branch for each pull request using Neon. Once the pull request is closed, the temporary branch is deleted.
- Main Database Updates: After merging into the main branch, a workflow updates your main database schema. This workflow requires manual approval before deployment.
- Simple Todo Management: Basic CRUD operations for managing todo items.
- Node.js (v14 or higher)
- PostgreSQL database server
- npm (Node Package Manager)
├── migrations/
│ └── 1698765432345_create-todos-table.js
├── .env.example
├── .env
├── index.js
├── package.json
└── README.md
- Clone the repository
- Install dependencies:
npm install
- Create a
.env
file based on.env.example
:cp .env.example .env
- Update the
.env
file with your PostgreSQL connection details:If using Neon for ephemeral branches, ensure your Neon project ID and API key are set as GitHub Secrets and/or environment variables.DATABASE_URL=postgres://username:password@localhost:5432/database_name
This project leverages GitHub Actions and Neon to automate database branching:
-
- When a pull request targeting the main branch is created or updated, the workflow triggers.
- A new ephemeral database branch (clone) is created on Neon using your managed PostgreSQL instance.
- Database migrations and tests run against this temporary copy.
- When the pull request is closed, the workflow deletes the ephemeral branch, cleaning up resources automatically.
-
- Uses the
neondatabase/create-branch-action
to spin up a database branch. - Uses
node-pg-migrate
to run migrations (migrate:up and migrate:down commands). - Automatically deletes the branch using
neondatabase/delete-branch-action
when the pull request closes.
- Uses the
- Run all pending migrations:
npm run migrate:up
- Revert the last migration:
npm run migrate:down
1698765432345_create-todos-table.js
- Creates the initial
todos
table with:id
(Primary Key)title
(varchar)completed
(boolean)created_at
(timestamp) (added later through pull request)
- Creates the initial
After a pull request is merged into the postgresSQL
branch, a separate GitHub Actions workflow updates the main database schema. This workflow requires manual approval before running the migrations to ensure that the changes are verified before being deployed to production.
-
- The workflow triggers on any push to the postgresSQL branch.
- It pauses at the environment approval step. You must approve the deployment in the GitHub repository's Environments settings.
- Once approved, the workflow checks out the repository, installs dependencies, and runs the migration command on the main database using the connection URL stored in the Github Actions secret.
-
Configure the Production Environment:
- Go to your repository’s Settings > Environments.
- Create an environment named production.
- In the environment protection rules, add yourself (or the designated reviewer) as a required reviewer.
-
Configure Secrets: Ensure your repository has a secret with your main PostgreSQL connection URL.
-
Merge and Approve: When a pull request is merged into postgresSQL, the workflow triggers. Approve the deployment manually when prompted in the GitHub Actions tab.
Start the application:
npm start
The application will:
- Connect to the PostgreSQL database (or Neon ephemeral branch, if triggered by a pull request).
- Display the current timestamp.
- List all todos in the database.
Variable | Description | Example |
---|---|---|
DATABASE_URL | PostgreSQL connection URL | postgres://username:password@localhost:5432/todos |
- Do not commit
.env
file to version control. - Ensure that sensitive data (such as database credentials) is stored securely.
- The
.env.example
file should not contain actual credentials.
npm start
- Run the applicationnpm run migrate
- Run database migrationsnpm run migrate:up
- Apply pending migrationsnpm run migrate:down
- Revert the last migration
pg
- PostgreSQL client for Node.jsnode-pg-migrate
- Database migration tooldotenv
- Environment variable management
By integrating automated ephemeral database branching, this project not only demonstrates standard PostgreSQL and migration practices but also shows how to incorporate modern CI/CD techniques to manage database changes safely in a collaborative environment.