]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/res/raw/db_41_58.sql
630e207cc2c0dc5b3864afabcbd50667adfdee6e
[mobile-ledger.git] / app / src / main / res / raw / db_41_58.sql
1 -- Copyright © 2021 Damyan Ivanov.
2 -- This file is part of MoLe.
3 -- MoLe is free software: you can distribute it and/or modify it
4 -- under the term of the GNU General Public License as published by
5 -- the Free Software Foundation, either version 3 of the License, or
6 -- (at your opinion), any later version.
7 --
8 -- MoLe is distributed in the hope that it will be useful,
9 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
10 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 -- GNU General Public License terms for details.
12 --
13 -- You should have received a copy of the GNU General Public License
14 -- along with MoLe. If not, see <https://www.gnu.org/licenses/>.
15
16 -- migrate from revision 41 to revision 58
17
18 -- profiles
19 create table profiles_new(
20  uuid text not null,
21  name text not null,
22  url text not null,
23  use_authentication integer not null,
24  auth_user text,
25  auth_password text,
26  order_no integer not null,
27  permit_posting integer not null default 0,
28  theme integer not null default -1,
29  preferred_accounts_filter varchar,
30  future_dates integer not null,
31  api_version integer not null,
32  show_commodity_by_default integer not null default 0,
33  default_commodity text,
34  show_comments_by_default integer not null default 1,
35  detected_version_pre_1_19 integer not null,
36  detected_version_major integer not null,
37  detected_version_minor integer not null,
38  primary key(uuid));
39
40 insert into profiles_new(
41  uuid, name, url, use_authentication, auth_user, auth_password, order_no,
42  permit_posting, theme, preferred_accounts_filter, future_dates, api_version,
43  show_commodity_by_default, default_commodity, show_comments_by_default,
44  detected_version_pre_1_19, detected_version_major, detected_version_minor)
45 select uuid, name, url, use_authentication, auth_user, auth_password, order_no,
46  permit_posting, theme, preferred_accounts_filter, future_dates, api_version,
47  show_commodity_by_default, default_commodity, show_comments_by_default,
48  coalesce(detected_version_pre_1_19,0), coalesce(detected_version_major,0),
49  coalesce(detected_version_minor,0)
50 from profiles;
51
52 -- options
53 create table options_new(profile varchar not null, name varchar not null, value varchar, primary key(profile, name));
54
55 insert into options_new(profile, name, value)
56 select profile, name, value from options;
57
58 -- accounts
59 create table accounts_new(
60     profile varchar not null,
61     name varchar not null,
62     name_upper varchar not null,
63     level integer not null,
64     parent_name varchar,
65     expanded integer not null default 1,
66     amounts_expanded integer not null default 0,
67     generation integer not null default 0,
68     primary key(profile, name));
69
70 insert into accounts_new(profile, name, name_upper, level, parent_name,
71     expanded, amounts_expanded, generation)
72 select profile, name, name_upper, level, parent_name, expanded,
73     amounts_expanded, generation from accounts;
74
75 -- account_values
76 create table account_values_new(
77     profile varchar not null,
78     account varchar not null,
79     currency varchar not null default '',
80     value real not null,
81     generation integer not null default 0,
82     primary key(profile, account, currency));
83
84 insert into account_values_new(
85     profile, account, currency, value, generation)
86 select profile, account, currency, value, generation
87 from account_values;
88
89 -- description_history
90 create table description_history_new(
91     description varchar collate NOCASE not null,
92     description_upper varchar not null,
93     generation integer not null default 0,
94     primary key(description));
95
96 insert into description_history_new(description, description_upper, generation)
97 select description, description_upper, generation from description_history;
98
99 -- transactions
100 create table transactions_new(
101     profile varchar not null,
102     id integer not null,
103     data_hash varchar not null,
104     year integer not null,
105     month integer not null,
106     day integer not null,
107     description varchar collate NOCASE not null,
108     comment varchar,
109     generation integer not null default 0,
110     primary key(profile,id));
111
112 insert into transactions_new(profile, id, data_hash, year, month, day, description,
113     comment, generation)
114 select profile, id, data_hash, year, month, day, description,
115        comment, generation
116 from transactions;
117
118 -- transaction_accounts
119 create table transaction_accounts_new(
120     profile varchar not null,
121     transaction_id integer not null,
122     order_no integer not null,
123     account_name varchar not null,
124     currency varchar not null default '',
125     amount real not null,
126     comment varchar,
127     generation integer not null default 0,
128     primary key(profile, transaction_id, order_no),
129     foreign key (profile,account_name) references accounts(profile,name)
130       on delete cascade on update restrict,
131     foreign key(profile, transaction_id) references transactions(profile,id)
132       on delete cascade on update restrict);
133
134 insert into transaction_accounts_new(profile, transaction_id, order_no, account_name,
135     currency, amount, comment, generation)
136 select profile, transaction_id, order_no, account_name,
137        currency, amount, comment, generation
138 from transaction_accounts;
139
140 --currencies
141 create table currencies_new(id integer not null primary key, name varchar not null,
142     position varchar not null, has_gap integer not null);
143
144 insert into currencies_new(id, name, position, has_gap)
145 select id, name, position, has_gap
146 from currencies;
147
148
149 -- drop originals
150 drop table transaction_accounts;
151 drop table transactions;
152 drop table account_values;
153 drop table accounts;
154 drop table description_history;
155 drop table profiles;
156 drop table options;
157 drop table currencies;
158
159 -- rename new
160 alter table options_new              rename to options;
161 alter table profiles_new             rename to profiles;
162 alter table accounts_new             rename to accounts;
163 alter table account_values_new       rename to account_values;
164 alter table description_history_new  rename to description_history;
165 alter table transactions_new         rename to transactions;
166 alter table transaction_accounts_new rename to transaction_accounts;
167 alter table currencies_new           rename to currencies;
168
169 -- indices
170 create        index fk_tran_acc_prof_acc        on transaction_accounts(profile, account_name);
171 create unique index un_transactions_data_hash   on transactions(profile,data_hash);
172 create        index idx_transaction_description on transactions(description);
173
174
175 -- new tables
176 CREATE TABLE templates (
177     id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
178     name TEXT NOT NULL,
179     regular_expression TEXT NOT NULL,
180     test_text TEXT,
181     transaction_description TEXT,
182     transaction_description_match_group INTEGER,
183     transaction_comment TEXT,
184     transaction_comment_match_group INTEGER,
185     date_year INTEGER,
186     date_year_match_group INTEGER,
187     date_month INTEGER,
188     date_month_match_group INTEGER,
189     date_day INTEGER,
190     date_day_match_group INTEGER,
191     is_fallback INTEGER NOT NULL DEFAULT 0);
192 CREATE TABLE template_accounts(
193     id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
194     template_id INTEGER NOT NULL,
195     acc TEXT,
196     position INTEGER NOT NULL,
197     acc_match_group INTEGER,
198     currency INTEGER,
199     currency_match_group INTEGER,
200     amount REAL,
201     amount_match_group INTEGER,
202     comment TEXT,
203     comment_match_group INTEGER,
204     negate_amount INTEGER,
205     FOREIGN KEY(template_id) REFERENCES templates(id) ON UPDATE RESTRICT ON DELETE CASCADE,
206     FOREIGN KEY(currency) REFERENCES currencies(id) ON UPDATE RESTRICT ON DELETE RESTRICT);
207 create index fk_template_accounts_template on template_accounts(template_id);
208 create index fk_template_accounts_currency on template_accounts(currency);