crafty-4/DBCHANGES.md
Silversthorn c668b74ed8 Update DBCHANGES.md
Corrections and Improvement for Create table exemple
2021-08-22 10:00:39 +00:00

1.9 KiB

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 migrations 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

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

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

import peewee


def migrate(migrator, database, **kwargs):
    db = database
    #Copy Paste here the class of the New Table from models.py
    class NewTable(peewee.Model):
        my_id = peewee.IntegerField(unique=True, primary_key=True)

        class Meta:
            table_name = 'new_table'
            database = db
            
    migrator.create_table(NewTable)



def rollback(migrator, database, **kwargs):
    migrator.drop_table('new_table') # Can be model class OR table name

Add columns/fields

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