Skip to content

Commit 7dc00ca

Browse files
author
bmpotter
authored
Merge pull request #65 from bmpotter/fix-upgrade-problem
Fix upgrade problem
2 parents 09d216b + 4b37b4d commit 7dc00ca

File tree

4 files changed

+78
-35
lines changed

4 files changed

+78
-35
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ services in the exchange.
8888
- Allow random PW creation for user creation
8989
- Consider changing all creates to POST, and update (via put/patch) return codes to 200
9090

91+
## Changes in 1.51.0
92+
93+
- Fixed error upgrading db schema from earlier than db schema 3
94+
9195
## Changes in 1.50.0
9296

9397
- Fix build error in Schema.scala in wiotp enviroment

src/main/resources/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.50.0
1+
1.51.0

src/main/scala/com/horizon/exchangeapi/AdminRoutes.scala

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ trait AdminRoutes extends ScalatraBase with FutureSupport with SwaggerSupport wi
167167
resp.setStatus(HttpCode.POST_OK)
168168
ApiResponse(ApiResponseType.OK, "db deleted successfully")
169169
case Failure(t) => resp.setStatus(HttpCode.INTERNAL_ERROR)
170-
ApiResponse(ApiResponseType.INTERNAL_ERROR, "db not completely deleted: "+t.toString)
170+
ApiResponse(ApiResponseType.INTERNAL_ERROR, "db not deleted: "+t.toString)
171171
}
172172
})
173173
})
@@ -220,25 +220,32 @@ trait AdminRoutes extends ScalatraBase with FutureSupport with SwaggerSupport wi
220220
post("/admin/upgradedb", operation(postAdminUpgradeDb)) ({
221221
credsAndLog().authenticate().authorizeTo(TAction(),Access.ADMIN)
222222
val resp = response
223+
val upgradeNotNeededMsg = "DB schema does not need upgrading, it is already at the latest schema version: "
223224

224225
// Assemble the list of db actions to alter schema of existing tables and create tables that are new in each of the schema versions we have to catch up on
225226
db.run(SchemaTQ.getSchemaRow.result.asTry.flatMap({ xs =>
226227
logger.debug("POST /admin/upgradedb current schema result: "+xs.toString)
227228
xs match {
228229
case Success(v) => if (v.nonEmpty) {
229-
val schemaRow = v.head
230-
SchemaTQ.getUpgradeActionsFrom(schemaRow.schemaVersion).transactionally.asTry
231-
}
232-
else DBIO.failed(new Throwable("DB upgrade error: did not find a row in the schemas table")).asTry
230+
val schemaRow = v.head
231+
if (SchemaTQ.isLatestSchemaVersion(schemaRow.schemaVersion)) DBIO.failed(new Throwable(upgradeNotNeededMsg + schemaRow.schemaVersion)).asTry // I do not think there is a way to pass a msg thru the Success path
232+
else SchemaTQ.getUpgradeActionsFrom(schemaRow.schemaVersion).transactionally.asTry
233+
}
234+
else DBIO.failed(new Throwable("DB upgrade error: did not find a row in the schemas table")).asTry
233235
case Failure(t) => DBIO.failed(t).asTry // rethrow the error to the next step
234236
}
235237
})).map({ xs =>
236238
logger.debug("POST /admin/upgradedb result: "+xs.toString)
237239
xs match {
238240
case Success(_) => resp.setStatus(HttpCode.POST_OK)
239-
ApiResponse(ApiResponseType.OK, "db table schemas upgraded successfully")
240-
case Failure(t) => resp.setStatus(HttpCode.INTERNAL_ERROR)
241-
ApiResponse(ApiResponseType.INTERNAL_ERROR, "db table schemas not upgraded: "+t.toString)
241+
ApiResponse(ApiResponseType.OK, "DB table schema upgraded to the latest successfully")
242+
case Failure(t) => if (t.getMessage.contains(upgradeNotNeededMsg)) {
243+
resp.setStatus(HttpCode.POST_OK)
244+
ApiResponse(ApiResponseType.OK, t.getMessage)
245+
} else {
246+
resp.setStatus(HttpCode.INTERNAL_ERROR)
247+
ApiResponse(ApiResponseType.INTERNAL_ERROR, "DB table schema not upgraded: " + t.toString)
248+
}
242249
}
243250
})
244251
})

