User is looking for a way to move data from one table to another while generating multiple rows based on a quantity column. They suggest a method using a Node JS function to automate this process, indicating a need for a built-in feature to simplify this task.
Hi CONTEXT: I have a small project where the user can book free vouchers/tickets and then redeem them one by one. MY CURRENT DATABASE STRUCTURE: I have a Postgres database on Supabase. I have 2 tables as follows (including just relevant columns in each table): **orders**: ``` - id bigint - quantity integer not null ``` **redeemable_tickets**: ``` - id uuid primary key - secret_token uuid - quantity int not null - redeemed_quantity int not null default 0 - last_redeemed_quantity_at timestamp with time zone - order_id references orders.id ``` Originally, and currently, when the user books something, they can select a quantity. When they redeem, then we increment the `redeemed_quantity` until it reaches the quantity. Then they cannot redeem any longer (fully redeemed). This approach worked in the beginning, but as you can see, repetitive (quantity repeating on orders and on redeemable_tickets) and limiting since we can see the latest redeeming timestamp only. However, as requirements and plans changed, now we need a new structure. Now, we have a new table called `tickets` with these columns: ``` - id uuid primary key - secret_token uuid - ticket_status_id references ticket_statuses.id - order_id references orders.id - updated_at timestamp with time zone ``` Following this new system, instead of creating 1 row per booking and then tracking the number of redemptions through the columns of `quantity` and `redeemed_quantity`, no we create one row per quantity. This means that if a user places an order with quantity of 5, the database creates 5 rows in the `tickets` table. Like this, each ticket has to be redeemed individually and like this, we can clearly see which ticket is redeemed and at what datetime exactly. WHAT I NEED TO DO: I have about 2k rows in the `redeemable_tickets` table. I need to move them to the new `tickets` table. My main concern is how to generate tickets based on the quantity. Should I just write a Node JS function that select all the `redeemable_tickets` rows, and then uses a loop to create X amount of rows in the new `tickets` table based on the `quantity` column? Would that be the wisest simplest approach? Thanks a lot