Issue: Does migrate() run every time I run server(Ex. flask server)
Author: NPZYMR
State: closed
Created: 2023-02-24 21:54:40

Suppose I've added create_table(fail_silently=True) when the flask server starts.
In this model class below, I want to add the column "test" in my DB corresponding field "test_field".

Question:
1. Do I need to add "test_field" in my model **EmpDetails** or outside the **class** or both(like I did in the code snippet) for migrations?
2. As I've created create_function() to run at the server(flask server) start, will it migrate() every time at the start of the server(_I need to make sure whether data added in the new column will not vanish if migrate runs every time_).
or if the schema changes are done in the database will it not rerun the migration? 

Code snippet for my model class:
```
class EmpDetails(Base):
    id = CharField(25, null=False)
    email = CharField(255, unique=True)
    name = CharField(255)
    location = CharField(255)
    created_at = DateTimeField(default=datetime.datetime.now)
    updated_at = DateTimeField(default=datetime.datetime.now)
    is_active = BooleanField(default=True)
    role = CharField(25)
    test_field = CharField(default='')   -------> here 

    class Meta:
        table_name = 'emp_details'

test_field = CharField(default='')     ---------> here
migrate(
    
     migrator.add_column(EmpDetails._meta.emp_details, 'test', test_field),
 )
```

Let's say you want to add a column to a model in an application. The typical process would be to:

1. Stop the application.
2. Update the schema of the database by running a migration once.
3. Restart the application with the new field present in the code.

You can always add a "check" in your `create_...()` function to see if the column exists, and if not, do the migration. This would only be necessary if you have lots of instances of your application running around which will be updated at some unknown time, e.g., this is a library or something.

> Let's say you want to add a column to a model in an application. The typical process would be to:
> 
> 1. Stop the application.
> 2. Update the schema of the database by running a migration once.
> 3. Restart the application with the new field present in the code.
> 
> You can always add a "check" in your `create_...()` function to see if the column exists, and if not, do the migration. This would only be necessary if you have lots of instances of your application running around which will be updated at some unknown time, e.g., this is a library or something.


I want to make sure that the Production env runs smoothly as whatever migrations I've written will be applied once, stopping production env won't be a good option very time.

what you mentioned about the check I didn't get that properly.

I'm calling create _table like this:

```
def create_tables():
    
    EmpDetails.create_table(fail_silently=True)

I'm going to add multiple models here
```

and calling this create_tables() at server start.


Secondly, where to add that new column field (Question 1)?

I think you are imagining you buy some security by doing it this way but you really do not. Schema changes are one-time operations. It's probably best to put them in standalone scripts and run them when you deploy application changes.