Skip to content

Commit

Permalink
seed from prisma environment url (#122)
Browse files Browse the repository at this point in the history
* seed from prisma environment url
  • Loading branch information
rubys authored Dec 24, 2024
1 parent 6d34c93 commit d86cdfe
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions gdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ export class GDF {
if (url.startsWith('file:')) return URL.parse(url).pathname
}

get prismaEnv() {
const schema = path.join(this._appdir, 'prisma/schema.prisma')
if (!fs.existsSync(schema)) return null

const schemaContent = fs.readFileSync(schema, 'utf-8')
const urlMatch = schemaContent.match(/url\s*=\s*env\("(.*?)"\)/)
if (urlMatch) return urlMatch[1]
}

get prismaSeed() {
return this.prisma && this.#pj.prisma?.seed
}
Expand Down
10 changes: 9 additions & 1 deletion templates/docker-entrypoint.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { writeFileSync } from 'node:fs'
<% if (prismaFile) { -%>
import path from 'node:path'
import fs from 'node:fs'
<% } else if (prismaEnv && sqlite3) { -%>
import fs from 'node:fs'
<% } -%>
<% } else { -%>
const { spawn } = require('node:child_process')
Expand All @@ -18,6 +20,8 @@ const { writeFileSync } = require('node:fs')
<% if (prismaFile) { -%>
const path = require('node:path')
const fs = require('node:fs')
<% } else if (prismaEnv && sqlite3) { -%>
const fs = require('node:fs')
<% } -%>
<% } -%>

Expand Down Expand Up @@ -63,12 +67,16 @@ if (process.env.DATABASE_URL) {
<%= tab(2) %>source = path.resolve('./.output/server', '<%- prismaFile %>')
<%= tab(2) %>if (!fs.existsSync(source) && fs.existsSync('/data')) fs.symlinkSync(target, source)
<% } -%>
<% } else if (prismaSeed && sqlite3 && prismaEnv) { -%>
<%= tab(2) %>const url = new URL(process.env.<%= prismaEnv %>)
<%= tab(2) %>const target = url.protocol === 'file:' && url.pathname
<%= tab(2) %>const newDb = target && !fs.existsSync(target)
<% } -%>
<% if (prismaFile && prismaSeed && sqlite3) { -%>
<%= tab(2) %>const newDb = !fs.existsSync(target)
<% } -%>
<%= tab(2) %>await exec('<%= npx %> prisma migrate deploy')
<% if (prismaFile && prismaSeed && sqlite3) { -%>
<% if (prismaSeed && sqlite3 && (prismaFile || prismaEnv)) { -%>
<%= tab(2) %>if (newDb) await exec('npx prisma db seed')
<% } -%>
<% } -%>
Expand Down
5 changes: 5 additions & 0 deletions test/base/windows/docker-entrypoint.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#!/usr/bin/env node

const { spawn } = require('node:child_process')
const fs = require('node:fs')

const env = { ...process.env }

;(async() => {
// If running the web server then migrate existing database
if (process.argv.slice(2).join(' ') === 'npx remix-serve ./build/index.js') {
const url = new URL(process.env.DATABASE_URL)
const target = url.protocol === 'file:' && url.pathname
const newDb = target && !fs.existsSync(target)
await exec('npx prisma migrate deploy')
if (newDb) await exec('npx prisma db seed')
}

// launch application
Expand Down
5 changes: 5 additions & 0 deletions test/frameworks/remix-indie/docker-entrypoint.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#!/usr/bin/env node

const { spawn } = require('node:child_process')
const fs = require('node:fs')

const env = { ...process.env }

;(async() => {
// If running the web server then migrate existing database
if (process.argv.slice(2).join(' ') === 'npx remix-serve ./build/index.js') {
const url = new URL(process.env.DATABASE_URL)
const target = url.protocol === 'file:' && url.pathname
const newDb = target && !fs.existsSync(target)
await exec('npx prisma migrate deploy')
if (newDb) await exec('npx prisma db seed')
}

// launch application
Expand Down

0 comments on commit d86cdfe

Please sign in to comment.