1a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar/*
2a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar * Copyright (C) 2017 The Android Open Source Project
3a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar *
4a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
5a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar * you may not use this file except in compliance with the License.
6a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar * You may obtain a copy of the License at
7a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar *
8a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
9a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar *
10a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar * Unless required by applicable law or agreed to in writing, software
11a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
12a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar * See the License for the specific language governing permissions and
14a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar * limitations under the License.
15a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar */
16a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar
17bdc4c86d3dff74f6634a38e2f7b316b0e823a2c8Alan Viverettepackage androidx.room.migration;
18a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar
19bdc4c86d3dff74f6634a38e2f7b316b0e823a2c8Alan Viveretteimport androidx.annotation.NonNull;
20bdc4c86d3dff74f6634a38e2f7b316b0e823a2c8Alan Viveretteimport androidx.sqlite.db.SupportSQLiteDatabase;
21a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar
22a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar/**
23a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar * Base class for a database migration.
24a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar * <p>
25a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar * Each migration can move between 2 versions that are defined by {@link #startVersion} and
26a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar * {@link #endVersion}.
27a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar * <p>
289ca57725ac137fe0f6ae50c1053ac37b160f43eaYigit Boyar * A migration can handle more than 1 version (e.g. if you have a faster path to choose when
299ca57725ac137fe0f6ae50c1053ac37b160f43eaYigit Boyar * going version 3 to 5 without going to version 4). If Room opens a database at version
309ca57725ac137fe0f6ae50c1053ac37b160f43eaYigit Boyar * 3 and latest version is &gt;= 5, Room will use the migration object that can migrate from
319ca57725ac137fe0f6ae50c1053ac37b160f43eaYigit Boyar * 3 to 5 instead of 3 to 4 and 4 to 5.
323a433f7ddbffa6131883cc3b23fc80edf54add19Yigit Boyar * <p>
339ca57725ac137fe0f6ae50c1053ac37b160f43eaYigit Boyar * If there are not enough migrations provided to move from the current version to the latest
349ca57725ac137fe0f6ae50c1053ac37b160f43eaYigit Boyar * version, Room will clear the database and recreate so even if you have no changes between 2
359ca57725ac137fe0f6ae50c1053ac37b160f43eaYigit Boyar * versions, you should still provide a Migration object to the builder.
36a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar */
37a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyarpublic abstract class Migration {
38a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar    public final int startVersion;
39a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar    public final int endVersion;
40a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar
41a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar    /**
42a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar     * Creates a new migration between {@code startVersion} and {@code endVersion}.
43a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar     *
44a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar     * @param startVersion The start version of the database.
45a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar     * @param endVersion The end version of the database after this migration is applied.
46a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar     */
47a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar    public Migration(int startVersion, int endVersion) {
48a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar        this.startVersion = startVersion;
49a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar        this.endVersion = endVersion;
50a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar    }
51a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar
52a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar    /**
53a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar     * Should run the necessary migrations.
54a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar     * <p>
55a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar     * This class cannot access any generated Dao in this method.
56ca7dfff7e510da1833cd4611056cf91f4c44ef19Yigit Boyar     * <p>
57ca7dfff7e510da1833cd4611056cf91f4c44ef19Yigit Boyar     * This method is already called inside a transaction and that transaction might actually be a
58ca7dfff7e510da1833cd4611056cf91f4c44ef19Yigit Boyar     * composite transaction of all necessary {@code Migration}s.
59a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar     *
60a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar     * @param database The database instance
61a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar     */
62e1635fb62c7bfac2b6f74fe6beacc050026f27fcSergey Vasilinets    public abstract void migrate(@NonNull SupportSQLiteDatabase database);
63a64756a18111a7312b3fa03b76d13381a8907176Yigit Boyar}
64