cygnal/scripts/migrations/0.3.py
Phyks (Lucas Verney) a7792f5dbb Rework upvotes
Upvotes now reset the `datetime` field of the report, making it appear
as if it was newly reported. They also extend the lifetime for accidents
and GCUM.

This introduces database migrations as well.

Closes https://framagit.org/phyks/cyclassist/issues/41.
2018-09-12 13:51:53 +02:00

42 lines
1002 B
Python

#!/usr/bin/env python
"""
Database migration from < 0.3 to 0.3 version.
"""
import os
import sys
import peewee
SCRIPT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.abspath(os.path.join(SCRIPT_DIRECTORY, '..', '..')))
from playhouse.migrate import *
from server.models import db, Report
from server.tools import UTC_now
def run_migration():
if type(db) == peewee.SqliteDatabase:
migrator = SqliteMigrator(db)
elif type(db) == peewee.MySQLDatabase:
migrator = MySQLMigrator(db)
elif type(db) == peewee.PostgresqlDatabase:
migrator = PostgresqlMigrator(db)
else:
return
migrate(
migrator.add_column('report', 'first_report_datetime',
peewee.DateTimeField(default=UTC_now)),
)
query = Report.select()
for report in query:
report.first_report_datetime = report.datetime
report.save()
if __name__ == '__main__':
db.connect()
run_migration()