Skip to content

Commit

Permalink
litestream restore (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubys authored Dec 24, 2024
1 parent d86cdfe commit 0d5a3e3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions templates/Dockerfile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ RUN <% if (options.cache) { %><%= pkg_cache %>
<% } -%>
<% if (litestream) { -%>
# Install litestream
RUN wget https://github.com/benbjohnson/litestream/releases/download/v0.3.13/litestream-v0.3.13-linux-amd64.deb && \
dpkg -i litestream-v0.3.13-linux-amd64.deb && \
rm litestream-v0.3.13-linux-amd64.deb
Expand Down
12 changes: 9 additions & 3 deletions templates/docker-entrypoint.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,16 @@ if (process.env.DATABASE_URL) {
<% } 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)
<%= tab(2) %><%= litestream ? 'let' : 'const' %> newDb = target && !fs.existsSync(target)
<% } -%>
<% if (prismaFile && prismaSeed && sqlite3) { -%>
<%= tab(2) %>const newDb = !fs.existsSync(target)
<%= tab(2) %><%= litestream ? 'let' : 'const' %> newDb = !fs.existsSync(target)
<% } -%>
<% if (litestream && prismaSeed && sqlite3 && (prismaFile || prismaEnv)) { -%>
<%= tab(2) %>if (newDb && process.env.BUCKET_NAME) {
<%= tab(3) %>await exec(`litestream restore -config litestream.yml -if-replica-exists ${target}`)
<%= tab(3) %>newDb = !fs.existsSync(target)
<%= tab(2) %>}
<% } -%>
<%= tab(2) %>await exec('<%= npx %> prisma migrate deploy')
<% if (prismaSeed && sqlite3 && (prismaFile || prismaEnv)) { -%>
Expand All @@ -90,7 +96,7 @@ if (process.env.DATABASE_URL) {

<% } -%>
<%= tab(1) %>// launch application
<% if (nextjsGeneration) { -%>
<% if (litestream) { -%>
<%= tab(1) %>if (process.env.BUCKET_NAME) {
<%= tab(2) %>await exec(`litestream replicate -config litestream.yml -exec ${JSON.stringify(process.argv.slice(2).join(' '))}`)
<%= tab(1) %>} else {
Expand Down
1 change: 1 addition & 0 deletions test/base/litestream/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ RUN apt-get update -qq && \
apt-get install --no-install-recommends -y ca-certificates openssl wget && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Install litestream
RUN wget https://github.com/benbjohnson/litestream/releases/download/v0.3.13/litestream-v0.3.13-linux-amd64.deb && \
dpkg -i litestream-v0.3.13-linux-amd64.deb && \
rm litestream-v0.3.13-linux-amd64.deb
Expand Down
1 change: 1 addition & 0 deletions test/frameworks/nuxt-prisma/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ RUN apt-get update -qq && \
apt-get install --no-install-recommends -y ca-certificates openssl wget && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Install litestream
RUN wget https://github.com/benbjohnson/litestream/releases/download/v0.3.13/litestream-v0.3.13-linux-amd64.deb && \
dpkg -i litestream-v0.3.13-linux-amd64.deb && \
rm litestream-v0.3.13-linux-amd64.deb
Expand Down
12 changes: 10 additions & 2 deletions test/frameworks/nuxt-prisma/docker-entrypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ const env = { ...process.env }
if (!fs.existsSync(source) && fs.existsSync('/data')) fs.symlinkSync(target, source)
source = path.resolve('./.output/server', './dev.db')
if (!fs.existsSync(source) && fs.existsSync('/data')) fs.symlinkSync(target, source)
const newDb = !fs.existsSync(target)
let newDb = !fs.existsSync(target)
if (newDb && process.env.BUCKET_NAME) {
await exec(`litestream restore -config litestream.yml -if-replica-exists ${target}`)
newDb = !fs.existsSync(target)
}
await exec('npx prisma migrate deploy')
if (newDb) await exec('npx prisma db seed')
}

// launch application
await exec(process.argv.slice(2).join(' '))
if (process.env.BUCKET_NAME) {
await exec(`litestream replicate -config litestream.yml -exec ${JSON.stringify(process.argv.slice(2).join(' '))}`)
} else {
await exec(process.argv.slice(2).join(' '))
}
})()

function exec(command) {
Expand Down

0 comments on commit 0d5a3e3

Please sign in to comment.