Skip to content

Commit

Permalink
DB Schema migration (#14)
Browse files Browse the repository at this point in the history
* Add schema migration

* add schema dump script

* Add README for scripts

* Add git hooks for dumping schema
  • Loading branch information
ilayaraja89 authored and sudhirj committed Jun 12, 2017
1 parent e246b68 commit 5dd55ee
Show file tree
Hide file tree
Showing 66 changed files with 2,673 additions and 5 deletions.
25 changes: 25 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"github.com/RealImage/QLedger/controllers"
"github.com/RealImage/QLedger/middlewares"
"github.com/julienschmidt/httprouter"
_ "github.com/lib/pq"
"github.com/mattes/migrate"
"github.com/mattes/migrate/database/postgres"
_ "github.com/mattes/migrate/source/file"
)

func main() {
Expand All @@ -19,6 +21,17 @@ func main() {
log.Panic("Unable to connect to Database:", err)
}
log.Println("Successfully established connection to database.")

log.Println("Starting db schema migration...")
driver, _ := postgres.WithInstance(db, &postgres.Config{})
m, _ := migrate.NewWithDatabaseInstance(
"file://migrations/postgres",
"postgres", driver)
version, _, _ := m.Version()
log.Println("Current schema version:", version)
m.Up()
version, _, _ = m.Version()
log.Println("Migrated schema version:", version)
appContext := &ledgerContext.AppContext{DB: db}

router := httprouter.New()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS accounts;
4 changes: 4 additions & 0 deletions migrations/postgres/20170607001_create_table_accounts.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE accounts (
id character varying NOT NULL,
data jsonb DEFAULT '{}'::jsonb NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE ONLY accounts
DROP CONSTRAINT IF EXISTS accounts_pkey;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE ONLY accounts
ADD CONSTRAINT accounts_pkey PRIMARY KEY (id);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS account_tags;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE account_tags (
account_id character varying NOT NULL,
key character varying NOT NULL,
value character varying
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE ONLY account_tags
DROP CONSTRAINT IF EXISTS account_tags_account_id_fkey;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE ONLY account_tags
ADD CONSTRAINT account_tags_account_id_fkey FOREIGN KEY (account_id) REFERENCES accounts(id);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS account_tags_lookup_idx;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE UNIQUE INDEX account_tags_lookup_idx ON account_tags USING btree (value, key, account_id);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS transactions;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE transactions (
id character varying NOT NULL,
"timestamp" timestamp without time zone NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE ONLY transactions
DROP CONSTRAINT IF EXISTS transactions_pkey;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE ONLY transactions
ADD CONSTRAINT transactions_pkey PRIMARY KEY (id);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS timestamp_idx;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX timestamp_idx ON transactions USING brin ("timestamp");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS transaction_tags;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE transaction_tags (
transaction_id character varying NOT NULL,
key character varying NOT NULL,
value character varying
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE ONLY transaction_tags
DROP CONSTRAINT IF EXISTS transaction_tags_transaction_id_fkey;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE ONLY transaction_tags
ADD CONSTRAINT transaction_tags_transaction_id_fkey FOREIGN KEY (transaction_id) REFERENCES transactions(id);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS transaction_tags_lookup_idx;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE UNIQUE INDEX transaction_tags_lookup_idx ON transaction_tags USING btree (value, key, transaction_id);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS lines;
6 changes: 6 additions & 0 deletions migrations/postgres/20170607012_create_table_lines.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE lines (
id bigint NOT NULL,
transaction_id character varying NOT NULL,
account_id character varying NOT NULL,
delta integer NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE ONLY lines
DROP CONSTRAINT IF EXISTS lines_pkey;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE ONLY lines
ADD CONSTRAINT lines_pkey PRIMARY KEY (id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE ONLY lines
DROP CONSTRAINT IF EXISTS lines_account_id_fkey;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE ONLY lines
ADD CONSTRAINT lines_account_id_fkey FOREIGN KEY (account_id) REFERENCES accounts(id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE ONLY lines
DROP CONSTRAINT IF EXISTS lines_txn_fkey;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE ONLY lines
ADD CONSTRAINT lines_txn_fkey FOREIGN KEY (transaction_id) REFERENCES transactions(id);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP SEQUENCE IF EXISTS lines_id_seq;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE SEQUENCE lines_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
OWNED BY lines.id;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE ONLY lines ALTER COLUMN id DROP DEFAULT;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE ONLY lines ALTER COLUMN id SET DEFAULT nextval('lines_id_seq'::regclass);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP VIEW IF EXISTS current_balances;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE VIEW current_balances AS
SELECT lines.account_id,
sum(lines.delta) AS balance
FROM lines
GROUP BY lines.account_id;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP VIEW IF EXISTS invalid_transactions;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE VIEW invalid_transactions AS
SELECT lines.transaction_id,
sum(lines.delta) AS sum
FROM lines
GROUP BY lines.transaction_id
HAVING (sum(lines.delta) > 0);
10 changes: 6 additions & 4 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ CREATE SEQUENCE lines_id_seq
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE lines_id_seq OWNED BY lines.id;
CREATE TABLE schema_migrations (
version bigint NOT NULL,
dirty boolean NOT NULL
);
CREATE TABLE transaction_tags (
transaction_id character varying NOT NULL,
key character varying NOT NULL,
Expand All @@ -54,14 +58,12 @@ CREATE TABLE transactions (
"timestamp" timestamp without time zone NOT NULL
);
ALTER TABLE ONLY lines ALTER COLUMN id SET DEFAULT nextval('lines_id_seq'::regclass);
ALTER TABLE ONLY account_tags
ADD CONSTRAINT account_tags_pkey PRIMARY KEY (account_id);
ALTER TABLE ONLY accounts
ADD CONSTRAINT accounts_pkey PRIMARY KEY (id);
ALTER TABLE ONLY lines
ADD CONSTRAINT lines_pkey PRIMARY KEY (id);
ALTER TABLE ONLY transaction_tags
ADD CONSTRAINT transaction_tags_pkey PRIMARY KEY (transaction_id);
ALTER TABLE ONLY schema_migrations
ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version);
ALTER TABLE ONLY transactions
ADD CONSTRAINT transactions_pkey PRIMARY KEY (id);
CREATE UNIQUE INDEX account_tags_lookup_idx ON account_tags USING btree (value, key, account_id);
Expand Down
12 changes: 12 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Scripts

#### Install Hooks

```
cd QLedger
bash scripts/install_hooks.sh
```

The above script install Git hooks which does the following tasks:

- Identify the schema changes and dumps to `schema.sql`
14 changes: 14 additions & 0 deletions scripts/hooks/git/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

echo "Checking for DB schema changes.."
DB=`echo $DATABASE_URL|cut -d/ -f4|cut -d? -f1`
pg_dump -s -x -O $DB | grep -v -e "^--" -e "^$" > schema.sql
SCHEMA_CHANGED=`git ls-files -m|grep schema.sql`
if [ "$SCHEMA_CHANGED" != "" ]; then
echo "Schema is changed and the changes are dumped to schema.sql"
echo "Please add the file to Git and commit"
echo "Aborted commit"
exit 1
else
echo "No changes in schema!"
fi
4 changes: 4 additions & 0 deletions scripts/install_hooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

cp scripts/hooks/git/pre-commit .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
6 changes: 6 additions & 0 deletions vendor/github.com/mattes/migrate/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions vendor/github.com/mattes/migrate/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/mattes/migrate/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5dd55ee

Please sign in to comment.