]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/res/raw/db_59.sql
more fall-outs after transition to surrogate IDs
[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 drop table accounts;
77 alter table accounts_new rename to accounts;
78
79 drop table profiles;
80 alter table profiles_new rename to profiles;
81
82 create index fk_account_profile on accounts(profile_id);
83 create unique index un_account_name on accounts(profile_id, name);
84
85 -- options
86 create table options_new(
87 name text not null,
88 profile_id integer not null,
89 value text,
90 primary key(profile_id,name));
91
92 insert into options_new(name, profile_id, value)
93 select o.name, p.id, o.value
94 from options o
95 join profiles p on p.deprecated_uuid = o.profile;
96
97 insert into options_new(name, profile_id, value)
98 select o.name, 0, o.value
99 from options o
100 where o.profile='-';
101
102 update options_new
103 set name='profile_id'
104   , value=(select p.id from profiles p where p.deprecated_uuid=value)
105 where name='profile_uuid';
106
107 drop table options;
108 alter table options_new rename to options;
109
110 update options
111 set name='profile_id'
112   , value=(select p.id from profiles p where p.deprecated_uuid=options.value)
113 where name='profile_uuid';
114
115 -- account_values
116 create table account_values_new(
117 id integer not null primary key,
118 account_id integer not null references accounts(id) on delete cascade on update restrict,
119 currency text not null default '',
120 value real not null,
121 generation integer not null default 0);
122
123 insert into account_values_new(account_id, currency, value, generation)
124 select a.id, av.currency, av.value, av.generation
125 from account_values av
126 join profiles p on p.deprecated_uuid=av.profile
127 join accounts a on a.profile_id = p.id and a.name = av.account;
128
129 drop table account_values;
130 alter table account_values_new rename to account_values;
131
132 create index fk_account_value_acc on account_values(account_id);
133 create unique index un_account_values on account_values(account_id, currency);
134
135 -- transactions
136 create table transactions_new(
137 id integer not null primary key,
138 profile_id integer not null references profiles(id) on delete cascade on update restrict,
139 ledger_id integer not null,
140 description text not null,
141 year integer not null,
142 month integer not null,
143 day integer not null,
144 comment text,
145 data_hash text not null,
146 generation integer not null);
147
148 insert into transactions_new(profile_id, ledger_id, description, year, month, day, comment, data_hash, generation)
149 select p.id, t.id, t.description, t.year, t.month, t.day, t.comment, t.data_hash, t.generation
150 from transactions t
151 join profiles p on p.deprecated_uuid = t.profile;
152
153 -- transaction_accounts
154 create table transaction_accounts_new(
155     id integer not null primary key,
156     transaction_id integer not null references transactions_new(id) on delete cascade on update restrict,
157     order_no integer not null,
158     account_name text not null,
159     currency text not null default '',
160     amount real not null,
161     comment text,
162     generation integer not null default 0);
163
164 insert into transaction_accounts_new(transaction_id, order_no, account_name,
165     currency, amount, comment, generation)
166 select t.id, ta.order_no, ta.account_name, ta.currency, ta.amount, ta.comment, ta.generation
167 from transaction_accounts ta
168 join profiles p on ta.profile=p.deprecated_uuid
169 join transactions_new t on ta.transaction_id = t.ledger_id and t.profile_id=p.id;
170
171 drop table transaction_accounts;
172 alter table transaction_accounts_new rename to transaction_accounts;
173
174 drop table transactions;
175 alter table transactions_new rename to transactions;
176
177 create index idx_transaction_description on transactions(description);
178 create unique index un_transactions_ledger_id on transactions(profile_id, ledger_id);
179 create index fk_transaction_profile on transactions(profile_id);
180
181 create unique index un_transaction_accounts on transaction_accounts(transaction_id, order_no);
182 create index fk_trans_acc_trans on transaction_accounts(transaction_id);