Skip to content

Commit

Permalink
Merge pull request #9 from RBusarow/preserve_indices_when_recreating_…
Browse files Browse the repository at this point in the history
…table

create indices after the table rename
  • Loading branch information
RBusarow authored Aug 6, 2020
2 parents d1b94eb + 2fe0f48 commit 5064b5e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class Migration(
val table1 = tableDiff.old
val table2 = tableDiff.new

var tableCreated = false

if (!tableDiff.primaryKeyChanged && tableDiff.fieldsDiff.onlyAdded) {
val sb = StringBuilder()
for (field in tableDiff.fieldsDiff.added) {
Expand All @@ -85,7 +87,11 @@ class Migration(
sb.setLength(sb.length - 1)
execSql(sb.toString())
}

} else if (tableDiff.fieldsDiff.wasChanged || tableDiff.nameChanged) {

tableCreated = true

val tableMerge = Table(table2, table2.name + mergeTableSuffix)
createTable(tableMerge)

Expand Down Expand Up @@ -132,15 +138,21 @@ class Migration(
tableDiff.indicesDiff.added.forEach {
createTableIndex(table2, it)
}
} else {
}

tableDiff.indicesDiff.removed.forEach {
dropTableIndex(it)
}
tableDiff.indicesDiff.added.forEach {
tableDiff.indicesDiff.removed.forEach {
dropTableIndex(it)
}

if (tableCreated) {
tableDiff.indicesDiff.same.forEach {
createTableIndex(table2, it)
}
}

tableDiff.indicesDiff.added.forEach {
createTableIndex(table2, it)
}
}

for (table in diff.removedTables) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ class IndicesDiff(val old: Table?, val new: Table) {
val same = ArrayList<Index>()
val added = ArrayList<Index>()
val removed = ArrayList<Index>()
val changed = ArrayList<Index>()

val wasChanged: Boolean
get() = added.isNotEmpty() || removed.isNotEmpty() || changed.isNotEmpty()
get() = added.isNotEmpty() || removed.isNotEmpty()

init {
init()
Expand Down Expand Up @@ -48,7 +47,10 @@ class IndicesDiff(val old: Table?, val new: Table) {
when (oldIndex) {
null -> added.add(newIndex)
newIndex -> same.add(newIndex)
else -> changed.add(newIndex)
else -> {
added.add(newIndex)
removed.add(oldIndex)
}
}
}
removed.addAll(oldIndicesNameMap.values)
Expand Down

0 comments on commit 5064b5e

Please sign in to comment.