NextcloudDatabase.kt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.RoomMigration
  44. import com.nextcloud.client.database.migrations.addLegacyMigrations
  45. import com.owncloud.android.db.ProviderMeta
  46. @Database(
  47. entities = [
  48. ArbitraryDataEntity::class,
  49. CapabilityEntity::class,
  50. ExternalLinkEntity::class,
  51. FileEntity::class,
  52. FilesystemEntity::class,
  53. ShareEntity::class,
  54. SyncedFolderEntity::class,
  55. UploadEntity::class,
  56. VirtualEntity::class
  57. ],
  58. version = ProviderMeta.DB_VERSION,
  59. autoMigrations = [
  60. AutoMigration(from = 65, to = 66),
  61. AutoMigration(from = 66, to = 67),
  62. AutoMigration(from = 68, to = 69),
  63. AutoMigration(from = 69, to = 70),
  64. AutoMigration(from = 71, to = 72),
  65. AutoMigration(from = 72, to = 73)
  66. ],
  67. exportSchema = true
  68. )
  69. @Suppress("Detekt.UnnecessaryAbstractClass") // needed by Room
  70. abstract class NextcloudDatabase : RoomDatabase() {
  71. abstract fun arbitraryDataDao(): ArbitraryDataDao
  72. abstract fun fileDao(): FileDao
  73. companion object {
  74. const val FIRST_ROOM_DB_VERSION = 65
  75. private var INSTANCE: NextcloudDatabase? = null
  76. @JvmStatic
  77. @Suppress("DeprecatedCallableAddReplaceWith")
  78. @Deprecated("Here for legacy purposes, inject this class or use getInstance(context, clock) instead")
  79. fun getInstance(context: Context): NextcloudDatabase {
  80. return getInstance(context, ClockImpl())
  81. }
  82. @JvmStatic
  83. fun getInstance(context: Context, clock: Clock): NextcloudDatabase {
  84. if (INSTANCE == null) {
  85. INSTANCE = Room
  86. .databaseBuilder(context, NextcloudDatabase::class.java, ProviderMeta.DB_NAME)
  87. .allowMainThreadQueries()
  88. .addLegacyMigrations(clock, context)
  89. .addMigrations(RoomMigration())
  90. .addMigrations(Migration67to68())
  91. .addMigrations(Migration70to71())
  92. .fallbackToDestructiveMigration()
  93. .build()
  94. }
  95. return INSTANCE!!
  96. }
  97. }
  98. }