Skip to content

Instantly share code, notes, and snippets.

INSERT INTO transaction VALUES(...);
INSERT INTO transaction_history
(id, transaction_id, status)
VALUES (1, 't1', 'opened');
SELECT * FROM transaction WHERE id='t1';
SELECT * FROM transaction_history WHERE transaction_id='t1';
INSERT INTO transaction_history
(id, transaction_id, status)
VALUES (2, 't1', 'unverified');
+--+----------+---------------+-------+---------+
|id| source_id| destination_id| amount| currency|
+--+----------+---------------+-------+---------+
|t1| b1 | b2 | 780 | USD |
|t2| b3 | b4 | 9900 | CAD |
+--+----------+---------------+-------+---------+
+-------+-----------------+---------------------+
| id | status | transaction_id |
+-------+-----------------+---------------------+
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
transaction = create_transaction()
transaction.status = 'unverified'
transaction = get_transaction()
print(transaction.status)
class Transaction(AppendOnlyModel):
__tablename__ = 'transaction'
__history_class__ = make_history_class(
'transaction',
status=HistoryStateColumn(
sa.Enum(*constants.TRANSACTION_STATUSES),
nullable=False)
)
source_id = ...
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):
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)
class Transaction:
class History:
id
transaction_id
status
_history = relation(
History,
and_(History.transaction_id == Transaction.transaction_id))