147 lines
4.4 KiB
Python
147 lines
4.4 KiB
Python
|
|
"""Peewee migrations -- 001_init.py.
|
||
|
|
|
||
|
|
Some examples (model - class or model name)::
|
||
|
|
|
||
|
|
> Model = migrator.orm['table_name'] # Return model in current state by name
|
||
|
|
> Model = migrator.ModelClass # Return model in current state by name
|
||
|
|
|
||
|
|
> migrator.sql(sql) # Run custom SQL
|
||
|
|
> migrator.run(func, *args, **kwargs) # Run python function with the given args
|
||
|
|
> migrator.create_model(Model) # Create a model (could be used as decorator)
|
||
|
|
> migrator.remove_model(model, cascade=True) # Remove a model
|
||
|
|
> migrator.add_fields(model, **fields) # Add fields to a model
|
||
|
|
> migrator.change_fields(model, **fields) # Change fields
|
||
|
|
> migrator.remove_fields(model, *field_names, cascade=True)
|
||
|
|
> migrator.rename_field(model, old_field_name, new_field_name)
|
||
|
|
> migrator.rename_table(model, new_table_name)
|
||
|
|
> migrator.add_index(model, *col_names, unique=False)
|
||
|
|
> migrator.add_not_null(model, *field_names)
|
||
|
|
> migrator.add_default(model, field_name, default)
|
||
|
|
> migrator.add_constraint(model, name, sql)
|
||
|
|
> migrator.drop_index(model, *col_names)
|
||
|
|
> migrator.drop_not_null(model, *field_names)
|
||
|
|
> migrator.drop_constraints(model, *constraints)
|
||
|
|
|
||
|
|
"""
|
||
|
|
|
||
|
|
from contextlib import suppress
|
||
|
|
|
||
|
|
import peewee as pw
|
||
|
|
from peewee_migrate import Migrator
|
||
|
|
|
||
|
|
|
||
|
|
with suppress(ImportError):
|
||
|
|
import playhouse.postgres_ext as pw_pext
|
||
|
|
|
||
|
|
|
||
|
|
def migrate(migrator: Migrator, database: pw.Database, *, fake=False):
|
||
|
|
"""Write your migrations here."""
|
||
|
|
|
||
|
|
@migrator.create_model
|
||
|
|
class BaseModel(pw.Model):
|
||
|
|
id = pw.AutoField()
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
table_name = "basemodel"
|
||
|
|
|
||
|
|
@migrator.create_model
|
||
|
|
class User(pw.Model):
|
||
|
|
id = pw.AutoField()
|
||
|
|
telegram_username = pw.TextField(unique=True)
|
||
|
|
telegram_chat_id = pw.IntegerField()
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
table_name = "user"
|
||
|
|
|
||
|
|
@migrator.create_model
|
||
|
|
class DomSubUsers(pw.Model):
|
||
|
|
id = pw.AutoField()
|
||
|
|
dom = pw.ForeignKeyField(column_name='dom_id', field='id', model=migrator.orm['user'])
|
||
|
|
sub = pw.ForeignKeyField(column_name='sub_id', field='id', model=migrator.orm['user'])
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
table_name = "dom_sub"
|
||
|
|
indexes = [(('dom_id', 'sub_id'), True)]
|
||
|
|
|
||
|
|
@migrator.create_model
|
||
|
|
class Order(pw.Model):
|
||
|
|
id = pw.AutoField()
|
||
|
|
name = pw.TextField()
|
||
|
|
weight = pw.IntegerField()
|
||
|
|
repeat = pw.FloatField()
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
table_name = "order"
|
||
|
|
|
||
|
|
@migrator.create_model
|
||
|
|
class OrdersPool(pw.Model):
|
||
|
|
id = pw.AutoField()
|
||
|
|
name = pw.TextField()
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
table_name = "orders_pool"
|
||
|
|
|
||
|
|
@migrator.create_model
|
||
|
|
class PunishmentStatus(pw.Model):
|
||
|
|
id = pw.AutoField()
|
||
|
|
confirmed_at = pw.DateTimeField(null=True)
|
||
|
|
created_at = pw.DateTimeField()
|
||
|
|
mastodon_id = pw.TextField()
|
||
|
|
text = pw.TextField()
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
table_name = "punishment_status"
|
||
|
|
|
||
|
|
@migrator.create_model
|
||
|
|
class OrderStatus(pw.Model):
|
||
|
|
id = pw.AutoField()
|
||
|
|
confirmed_at = pw.DateTimeField(null=True)
|
||
|
|
created_at = pw.DateTimeField()
|
||
|
|
due_at = pw.DateTimeField()
|
||
|
|
mastodon_id = pw.TextField()
|
||
|
|
punishment = pw.ForeignKeyField(column_name='punishment_id', field='id', model=migrator.orm['punishment_status'], null=True)
|
||
|
|
text = pw.TextField()
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
table_name = "order_status"
|
||
|
|
|
||
|
|
@migrator.create_model
|
||
|
|
class Repeat(pw.Model):
|
||
|
|
id = pw.AutoField()
|
||
|
|
count = pw.IntegerField(default=0)
|
||
|
|
orders = pw.TextField()
|
||
|
|
probability = pw.FloatField()
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
table_name = "repeat"
|
||
|
|
|
||
|
|
@migrator.create_model
|
||
|
|
class SkipDay(pw.Model):
|
||
|
|
id = pw.AutoField()
|
||
|
|
date = pw.DateField(unique=True)
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
table_name = "skip_day"
|
||
|
|
|
||
|
|
|
||
|
|
def rollback(migrator: Migrator, database: pw.Database, *, fake=False):
|
||
|
|
"""Write your rollback migrations here."""
|
||
|
|
|
||
|
|
migrator.remove_model('skip_day')
|
||
|
|
|
||
|
|
migrator.remove_model('repeat')
|
||
|
|
|
||
|
|
migrator.remove_model('order_status')
|
||
|
|
|
||
|
|
migrator.remove_model('punishment_status')
|
||
|
|
|
||
|
|
migrator.remove_model('orders_pool')
|
||
|
|
|
||
|
|
migrator.remove_model('order')
|
||
|
|
|
||
|
|
migrator.remove_model('dom_sub')
|
||
|
|
|
||
|
|
migrator.remove_model('user')
|
||
|
|
|
||
|
|
migrator.remove_model('basemodel')
|