1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2020 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.migrations.db
21 
22 import java.sql.{Connection, ResultSet}
23 import scala.collection.mutable.ArrayBuffer
24 import org.flywaydb.core.api.migration.{BaseJavaMigration, Context}
25 
26 abstract class Migration extends BaseJavaMigration {
27 
28   override def migrate(context: Context): Unit = {
29     val connection = context.getConnection
30     val autoCommit = connection.getAutoCommit
31     connection.setAutoCommit(false)
32     migrate(connection)
33     connection.commit()
34     connection.setAutoCommit(autoCommit)
35   }
36 
37   def migrate(connection: Connection): Unit
38 }
39 
40 object SqlTools {
41 
42   implicit class ImprovedResultSet(set: ResultSet) {
43 
44     @SuppressWarnings(Array("org.wartremover.warts.While", "org.wartremover.warts.MutableDataStructures"))
45     def toListOfRows(columns: List[String]): ArrayBuffer[List[String]] = {
46       val rows = ArrayBuffer[List[String]]()
47       while (set.next()) rows.append(columns.map(set.getString))
48       rows
49     }
50 
51     @SuppressWarnings(Array("org.wartremover.warts.While", "org.wartremover.warts.MutableDataStructures"))
52     def toMaps(columns: String*): ArrayBuffer[Map[String, String]] = {
53       val rows = ArrayBuffer[Map[String, String]]()
54       while (set.next()) rows.append(columns.map(k => (k -> set.getString(k))).toMap)
55       rows
56     }
57   }
58 }
Line Stmt Id Pos Tree Symbol Tests Code
29 19890 1060 - 1081 Apply org.flywaydb.core.api.migration.Context.getConnection org.make.api.databasetest context.getConnection()
30 21993 1103 - 1127 Apply java.sql.Connection.getAutoCommit org.make.api.databasetest connection.getAutoCommit()
31 20980 1132 - 1163 Apply java.sql.Connection.setAutoCommit org.make.api.databasetest connection.setAutoCommit(false)
32 19967 1168 - 1187 Apply org.make.api.migrations.db.Migration.migrate org.make.api.databasetest Migration.this.migrate(connection)
33 21592 1192 - 1211 Apply java.sql.Connection.commit org.make.api.databasetest connection.commit()
34 20606 1216 - 1252 Apply java.sql.Connection.setAutoCommit org.make.api.databasetest connection.setAutoCommit(autoCommit)
46 20139 1577 - 1604 Apply scala.collection.IterableFactory.apply org.make.api.databasetest scala.collection.mutable.ArrayBuffer.apply[List[String]]()
47 21759 1618 - 1628 Apply java.sql.ResultSet.next org.make.api.databasetest ImprovedResultSet.this.set.next()
47 21998 1630 - 1669 Apply scala.collection.mutable.Buffer.append rows.append(columns.map[String](((x$1: String) => ImprovedResultSet.this.set.getString(x$1))))
47 20778 1654 - 1667 Apply java.sql.ResultSet.getString ImprovedResultSet.this.set.getString(x$1)
47 21597 1611 - 1611 Literal <nosymbol> org.make.api.databasetest ()
47 20930 1641 - 1641 Apply org.make.api.migrations.db.SqlTools.ImprovedResultSet.while$1 while$1()
47 19952 1630 - 1669 Block <nosymbol> { rows.append(columns.map[String](((x$1: String) => ImprovedResultSet.this.set.getString(x$1)))); while$1() }
47 20723 1611 - 1611 Block <nosymbol> org.make.api.databasetest ()
47 19852 1642 - 1668 Apply scala.collection.immutable.List.map columns.map[String](((x$1: String) => ImprovedResultSet.this.set.getString(x$1)))
53 20254 1883 - 1917 Apply scala.collection.IterableFactory.apply org.make.api.databasetest scala.collection.mutable.ArrayBuffer.apply[Map[String,String]]()
54 19932 1973 - 1994 Apply scala.Predef.ArrowAssoc.-> scala.Predef.ArrowAssoc[String](k).->[String](ImprovedResultSet.this.set.getString(k))
54 21845 1997 - 1997 TypeApply scala.<:<.refl scala.this.<:<.refl[(String, String)]
54 21052 1955 - 2002 ApplyToImplicitArgs scala.collection.IterableOnceOps.toMap columns.map[(String, String)](((k: String) => scala.Predef.ArrowAssoc[String](k).->[String](ImprovedResultSet.this.set.getString(k)))).toMap[String, String](scala.this.<:<.refl[(String, String)])
54 21560 1954 - 1954 Apply org.make.api.migrations.db.SqlTools.ImprovedResultSet.while$2 while$2()
54 20053 1943 - 2003 Apply scala.collection.mutable.Buffer.append rows.append(columns.map[(String, String)](((k: String) => scala.Predef.ArrowAssoc[String](k).->[String](ImprovedResultSet.this.set.getString(k)))).toMap[String, String](scala.this.<:<.refl[(String, String)]))
54 20752 1978 - 1994 Apply java.sql.ResultSet.getString ImprovedResultSet.this.set.getString(k)
54 21793 1931 - 1941 Apply java.sql.ResultSet.next org.make.api.databasetest ImprovedResultSet.this.set.next()
54 21751 1924 - 1924 Block <nosymbol> org.make.api.databasetest ()
54 20100 1924 - 1924 Literal <nosymbol> org.make.api.databasetest ()
54 20731 1943 - 2003 Block <nosymbol> { rows.append(columns.map[(String, String)](((k: String) => scala.Predef.ArrowAssoc[String](k).->[String](ImprovedResultSet.this.set.getString(k)))).toMap[String, String](scala.this.<:<.refl[(String, String)])); while$2() }