W
Wasp4w ago
Jorge

Rename DB column and migrate its data

Using prisma I had a column "balance" that I renamed "startBalance", and added a new one "currentBalance": The previous model: model TradingSession { id String @id @default(uuid()) createdAt DateTime @default(now()) ... balance Int ... } model TradingSession { id String @id @default(uuid()) createdAt DateTime @default(now()) ... startBalance Int currentBalance Int ... } How to handle migration of existing data for column balance to startBalance ?
3 Replies
kapa.ai
kapa.ai4w ago
Hi @Jorge. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To handle the migration of existing data from the "balance" column to "startBalance" in Prisma, you'll need to perform a data migration along with your schema changes. Here's a general approach you can follow: 1. First, create a new migration using the Wasp CLI command:
wasp db migrate-dev --name rename_balance_to_start_balance
wasp db migrate-dev --name rename_balance_to_start_balance
2. This will generate a new migration file. You'll need to modify this file to include both the schema changes and the data migration. 3. In the migration file, you'll want to add a step to copy the data from the old "balance" column to the new "startBalance" column before dropping the old column. Here's an example of what your migration file might look like:
-- Rename the column
ALTER TABLE "TradingSession" RENAME COLUMN "balance" TO "startBalance";

-- Add the new column
ALTER TABLE "TradingSession" ADD COLUMN "currentBalance" INTEGER NOT NULL DEFAULT 0;

-- Update the currentBalance to match startBalance for existing records
UPDATE "TradingSession" SET "currentBalance" = "startBalance";
-- Rename the column
ALTER TABLE "TradingSession" RENAME COLUMN "balance" TO "startBalance";

-- Add the new column
ALTER TABLE "TradingSession" ADD COLUMN "currentBalance" INTEGER NOT NULL DEFAULT 0;

-- Update the currentBalance to match startBalance for existing records
UPDATE "TradingSession" SET "currentBalance" = "startBalance";
4. After creating and modifying the migration file, you can apply it using:
wasp db migrate-dev
wasp db migrate-dev
It's important to note that Wasp uses Prisma under the hood for database operations, so the Prisma migration commands are available through the Wasp CLI. The wasp db migrate-dev command will apply your migrations to your development database. For more detailed information on database commands in Wasp, you can refer to the Wasp CLI Database Commands documentation. Remember to always backup your database before performing migrations, especially when dealing with data transformations. If you need to perform more complex data migrations, you might want to consider writing a separate script to handle the data transformation. This could be done using Prisma Client in a Node.js script, which you would run after applying the schema changes. Lastly, it's worth noting that there's an ongoing discussion in the Wasp community about improving support for data migrations. You can follow the progress on this in the GitHub issue about supporting data migrations. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Jorge, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
miho
miho4w ago
Did the bot's answer help you here?
Jorge
JorgeOP5d ago
Hi @miho, sorry for late answer but yes this is solved 👍🏼 thanks you

Did you find this page helpful?