Skip to content

Commit

Permalink
fix(db): db seed should try all known config file names by default (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
btisdall committed Jan 25, 2023
1 parent 07177bf commit c55fe39
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 6 deletions.
4 changes: 0 additions & 4 deletions packages/db/lib/seed.mjs
@@ -1,4 +1,3 @@
import { resolve } from 'path'
import pino from 'pino'
import pretty from 'pino-pretty'
import { access } from 'fs/promises'
Expand Down Expand Up @@ -36,9 +35,6 @@ async function seed (_args) {

try {
const { configManager, args } = await loadConfig({
default: {
config: resolve(process.cwd(), 'platformatic.db.json')
},
alias: {
c: 'config'
}
Expand Down
1 change: 1 addition & 0 deletions packages/db/package.json
Expand Up @@ -68,6 +68,7 @@
"pino-pretty": "^9.1.1",
"postgrator": "^7.1.1",
"rfdc": "^1.3.0",
"rimraf": "^4.0.7",
"ua-parser-js": "^1.0.32"
},
"standard": {
Expand Down
33 changes: 33 additions & 0 deletions packages/db/test/cli/seed.test.mjs
Expand Up @@ -5,6 +5,9 @@ import { execa } from 'execa'
import stripAnsi from 'strip-ansi'
import split from 'split2'
import path from 'path'
import rimraf from 'rimraf'
import { copyFile, mkdir, mkdtemp, readdir } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { urlDirname } from '../../lib/utils.js'

const dbLocation = path.resolve(path.join(urlDirname(import.meta.url), '..', 'fixtures', 'sqlite', 'db'))
Expand Down Expand Up @@ -81,6 +84,36 @@ test('seed and start', async ({ comment, equal, match, teardown }) => {
}
})

test('valid config files', async ({ comment }) => {
const fixturesDir = path.join(urlDirname(import.meta.url), '..', 'fixtures')
const validConfigFiles = await readdir(path.join(fixturesDir, 'valid-config-files'))
comment(`valid config files to try: ${validConfigFiles.join(', ')}`)

for (const configFile of validConfigFiles) {
test(`uses ${configFile}`, async ({ comment, match, teardown }) => {
const cwd = await mkdtemp(path.join(tmpdir(), 'seed-'))
comment(`cwd ${cwd}`)

comment('migrating and seeding')
await copyFile(path.join(fixturesDir, 'valid-config-files', configFile), path.join(cwd, configFile))
await mkdir(path.join(cwd, 'migrations'))
await copyFile(path.join(fixturesDir, 'sqlite', 'migrations', '001.do.sql'), path.join(cwd, 'migrations', '001.do.sql'))
const seed = path.join(urlDirname(import.meta.url), '..', 'fixtures', 'sqlite', 'seed.js')

const { stdout } = await execa('node', [cliPath, 'seed', seed], {
cwd
})

{
const sanitized = stripAnsi(stdout)
match(sanitized, /seeding complete/)
}

teardown(() => rimraf.sync(cwd))
})
}
})

test('missing config file', async ({ equal, match }) => {
try {
await execa('node', [cliPath, 'seed'])
Expand Down
17 changes: 17 additions & 0 deletions packages/db/test/fixtures/valid-config-files/platformatic.db.json
@@ -0,0 +1,17 @@
{
"core": {
"connectionString": "sqlite://./db"
},
"server": {
"hostname": "127.0.0.1",
"port": 0,
"logger": {
"level": "info"
}
},
"migrations": {
"dir": "./migrations",
"table": "versions",
"autoApply": true
}
}
17 changes: 17 additions & 0 deletions packages/db/test/fixtures/valid-config-files/platformatic.db.json5
@@ -0,0 +1,17 @@
{
core: {
connectionString: 'sqlite://./db'
},
server: {
hostname: '127.0.0.1',
port: 0,
logger: {
level: 'info'
}
},
migrations: {
dir: './migrations',
table: 'versions',
autoApply: true
}
}
14 changes: 14 additions & 0 deletions packages/db/test/fixtures/valid-config-files/platformatic.db.tml
@@ -0,0 +1,14 @@
[core]
connectionString = "sqlite://./db"

[server]
hostname = "127.0.0.1"
port = 0

[server.logger]
level = "info"

[migrations]
dir = "./migrations"
table = "versions"
autoApply = true
14 changes: 14 additions & 0 deletions packages/db/test/fixtures/valid-config-files/platformatic.db.toml
@@ -0,0 +1,14 @@
[core]
connectionString = "sqlite://./db"

[server]
hostname = "127.0.0.1"
port = 0

[server.logger]
level = "info"

[migrations]
dir = "./migrations"
table = "versions"
autoApply = true
11 changes: 11 additions & 0 deletions packages/db/test/fixtures/valid-config-files/platformatic.db.yaml
@@ -0,0 +1,11 @@
core:
connectionString: sqlite://./db
server:
hostname: 127.0.0.1
port: 0
logger:
level: info
migrations:
dir: './migrations'
table: versions
autoApply: true
11 changes: 11 additions & 0 deletions packages/db/test/fixtures/valid-config-files/platformatic.db.yml
@@ -0,0 +1,11 @@
core:
connectionString: sqlite://./db
server:
hostname: 127.0.0.1
port: 0
logger:
level: info
migrations:
dir: './migrations'
table: versions
autoApply: true
16 changes: 14 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c55fe39

Please sign in to comment.