-
Notifications
You must be signed in to change notification settings - Fork 496
/
VerifyMigrationTask.kt
201 lines (174 loc) · 7.27 KB
/
VerifyMigrationTask.kt
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package app.cash.sqldelight.gradle
import app.cash.sqldelight.VERSION
import app.cash.sqldelight.core.SqlDelightCompilationUnit
import app.cash.sqldelight.core.SqlDelightDatabaseProperties
import app.cash.sqldelight.core.SqlDelightEnvironment
import app.cash.sqldelight.core.lang.SqlDelightQueriesFile
import app.cash.sqldelight.core.lang.util.forInitializationStatements
import app.cash.sqldelight.core.lang.util.rawSqlText
import app.cash.sqldelight.dialect.api.SqlDelightDialect
import app.cash.sqlite.migrations.CatalogDatabase
import app.cash.sqlite.migrations.ObjectDifferDatabaseComparator
import app.cash.sqlite.migrations.findDatabaseFiles
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.FileTree
import org.gradle.api.logging.Logging
import org.gradle.api.provider.Property
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.IgnoreEmptyDirectories
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.SkipWhenEmpty
import org.gradle.api.tasks.TaskAction
import org.gradle.workers.WorkAction
import org.gradle.workers.WorkParameters
import java.io.File
import java.util.ServiceLoader
@CacheableTask
abstract class VerifyMigrationTask : SqlDelightWorkerTask() {
@Suppress("unused")
// Required to invalidate the task on version updates.
@Input
val pluginVersion = VERSION
@get:Input abstract val projectName: Property<String>
/** Directory where the database files are copied for the migration scripts to run against. */
@get:Internal abstract var workingDirectory: File
@get:Nested abstract var properties: SqlDelightDatabasePropertiesImpl
@get:Nested abstract var compilationUnit: SqlDelightCompilationUnitImpl
@Input var verifyMigrations: Boolean = false
@Input var verifyDefinitions: Boolean = true
/* Tasks without an output are never considered UP-TO-DATE by Gradle. Adding an output file that's created when the
* task completes successfully works around the lack of an output for this task. There may be a better solution once
* https://github.com/gradle/gradle/issues/14223 is resolved. */
@OutputFile
internal fun getDummyOutputFile(): File = File(temporaryDir, "success.txt")
@TaskAction
fun verifyMigrations() {
runCatching {
val workQueue = workQueue()
workQueue.submit(VerifyMigrationAction::class.java) {
it.workingDirectory.set(workingDirectory)
it.projectName.set(projectName)
it.properties.set(properties)
it.verifyMigrations.set(verifyMigrations)
it.compilationUnit.set(compilationUnit)
it.verifyDefinitions.set(verifyDefinitions)
}
workQueue.await()
}.onSuccess {
getDummyOutputFile().createNewFile()
}.onFailure {
throw it
}
}
@InputFiles
@SkipWhenEmpty
@IgnoreEmptyDirectories
@PathSensitive(PathSensitivity.RELATIVE)
override fun getSource(): FileTree {
return super.getSource()
}
interface VerifyMigrationWorkParameters : WorkParameters {
val workingDirectory: DirectoryProperty
val projectName: Property<String>
val properties: Property<SqlDelightDatabaseProperties>
val compilationUnit: Property<SqlDelightCompilationUnit>
val verifyMigrations: Property<Boolean>
val verifyDefinitions: Property<Boolean>
}
abstract class VerifyMigrationAction : WorkAction<VerifyMigrationWorkParameters> {
private val logger = Logging.getLogger(VerifyMigrationTask::class.java)
private val sourceFolders: List<File>
get() = parameters.compilationUnit.get().sourceFolders.map { it.folder }
private val environment by lazy {
SqlDelightEnvironment(
sourceFolders = sourceFolders.filter { it.exists() },
dependencyFolders = emptyList(),
moduleName = parameters.projectName.get(),
properties = parameters.properties.get(),
verifyMigrations = parameters.verifyMigrations.get(),
compilationUnit = parameters.compilationUnit.get(),
dialect = ServiceLoader.load(SqlDelightDialect::class.java).findFirst().get(),
)
}
override fun execute() {
if (!environment.dialect.isSqlite) return
parameters.workingDirectory.get().asFile.deleteRecursively()
val catalog = createCurrentDb()
val databaseFiles = sourceFolders.asSequence()
.findDatabaseFiles()
check(!parameters.verifyMigrations.get() || databaseFiles.count() > 0) {
"Verifying a migration requires a database file to be present. To generate one, use the generate Gradle task."
}
databaseFiles.forEach { dbFile ->
checkMigration(dbFile, catalog)
}
checkForGaps()
}
private fun createCurrentDb(): CatalogDatabase {
val sourceFiles = ArrayList<SqlDelightQueriesFile>()
environment.forSourceFiles { file ->
if (file is SqlDelightQueriesFile) sourceFiles.add(file)
}
val initStatements = ArrayList<CatalogDatabase.InitStatement>()
sourceFiles.forInitializationStatements(
environment.dialect.allowsReferenceCycles,
) { sqlText ->
initStatements.add(CatalogDatabase.InitStatement(sqlText, "Error compiling $sqlText"))
}
return CatalogDatabase.withInitStatements(initStatements)
}
private fun checkMigration(dbFile: File, currentDb: CatalogDatabase) {
val actualCatalog = createActualDb(dbFile)
val databaseComparator = ObjectDifferDatabaseComparator(
ignoreDefinitions = !parameters.verifyDefinitions.get(),
circularReferenceExceptionLogger = {
logger.debug(it)
},
)
val diffReport = databaseComparator.compare(currentDb, actualCatalog).let { diff ->
buildString(diff::printTo)
}
check(diffReport.isEmpty()) {
"Error migrating from ${dbFile.name}, fresh database looks" +
" different from migration database:\n$diffReport"
}
}
private fun createActualDb(dbFile: File): CatalogDatabase {
val version = dbFile.nameWithoutExtension.toInt()
val copy = dbFile.copyTo(File(parameters.workingDirectory.get().asFile, dbFile.name))
val initStatements = ArrayList<CatalogDatabase.InitStatement>()
environment.forMigrationFiles { file ->
check(file.name.any { it in '0'..'9' }) {
"Migration files must have an integer value somewhere in their filename but ${file.name} does not."
}
if (version > file.version) return@forMigrationFiles
file.sqlStmtList!!.stmtList.forEach {
initStatements.add(
CatalogDatabase.InitStatement(
it.rawSqlText(),
"Error compiling ${file.name}",
),
)
}
}
return CatalogDatabase.fromFile(copy.absolutePath, initStatements).also { copy.delete() }
}
private fun checkForGaps() {
var lastMigrationVersion: Int? = null
environment.forMigrationFiles {
val actual = it.version
val expected = lastMigrationVersion?.plus(1) ?: actual
check(actual == expected) {
"Gap in migrations detected. Expected migration $expected, got $actual."
}
lastMigrationVersion = actual
}
}
}
}