This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSERT INTO transaction VALUES(...); | |
INSERT INTO transaction_history | |
(id, transaction_id, status) | |
VALUES (1, 't1', 'opened'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * FROM transaction WHERE id='t1'; | |
SELECT * FROM transaction_history WHERE transaction_id='t1'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSERT INTO transaction_history | |
(id, transaction_id, status) | |
VALUES (2, 't1', 'unverified'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+--+----------+---------------+-------+---------+ | |
|id| source_id| destination_id| amount| currency| | |
+--+----------+---------------+-------+---------+ | |
|t1| b1 | b2 | 780 | USD | | |
|t2| b3 | b4 | 9900 | CAD | | |
+--+----------+---------------+-------+---------+ | |
+-------+-----------------+---------------------+ | |
| id | status | transaction_id | | |
+-------+-----------------+---------------------+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
"transaction".id, "transaction".amount, | |
"transaction".currency, current_status.status | |
FROM "transaction" JOIN ( | |
SELECT | |
transaction_id, MAX(id) AS max_id | |
FROM transaction_history | |
GROUP BY transaction_id) max_ids JOIN ( | |
SELECT | |
id, transaction_id, status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
transaction = create_transaction() | |
transaction.status = 'unverified' | |
transaction = get_transaction() | |
print(transaction.status) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Transaction(AppendOnlyModel): | |
__tablename__ = 'transaction' | |
__history_class__ = make_history_class( | |
'transaction', | |
status=HistoryStateColumn( | |
sa.Enum(*constants.TRANSACTION_STATUSES), | |
nullable=False) | |
) | |
source_id = ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AgeAttr(object): | |
def __get__(self, instance, owner): | |
return instance.value | |
def __set__(self, instance, value): | |
if value < 0: | |
raise ValueError('Age cannot be negative') | |
instance.value = value | |
class User(object): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class _AppendOnlyAttribute(object): | |
def __init__(self, name): | |
self.name = name | |
def __get__(self, instance, owner): | |
if instance: | |
return getattr(instance._history[0], self.name) | |
else: | |
return getattr(owner.__history_class__, self.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Transaction: | |
class History: | |
id | |
transaction_id | |
status | |
_history = relation( | |
History, | |
and_(History.transaction_id == Transaction.transaction_id)) |
OlderNewer