Example #1
0
    def _migration_1(self):
        """Apply migration #1."""
        tables = reflection.introspect(self._db).model_names
        migrator = SqliteMigrator(self._db)
        if "habitmodel" in tables and "activitymodel" in tables:
            with self._db.transaction():
                migrate(migrator.rename_table("habitmodel", "habit"),
                        migrator.rename_table("activitymodel", "activity"))
            logger.debug("Migration #1: Renamed habit, activity tables.")

        # Create new tables
        self._db.create_tables([Config, Summary], safe=True)
        logger.debug("Migration #1: Created tables.")

        # Set DB version
        Config.insert(name="version",
                      value="1").on_conflict("replace").execute()
        logger.debug("Migration #1: DB version updated to 1.")

        # Update summaries
        for h in Habit.select():
            activities = Activity.select()\
                                 .where(Activity.for_habit == h)\
                                 .order_by(Activity.update_date.asc())
            streak = 0
            if len(activities) != 0:
                last_date = activities[0].update_date
                for a in activities:
                    delta = last_date - a.update_date
                    if abs(delta.days) > 1:
                        break
                    streak += 1
                    last_date = a.update_date

            # Update summary for the habit
            s = Summary.get_or_create(for_habit=h,
                                      target=0,
                                      target_date=h.created_date)
            s[0].streak = streak
            s[0].save()
        logger.debug("Migration #1: Summary updated for habits.")
        return 0
Example #2
0
 def make_migrate_operations(self, db: _DatabaseSub) -> T.List[Operation]:
     migrator = SqliteMigrator(db)
     ops = [migrator.rename_table("metrics", "classifiermetrics")]
     return ops