1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.room;
18 
19 import android.content.Context;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.Nullable;
23 import androidx.annotation.RestrictTo;
24 import androidx.sqlite.db.SupportSQLiteOpenHelper;
25 
26 import java.util.List;
27 import java.util.Set;
28 
29 /**
30  * Configuration class for a {@link RoomDatabase}.
31  */
32 @SuppressWarnings("WeakerAccess")
33 public class DatabaseConfiguration {
34     /**
35      * The factory to use to access the database.
36      */
37     @NonNull
38     public final SupportSQLiteOpenHelper.Factory sqliteOpenHelperFactory;
39     /**
40      * The context to use while connecting to the database.
41      */
42     @NonNull
43     public final Context context;
44     /**
45      * The name of the database file or null if it is an in-memory database.
46      */
47     @Nullable
48     public final String name;
49 
50     /**
51      * Collection of available migrations.
52      */
53     @NonNull
54     public final RoomDatabase.MigrationContainer migrationContainer;
55 
56     @Nullable
57     public final List<RoomDatabase.Callback> callbacks;
58 
59     /**
60      * Whether Room should throw an exception for queries run on the main thread.
61      */
62     public final boolean allowMainThreadQueries;
63 
64     /**
65      * The journal mode for this database.
66      */
67     public final RoomDatabase.JournalMode journalMode;
68 
69     /**
70      * If true, Room should crash if a migration is missing.
71      */
72     public final boolean requireMigration;
73 
74     /**
75      * The collection of schema versions from which migrations aren't required.
76      */
77     private final Set<Integer> mMigrationNotRequiredFrom;
78 
79     /**
80      * Creates a database configuration with the given values.
81      *
82      * @param context The application context.
83      * @param name Name of the database, can be null if it is in memory.
84      * @param sqliteOpenHelperFactory The open helper factory to use.
85      * @param migrationContainer The migration container for migrations.
86      * @param callbacks The list of callbacks for database events.
87      * @param allowMainThreadQueries Whether to allow main thread reads/writes or not.
88      * @param journalMode The journal mode. This has to be either TRUNCATE or WRITE_AHEAD_LOGGING.
89      * @param requireMigration True if Room should require a valid migration if version changes,
90      *                        instead of recreating the tables.
91      * @param migrationNotRequiredFrom The collection of schema versions from which migrations
92      *                                 aren't required.
93      *
94      * @hide
95      */
96     @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
DatabaseConfiguration(@onNull Context context, @Nullable String name, @NonNull SupportSQLiteOpenHelper.Factory sqliteOpenHelperFactory, @NonNull RoomDatabase.MigrationContainer migrationContainer, @Nullable List<RoomDatabase.Callback> callbacks, boolean allowMainThreadQueries, RoomDatabase.JournalMode journalMode, boolean requireMigration, @Nullable Set<Integer> migrationNotRequiredFrom)97     public DatabaseConfiguration(@NonNull Context context, @Nullable String name,
98             @NonNull SupportSQLiteOpenHelper.Factory sqliteOpenHelperFactory,
99             @NonNull RoomDatabase.MigrationContainer migrationContainer,
100             @Nullable List<RoomDatabase.Callback> callbacks,
101             boolean allowMainThreadQueries,
102             RoomDatabase.JournalMode journalMode,
103             boolean requireMigration,
104             @Nullable Set<Integer> migrationNotRequiredFrom) {
105         this.sqliteOpenHelperFactory = sqliteOpenHelperFactory;
106         this.context = context;
107         this.name = name;
108         this.migrationContainer = migrationContainer;
109         this.callbacks = callbacks;
110         this.allowMainThreadQueries = allowMainThreadQueries;
111         this.journalMode = journalMode;
112         this.requireMigration = requireMigration;
113         this.mMigrationNotRequiredFrom = migrationNotRequiredFrom;
114     }
115 
116     /**
117      * Returns whether a migration is required from the specified version.
118      *
119      * @param version  The schema version.
120      * @return True if a valid migration is required, false otherwise.
121      */
isMigrationRequiredFrom(int version)122     public boolean isMigrationRequiredFrom(int version) {
123         // Migrations are required from this version if we generally require migrations AND EITHER
124         // there are no exceptions OR the supplied version is not one of the exceptions.
125         return requireMigration
126                 && (mMigrationNotRequiredFrom == null
127                 || !mMigrationNotRequiredFrom.contains(version));
128 
129     }
130 }
131