diff --git a/contracts/order_details.yaml b/contracts/order_details.yaml new file mode 100644 index 0000000..c54013f --- /dev/null +++ b/contracts/order_details.yaml @@ -0,0 +1,23 @@ +id: 7b7eabe6-a52a-4e19-8bf3-9015eadbbed0 +dataAssetResourceName: postgres://prod.store.com:5432:tutorial.public.order_details +spec-version: 0.1.0 +name: OrderDetails +namespace: Tutorial +doc: Details of items for an order. Each row represents the different items that were part of an order including their quantity and price. +owner: chadgable@gable.ai +schema: + - name: order_id + doc: The identifier of the order the item is associated with + type: int32 + - name: product_id + doc: The identifier of the product that was ordered + type: int32 + - name: quantity + doc: The quantity of the product in the order + type: int32 + - name: total_price + doc: The total price of the item in the order + type: decimal256 + - name: vendor_id + doc: The identifier of the vendor that the product was ordered from + type: int32 diff --git a/db_migrations/versions/7a341d712aa5_order_detail_changes.py b/db_migrations/versions/7a341d712aa5_order_detail_changes.py new file mode 100644 index 0000000..c4868e3 --- /dev/null +++ b/db_migrations/versions/7a341d712aa5_order_detail_changes.py @@ -0,0 +1,51 @@ +"""order_detail changes + +Revision ID: 7a341d712aa5 +Revises: 78da3a78bfd6 +Create Date: 2023-09-29 13:22:35.204135 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '7a341d712aa5' +down_revision: Union[str, None] = '78da3a78bfd6' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # Make the vendor optional + op.alter_column( + 'order_details', + 'vendor_id', + nullable=True, + ) + + # Convert existing values to integers (for example, if representing cents) + op.execute('UPDATE order_details SET unit_price = ROUND(unit_price * 100)') + op.execute('UPDATE order_details SET total_price = ROUND(total_price * 100)') + + # Alter the column type + op.alter_column('order_details', 'unit_price', type_=sa.Integer) + op.alter_column('order_details', 'total_price', type_=sa.Integer) + + +def downgrade() -> None: + op.alter_column( + 'order_details', + 'vendor_id', + nullable=False, + ) + + # Alter the column type back to Numeric + op.alter_column('order_details', 'unit_price', type_=sa.Numeric(10, 2)) + op.alter_column('order_details', 'total_price', type_=sa.Numeric(10, 2)) + + # Convert integer values back to their decimal representations + op.execute('UPDATE order_details SET unit_price = total_price / 100.0') + op.execute('UPDATE order_details SET total_price = total_price / 100.0')