62 lines
1.3 KiB
Python
62 lines
1.3 KiB
Python
from peewee import (
|
|
AutoField,
|
|
DateField,
|
|
IntegerField,
|
|
IntegrityError,
|
|
Model,
|
|
MySQLDatabase,
|
|
SqliteDatabase,
|
|
)
|
|
|
|
from config import conf
|
|
|
|
backend = conf["db"].get("backend", None)
|
|
if backend == "sqlite":
|
|
# SQLite database
|
|
db = SqliteDatabase(conf["sqlite"].get("filename", "sqlite.db"))
|
|
elif backend == "mysql":
|
|
db = MySQLDatabase(
|
|
conf["mysql"].get("database"),
|
|
user=conf["mysql"].get("user"),
|
|
password=conf["mysql"].get("password"),
|
|
host=conf["mysql"].get("host", "localhost"),
|
|
port=conf["mysql"].getint("port", 3306),
|
|
)
|
|
else:
|
|
db = None
|
|
|
|
|
|
class BaseModel(Model):
|
|
class Meta:
|
|
database = db
|
|
|
|
|
|
class Price(BaseModel):
|
|
id = AutoField()
|
|
date = DateField(unique=True)
|
|
price = IntegerField()
|
|
price500 = IntegerField()
|
|
|
|
|
|
def init():
|
|
db.connect()
|
|
db.create_tables([Price])
|
|
|
|
|
|
def migrate_001():
|
|
backend = conf["db"].get("backend", None)
|
|
if backend == "sqlite":
|
|
from playhouse.migrate import SqliteMigrator
|
|
|
|
migrator = SqliteMigrator(db)
|
|
elif backend == "mysql":
|
|
from playhouse.migrate import MySQLMigrator
|
|
|
|
migrator = MysqlMigrator(db)
|
|
|
|
from playhouse.migrate import migrate
|
|
|
|
price500_field = IntegerField(null=True)
|
|
with db.atomic():
|
|
migrate(migrator.add_column("price", "price500", price500_field))
|