Skip to content

Commit

Permalink
Setup CI
Browse files Browse the repository at this point in the history
  • Loading branch information
jeppester committed Oct 26, 2024
1 parent 3a7b363 commit c267312
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 77 deletions.
13 changes: 13 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 2
updates:
- package-ecosystem: 'npm'
directory: '/'
schedule:
interval: 'weekly'
allow:
- dependency-type: 'all'

- package-ecosystem: 'github-actions'
directory: '/'
schedule:
interval: 'daily'
86 changes: 86 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name: CI

on: [push]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: checkout repository
uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: '.tool-versions'
cache: 'npm'
- name: npm
run: npm ci
- name: lint
run: npm run lint
schema:
runs-on: ubuntu-latest

env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres
NODE_ENV: production

services:
postgres:
image: postgres:15.5-alpine
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 2s --health-timeout 5s --health-retries 5

steps:
- name: checkout repository
uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: '.tool-versions'
cache: 'npm'
- name: npm
run: npm ci
- name: run migrations
run: |
node ace db:migrate
- name: check git diff
run: git diff --exit-code db/types.d.ts

tests:
runs-on: ubuntu-latest

env:
DATABASE_SERVER: postgres://postgres:postgres@localhost:5432
NODE_ENV: test

services:
postgres:
image: postgres:15.5-alpine
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 2s --health-timeout 5s --health-retries 5

steps:
- name: checkout repository
uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: '.tool-versions'
cache: 'npm'
- name: npm
run: npm ci
- name: playwright
run: npx playwright install chromium
- name: setup database
run: |
node ace db:create
node ace db:migrate
- name: tests
run: node ace test --coverage
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs v20.16.0
2 changes: 1 addition & 1 deletion app/services/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import env from '#start/env'
import { databaseConfig } from '#config/database'

const dialect = new PostgresDialect({
pool: new PG.Pool(databaseConfig[env.get('NODE_ENV')]()),
pool: new PG.Pool(databaseConfig[env.get('NODE_ENV') as keyof typeof databaseConfig]()),
})

export const globalDb = new Kysely<DB>({
Expand Down
11 changes: 8 additions & 3 deletions commands/db_migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { CommandOptions } from '@adonisjs/core/types/ace'
import { databaseConfig } from '#config/database'
import { Cli as kyselyCodegenCli } from 'kysely-codegen/dist/cli/cli.js'
import env from '#start/env'
import { default as PG } from 'pg'
import { promisify } from 'node:util'

export default class KyselyMigrate extends BaseCommand {
static commandName = 'db:migrate'
Expand Down Expand Up @@ -45,7 +47,10 @@ export default class KyselyMigrate extends BaseCommand {
* Runs migrations up method
*/
async run() {
this.logger.info(`Migrating ${databaseConfig[env.get('NODE_ENV')]().database}`)
const config = databaseConfig[env.get('NODE_ENV') as keyof typeof databaseConfig]()
const client = new PG.Client(config)

this.logger.info(`Migrating ${client.database}`)

const { error, results } = await this.migrator.migrateToLatest()

Expand Down Expand Up @@ -78,8 +83,8 @@ export default class KyselyMigrate extends BaseCommand {
* Update database types
*/
this.logger.info('Updating database types with kysely-codegen')
const config = databaseConfig[env.get('NODE_ENV')]()
const url = `postgres://${config.user}:${config.password}@${config.host}:${config.port}/${config.database}`
const url = `postgres://${client.user}:${client.password}@${client.host}:${client.port}/${client.database}`

await new kyselyCodegenCli().generate({
url,
dialectName: 'postgres',
Expand Down
10 changes: 4 additions & 6 deletions config/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import env from '#start/env'
import { basename, join } from 'node:path'
import PG from 'pg'

type DatabaseConfig = Record<string, () => PG.ClientConfig>

const requireEnvVar = (key: string) => {
const value = env.get(key)
if (!value) throw new Error(`${key} required for ${env.get('NODE_ENV')}`)
Expand All @@ -13,13 +11,13 @@ const requireEnvVar = (key: string) => {
const appFolderName = basename(join(import.meta.dirname, '..')).replace(/[^\w]+/, '_')

// const type = Record<>
export const databaseConfig: DatabaseConfig = {
production: () => {
export const databaseConfig = {
production: (): PG.ClientConfig => {
return {
connectionString: requireEnvVar('DATABASE_URL'),
}
},
development: () => {
development: (): PG.ClientConfig => {
const url = new URL(requireEnvVar('DATABASE_SERVER'))
return {
host: url.hostname,
Expand All @@ -29,7 +27,7 @@ export const databaseConfig: DatabaseConfig = {
database: `${appFolderName}_development`,
}
},
test: () => {
test: (): PG.ClientConfig => {
const url = new URL(requireEnvVar('DATABASE_SERVER'))
return {
host: url.hostname,
Expand Down
Loading

0 comments on commit c267312

Please sign in to comment.