group BusinessSubscriptionTransition;

getTransitionsByKey(external_key) ::= <<
  select
    total_ordering
  , bundle_id
  , external_key
  , account_id
  , account_key
  , subscription_id
  , requested_timestamp
  , event
  , prev_product_name
  , prev_product_type
  , prev_product_category
  , prev_slug
  , prev_phase
  , prev_billing_period
  , prev_price
  , prev_price_list
  , prev_mrr
  , prev_currency
  , prev_start_date
  , prev_state
  , next_product_name
  , next_product_type
  , next_product_category
  , next_slug
  , next_phase
  , next_billing_period
  , next_price
  , next_price_list
  , next_mrr
  , next_currency
  , next_start_date
  , next_state
  from bst
  where external_key=:external_key
  order by requested_timestamp asc
  ;
>>

getTransitionForSubscription(subscription_id) ::= <<
  select
    total_ordering
  , bundle_id
  , external_key
  , account_id
  , account_key
  , subscription_id
  , requested_timestamp
  , event
  , prev_product_name
  , prev_product_type
  , prev_product_category
  , prev_slug
  , prev_phase
  , prev_billing_period
  , prev_price
  , prev_price_list
  , prev_mrr
  , prev_currency
  , prev_start_date
  , prev_state
  , next_product_name
  , next_product_type
  , next_product_category
  , next_slug
  , next_phase
  , next_billing_period
  , next_price
  , next_price_list
  , next_mrr
  , next_currency
  , next_start_date
  , next_state
  from bst
  where subscription_id = :subscription_id
  order by requested_timestamp asc
  ;
>>

createTransition() ::= <<
  insert into bst(
    total_ordering
  , bundle_id
  , external_key
  , account_id
  , account_key
  , subscription_id
  , requested_timestamp
  , event
  , prev_product_name
  , prev_product_type
  , prev_product_category
  , prev_slug
  , prev_phase
  , prev_billing_period
  , prev_price
  , prev_price_list
  , prev_mrr
  , prev_currency
  , prev_start_date
  , prev_state
  , next_product_name
  , next_product_type
  , next_product_category
  , next_slug
  , next_phase
  , next_billing_period
  , next_price
  , next_price_list
  , next_mrr
  , next_currency
  , next_start_date
  , next_state
  ) values (
    :total_ordering
  , :bundle_id
  , :external_key
  , :account_id
  , :account_key
  , :subscription_id
  , :requested_timestamp
  , :event
  , :prev_product_name
  , :prev_product_type
  , :prev_product_category
  , :prev_slug
  , :prev_phase
  , :prev_billing_period
  , :prev_price
  , :prev_price_list
  , :prev_mrr
  , :prev_currency
  , :prev_start_date
  , :prev_state
  , :next_product_name
  , :next_product_type
  , :next_product_category
  , :next_slug
  , :next_phase
  , :next_billing_period
  , :next_price
  , :next_price_list
  , :next_mrr
  , :next_currency
  , :next_start_date
  , :next_state
  );
>>

deleteTransitionsForBundle(bundle_id) ::= <<
  delete from bst
  where bundle_id=:bundle_id
  ;
>>

test() ::= <<
  select 1 from bst;
>>
