Fix database issues

pull/2020/head
Dharanish 1 year ago
parent 0de7c7f8bd
commit fc16c7078a

@ -81,12 +81,4 @@ class SQLiteEntryList(database: Database) : EntryList() {
override fun recomputeFrom(originalEntries: EntryList, frequency: Frequency, isNumerical: Boolean) { override fun recomputeFrom(originalEntries: EntryList, frequency: Frequency, isNumerical: Boolean) {
throw UnsupportedOperationException() throw UnsupportedOperationException()
} }
override fun clear() {
super.clear()
repository.execSQL(
"delete from repetitions where habit = ?",
habitId.toString()
)
}
} }

@ -39,13 +39,23 @@ class SQLiteHabitList @Inject constructor(private val modelFactory: ModelFactory
loaded = true loaded = true
list.groupId = this.groupId list.groupId = this.groupId
list.removeAll() list.removeAll()
val records = repository.findAll("order by position") val records = repository.findAll("order by group_id, position")
var shouldRebuildOrder = false
var currentGroup: Long? = null
var expectedPosition = 0
for (rec in records) { for (rec in records) {
if (currentGroup != rec.groupId) {
currentGroup = rec.groupId
expectedPosition = 0
}
if (rec.position != expectedPosition) shouldRebuildOrder = true
val h = modelFactory.buildHabit() val h = modelFactory.buildHabit()
rec.copyTo(h) rec.copyTo(h)
(h.originalEntries as SQLiteEntryList).habitId = h.id (h.originalEntries as SQLiteEntryList).habitId = h.id
if (h.groupId == list.groupId) list.add(h) if (h.groupId == list.groupId) list.add(h)
expectedPosition++
} }
if (shouldRebuildOrder) rebuildOrder()
} }
@Synchronized @Synchronized
@ -61,6 +71,26 @@ class SQLiteHabitList @Inject constructor(private val modelFactory: ModelFactory
observable.notifyListeners() observable.notifyListeners()
} }
@Synchronized
private fun rebuildOrder() {
val records = repository.findAll("order by group_id, position")
repository.executeAsTransaction {
var currentGroup: Long? = null
var expectedPosition = 0
for (r in records) {
if (currentGroup != r.groupId) {
currentGroup = r.groupId
expectedPosition = 0
}
if (r.position != expectedPosition) {
r.position = expectedPosition
repository.save(r)
}
expectedPosition++
}
}
}
@Synchronized @Synchronized
override fun add(position: Int, habit: Habit) { override fun add(position: Int, habit: Habit) {
loadRecords() loadRecords()
@ -137,6 +167,7 @@ class SQLiteHabitList @Inject constructor(private val modelFactory: ModelFactory
h.originalEntries.clear() h.originalEntries.clear()
repository.remove(record) repository.remove(record)
} }
rebuildOrder()
observable.notifyListeners() observable.notifyListeners()
} }
@ -189,6 +220,7 @@ class SQLiteHabitList @Inject constructor(private val modelFactory: ModelFactory
@Synchronized @Synchronized
override fun repair() { override fun repair() {
loadRecords() loadRecords()
rebuildOrder()
observable.notifyListeners() observable.notifyListeners()
} }

@ -5,7 +5,22 @@ create table SharedIds (
insert into SharedIds (name, next_id) values ('habitandgroup', (select coalesce(max(id),0) from Habits) + 1 ); insert into SharedIds (name, next_id) values ('habitandgroup', (select coalesce(max(id),0) from Habits) + 1 );
alter table Habits rename to HabitsOld; create table HabitGroups (
id integer primary key,
archived integer,
color integer,
description text not null default "",
highlight integer,
name text,
position integer,
reminder_days integer not null default 127,
reminder_hour integer,
reminder_min integer,
question text not null default "",
uuid text
);
alter table Habits rename to Habits_old;
create table Habits ( create table Habits (
id integer primary key, id integer primary key,
@ -25,29 +40,37 @@ create table Habits (
target_value real not null default 0, target_value real not null default 0,
unit text not null default "", unit text not null default "",
question text, question text,
uuid text uuid text,
group_id integer,
group_uuid text,
foreign key(group_id)
references HabitGroups(id)
on update cascade
on delete cascade
); );
insert into Habits (id, archived, color, description, freq_den, freq_num, highlight, name, position, reminder_min, reminder_days, type, target_type, target_value, unit, question, uuid) insert into Habits (id, archived, color, description, freq_den, freq_num, highlight, name, position, reminder_min, reminder_days, type, target_type, target_value, unit, question, uuid)
select id, archived, color, description, freq_den, freq_num, highlight, name, position, reminder_min, reminder_days, type, target_type, target_value, unit, question, uuid select id, archived, color, description, freq_den, freq_num, highlight, name, position, reminder_min, reminder_days, type, target_type, target_value, unit, question, uuid
from HabitsOld; from Habits_old;
drop table HabitsOld; PRAGMA foreign_keys = OFF;
begin transaction;
create table HabitGroups ( alter table Repetitions rename to Repetitions_old;
id integer primary key, create table Repetitions (
archived integer, id integer primary key autoincrement,
color integer, habit integer,
description text not null default "", timestamp integer,
highlight integer, value integer not null,
name text, notes text,
position integer, foreign key (habit)
reminder_days integer not null default 127, references Habits(id)
reminder_hour integer, on update cascade
reminder_min integer, on delete cascade
question text not null default "",
uuid text
); );
insert into Repetitions select * from Repetitions_old;
drop table Repetitions_old;
drop table Habits_old;
alter table Habits add column group_uuid text references habitgroups(uuid); commit;
alter table Habits add column group_id integer references habitgroups(id); PRAGMA foreign_keys = ON;

@ -52,7 +52,7 @@ class EditHabitCommandTest : BaseUnitTest() {
@Test @Test
fun testExecute() { fun testExecute() {
command = EditHabitCommand(habitList, habit.uuid!!, modified) command = EditHabitCommand(habitList, habit.id!!, modified)
val originalScore = habit.scores[today].value val originalScore = habit.scores[today].value
assertThat(habit.name, equalTo("original")) assertThat(habit.name, equalTo("original"))
command.run() command.run()

@ -35,7 +35,6 @@ import org.junit.Assert.assertThrows
import org.junit.Test import org.junit.Test
import org.mockito.kotlin.mock import org.mockito.kotlin.mock
import org.mockito.kotlin.verify import org.mockito.kotlin.verify
import java.util.ArrayList
import kotlin.test.assertNull import kotlin.test.assertNull
class SQLiteHabitListTest : BaseUnitTest() { class SQLiteHabitListTest : BaseUnitTest() {
@ -49,6 +48,7 @@ class SQLiteHabitListTest : BaseUnitTest() {
override fun setUp() { override fun setUp() {
super.setUp() super.setUp()
val db: Database = buildMemoryDatabase() val db: Database = buildMemoryDatabase()
db.execute("PRAGMA foreign_keys = ON;")
modelFactory = SQLModelFactory(db) modelFactory = SQLModelFactory(db)
habitList = SQLiteHabitList(modelFactory) habitList = SQLiteHabitList(modelFactory)
fixtures = HabitFixtures(modelFactory, habitList) fixtures = HabitFixtures(modelFactory, habitList)
@ -98,8 +98,8 @@ class SQLiteHabitListTest : BaseUnitTest() {
habit.name = "Hello world with id" habit.name = "Hello world with id"
habit.id = 12300L habit.id = 12300L
habitList.add(habit) habitList.add(habit)
assertThat(habit.id, equalTo(12300L)) assertThat(habit.id, equalTo(11L))
val record = repository.find(12300L) val record = repository.find(11L)
assertThat(record!!.name, equalTo(habit.name)) assertThat(record!!.name, equalTo(habit.name))
} }

@ -56,7 +56,7 @@ class ReminderSchedulerTest : BaseUnitTest() {
habit = fixtures.createEmptyHabit() habit = fixtures.createEmptyHabit()
habit.id = habitId habit.id = habitId
reminderScheduler = reminderScheduler =
ReminderScheduler(commandRunner, habitList, sys, widgetPreferences) ReminderScheduler(commandRunner, habitList, habitGroupList, sys, widgetPreferences)
setFixedTimeZone(TimeZone.getTimeZone("GMT-4")) setFixedTimeZone(TimeZone.getTimeZone("GMT-4"))
} }

@ -82,8 +82,8 @@ class HabitCardListCacheTest : BaseUnitTest() {
val h = habitList.getByPosition(3) val h = habitList.getByPosition(3)
val score = h.scores[today].value val score = h.scores[today].value
assertThat(cache.getHabitByPosition(3), equalTo(h)) assertThat(cache.getHabitByPosition(3), equalTo(h))
assertThat(cache.getScore(h.uuid!!), equalTo(score)) assertThat(cache.getScore(h.id!!), equalTo(score))
val actualCheckmarks = cache.getCheckmarks(h.uuid!!) val actualCheckmarks = cache.getCheckmarks(h.id!!)
val expectedCheckmarks = h val expectedCheckmarks = h
.computedEntries .computedEntries

Loading…
Cancel
Save