crafty-4/DBCHANGES.md

72 lines
1.8 KiB
Markdown
Raw Normal View History

2021-08-18 15:11:53 +00:00
# Database change guide for contributors
When updating a database schema modify the schema in `app/classes/shared/models.py` and create a new migration with the `migration add <name>` command (in Crafty's prompt).
A full list of helper functions you can find in `app/classes/shared/models.py`
## Example migration files
### Rename column/field
```py
def migrate(migrator, database, **kwargs):
migrator.rename_column('my_table', 'old_name', 'new_name') # First argument can be model class OR table name
def rollback(migrator, database, **kwargs):
migrator.rename_column('my_table', 'new_name', 'old_name') # First argument can be model class OR table name
```
### Rename table/model
```py
def migrate(migrator, database, **kwargs):
migrator.rename_table('old_name', 'new_name') # First argument can be model class OR table name
def rollback(migrator, database, **kwargs):
migrator.rename_table('new_name', 'old_name') # First argument can be model class OR table name
```
### Create table/model
```py
import peewee
def migrate(migrator, database, **kwargs):
class NewTable(peewee.Model):
my_id = peewee.IntegerField(unique=True, primary_key=True)
class Meta:
table_name = 'new_table'
database = database
create_table(NewTable)
def rollback(migrator, database, **kwargs):
drop_table('new_table') # Can be model class OR table name
```
### Add columns/fields
```py
import peewee
def migrate(migrator, database, **kwargs):
migrator.add_columns('table_name', new_field_name=peewee.CharField(default="")) # First argument can be model class OR table name
def rollback(migrator, database, **kwargs):
migrator.drop_columns('table_name', ['new_field_name']) # First argument can be model class OR table name
```