-
-
Notifications
You must be signed in to change notification settings - Fork 42
feat: migrate to postgres #135
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
Open
lraveri
wants to merge
15
commits into
fastify:main
Choose a base branch
from
lraveri:feat/issue-74
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
be434c8
feat: migrate to postgres
lraveri 282749a
feat(db): move updated_at handling to app and remove pg triggers
lraveri 266469b
test(tasks): simplify updated_at assertion in update test
lraveri 1f661ae
ci: switch workflow db service to postgres
lraveri d62c6da
feat(db): upgrade postgres to 18 and change volume mount for version …
lraveri b2ebc8e
chore(types): update env vars to Postgres
lraveri d5f0bab
fix(tasks): stream csv export with pipeline to avoid pg hangs
lraveri 178b8f1
fix(tasks): revert csv stream to pipe chain
lraveri d31ce8f
chore(ci): add temporary 10m job timeout
lraveri 86a6732
chore: add debug logs
lraveri 315c21b
chore: trigger ci
lraveri 44cd4cb
Merge branch 'main' into feat/issue-74
lraveri bcbca49
chore: add pg-query-stream for postgres streaming
lraveri 8793afd
chore: removed debug logs
lraveri b93cca2
chore: remove ci job timeout
lraveri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,19 @@ | ||
| services: | ||
| db: | ||
| image: mysql:8.4 | ||
| image: postgres:18 | ||
| environment: | ||
| MYSQL_ROOT_PASSWORD: root_password | ||
| MYSQL_DATABASE: ${MYSQL_DATABASE} | ||
| MYSQL_USER: ${MYSQL_USER} | ||
| MYSQL_PASSWORD: ${MYSQL_PASSWORD} | ||
| POSTGRES_DB: ${POSTGRES_DATABASE} | ||
| POSTGRES_USER: ${POSTGRES_USER} | ||
| POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} | ||
| ports: | ||
| - 3306:3306 | ||
| - 5432:5432 | ||
| healthcheck: | ||
| test: ["CMD", "mysqladmin", "ping", "-u${MYSQL_USER}", "-p${MYSQL_PASSWORD}"] | ||
| test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DATABASE}"] | ||
| interval: 10s | ||
| timeout: 5s | ||
| retries: 3 | ||
| volumes: | ||
| - db_data:/var/lib/mysql | ||
| - db_data:/var/lib/postgresql | ||
|
|
||
| volumes: | ||
| db_data: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| CREATE TABLE users ( | ||
| id INT AUTO_INCREMENT PRIMARY KEY, | ||
| id SERIAL PRIMARY KEY, | ||
| email VARCHAR(255) UNIQUE NOT NULL, | ||
| username VARCHAR(255) NOT NULL, | ||
| password VARCHAR(255) NOT NULL, | ||
| created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
| updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | ||
| created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP, | ||
| updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| CREATE TABLE tasks ( | ||
| id INT AUTO_INCREMENT PRIMARY KEY, | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(255) NOT NULL, | ||
| author_id INT NOT NULL, | ||
| assigned_user_id INT, | ||
| filename VARCHAR(255), | ||
| status VARCHAR(50) NOT NULL, | ||
| created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
| updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | ||
| created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP, | ||
| updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP, | ||
| FOREIGN KEY (author_id) REFERENCES users(id), | ||
| FOREIGN KEY (assigned_user_id) REFERENCES users(id) | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| CREATE TABLE roles ( | ||
| id INT AUTO_INCREMENT PRIMARY KEY, | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(255) NOT NULL | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,48 @@ | ||
| import { createConnection, Connection } from 'mysql2/promise' | ||
| import { Client } from 'pg' | ||
|
|
||
| if (Number(process.env.CAN_CREATE_DATABASE) !== 1) { | ||
| throw new Error("You can't create the database. Set `CAN_CREATE_DATABASE=1` environment variable to allow this operation.") | ||
| } | ||
|
|
||
| async function createDatabase () { | ||
| const connection = await createConnection({ | ||
| host: process.env.MYSQL_HOST, | ||
| port: Number(process.env.MYSQL_PORT), | ||
| user: process.env.MYSQL_USER, | ||
| password: process.env.MYSQL_PASSWORD | ||
| const databaseName = process.env.POSTGRES_DATABASE | ||
| if (!databaseName) { | ||
| throw new Error('Missing `POSTGRES_DATABASE` environment variable.') | ||
| } | ||
|
|
||
| const connection = new Client({ | ||
| host: process.env.POSTGRES_HOST, | ||
| port: Number(process.env.POSTGRES_PORT), | ||
| user: process.env.POSTGRES_USER, | ||
| password: process.env.POSTGRES_PASSWORD, | ||
| database: 'postgres' | ||
| }) | ||
|
|
||
| try { | ||
| await createDB(connection) | ||
| console.log(`Database ${process.env.MYSQL_DATABASE} has been created successfully.`) | ||
| await connection.connect() | ||
| await createDB(connection, databaseName) | ||
| console.log(`Database ${databaseName} has been created successfully.`) | ||
| } catch (error) { | ||
| console.error('Error creating database:', error) | ||
| } finally { | ||
| await connection.end() | ||
| } | ||
| } | ||
|
|
||
| async function createDB (connection: Connection) { | ||
| await connection.query(`CREATE DATABASE IF NOT EXISTS \`${process.env.MYSQL_DATABASE}\``) | ||
| console.log(`Database ${process.env.MYSQL_DATABASE} created or already exists.`) | ||
| async function createDB (connection: Client, databaseName: string) { | ||
| const exists = await connection.query( | ||
| 'SELECT 1 FROM pg_database WHERE datname = $1', | ||
| [databaseName] | ||
| ) | ||
|
|
||
| if (exists.rowCount > 0) { | ||
| console.log(`Database ${databaseName} already exists.`) | ||
| return | ||
| } | ||
|
|
||
| const safeDbName = databaseName.replace(/"/g, '""') | ||
| await connection.query(`CREATE DATABASE "${safeDbName}"`) | ||
| console.log(`Database ${databaseName} created.`) | ||
| } | ||
|
|
||
| createDatabase() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,43 @@ | ||
| import { createConnection, Connection } from 'mysql2/promise' | ||
| import { Client } from 'pg' | ||
|
|
||
| if (Number(process.env.CAN_DROP_DATABASE) !== 1) { | ||
| throw new Error("You can't drop the database. Set `CAN_DROP_DATABASE=1` environment variable to allow this operation.") | ||
| } | ||
|
|
||
| async function dropDatabase () { | ||
| const connection = await createConnection({ | ||
| host: process.env.MYSQL_HOST, | ||
| port: Number(process.env.MYSQL_PORT), | ||
| user: process.env.MYSQL_USER, | ||
| password: process.env.MYSQL_PASSWORD | ||
| const databaseName = process.env.POSTGRES_DATABASE | ||
| if (!databaseName) { | ||
| throw new Error('Missing `POSTGRES_DATABASE` environment variable.') | ||
| } | ||
|
|
||
| const connection = new Client({ | ||
| host: process.env.POSTGRES_HOST, | ||
| port: Number(process.env.POSTGRES_PORT), | ||
| user: process.env.POSTGRES_USER, | ||
| password: process.env.POSTGRES_PASSWORD, | ||
| database: 'postgres' | ||
| }) | ||
|
|
||
| try { | ||
| await dropDB(connection) | ||
| console.log(`Database ${process.env.MYSQL_DATABASE} has been dropped successfully.`) | ||
| await connection.connect() | ||
| await dropDB(connection, databaseName) | ||
| console.log(`Database ${databaseName} has been dropped successfully.`) | ||
| } catch (error) { | ||
| console.error('Error dropping database:', error) | ||
| } finally { | ||
| await connection.end() | ||
| } | ||
| } | ||
|
|
||
| async function dropDB (connection: Connection) { | ||
| await connection.query(`DROP DATABASE IF EXISTS \`${process.env.MYSQL_DATABASE}\``) | ||
| console.log(`Database ${process.env.MYSQL_DATABASE} dropped.`) | ||
| async function dropDB (connection: Client, databaseName: string) { | ||
| await connection.query( | ||
| 'SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = $1 AND pid <> pg_backend_pid()', | ||
| [databaseName] | ||
| ) | ||
|
|
||
| const safeDbName = databaseName.replace(/"/g, '""') | ||
| await connection.query(`DROP DATABASE IF EXISTS "${safeDbName}"`) | ||
| console.log(`Database ${databaseName} dropped.`) | ||
| } | ||
|
|
||
| dropDatabase() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used anywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hit this bug in CI while running the test “Task image upload, retrieval and delete”:
Cannot find module 'pg-query-stream'Run: https://github.com/fastify/demo/actions/runs/20621476554/job/59224136187
Adding the dependency fixes the error. What’s odd is that the tests pass locally without it.
Any idea why CI behaves differently here and how can I fix it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is required by knex.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yess, it's required by knex as mentioned here.