how to prevent data lose when performing migration in prisma - sql

I have a user's schema: name, age, etc...
A user signs up in production,
I then decide I'd also like to capture "email" and save it in the User's table.
but since the previous records didn't have the email rece, prisma will want me to reset database
is there any walk around to prevent this database reset when migrating in prisma.js

A workaround might be manually adding a new field into your database, then introspecting your database, meaning pulling database structure in your prisma schema. After adding the field in your table, run npx prisma db pull. This should update your Prisma schema file. You might also need to regenerate prisma client using npx prisma generate.
Please also look here and here

Related

Flyway vs Prisma Migrations

I'm migrating an existing TypeORM + PostgresQL project from TypeORM to Prisma. This existing project was handling migrations with Flyway: I wrote the SQL scripts to change the DB and those script were executed against de DB via Flyway. Now that I'm using prisma, I would like to take advantage of prisma migration. However, I would still like to have full control of the migrations, and to that extent, I would like to keep using Flyway. My idea was to generate the SQL files with prisma and then use Flyway to run them against de DB. What I've read so far, is that prisma cannot be used to just generate migrations (it will run them eventually, even if I use the --create-only flag, as discussed in here). I found in the help of prisma cli the prisma migrate diff command and I saw that it receives two urls to compare the schemas of two databases and generate de diff as an SQL file. I was wondering if there is something like prisma migrate diff that receives a url and a schema.prisma file so I can generate the diff using the url to my DB and my current schema file. Or is there any other way to just generate prisma migrations without executing them?
Now, that is what I would prefer. In case there is no way to just generate prisma migrations, I think it's fine to use it to run the migrations. However, I'm a little concerned on possible conflicts between the Flyway migrations that have been executed in the past and the new prisma migrations. I know both of them create tables in the DB to keep track of the migrations and Flyway's and Prisma's table should be different, so there should be no problems. But I don't feel confident about this. Has anyone migrate from Flyway to Prisma than can give me some advise on this? Or can I just ignore Flyway migrations.
 I was wondering if there is something like prisma migrate diff that receives a url and a schema.prisma file so I can generate the diff using the url to my DB and my current schema file
Yes. You can point prisma migrate diff to your Prisma schema as the "to" state and your DATABASE_URL as the "from" state as follows:
npx prisma migrate diff \
-—from-schema-datamodel ./prisma/schema.prisma # path to your Prisma schema
-—to-url $DATABASE_URL # enviroment variable
-—script diff-migration.sql
You can then create a flyway migration using the generated SQL from prisma migrate diff.
You can learn more about the different arguments prisma migrate diff accepts in our CLI reference. Don't hesitate to let me know if you hit a snag. :)

Sequelize model to SQL

I have a project which uses sequelize to communicate with part of a database. The project should not have the responsibility to update the database structure so no sequelize syncing or migrations are being used.
I got asked to find a way to still be able to generate the database from the sequelize models. The ideia is to be able to update models and run a command that generates an SQL with the necessary queries to create the tables, based on the sequelize models. Not even asking for migrations, only the table creation.
Any solution?

How to store and manage a sql schema, with the ability to update and insert data

I am wondering what the best way (or any way) to manage a database schema. I have a sql file with a bunch of statements like CREATE TABLE Users { id SERIAL PRIMARY KEY ....}; which represents the schema for my database.
I have postgres installed on my dev machine however am having trouble syncing the changes I make to the schema with the local database. Currently I just run drop the entire database and run the schema file on the database.
I figure there has to be a better way I don't know about. I will also need to be able to set a database up for production when the project becomes more stabilized and obviously dropping a table in production wont work.
Suggestions?

Schema Change/Update script for Database deploy

I have a need to change the database schema . I'm planning to write Schema change and update scripts for tracking database changes and updating them. I followed
Versioning Databases – Change Scripts
for a start, I got a gist of what he is getting at however since I haven't worked much on SQL scripts before, a tutorial or something to start with would be good. I did some research on the web and came to know that most people use Automatic comparing tools to generate the script which I don't want to do for obvious reason that I won't learn the anything in the process.
I'm looking for some tutorials/links on How to write Change scripts and Update scripts ? Especially update scripts as I couln't find even a single script/pseudo-code on how to do update schema by comparing SchemaChangeLog table, connecting to the table using scripts...
Thanks in advance!
I would recommend using a database migration tool like liquibase.
Each change to the database is captured as a changeset and liquibase will automatically keep track of which changesets have been applied to the database, enabling updates and rollbacks.

Execute code first migration in code?

I'm working on a multi-tenant MVC 4 application and I'm going with the one schema per customer approach. I'd like to run the database migrations in code when a customer signs up. Is this possible using EF 5/Code First Migrations?
So when a customer signs up, I'll create an account in dbo. I'll then check if their subdomain exists as a schema in the database, if not, I'll create the schema and ideally run the migrations.
Thanks!
Clarification
When I create the new schema for the customer in the database, I want to run the migrations for that new schema. So for example,
I'll have schema1.Products and schema2.Products.
If I'm getting it right what you want...
You could use something like this
var migrator = new DbMigrator(new Configuration());
if (migrator.GetPendingMigrations().Any())
migrator.Update();
Or even better you may want to create your own initializer - e.g. check these posts of mine...
What is the correct use of IDatabaseInitializer in EF?
How to create initializer to create and migrate mysql database?
The problem I see with your approach is that'd you'd have to 'separate' the account model/database - and the one that you're trying to migrate. Since you mentioned multi-tenant that may already be the case.
But I guess you could also crete the 'base entities' for accounts etc. - and then migrate the rest on top. That's a bit complex scenario - the model is created once (per connection) on start (first use) and cached from then on. So the only for that would be to restart reload, I think (don't hold my word for it, just thinking out loud here)
Not sure if this is what you're asking for, you can run code migrations from command line:
packages\EntityFramework.5.0.0\tools\migrate.exe Example.dll /startUpDirectory:Example\bin
So in theory you could call this whenever a new customer signed up.