From e20e0d8049c843abfd8b9e2fd9da7461f9f8ab1c Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Wed, 10 Feb 2021 23:19:52 +0200 Subject: [PATCH] fix initial database creation old-style revision support is still needed because Room is a second-class citizen, yet --- .../utils/MobileLedgerDatabase.java | 3 --- app/src/main/res/raw/create_db.sql | 13 +++++----- app/src/main/res/raw/sql_54.sql | 11 ++++++++- app/src/main/res/raw/sql_55.sql | 24 +++++++++++++++++++ 4 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 app/src/main/res/raw/sql_55.sql diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java b/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java index 0a9a509b..1c66f07d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java @@ -72,9 +72,6 @@ public class MobileLedgerDatabase extends SQLiteOpenHelper { applyRevision(db, i); } private void applyRevision(SQLiteDatabase db, int rev_no) { - if (rev_no == 55) - return; - String rev_file = String.format(Locale.US, "sql_%d", rev_no); applyRevisionFile(db, rev_file); diff --git a/app/src/main/res/raw/create_db.sql b/app/src/main/res/raw/create_db.sql index fb5e0435..af9ee1ac 100644 --- a/app/src/main/res/raw/create_db.sql +++ b/app/src/main/res/raw/create_db.sql @@ -29,10 +29,9 @@ create table transaction_accounts(profile varchar not null, transaction_id integ create unique index un_transaction_accounts_order on transaction_accounts(profile, transaction_id, order_no); create table currencies(id integer not null primary key, name varchar not null, position varchar not null, has_gap integer not null); -create table patterns(id INTEGER not null primary key, name TEXT not null, regular_expression TEXT not null, test_text TEXT, transaction_description TEXT, transaction_description_match_group INTEGER, transaction_comment TEXT, transaction_comment_match_group INTEGER, date_year INTEGER, date_year_match_group INTEGER, date_month INTEGER, date_month_match_group INTEGER, date_day INTEGER, date_day_match_group INTEGER); -create unique index un_patterns_id on patterns(id); -create table pattern_accounts(id INTEGER not null primary key, pattern_id INTEGER not null, position INTEGER not null, acc TEXT, acc_match_group INTEGER, currency INTEGER, currency_match_group INTEGER, amount REAL, amount_match_group INTEGER, negate_amount INTEGER, comment TEXT, comment_match_group INTEGER, constraint fk_pattern_account_pattern foreign key(pattern_id) references patterns(id), constraint fk_pattern_account_currency foreign key(currency) references currencies(id)); -create unique index un_pattern_accounts on pattern_accounts(id); -create index fk_pattern_accounts_pattern on pattern_accounts(pattern_id); -create index fk_pattern_accounts_currency on pattern_accounts(currency); --- updated to revision 53 \ No newline at end of file +CREATE TABLE templates (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name TEXT NOT NULL, regular_expression TEXT NOT NULL, test_text TEXT, transaction_description TEXT, transaction_description_match_group INTEGER, transaction_comment TEXT, transaction_comment_match_group INTEGER, date_year INTEGER, date_year_match_group INTEGER, date_month INTEGER, date_month_match_group INTEGER, date_day INTEGER, date_day_match_group INTEGER); +CREATE TABLE template_accounts(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, template_id INTEGER NOT NULL, acc TEXT, position INTEGER NOT NULL, acc_match_group INTEGER, currency INTEGER, currency_match_group INTEGER, amount REAL, amount_match_group INTEGER, comment TEXT, comment_match_group INTEGER, negate_amount INTEGER, FOREIGN KEY(template_id) REFERENCES templates(id) ON UPDATE RESTRICT ON DELETE CASCADE, FOREIGN KEY(currency) REFERENCES currencies(id) ON UPDATE RESTRICT ON DELETE RESTRICT); +create index fk_template_accounts_template on template_accounts(template_id); +create index fk_template_accounts_currency on template_accounts(currency); + +-- updated to revision 55 \ No newline at end of file diff --git a/app/src/main/res/raw/sql_54.sql b/app/src/main/res/raw/sql_54.sql index 1d6565f4..50991462 100644 --- a/app/src/main/res/raw/sql_54.sql +++ b/app/src/main/res/raw/sql_54.sql @@ -13,4 +13,13 @@ -- You should have received a copy of the GNU General Public License -- along with MoLe. If not, see . --- this is handled in a Room migration \ No newline at end of file +-- copied from the Room migration + +CREATE TABLE templates (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name TEXT NOT NULL, regular_expression TEXT NOT NULL, test_text TEXT, transaction_description TEXT, transaction_description_match_group INTEGER, transaction_comment TEXT, transaction_comment_match_group INTEGER, date_year INTEGER, date_year_match_group INTEGER, date_month INTEGER, date_month_match_group INTEGER, date_day INTEGER, date_day_match_group INTEGER); +CREATE TABLE template_accounts (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, template_id INTEGER NOT NULL, acc TEXT, position INTEGER NOT NULL, acc_match_group INTEGER, currency INTEGER, currency_match_group INTEGER, amount REAL, amount_match_group INTEGER, comment TEXT, comment_match_group INTEGER, negate_amount INTEGER, FOREIGN KEY(template_id) REFERENCES templates(id) ON UPDATE NO ACTION ON DELETE NO ACTION , FOREIGN KEY(currency) REFERENCES currencies(id) ON UPDATE NO ACTION ON DELETE NO ACTION); +insert into templates(id, name, regular_expression, test_text, transaction_description, transaction_description_match_group, transaction_comment, transaction_comment_match_group, date_year, date_year_match_group, date_month, date_month_match_group, date_day, date_day_match_group) select id, name, regular_expression, test_text, transaction_description, transaction_description_match_group, transaction_comment, transaction_comment_match_group, date_year, date_year_match_group, date_month, date_month_match_group, date_day, date_day_match_group from patterns; +insert into template_accounts(id, template_id, acc, position, acc_match_group, currency, currency_match_group, amount, amount_match_group, amount, amount_match_group, comment, comment_match_group, negate_amount) select id, pattern_id, acc, position, acc_match_group, currency, currency_match_group, amount, amount_match_group, amount, amount_match_group, comment, comment_match_group, negate_amount from pattern_accounts; +create index fk_template_accounts_template on template_accounts(template_id); +create index fk_template_accounts_currency on template_accounts(currency); +drop table pattern_accounts; +drop table patterns; \ No newline at end of file diff --git a/app/src/main/res/raw/sql_55.sql b/app/src/main/res/raw/sql_55.sql new file mode 100644 index 00000000..9ae20151 --- /dev/null +++ b/app/src/main/res/raw/sql_55.sql @@ -0,0 +1,24 @@ +-- Copyright © 2020 Damyan Ivanov. +-- This file is part of MoLe. +-- MoLe is free software: you can distribute it and/or modify it +-- under the term of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- (at your opinion), any later version. +-- +-- MoLe is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License terms for details. +-- +-- You should have received a copy of the GNU General Public License +-- along with MoLe. If not, see . + +-- copied from the Room migration + +CREATE TABLE template_accounts_new(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, template_id INTEGER NOT NULL, acc TEXT, position INTEGER NOT NULL, acc_match_group INTEGER, currency INTEGER, currency_match_group INTEGER, amount REAL, amount_match_group INTEGER, comment TEXT, comment_match_group INTEGER, negate_amount INTEGER, FOREIGN KEY(template_id) REFERENCES templates(id) ON UPDATE RESTRICT ON DELETE CASCADE, FOREIGN KEY(currency) REFERENCES currencies(id) ON UPDATE RESTRICT ON DELETE RESTRICT); +insert into template_accounts_new(id, template_id, acc, position, acc_match_group, currency, currency_match_group, amount, amount_match_group, amount, amount_match_group, comment, comment_match_group, negate_amount) select id, template_id, acc, position, acc_match_group, currency, currency_match_group, amount, amount_match_group, amount, amount_match_group, comment, comment_match_group, negate_amount from template_accounts; +drop table template_accounts; +alter table template_accounts_new rename to template_accounts; + +create index fk_template_accounts_template on template_accounts(template_id); +create index fk_template_accounts_currency on template_accounts(currency); -- 2.39.2