NextcloudDatabase.kt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Álvaro Brey
  5. * Copyright (C) 2022 Álvaro Brey
  6. * Copyright (C) 2022 Nextcloud GmbH
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. package com.nextcloud.client.database
  23. import android.content.Context
  24. import androidx.room.AutoMigration
  25. import androidx.room.Database
  26. import androidx.room.Room
  27. import androidx.room.RoomDatabase
  28. import com.nextcloud.client.core.Clock
  29. import com.nextcloud.client.core.ClockImpl
  30. import com.nextcloud.client.database.dao.ArbitraryDataDao
  31. import com.nextcloud.client.database.dao.FileDao
  32. import com.nextcloud.client.database.entity.ArbitraryDataEntity
  33. import com.nextcloud.client.database.entity.CapabilityEntity
  34. import com.nextcloud.client.database.entity.ExternalLinkEntity
  35. import com.nextcloud.client.database.entity.FileEntity
  36. import com.nextcloud.client.database.entity.FilesystemEntity
  37. import com.nextcloud.client.database.entity.ShareEntity
  38. import com.nextcloud.client.database.entity.SyncedFolderEntity
  39. import com.nextcloud.client.database.entity.UploadEntity
  40. import com.nextcloud.client.database.entity.VirtualEntity
  41. import com.nextcloud.client.database.migrations.Migration67to68
  42. import com.nextcloud.client.database.migrations.Migration70to71
  43. import com.nextcloud.client.database.migrations.Migration73to74
  44. import com.nextcloud.client.database.migrations.RoomMigration
  45. import com.nextcloud.client.database.migrations.addLegacyMigrations
  46. import com.owncloud.android.db.ProviderMeta
  47. @Database(
  48. entities = [
  49. ArbitraryDataEntity::class,
  50. CapabilityEntity::class,
  51. ExternalLinkEntity::class,
  52. FileEntity::class,
  53. FilesystemEntity::class,
  54. ShareEntity::class,
  55. SyncedFolderEntity::class,
  56. UploadEntity::class,
  57. VirtualEntity::class
  58. ],
  59. version = ProviderMeta.DB_VERSION,
  60. autoMigrations = [
  61. AutoMigration(from = 65, to = 66),
  62. AutoMigration(from = 66, to = 67),
  63. AutoMigration(from = 68, to = 69),
  64. AutoMigration(from = 69, to = 70),
  65. AutoMigration(from = 71, to = 72),
  66. AutoMigration(from = 72, to = 73)
  67. ],
  68. exportSchema = true
  69. )
  70. @Suppress("Detekt.UnnecessaryAbstractClass") // needed by Room
  71. abstract class NextcloudDatabase : RoomDatabase() {
  72. abstract fun arbitraryDataDao(): ArbitraryDataDao
  73. abstract fun fileDao(): FileDao
  74. companion object {
  75. const val FIRST_ROOM_DB_VERSION = 65
  76. private var INSTANCE: NextcloudDatabase? = null
  77. @JvmStatic
  78. @Suppress("DeprecatedCallableAddReplaceWith")
  79. @Deprecated("Here for legacy purposes, inject this class or use getInstance(context, clock) instead")
  80. fun getInstance(context: Context): NextcloudDatabase {
  81. return getInstance(context, ClockImpl())
  82. }
  83. @JvmStatic
  84. fun getInstance(context: Context, clock: Clock): NextcloudDatabase {
  85. if (INSTANCE == null) {
  86. INSTANCE = Room
  87. .databaseBuilder(context, NextcloudDatabase::class.java, ProviderMeta.DB_NAME)
  88. .allowMainThreadQueries()
  89. .addLegacyMigrations(clock, context)
  90. .addMigrations(RoomMigration())
  91. .addMigrations(Migration67to68())
  92. .addMigrations(Migration70to71())
  93. .addMigrations(Migration73to74())
  94. .fallbackToDestructiveMigration()
  95. .build()
  96. }
  97. return INSTANCE!!
  98. }
  99. }
  100. }