-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
timeseries: add a class to persist start dates in mysql (#253)
Also: - Secure the db lock before doing schema migration - Move the lock table creation out of schema migration
- Loading branch information
Adrien Surée
authored
Apr 3, 2018
1 parent
0bb80a5
commit 6bcee28
Showing
3 changed files
with
47 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
timeseries/src/main/scala/com/criteo/cuttle/timeseries/contrib/PersistStartDate.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.criteo.cuttle.timeseries.contrib | ||
|
||
import doobie.implicits._ | ||
import com.criteo.cuttle._ | ||
import java.time._ | ||
|
||
class PersistInstant(xa: XA) { | ||
def set(id: String, t: Instant): Instant = { | ||
sql"REPLACE INTO instant_data VALUES (${id}, ${t})" | ||
.update.run.transact(xa).unsafeRunSync | ||
t | ||
} | ||
|
||
def get(id: String): Option[Instant] = | ||
sql"SELECT instant FROM instant_data WHERE id = ${id}" | ||
.query[Instant].option.transact(xa).unsafeRunSync | ||
} | ||
|
||
object PersistInstant { | ||
private val schemaUpgrades = List( | ||
sql""" | ||
CREATE TABLE instant_data ( | ||
id VARCHAR(1000) NOT NULL, | ||
instant DATETIME NOT NULL, | ||
PRIMARY KEY (id) | ||
) ENGINE = INNODB | ||
""".update.run | ||
) | ||
|
||
def apply(xa: XA): PersistInstant = { | ||
utils.updateSchema("instant", schemaUpgrades).transact(xa).unsafeRunSync | ||
new PersistInstant(xa) | ||
} | ||
} |