|
1 | 1 | package org.jetbrains.exposed.sql
|
2 | 2 |
|
3 |
| -import org.flywaydb.core.Flyway |
4 |
| -import org.flywaydb.core.api.FlywayException |
5 |
| -import org.flywaydb.core.api.output.MigrateResult |
6 | 3 | import org.jetbrains.annotations.TestOnly
|
7 |
| -import org.jetbrains.exposed.exceptions.ExposedMigrationException |
8 | 4 | import org.jetbrains.exposed.sql.statements.api.ExposedConnection
|
9 | 5 | import org.jetbrains.exposed.sql.statements.api.ExposedDatabaseMetadata
|
10 | 6 | import org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManager
|
11 | 7 | import org.jetbrains.exposed.sql.transactions.TransactionManager
|
12 | 8 | import org.jetbrains.exposed.sql.vendors.*
|
13 |
| -import java.io.File |
14 | 9 | import java.math.BigDecimal
|
15 | 10 | import java.sql.Connection
|
16 | 11 | import java.sql.DriverManager
|
@@ -109,160 +104,6 @@ class Database private constructor(
|
109 | 104 | */
|
110 | 105 | internal var dataSourceReadOnly: Boolean = false
|
111 | 106 |
|
112 |
| - /** |
113 |
| - * @param tables The tables to which the migration will be applied. |
114 |
| - * @param user The user of the database. |
115 |
| - * @param password The password of the database. |
116 |
| - * @param oldVersion The version to migrate from. Pending migrations up to [oldVersion] are applied before applying the migration to [newVersion]. |
117 |
| - * @param newVersion The version to migrate to. |
118 |
| - * @param migrationTitle The title of the migration. |
119 |
| - * @param migrationScriptDirectory The directory in which to create the migration script. |
120 |
| - * @param withLogs By default, a description for each intermediate step, as well as its execution time, is logged at |
121 |
| - * the INFO level. This can be disabled by setting [withLogs] to `false`. |
122 |
| - * |
123 |
| - * @throws ExposedMigrationException if the migration fails. |
124 |
| - * |
125 |
| - * Applies a database migration from [oldVersion] to [newVersion]. |
126 |
| - * |
127 |
| - * If a migration script with the same name already exists, the existing one will be used as is and a new one will |
128 |
| - * not be generated. This allows you to generate a migration script before the migration and modify it manually if |
129 |
| - * needed. |
130 |
| - */ |
131 |
| - @ExperimentalDatabaseMigrationApi |
132 |
| - @Suppress("LongParameterList", "TooGenericExceptionCaught") |
133 |
| - fun migrate( |
134 |
| - vararg tables: Table, |
135 |
| - user: String, |
136 |
| - password: String, |
137 |
| - oldVersion: String, |
138 |
| - newVersion: String, |
139 |
| - migrationTitle: String, |
140 |
| - migrationScriptDirectory: String, |
141 |
| - withLogs: Boolean = true |
142 |
| - ) { |
143 |
| - val flyway = Flyway |
144 |
| - .configure() |
145 |
| - .baselineOnMigrate(true) |
146 |
| - .baselineVersion(oldVersion) |
147 |
| - .dataSource(url, user, password) |
148 |
| - .locations("filesystem:$migrationScriptDirectory") |
149 |
| - .load() |
150 |
| - |
151 |
| - with(TransactionManager.current()) { |
152 |
| - db.dialect.resetCaches() |
153 |
| - |
154 |
| - try { |
155 |
| - val migrationScript = File("$migrationScriptDirectory/$migrationTitle.sql") |
156 |
| - if (!migrationScript.exists()) { |
157 |
| - SchemaUtils.generateMigrationScript( |
158 |
| - tables = *tables, |
159 |
| - newVersion = newVersion, |
160 |
| - title = migrationTitle, |
161 |
| - scriptDirectory = migrationScriptDirectory, |
162 |
| - withLogs = withLogs |
163 |
| - ) |
164 |
| - } |
165 |
| - } catch (exception: Exception) { |
166 |
| - throw ExposedMigrationException( |
167 |
| - exception = exception, |
168 |
| - message = "Failed to generate migration script for migration from $oldVersion to $newVersion: ${exception.message.orEmpty()}" |
169 |
| - ) |
170 |
| - } |
171 |
| - |
172 |
| - try { |
173 |
| - SchemaUtils.logTimeSpent("Migrating database from $oldVersion to $newVersion", withLogs = true) { |
174 |
| - val migrateResult: MigrateResult = flyway.migrate() |
175 |
| - if (withLogs) { |
176 |
| - exposedLogger.info("Migration of database ${if (migrateResult.success) "succeeded" else "failed"}.") |
177 |
| - } |
178 |
| - } |
179 |
| - } catch (exception: FlywayException) { |
180 |
| - flyway.repair() |
181 |
| - throw ExposedMigrationException( |
182 |
| - exception = exception, |
183 |
| - message = "Migration failed from version $oldVersion to $newVersion: ${exception.message.orEmpty()}" |
184 |
| - ) |
185 |
| - } |
186 |
| - |
187 |
| - db.dialect.resetCaches() |
188 |
| - } |
189 |
| - } |
190 |
| - |
191 |
| - /** |
192 |
| - * @param tables The tables to which the migration will be applied. |
193 |
| - * @param dataSource The [DataSource] object to be used as a means of getting a connection. |
194 |
| - * @param oldVersion The version to migrate from. Pending migrations up to [oldVersion] are applied before applying the migration to [newVersion]. |
195 |
| - * @param newVersion The version to migrate to. |
196 |
| - * @param migrationTitle The title of the migration. |
197 |
| - * @param migrationScriptDirectory The directory in which to create the migration script. |
198 |
| - * @param withLogs By default, a description for each intermediate step, as well as its execution time, is logged at |
199 |
| - * the INFO level. This can be disabled by setting [withLogs] to `false`. |
200 |
| - * |
201 |
| - * @throws ExposedMigrationException if the migration fails. |
202 |
| - * |
203 |
| - * Applies a database migration from [oldVersion] to [newVersion]. |
204 |
| - * For PostgreSQLNG, "jdbc:pgsql" in the database URL is replaced with "jdbc:postgresql" because the former is not |
205 |
| - * supported by Flyway. |
206 |
| - */ |
207 |
| - @ExperimentalDatabaseMigrationApi |
208 |
| - @Suppress("LongParameterList", "TooGenericExceptionCaught") |
209 |
| - fun migrate( |
210 |
| - vararg tables: Table, |
211 |
| - dataSource: DataSource, |
212 |
| - oldVersion: String, |
213 |
| - newVersion: String, |
214 |
| - migrationTitle: String, |
215 |
| - migrationScriptDirectory: String, |
216 |
| - withLogs: Boolean = true |
217 |
| - ) { |
218 |
| - val flyway = Flyway |
219 |
| - .configure() |
220 |
| - .baselineOnMigrate(true) |
221 |
| - .baselineVersion(oldVersion) |
222 |
| - .dataSource(dataSource) |
223 |
| - .locations("filesystem:$migrationScriptDirectory") |
224 |
| - .load() |
225 |
| - |
226 |
| - with(TransactionManager.current()) { |
227 |
| - db.dialect.resetCaches() |
228 |
| - |
229 |
| - try { |
230 |
| - val migrationScript = File("$migrationScriptDirectory/$migrationTitle.sql") |
231 |
| - if (!migrationScript.exists()) { |
232 |
| - SchemaUtils.generateMigrationScript( |
233 |
| - tables = *tables, |
234 |
| - newVersion = newVersion, |
235 |
| - title = migrationTitle, |
236 |
| - scriptDirectory = migrationScriptDirectory, |
237 |
| - withLogs = withLogs |
238 |
| - ) |
239 |
| - } |
240 |
| - } catch (exception: Exception) { |
241 |
| - throw ExposedMigrationException( |
242 |
| - exception = exception, |
243 |
| - message = "Failed to generate migration script for migration from $oldVersion to $newVersion: ${exception.message.orEmpty()}" |
244 |
| - ) |
245 |
| - } |
246 |
| - |
247 |
| - try { |
248 |
| - SchemaUtils.logTimeSpent("Migrating database from $oldVersion to $newVersion", withLogs = true) { |
249 |
| - val migrateResult: MigrateResult = flyway.migrate() |
250 |
| - if (withLogs) { |
251 |
| - exposedLogger.info("Migration of database ${if (migrateResult.success) "succeeded" else "failed"}.") |
252 |
| - } |
253 |
| - } |
254 |
| - } catch (exception: FlywayException) { |
255 |
| - flyway.repair() |
256 |
| - throw ExposedMigrationException( |
257 |
| - exception = exception, |
258 |
| - message = "Migration failed from version $oldVersion to $newVersion: ${exception.message.orEmpty()}" |
259 |
| - ) |
260 |
| - } |
261 |
| - |
262 |
| - db.dialect.resetCaches() |
263 |
| - } |
264 |
| - } |
265 |
| - |
266 | 107 | companion object {
|
267 | 108 | internal val dialects = ConcurrentHashMap<String, () -> DatabaseDialect>()
|
268 | 109 |
|
|
0 commit comments