diff --git a/sql/create-tables.sql b/sql/create-tables.sql new file mode 100644 index 0000000..2543dce --- /dev/null +++ b/sql/create-tables.sql @@ -0,0 +1,23 @@ +CREATE SEQUENCE seq_person; +CREATE SEQUENCE seq_transaction; + +CREATE TABLE person +( + id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('seq_person'), + balance BIGINT NOT NULL +); + +CREATE TABLE transaction +( + id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('seq_transaction'), + person_from BIGINT NOT NULL REFERENCES person (id), + person_to BIGINT NOT NULL REFERENCES person (id), + amount BIGINT NOT NULL + +); + +INSERT INTO person +VALUES (1, 1000); + +INSERT INTO person +VALUES (2, 1000); \ No newline at end of file diff --git a/sql/drop-tables.sql b/sql/drop-tables.sql new file mode 100644 index 0000000..6dfa609 --- /dev/null +++ b/sql/drop-tables.sql @@ -0,0 +1,7 @@ +drop table transaction cascade; + +drop table person cascade; + +drop sequence seq_person; + +drop sequence seq_transaction; \ No newline at end of file diff --git a/sql/fail-transaction.sql b/sql/fail-transaction.sql new file mode 100644 index 0000000..87b1400 --- /dev/null +++ b/sql/fail-transaction.sql @@ -0,0 +1,6 @@ +INSERT INTO person values (1, 100); +INSERT INTO person values (2, 100); + +UPDATE person SET balance = (balance - 10) WHERE id = 1; +INSERT INTO transaction(person_from, person_to, amount) values (1, 3, 10); +UPDATE person SET balance = (balance + 10) WHERE id = 2; diff --git a/sql/refresh-balance.sql b/sql/refresh-balance.sql new file mode 100644 index 0000000..01240bc --- /dev/null +++ b/sql/refresh-balance.sql @@ -0,0 +1,3 @@ +DELETE FROM person WHERE id = 3; +DELETE FROM transaction; +UPDATE person SET balance = 1000 WHERE id in (1,2) \ No newline at end of file diff --git a/sql/success-transaction.sql b/sql/success-transaction.sql new file mode 100644 index 0000000..df4d68c --- /dev/null +++ b/sql/success-transaction.sql @@ -0,0 +1,10 @@ +UPDATE person SET balance=100 WHERE id IN (1,2); +DELETE FROM transaction WHERE person_from = 1; + +BEGIN; + +UPDATE person SET balance = (balance - 10) WHERE id = 1; +INSERT INTO transaction(person_from, person_to, amount) values (1, 3, 10); +UPDATE person SET balance = (balance + 10) WHERE id = 2; + +COMMIT; \ No newline at end of file