Recount-Server/tests/testdb.go

92 lines
2.2 KiB
Go
Raw Normal View History

2023-12-31 17:21:09 -08:00
package debug_mode
import (
_ "github.com/mattn/go-sqlite3"
"github.com/jmoiron/sqlx"
2024-01-01 20:47:34 -08:00
"github.com/rs/zerolog/log"
2023-12-31 17:21:09 -08:00
)
func Init_testdb(DB_TYPE string, DB_CONNECTION_STRING string) {
db, err := sqlx.Connect(DB_TYPE, DB_CONNECTION_STRING)
if err != nil {
2024-01-01 20:47:34 -08:00
log.Fatal().
Err(err).
Msg("Couldn't open test db")
2023-12-31 17:21:09 -08:00
}
defer db.Close()
init_sql := `
CREATE TABLE accounts (
2024-01-01 20:59:23 -08:00
acnt_id Integer PRIMARY KEY,
2023-12-31 17:21:09 -08:00
acnt_dsply_name varchar(50) NOT NULL,
2024-01-01 20:59:23 -08:00
acnt_description varchar(250) NULL
2023-12-31 17:21:09 -08:00
);
CREATE TABLE buckets (
2024-01-01 20:59:23 -08:00
bkt_id Integer PRIMARY KEY,
2023-12-31 17:21:09 -08:00
bkt_dsply_code varchar(5) NOT NULL,
bkt_dsply_name varchar(50) NULL,
2024-01-01 20:59:23 -08:00
bkt_description varchar(250) NULL
2023-12-31 17:21:09 -08:00
);
CREATE TABLE transactions (
2024-01-01 20:59:23 -08:00
trns_id Integer PRIMARY KEY,
2023-12-31 17:21:09 -08:00
trns_amount money NOT NULL,
trns_description varchar(250) NULL,
trns_account int4 NOT NULL,
trns_bucket int4 NULL,
trns_date Date not null,
2024-01-01 20:59:23 -08:00
FOREIGN KEY (trns_account)
REFERENCES accounts (acnt_id)
ON UPDATE CASCADE ON DELETE SET NULL,
FOREIGN KEY (trns_bucket)
REFERENCES buckets (bkt_id)
ON UPDATE CASCADE ON DELETE SET NULL
2023-12-31 17:21:09 -08:00
);
2024-01-01 20:59:23 -08:00
CREATE TABLE transaction_breakdown (
trns_brkdwn_id Integer PRIMARY KEY,
trns_brkdwn_amount money NOT NULL,
trns_brkdwn_parent_transaction int4 NOT NULL,
trns_brkdwn_catagory int4 NULL,
trns_brkdwn_bucket int4 NULL,
FOREIGN KEY (trns_brkdwn_parent_transaction)
REFERENCES transactions (trns_id)
ON UPDATE CASCADE ON DELETE SET NULL,
FOREIGN KEY (trns_brkdwn_catagory)
REFERENCES transaction_categories(trns_ctgry_id)
ON UPDATE CASCADE ON DELETE SET NULL,
FOREIGN KEY (trns_brkdwn_bucket)
REFERENCES buckets (bkt_id)
ON UPDATE CASCADE ON DELETE SET NULL
);
2023-12-31 17:21:09 -08:00
CREATE TABLE transaction_categories (
2024-01-01 20:59:23 -08:00
trns_ctgry_id Integer PRIMARY KEY,
2023-12-31 17:21:09 -08:00
trns_ctgry_dsply_code varchar(5) NOT NULL,
trns_ctgry_dsply_name varchar(50) NOT NULL,
2024-01-01 20:59:23 -08:00
trns_ctgry_description varchar(250) NULL
2023-12-31 17:21:09 -08:00
);
`
tx := db.MustBegin()
tx.MustExec(init_sql)
2024-01-01 20:47:34 -08:00
err = tx.Commit()
2023-12-31 17:21:09 -08:00
if err != nil {
2024-01-01 20:47:34 -08:00
log.Fatal().
Err(err).
Msg("Could not commit transaction")
2023-12-31 17:21:09 -08:00
}
2024-01-01 20:47:34 -08:00
log.Debug().Msg("Test database initialized")
2023-12-31 17:21:09 -08:00
}