Remove IPAddress model, add User model

This commit is contained in:
2024-11-07 11:03:21 -06:00
parent 04ed915f28
commit 361fc13741
2 changed files with 76 additions and 5 deletions

View File

@@ -0,0 +1,66 @@
"""Peewee migrations -- 004_create_user_remove_ipaddress.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 User(pw.Model):
id = pw.AutoField()
email = pw.CharField(max_length=45, unique=True)
password_hash = pw.CharField(max_length=96)
created_at = pw.DateTimeField()
updated_at = pw.DateTimeField()
class Meta:
table_name = "user"
migrator.remove_model('ipaddress')
def rollback(migrator: Migrator, database: pw.Database, *, fake=False):
"""Write your rollback migrations here."""
@migrator.create_model
class IPAddress(pw.Model):
ip = pw.CharField(max_length=255, primary_key=True)
last_seen = pw.DateTimeField()
count = pw.IntegerField(default=0)
class Meta:
table_name = "ipaddress"
migrator.remove_model('user')

View File

@@ -3,10 +3,11 @@ This module defines the database models for the LinkPulse backend.
It also provides a base model with database connection details. It also provides a base model with database connection details.
""" """
from datetime import datetime
from os import getenv from os import getenv
import structlog import structlog
from peewee import CharField, DateTimeField, IntegerField, Model from peewee import CharField, DateTimeField, IntegerField, AutoField, Model
from playhouse.db_url import connect from playhouse.db_url import connect
logger = structlog.get_logger() logger = structlog.get_logger()
@@ -26,7 +27,11 @@ class BaseModel(Model):
database = connect(url=_get_database_url()) database = connect(url=_get_database_url())
class IPAddress(BaseModel): class User(BaseModel):
ip = CharField(primary_key=True) id = AutoField(primary_key=True)
last_seen = DateTimeField() # timezone naive # arbitrary max length, but statistically reasonable and limits UI concerns/abuse cases
count = IntegerField(default=0) email = CharField(unique=True, max_length=45)
# full hash with encoded salt/parameters, argon2 but assume nothing
password_hash = CharField(max_length=96)
created_at = DateTimeField(default=datetime.now)
updated_at = DateTimeField(default=datetime.now)