-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathAppDatabase.kt
More file actions
106 lines (98 loc) · 3.63 KB
/
Copy pathAppDatabase.kt
File metadata and controls
106 lines (98 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright (c) 2024-2026. The LibreFit Contributors
*
* LibreFit is subject to additional terms covering author attribution and trademark usage;
* see the ADDITIONAL_TERMS.md and TRADEMARK_POLICY.md files in the project root.
*/
package org.librefit.db
import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import org.librefit.db.converters.ExerciseDCConverter
import org.librefit.db.converters.LocalDateTimeConverter
import org.librefit.db.dao.DatasetDao
import org.librefit.db.dao.MeasurementDao
import org.librefit.db.dao.WorkoutDao
import org.librefit.db.entity.Exercise
import org.librefit.db.entity.ExerciseDC
import org.librefit.db.entity.Measurement
import org.librefit.db.entity.Set
import org.librefit.db.entity.Workout
@Database(
entities = [Workout::class, Exercise::class, Set::class, Measurement::class, ExerciseDC::class],
version = 4,
exportSchema = true,
autoMigrations = [
AutoMigration(from = 1, to = 2)
]
)
@TypeConverters(LocalDateTimeConverter::class, ExerciseDCConverter::class)
abstract class AppDatabase : RoomDatabase() {
companion object {
const val NAME = "librefit_database"
val MIGRATION_3_4 = object : Migration(3, 4) {
override fun migrate(db: SupportSQLiteDatabase) {
// Add HIIT countdown fields to exercises table
db.execSQL(
"""
ALTER TABLE exercises
ADD COLUMN targetDuration INTEGER NOT NULL DEFAULT 0
""".trimIndent()
)
db.execSQL(
"""
ALTER TABLE exercises
ADD COLUMN autoAdvanceSets INTEGER NOT NULL DEFAULT 0
""".trimIndent()
)
// Add cautions field to dataset (ExerciseDC) table
db.execSQL(
"""
ALTER TABLE dataset
ADD COLUMN cautions TEXT NOT NULL DEFAULT ''
""".trimIndent()
)
}
}
val MIGRATION_2_3 = object : Migration(2, 3) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL(
"""
ALTER TABLE exercises
ADD COLUMN position INTEGER NOT NULL DEFAULT 0
""".trimIndent()
)
db.execSQL(
"""
UPDATE exercises AS current
SET position = (
SELECT COUNT(*) - 1
FROM exercises AS previous
WHERE previous.workoutId = current.workoutId
AND previous.id <= current.id
)
""".trimIndent()
)
db.execSQL(
"""
CREATE INDEX IF NOT EXISTS index_exercises_workoutId_position
ON exercises(workoutId, position)
""".trimIndent()
)
db.execSQL(
"""
CREATE INDEX IF NOT EXISTS index_exercises_idExerciseDC
ON exercises(idExerciseDC)
""".trimIndent()
)
}
}
}
abstract fun getWorkoutDao(): WorkoutDao
abstract fun getMeasurementDao(): MeasurementDao
abstract fun getDatasetDao(): DatasetDao
}