Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
server
Manage
Activity
Members
Labels
Plan
Issues
25
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cokoapps
server
Commits
f7040501
Commit
f7040501
authored
9 months ago
by
Yannis Barlas
Browse files
Options
Downloads
Patches
Plain Diff
feat(server): record last successful migrate run
parent
6d906bc2
No related branches found
No related tags found
1 merge request
!123
v4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/dbManager/migrate.js
+39
-12
39 additions, 12 deletions
src/dbManager/migrate.js
with
39 additions
and
12 deletions
src/dbManager/migrate.js
+
39
−
12
View file @
f7040501
...
...
@@ -9,8 +9,6 @@ const isFunction = require('lodash/isFunction')
const
logger
=
require
(
'
../logger
'
)
const
db
=
require
(
'
./db
'
)
const
v4BaseMessage
=
'
Starting with coko server v4,
'
const
resolveRelative
=
m
=>
require
.
resolve
(
m
,
{
paths
:
[
process
.
cwd
()]
})
const
tryRequireRelative
=
componentPath
=>
{
...
...
@@ -98,6 +96,7 @@ const getTimestampFromName = migrationName => {
}
const
isMigrationAfterThreshold
=
(
migrationName
,
threshold
)
=>
{
if
(
!
threshold
)
return
false
// table hasn't been created yet, so no restrictions yet
const
migrationUnixTimestamp
=
getTimestampFromName
(
migrationName
)
return
migrationUnixTimestamp
>
threshold
}
...
...
@@ -122,17 +121,26 @@ const customResolver = (params, threshold) => {
const
isSql
=
extname
(
filePath
)
===
'
.sql
'
const
isPastThreshold
=
isMigrationAfterThreshold
(
name
,
threshold
)
if
(
isPastThreshold
)
{
if
(
isSql
)
{
// TO DO -- migration error?
throw
new
Error
(
`
${
v4BaseMessage
}
migration files must be js files. Use knex.raw if you need to write sql code.`
,
class
MigrationResolverRulesError
extends
Error
{
constructor
(
message
)
{
super
(
`Starting with coko server v4:
${
message
}
. This error occured in
${
name
}
.`
,
)
this
.
name
=
'
MigrationResolverRulesError
'
}
}
if
(
!
doesMigrationFilenameStartWithUnixTimestamp
(
name
))
{
throw
new
Error
(
`
${
v4BaseMessage
}
migration files must start with a unix timestamp larger than 1000000000, followed by a dash (-).`
,
if
(
!
doesMigrationFilenameStartWithUnixTimestamp
(
name
))
{
throw
new
MigrationResolverRulesError
(
`Migration files must start with a unix timestamp larger than 1000000000, followed by a dash (-)`
,
)
}
if
(
isPastThreshold
)
{
if
(
isSql
)
{
// TO DO -- migration error?
throw
new
MigrationResolverRulesError
(
`Migration files must be js files. Use knex.raw if you need to write sql code`
,
)
}
}
...
...
@@ -152,8 +160,8 @@ const customResolver = (params, threshold) => {
if
(
isPastThreshold
)
{
if
(
!
migration
.
down
||
!
isFunction
(
migration
.
down
))
{
throw
new
Error
(
`
${
v4BaseMessage
}
a
ll migrations need to define a down function so that the migration can be rolled back`
,
throw
new
MigrationResolverRules
Error
(
`
A
ll migrations need to define a down function so that the migration can be rolled back`
,
)
}
}
...
...
@@ -205,6 +213,21 @@ const getMetaCreated = async () => {
return
createdDateAsUnixTimestamp
}
const
updateLastSuccessfulMigrateCheckpoint
=
async
()
=>
{
logger
.
info
(
'
Migrate: Updating last successful migration checkpoint
'
)
const
lastMigrationRow
=
await
db
(
'
migrations
'
)
.
select
(
'
id
'
)
.
orderBy
(
'
runAt
'
,
'
desc
'
)
.
first
()
await
db
(
'
coko_server_meta
'
).
update
({
lastSuccessfulMigrateCheckpoint
:
lastMigrationRow
.
id
,
})
logger
.
info
(
'
Migrate: Last successful migration checkpoint updated
'
)
}
/**
* After installing v4, some rules will apply for migrations, but only for new
* migrations, so that developers don't have to rewrite all existing migrations.
...
...
@@ -216,7 +239,11 @@ const getMetaCreated = async () => {
const
migrate
=
async
options
=>
{
const
threshold
=
await
getMetaCreated
()
const
umzug
=
getUmzug
(
threshold
)
await
umzug
.
up
(
options
)
logger
.
info
(
'
Migrate: All migrations ran successfully!
'
)
await
updateLastSuccessfulMigrateCheckpoint
()
}
module
.
exports
=
migrate
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment