|
1 | | -# SQLiteCopyOpenHelper |
2 | | -An open helper for Android that will copy & open a pre-populated database |
3 | | - |
4 | | -## Room |
5 | | - |
6 | | -If you are using Room, utilize the [built-in methods for prepopulating a database](https://developer.android.com/training/data-storage/room/prepopulate). |
7 | | - |
8 | | - |
9 | | -## Usage |
10 | | - |
11 | | -Add the [Android SQLite framework](https://developer.android.com/jetpack/androidx/releases/sqlite) as a dependency for a default factory implementation. |
12 | | - |
13 | | -```groovy |
14 | | -dependencies { |
15 | | - implementation("androidx.sqlite:sqlite-framework:$sqlite_version") |
16 | | -} |
17 | | -``` |
18 | | - |
19 | | -### Prepopulate from an app asset |
20 | | - |
21 | | -Copy your database file to the assets folder. |
22 | | -``` |
23 | | -src/main/java/... |
24 | | -src/main/assets/database.sqlite |
25 | | -``` |
26 | | - |
27 | | -Then use the `CopySource.FromAssetPath` configuration to specify the path to your database. |
28 | | -```kotlin |
29 | | -val source = CopySource.FromAssetPath("database.sqlite") |
30 | | -val migrationStrategy = .. |
31 | | -val factory = SQLiteCopyOpenHelper.Factory( |
32 | | - delegate = FrameworkSQLiteOpenHelperFactory(), |
33 | | - copyConfig = CopyConfig(source, migrationStrategy), |
34 | | -) |
35 | | -``` |
36 | | - |
37 | | -### Prepopulate from other sources |
38 | | - |
39 | | -```kotlin |
40 | | -val source = CopySource.FromFile(File(..)) |
41 | | -... |
42 | | -val source = CopySource.FromInputStream { inputStream } |
43 | | -``` |
44 | | - |
45 | | -### Handle migrations that include prepackaged databases |
46 | | - |
47 | | -The default migration strategy is `Destructive` for prepackaged databases, meaning when the database |
48 | | -version on the device does not match the latest schema version, the database tables will be |
49 | | -recreated. |
50 | | - |
51 | | -If valid migrations are necessary between versions then the `Required` strategy can also be used, or |
52 | | -`DestructiveOnDowngrade` if the database does not support downgrades. |
53 | | - |
54 | | -Destructive migrations can be enabled for only a set of specific starting schema versions as well. |
55 | | -```kt |
56 | | -val migrationStrategy = MigrationStrategy.DestructiveFrom(setOf( |
57 | | - 1, 2, 3, // the first migration only supports version 4 -> 5 |
58 | | -)) |
59 | | -``` |
60 | | - |
61 | | -### SQLDelight |
62 | | - |
63 | | -The `AndroidSqliteDriver` for [SQLDelight](https://github.com/cashapp/sqldelight) accepts a `SupportSQLiteOpenHelper.Factory`, so simply pass |
64 | | -in a `SQLiteCopyOpenHelper.Factory`. |
65 | | -```kotlin |
66 | | -val driver = AndroidSqliteDriver( |
67 | | - context = applicationContext, |
68 | | - schema = Database.Schema, |
69 | | - factory = SQLiteCopyOpenHelper.Factory(..), |
70 | | - name = "database.sqlite", |
71 | | -) |
72 | | -``` |
73 | | - |
74 | | -## Installation |
75 | | - |
76 | | -```groovy |
77 | | -dependencyResolutionManagement { |
78 | | - repositories { |
79 | | - mavenCentral() |
80 | | - } |
81 | | -} |
82 | | -
|
83 | | -dependencies { |
84 | | - implementation("io.github.reline:sqlitecopyopenhelper:<version>") |
85 | | -} |
86 | | -``` |
87 | | - |
88 | | -License |
89 | | --------- |
90 | | - |
91 | | - Copyright 2020 Nathan Reline |
92 | | - |
93 | | - Licensed under the Apache License, Version 2.0 (the "License"); |
94 | | - you may not use this file except in compliance with the License. |
95 | | - You may obtain a copy of the License at |
96 | | - |
97 | | - http://www.apache.org/licenses/LICENSE-2.0 |
98 | | - |
99 | | - Unless required by applicable law or agreed to in writing, software |
100 | | - distributed under the License is distributed on an "AS IS" BASIS, |
101 | | - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
102 | | - See the License for the specific language governing permissions and |
103 | | - limitations under the License. |
| 1 | +[Moved to Codeberg](https://codeberg.org/nathanreline/SQLiteCopyOpenHelper) |
0 commit comments