src/main/scala/com/horizon/exchangeapi/tables/Schema.scala

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,64 @@ class SchemaTable(tag: Tag) extends Table[SchemaRow](tag, "schema") {
3737

3838
// Instance to access the schemas table
3939
object SchemaTQ {
40-
def latestSchemaVersion = upgradeSchemaVector.size - 1
41-
4240
// Each index in this vector contains the db schema upgrade actions to get from (index-1) version to (index) version
43-
val upgradeSchemaVector = Vector(
44-
/* 0 */ DBIO.seq(), // v1.35.0 - no changes needed to get to time zero
45-
/* 1 */ DBIO.seq(NodeStatusTQ.rows.schema.create), // v1.37.0
46-
/* 2 */ DBIO.seq(sqlu"alter table agbots drop column patterns", AgbotPatternsTQ.rows.schema.create), // v1.38.0
47-
/* 3 */ DBIO.seq(ServicesTQ.rows.schema.create), // v1.45.0
48-
/* 4 */ DBIO.seq(WorkloadKeysTQ.rows.schema.create, MicroserviceKeysTQ.rows.schema.create, ServiceKeysTQ.rows.schema.create, PatternKeysTQ.rows.schema.create), // v1.46.0
49-
/* 5 */ DBIO.seq(sqlu"alter table patterns add column services character varying not null default ''"), // v1.47.0
50-
/* 6 */ DBIO.seq(sqlu"alter table nodes add column regservices character varying not null default ''"), // v1.47.0
51-
/* 7 */ DBIO.seq(
52-
sqlu"alter table nodeagreements add column services character varying not null default ''",
53-
sqlu"alter table nodeagreements add column agrsvcorgid character varying not null default ''",
54-
sqlu"alter table nodeagreements add column agrsvcpattern character varying not null default ''",
55-
sqlu"alter table nodeagreements add column agrsvcurl character varying not null default ''"
56-
), // v1.47.0
57-
/* 8 */ DBIO.seq(
58-
sqlu"alter table agbotagreements add column serviceOrgid character varying not null default ''",
59-
sqlu"alter table agbotagreements add column servicePattern character varying not null default ''",
60-
sqlu"alter table agbotagreements add column serviceUrl character varying not null default ''",
61-
sqlu"alter table nodestatus add column services character varying not null default ''",
62-
sqlu"alter table services rename column pkg to imagestore"
63-
) // v1.48.0
64-
)
41+
// val upgradeSchemaVector = Vector(
42+
// /* 0 */ DBIO.seq(), // v1.35.0 - no changes needed to get to time zero
43+
// /* 1 */ DBIO.seq(NodeStatusTQ.rows.schema.create), // v1.37.0
44+
// /* 2 */ DBIO.seq(sqlu"alter table agbots drop column patterns", AgbotPatternsTQ.rows.schema.create), // v1.38.0
45+
// /* 3 */ DBIO.seq(ServicesTQ.rows.schema.create), // v1.45.0
46+
// /* 4 */ DBIO.seq(WorkloadKeysTQ.rows.schema.create, MicroserviceKeysTQ.rows.schema.create, ServiceKeysTQ.rows.schema.create, PatternKeysTQ.rows.schema.create), // v1.46.0
47+
// /* 5 */ DBIO.seq(sqlu"alter table patterns add column services character varying not null default ''"), // v1.47.0
48+
// /* 6 */ DBIO.seq(sqlu"alter table nodes add column regservices character varying not null default ''"), // v1.47.0
49+
// /* 7 */ DBIO.seq(
50+
// sqlu"alter table nodeagreements add column services character varying not null default ''",
51+
// sqlu"alter table nodeagreements add column agrsvcorgid character varying not null default ''",
52+
// sqlu"alter table nodeagreements add column agrsvcpattern character varying not null default ''",
53+
// sqlu"alter table nodeagreements add column agrsvcurl character varying not null default ''"
54+
// ), // v1.47.0
55+
// /* 8 */ DBIO.seq(
56+
// sqlu"alter table agbotagreements add column serviceOrgid character varying not null default ''",
57+
// sqlu"alter table agbotagreements add column servicePattern character varying not null default ''",
58+
// sqlu"alter table agbotagreements add column serviceUrl character varying not null default ''",
59+
// sqlu"alter table nodestatus add column services character varying not null default ''",
60+
// sqlu"alter table services rename column pkg to imagestore"
61+
// ) // v1.48.0
62+
// )
63+
64+
// Returns the db actions necessary to get the schema from step-1 to step. The fromSchemaVersion arg is there because sometimes where you
65+
// originally came from affects how to get to the next step.
66+
def getUpgradeSchemaStep(fromSchemaVersion: Int, step: Int)(implicit logger: Logger): DBIO[_] = {
67+
step match {
68+
case 0 => DBIO.seq() // v1.35.0 - no changes needed to get to time zero
69+
case 1 => DBIO.seq(NodeStatusTQ.rows.schema.create) // v1.37.0
70+
case 2 => DBIO.seq(sqlu"alter table agbots drop column patterns", AgbotPatternsTQ.rows.schema.create) // v1.38.0
71+
case 3 => DBIO.seq(ServicesTQ.rows.schema.create) // v1.45.0
72+
case 4 => DBIO.seq(WorkloadKeysTQ.rows.schema.create, MicroserviceKeysTQ.rows.schema.create, ServiceKeysTQ.rows.schema.create, PatternKeysTQ.rows.schema.create) // v1.46.0
73+
case 5 => DBIO.seq(sqlu"alter table patterns add column services character varying not null default ''") // v1.47.0
74+
case 6 => DBIO.seq(sqlu"alter table nodes add column regservices character varying not null default ''") // v1.47.0
75+
case 7 => DBIO.seq( // v1.47.0
76+
sqlu"alter table nodeagreements add column services character varying not null default ''",
77+
sqlu"alter table nodeagreements add column agrsvcorgid character varying not null default ''",
78+
sqlu"alter table nodeagreements add column agrsvcpattern character varying not null default ''",
79+
sqlu"alter table nodeagreements add column agrsvcurl character varying not null default ''"
80+
)
81+
case 8 => val actions = ListBuffer[DBIO[_]]() // v1.48.0
82+
actions += sqlu"alter table agbotagreements add column serviceOrgid character varying not null default ''"
83+
actions += sqlu"alter table agbotagreements add column servicePattern character varying not null default ''"
84+
actions += sqlu"alter table agbotagreements add column serviceUrl character varying not null default ''"
85+
actions += sqlu"alter table nodestatus add column services character varying not null default ''"
86+
// If in this current level of code we started upgrading from 2 or less, that means we created the services table with the correct schema, so no need to modify it
87+
if (fromSchemaVersion >= 3) actions += sqlu"alter table services rename column pkg to imagestore"
88+
DBIO.seq(actions: _*) // convert the list of actions to a DBIO seq
89+
case other => logger.error("getUpgradeSchemaStep was given invalid step "+other); DBIO.seq() // should never get here
90+
}
91+
}
92+
val latestSchemaVersion = 8 // NOTE: THIS MUST BE CHANGED WHEN YOU ADD TO getUpgradeSchemaStep()
6593
val latestSchemaDescription = "Added columns agbotagreements and nodestatus tables, and changed column pkg to imagestore in services table"
6694

95+
96+
def isLatestSchemaVersion(fromSchemaVersion: Int) = fromSchemaVersion >= latestSchemaVersion
97+
6798
val rows = TableQuery[SchemaTable]
6899

69100
def getSchemaRow = rows.filter(_.id === 0)
@@ -72,14 +103,15 @@ object SchemaTQ {
72103
// Returns a sequence of DBIOActions that will upgrade the DB schema from fromSchemaVersion to the latest schema version.
73104
// It is assumed that this sequence of DBIOActions will be run transactionally.
74105
def getUpgradeActionsFrom(fromSchemaVersion: Int)(implicit logger: Logger): DBIO[_] = {
75-
if (fromSchemaVersion >= latestSchemaVersion) { // nothing to do
106+
if (isLatestSchemaVersion(fromSchemaVersion)) { // nothing to do
76107
logger.debug("Already at latest DB schema version - nothing to do")
77108
return DBIO.seq()
78109
}
79110
val actions = ListBuffer[DBIO[_]]()
80111
for (i <- (fromSchemaVersion+1) to latestSchemaVersion) {
81112
logger.debug("Adding DB schema upgrade actions to get from schema version "+(i-1)+" to "+i)
82-
actions += upgradeSchemaVector(i)
113+
//actions += upgradeSchemaVector(i)
114+
actions += getUpgradeSchemaStep(fromSchemaVersion, i)
83115
}
84116
actions += getSetVersionAction // record that we are at the latest schema version now
85117
return DBIO.seq(actions: _*) // convert the list of actions to a DBIO seq

0 commit comments

Comments
 (0)