]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/res/raw/db_59.sql
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / res / raw / db_59.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 58 to revision 59
17
18 -- pragmas need to be outside of transaction control
19 -- foreign_keys is needed so that foreign key constraints are redirected
20
21 commit transaction;
22 pragma foreign_keys = on;
23
24 begin transaction;
25
26 -- profiles
27 CREATE TABLE profiles_new (
28 id INTEGER NOT NULL PRIMARY KEY,
29 deprecated_uuid text,
30 name TEXT NOT NULL,
31 url TEXT NOT NULL,
32 use_authentication INTEGER NOT NULL,
33 auth_user TEXT,
34 auth_password TEXT,
35 order_no INTEGER NOT NULL,
36 permit_posting INTEGER NOT NULL,
37 theme INTEGER NOT NULL DEFAULT -1,
38 preferred_accounts_filter TEXT,
39 future_dates INTEGER NOT NULL,
40 api_version INTEGER NOT NULL,
41 show_commodity_by_default INTEGER NOT NULL,
42 default_commodity TEXT,
43 show_comments_by_default INTEGER NOT NULL DEFAULT 1,
44 detected_version_pre_1_19 INTEGER NOT NULL,
45 detected_version_major INTEGER NOT NULL,
46 detected_version_minor INTEGER NOT NULL);
47
48 insert into profiles_new(
49        deprecated_uuid, name, url, use_authentication, auth_user, auth_password,
50        order_no, permit_posting, theme, preferred_accounts_filter, future_dates, api_version,
51        show_commodity_by_default, default_commodity, show_comments_by_default, detected_version_pre_1_19,
52        detected_version_major, detected_version_minor)
53 select uuid, name, url, use_authentication, auth_user, auth_password,
54        order_no, permit_posting, theme, preferred_accounts_filter, future_dates, api_version,
55        show_commodity_by_default, default_commodity, show_comments_by_default, detected_version_pre_1_19,
56        detected_version_major, detected_version_minor
57 from profiles;
58
59 -- accounts
60 create table accounts_new(
61 id integer primary key not null,
62 profile_id integer not null references profiles_new(id) on delete cascade on update restrict,
63 level INTEGER NOT NULL,
64 name TEXT NOT NULL,
65 name_upper TEXT NOT NULL,
66 parent_name TEXT,
67 expanded INTEGER NOT NULL DEFAULT 1,
68 amounts_expanded INTEGER NOT NULL DEFAULT 0,
69 generation INTEGER NOT NULL DEFAULT 0);
70
71 insert into accounts_new(profile_id, level, name, name_upper, parent_name, expanded, amounts_expanded, generation)
72 select p.id, a.level, a.name, a.name_upper, a.parent_name, a.expanded, a.amounts_expanded, a.generation
73 from profiles_new p
74 join accounts a on a.profile=p.deprecated_uuid;
75
76 -- options
77 create table options_new(
78 name text not null,
79 profile_id integer not null,
80 value text,
81 primary key(profile_id,name));
82
83 insert into options_new(name, profile_id, value)
84 select o.name, p.id, o.value
85 from options o
86 join profiles_new p on p.deprecated_uuid = o.profile;
87
88 insert into options_new(name, profile_id, value)
89 select o.name, 0, o.value
90 from options o
91 where o.profile='-';
92
93 update options_new
94 set name='profile_id'
95   , value=(select p.id from profiles_new p where p.deprecated_uuid=value)
96 where name='profile_uuid';
97
98 update options_new
99 set name='profile_id'
100   , value=(select p.id from profiles_new p where p.deprecated_uuid=options_new.value)
101 where name='profile_uuid';
102
103 -- account_values
104 create table account_values_new(
105 id integer not null primary key,
106 account_id integer not null references accounts_new(id) on delete cascade on update restrict,
107 currency text not null default '',
108 value real not null,
109 generation integer not null default 0);
110
111 insert into account_values_new(account_id, currency, value, generation)
112 select a.id, av.currency, av.value, av.generation
113 from account_values av
114 join profiles_new p on p.deprecated_uuid=av.profile
115 join accounts_new a on a.profile_id = p.id and a.name = av.account;
116
117 -- transactions
118 create table transactions_new(
119 id integer not null primary key,
120 profile_id integer not null references profiles_new(id) on delete cascade on update restrict,
121 ledger_id integer not null,
122 description text not null,
123 year integer not null,
124 month integer not null,
125 day integer not null,
126 comment text,
127 data_hash text not null,
128 generation integer not null);
129
130 insert into transactions_new(profile_id, ledger_id, description, year, month, day, comment, data_hash, generation)
131 select p.id, t.id, t.description, t.year, t.month, t.day, t.comment, t.data_hash, t.generation
132 from transactions t
133 join profiles_new p on p.deprecated_uuid = t.profile;
134
135 -- transaction_accounts
136 create table transaction_accounts_new(
137     id integer not null primary key,
138     transaction_id integer not null references transactions_new(id) on delete cascade on update restrict,
139     order_no integer not null,
140     account_name text not null,
141     currency text not null default '',
142     amount real not null,
143     comment text,
144     generation integer not null default 0);
145
146 insert into transaction_accounts_new(transaction_id, order_no, account_name,
147     currency, amount, comment, generation)
148 select t.id, ta.order_no, ta.account_name, ta.currency, ta.amount, ta.comment, ta.generation
149 from transaction_accounts ta
150 join profiles_new p on ta.profile=p.deprecated_uuid
151 join transactions_new t on ta.transaction_id = t.ledger_id and t.profile_id=p.id;
152
153 -- table drop/rename
154 drop table options;
155 alter table options_new rename to options;
156
157 drop table account_values;
158 alter table account_values_new rename to account_values;
159
160 drop table transaction_accounts;
161 alter table transaction_accounts_new rename to transaction_accounts;
162
163 drop table transactions;
164 alter table transactions_new rename to transactions;
165
166 drop table accounts;
167 alter table accounts_new rename to accounts;
168
169 drop table profiles;
170 alter table profiles_new rename to profiles;
171
172 -- indices
173 create index fk_account_profile on accounts(profile_id);
174 create unique index un_account_name on accounts(profile_id, name);
175
176 create index fk_account_value_acc on account_values(account_id);
177 create unique index un_account_values on account_values(account_id, currency);
178
179 create index idx_transaction_description on transactions(description);
180 create unique index un_transactions_ledger_id on transactions(profile_id, ledger_id);
181 create index fk_transaction_profile on transactions(profile_id);
182
183 create unique index un_transaction_accounts on transaction_accounts(transaction_id, order_no);
184 create index fk_trans_acc_trans on transaction_accounts(transaction_id);