class Admin(db.Schema): __tablename__ = "admins" id = Field(int, null=False) organisation_id = Field(int) user_id = Field(int) user = Association.has_one(User, on=["user_id", "id"])
class RiderMigration(Schema): migration = Field(str, null=False, primary_key=True) applied_at = Field(str) @classmethod def new(cls, migration): return cls.insert({ "migration": migration, "applied_at": datetime.now() })
class Organisation(db.Schema): __tablename__ = "organisations" id = Field(int, null=False) name = Field(str, null=False) admin = Association.has_one("tests.base.Admin", on=["id", "organisation_id"]) users = Association.has_many(User, on=["id", "organisation_id"])
class Data(db.Schema): """For testing wildcard select.""" __tablename__ = "data" _allow_wildcard_select = False id = Field(int, null=False) user_id = Field(int) value = Field(str)
class User(db.Schema): __tablename__ = "users" id = Field(int, null=False) organisation_id = Field(int, null=True) name = Field(str, null=False, default="default name") data = Association.has_many(Data, on=["id", "user_id"]) @classmethod def new(cls, org_id=None, name=None): return cls.insert({cls.organisation_id: org_id, cls.name: name}) @classmethod def get_by_name(cls, name): return Query(cls).get_or_none().where(cls.name == name).execute()
class User(BaseSchema): __tablename__ = "users" email = Field(str) password = Field(str) type = Field(str, default="user") @classmethod def get_by_email(cls, email): user = ( Query(cls) .get_or_none() .where(cls.email == email, op.is_null(cls.tombstoned)) .execute() ) return user
class BaseSchema(db.Schema): id = Field(int, primary_key=True) created = Field(datetime.datetime, caster=cast_date) updated = Field(datetime.datetime, caster=cast_date) tombstoned = Field(datetime.datetime, caster=cast_date, default=None) @classmethod def update(cls, old, new): new["updated"] = datetime.datetime.now() return super().update(old, new) @classmethod def insert(cls, data): data["updated"] = datetime.datetime.now() data["created"] = datetime.datetime.now() return super().insert(data) @classmethod def update_eh(cls, *args, **kwargs): try: return super().update(*args, **kwargs), None except FieldError as e: return None, e.args[0] @classmethod def insert_eh(cls, *args, **kwargs): try: return super().insert(*args, **kwargs), None except FieldError as e: return None, e.args[0] @classmethod def get_by_id(cls, id): return ( Query(cls) .select() .where(cls.id == id, fn.is_null(cls.tombstoned)) .execute() )
class RiderLog(Schema): id = Field(str, null=False, primary_key=True) migration = Field(str, null=False) operation = Field(str, null=False) username = Field(str, null=False) hostname = Field(str, null=False) time = Field(str, null=False) @classmethod def new(cls, migration, operation): return cls.insert({ "id": str(uuid.uuid4()), "migration": migration, "operation": operation, "username": getpass.getuser(), "hostname": socket.gethostname(), "time": datetime.now(), })
class NoPK(db.Schema): """Table without primary key""" __tablename__ = "nopk" not_id = Field(int)