2 * Copyright © 2021 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.db;
20 import androidx.annotation.NonNull;
21 import androidx.room.Database;
22 import androidx.room.Room;
23 import androidx.room.RoomDatabase;
24 import androidx.room.migration.Migration;
25 import androidx.sqlite.db.SupportSQLiteDatabase;
27 import net.ktnx.mobileledger.App;
28 import net.ktnx.mobileledger.dao.AccountDAO;
29 import net.ktnx.mobileledger.dao.CurrencyDAO;
30 import net.ktnx.mobileledger.dao.TemplateAccountDAO;
31 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
32 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
34 @Database(version = 56, entities = {TemplateHeader.class, TemplateAccount.class, Currency.class,
35 net.ktnx.mobileledger.db.Account.class
37 abstract public class DB extends RoomDatabase {
38 private static DB instance;
39 public static DB get() {
42 synchronized (DB.class) {
47 Room.databaseBuilder(App.instance, DB.class, MobileLedgerDatabase.DB_NAME)
48 .addMigrations(new Migration[]{new Migration(51, 52) {
50 public void migrate(@NonNull SupportSQLiteDatabase db) {
51 db.beginTransaction();
53 db.execSQL("create index fk_pattern_accounts_pattern on " +
54 "pattern_accounts(pattern_id);");
55 db.execSQL("create index fk_pattern_accounts_currency on " +
56 "pattern_accounts(currency);");
57 db.setTransactionSuccessful();
63 }, new Migration(52, 53) {
65 public void migrate(@NonNull SupportSQLiteDatabase db) {
67 "alter table pattern_accounts add negate_amount boolean;");
69 }, new Migration(53, 54) {
71 public void migrate(@NonNull SupportSQLiteDatabase db) {
72 db.execSQL("CREATE TABLE templates (id INTEGER PRIMARY KEY " +
73 "AUTOINCREMENT NOT NULL, name TEXT NOT NULL, " +
74 "regular_expression TEXT NOT NULL, test_text TEXT, " +
75 "transaction_description TEXT, " +
76 "transaction_description_match_group INTEGER, " +
77 "transaction_comment TEXT, " +
78 "transaction_comment_match_group INTEGER, date_year " +
79 "INTEGER, date_year_match_group INTEGER, date_month " +
80 "INTEGER, date_month_match_group INTEGER, date_day " +
81 "INTEGER, date_day_match_group INTEGER)");
83 "CREATE TABLE template_accounts (id INTEGER PRIMARY KEY " +
84 "AUTOINCREMENT NOT NULL, template_id INTEGER NOT NULL, " +
85 "acc TEXT, position INTEGER NOT NULL, acc_match_group " +
86 "INTEGER, currency INTEGER, currency_match_group INTEGER," +
87 " amount REAL, amount_match_group INTEGER, comment TEXT, " +
88 "comment_match_group INTEGER, negate_amount INTEGER, " +
89 "FOREIGN KEY(template_id) REFERENCES templates(id) ON " +
90 "UPDATE NO ACTION ON DELETE NO ACTION , FOREIGN KEY" +
91 "(currency) REFERENCES currencies(id) ON UPDATE NO ACTION" +
92 " ON DELETE NO ACTION )");
93 db.execSQL("insert into templates(id, name, regular_expression, " +
94 "test_text, transaction_description, " +
95 "transaction_description_match_group, " +
96 "transaction_comment, transaction_comment_match_group," +
97 " date_year, date_year_match_group, date_month, " +
98 "date_month_match_group, date_day, " +
99 "date_day_match_group)" +
100 " select id, name, regular_expression, test_text, " +
101 "transaction_description, " +
102 "transaction_description_match_group, " +
103 "transaction_comment, transaction_comment_match_group," +
104 " date_year, date_year_match_group, date_month, " +
105 "date_month_match_group, date_day, " +
106 "date_day_match_group from patterns");
107 db.execSQL("insert into template_accounts(id, template_id, acc, " +
108 "position, acc_match_group, currency, " +
109 "currency_match_group, amount, amount_match_group, " +
110 "amount, amount_match_group, comment, " +
111 "comment_match_group, negate_amount) select id, " +
112 "pattern_id, acc, position, acc_match_group, " +
114 "currency_match_group, amount, amount_match_group, " +
115 "amount, amount_match_group, comment, " +
116 "comment_match_group, negate_amount from " +
118 db.execSQL("create index fk_template_accounts_template on " +
119 "template_accounts(template_id)");
120 db.execSQL("create index fk_template_accounts_currency on " +
121 "template_accounts(currency)");
122 db.execSQL("drop table pattern_accounts");
123 db.execSQL("drop table patterns");
125 }, new Migration(54, 55) {
127 public void migrate(@NonNull SupportSQLiteDatabase db) {
129 "CREATE TABLE template_accounts_new (id INTEGER PRIMARY " +
131 "AUTOINCREMENT NOT NULL, template_id INTEGER NOT NULL, " +
132 "acc TEXT, position INTEGER NOT NULL, acc_match_group " +
133 "INTEGER, currency INTEGER, currency_match_group INTEGER," +
134 " amount REAL, amount_match_group INTEGER, comment TEXT, " +
135 "comment_match_group INTEGER, negate_amount INTEGER, " +
136 "FOREIGN KEY(template_id) REFERENCES templates(id) ON " +
137 "UPDATE RESTRICT ON DELETE CASCADE , FOREIGN KEY" +
138 "(currency) REFERENCES currencies(id) ON UPDATE RESTRICT" +
139 " ON DELETE RESTRICT)");
141 "insert into template_accounts_new(id, template_id, acc, " +
142 "position, acc_match_group, currency, " +
143 "currency_match_group, amount, amount_match_group, " +
144 "amount, amount_match_group, comment, " +
145 "comment_match_group, negate_amount) select id, " +
146 "template_id, acc, position, acc_match_group, " +
148 "currency_match_group, amount, amount_match_group, " +
149 "amount, amount_match_group, comment, " +
150 "comment_match_group, negate_amount from " +
151 "template_accounts");
152 db.execSQL("drop table template_accounts");
153 db.execSQL("alter table template_accounts_new rename to " +
154 "template_accounts");
155 db.execSQL("create index fk_template_accounts_template on " +
156 "template_accounts(template_id)");
157 db.execSQL("create index fk_template_accounts_currency on " +
158 "template_accounts(currency)");
162 .addCallback(new Callback() {
164 public void onOpen(@NonNull SupportSQLiteDatabase db) {
166 db.execSQL("PRAGMA foreign_keys = ON");
172 public abstract TemplateHeaderDAO getTemplateDAO();
174 public abstract TemplateAccountDAO getTemplateAccountDAO();
176 public abstract CurrencyDAO getCurrencyDAO();
178 public abstract AccountDAO getAccountDAO();