def __init__(self, default: TimestampArg = TimestampNow(), **kwargs) -> None: self._validate_default(default, TimestampArg.__args__) # type: ignore if isinstance(default, datetime): default = TimestampCustom.from_datetime(default) if default == datetime.now: default = TimestampNow() self.default = default kwargs.update({"default": default}) super().__init__(**kwargs)
class MyTableDefault(Table): """ A table containing all of the possible `default` arguments for `Timestamp`. """ created_on = Timestamp(default=TimestampNow())
def __init__(self, default: TimestampArg = TimestampNow(), **kwargs) -> None: self._validate_default(default, TimestampArg.__args__) # type: ignore if isinstance(default, datetime): if default.tzinfo is not None: raise ValueError( "Timestamp only stores timezone naive datetime objects - " "use Timestamptz instead.") default = TimestampCustom.from_datetime(default) if default == datetime.now: default = TimestampNow() self.default = default kwargs.update({"default": default}) super().__init__(**kwargs)
def deserialise_legacy_params(name: str, value: str) -> t.Any: """ Earlier versions of Piccolo serialised parameters differently. This is here purely for backwards compatibility. """ if name == "references": components = value.split("|") if len(components) == 1: class_name = components[0] tablename = None elif len(components) == 2: class_name, tablename = components else: raise ValueError( "Unrecognised Table serialisation - should either be " "`SomeClassName` or `SomeClassName|some_table_name`.") _Table: t.Type[Table] = type( class_name, (Table, ), {}, ) if tablename: _Table._meta.tablename = tablename return _Table ########################################################################### if name == "on_delete": enum_name, item_name = value.split(".") if enum_name == "OnDelete": return getattr(OnDelete, item_name) ########################################################################### if name == "on_update": enum_name, item_name = value.split(".") if enum_name == "OnUpdate": return getattr(OnUpdate, item_name) ########################################################################### if name == "default": if value in ("TimestampDefault.now", "DatetimeDefault.now"): return TimestampNow() else: try: _value = datetime.datetime.fromisoformat(value) except ValueError: pass else: return _value return value
class Migration(Table): name = Varchar(length=200) app_name = Varchar(length=200) ran_on = Timestamp(default=TimestampNow()) @classmethod async def get_migrations_which_ran(cls, app_name: t.Optional[str] = None ) -> t.List[str]: """ Returns the names of migrations which have already run, by inspecting the database. """ query = cls.select(cls.name, cls.ran_on).order_by(cls.ran_on) if app_name is not None: query = query.where(cls.app_name == app_name) return [i["name"] for i in await query.run()]
async def forwards(): manager = MigrationManager(migration_id=ID, app_name="forum") manager.add_table("Topic", tablename="topic") manager.add_column( table_class_name="Topic", tablename="topic", column_name="title", column_class_name="Varchar", params={ "length": 255, "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Topic", tablename="topic", column_name="description", column_class_name="Text", params={ "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Topic", tablename="topic", column_name="created", column_class_name="Timestamp", params={ "default": TimestampNow(), "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Topic", tablename="topic", column_name="category", column_class_name="ForeignKey", params={ "references": Category, "on_delete": OnDelete.cascade, "on_update": OnUpdate.cascade, "default": None, "null": True, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Topic", tablename="topic", column_name="topic_user", column_class_name="ForeignKey", params={ "references": BaseUser, "on_delete": OnDelete.cascade, "on_update": OnUpdate.cascade, "default": None, "null": True, "primary": False, "key": False, "unique": False, "index": False, }, ) return manager
async def forwards(): manager = MigrationManager(migration_id=ID, app_name="ads") manager.add_table("Ad", tablename="ad") manager.add_column( table_class_name="Ad", tablename="ad", column_name="title", column_class_name="Varchar", params={ "length": 255, "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Ad", tablename="ad", column_name="slug", column_class_name="Varchar", params={ "length": 255, "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Ad", tablename="ad", column_name="content", column_class_name="Text", params={ "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Ad", tablename="ad", column_name="created", column_class_name="Timestamp", params={ "default": TimestampNow(), "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Ad", tablename="ad", column_name="view", column_class_name="Integer", params={ "default": 0, "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Ad", tablename="ad", column_name="price", column_class_name="Integer", params={ "default": 0, "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Ad", tablename="ad", column_name="room", column_class_name="Integer", params={ "default": 0, "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Ad", tablename="ad", column_name="visitor", column_class_name="Integer", params={ "default": 0, "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Ad", tablename="ad", column_name="address", column_class_name="Varchar", params={ "length": 255, "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Ad", tablename="ad", column_name="city", column_class_name="Varchar", params={ "length": 255, "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Ad", tablename="ad", column_name="ad_user", column_class_name="ForeignKey", params={ "references": User, "on_delete": OnDelete.cascade, "on_update": OnUpdate.cascade, "default": None, "null": True, "primary": False, "key": False, "unique": False, "index": False, }, ) return manager
async def forwards(): manager = MigrationManager(migration_id=ID, app_name="ads") manager.add_table("Rent", tablename="rent") manager.add_table("Image", tablename="image") manager.add_table("Review", tablename="review") manager.add_table("Notification", tablename="notification") manager.add_column( table_class_name="Rent", tablename="rent", column_name="start_date", column_class_name="Timestamp", params={ "default": TimestampNow(), "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Rent", tablename="rent", column_name="end_date", column_class_name="Timestamp", params={ "default": TimestampNow(), "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Rent", tablename="rent", column_name="client", column_class_name="ForeignKey", params={ "references": User, "on_delete": OnDelete.cascade, "on_update": OnUpdate.cascade, "default": None, "null": True, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Rent", tablename="rent", column_name="ad_rent", column_class_name="ForeignKey", params={ "references": Ad, "on_delete": OnDelete.cascade, "on_update": OnUpdate.cascade, "default": None, "null": True, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Image", tablename="image", column_name="path", column_class_name="Varchar", params={ "length": 255, "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Image", tablename="image", column_name="ad_image", column_class_name="ForeignKey", params={ "references": Ad, "on_delete": OnDelete.cascade, "on_update": OnUpdate.cascade, "default": None, "null": True, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Review", tablename="review", column_name="content", column_class_name="Text", params={ "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Review", tablename="review", column_name="created", column_class_name="Timestamp", params={ "default": TimestampNow(), "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Review", tablename="review", column_name="review_grade", column_class_name="Integer", params={ "default": 0, "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Review", tablename="review", column_name="review_user", column_class_name="ForeignKey", params={ "references": User, "on_delete": OnDelete.cascade, "on_update": OnUpdate.cascade, "default": None, "null": True, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Review", tablename="review", column_name="ad", column_class_name="ForeignKey", params={ "references": Ad, "on_delete": OnDelete.cascade, "on_update": OnUpdate.cascade, "default": None, "null": True, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Notification", tablename="notification", column_name="message", column_class_name="Varchar", params={ "length": 150, "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Notification", tablename="notification", column_name="created", column_class_name="Timestamp", params={ "default": TimestampNow(), "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Notification", tablename="notification", column_name="is_read", column_class_name="Boolean", params={ "default": False, "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Notification", tablename="notification", column_name="sender", column_class_name="ForeignKey", params={ "references": User, "on_delete": OnDelete.cascade, "on_update": OnUpdate.cascade, "default": None, "null": True, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Notification", tablename="notification", column_name="recipient", column_class_name="ForeignKey", params={ "references": User, "on_delete": OnDelete.cascade, "on_update": OnUpdate.cascade, "default": None, "null": True, "primary": False, "key": False, "unique": False, "index": False, }, ) return manager
class MyTableDefault(Table): created_on = Timestamp(default=TimestampNow())
async def forwards(): manager = MigrationManager(migration_id=ID, app_name="questions") manager.add_table("Question", tablename="question") manager.add_column( table_class_name="Question", tablename="question", column_name="title", column_class_name="Varchar", params={ "length": 200, "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Question", tablename="question", column_name="slug", column_class_name="Varchar", params={ "length": 200, "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Question", tablename="question", column_name="description", column_class_name="Text", params={ "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Question", tablename="question", column_name="created_at", column_class_name="Timestamp", params={ "default": TimestampNow(), "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Question", tablename="question", column_name="view", column_class_name="Integer", params={ "default": 0, "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Question", tablename="question", column_name="question_like", column_class_name="Integer", params={ "default": 0, "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Question", tablename="question", column_name="accepted_answer", column_class_name="Boolean", params={ "default": False, "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Question", tablename="question", column_name="user", column_class_name="ForeignKey", params={ "references": User, "on_delete": OnDelete.cascade, "on_update": OnUpdate.cascade, "default": None, "null": True, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Question", tablename="question", column_name="category", column_class_name="ForeignKey", params={ "references": Category, "on_delete": OnDelete.cascade, "on_update": OnUpdate.cascade, "default": None, "null": True, "primary": False, "key": False, "unique": False, "index": False, }, ) return manager
async def forwards(): manager = MigrationManager( migration_id=ID, app_name="mega", description=DESCRIPTION ) manager.add_table("MegaTable", tablename="mega_table") manager.add_table("SmallTable", tablename="small_table") manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="bigint_col", column_class_name="BigInt", column_class=BigInt, params={ "default": 0, "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="boolean_col", column_class_name="Boolean", column_class=Boolean, params={ "default": False, "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="bytea_col", column_class_name="Bytea", column_class=Bytea, params={ "default": b"", "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="date_col", column_class_name="Date", column_class=Date, params={ "default": DateNow(), "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="foreignkey_col", column_class_name="ForeignKey", column_class=ForeignKey, params={ "references": SmallTable, "on_delete": OnDelete.cascade, "on_update": OnUpdate.cascade, "null": True, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="integer_col", column_class_name="Integer", column_class=Integer, params={ "default": 0, "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="interval_col", column_class_name="Interval", column_class=Interval, params={ "default": IntervalCustom( weeks=0, days=0, hours=0, minutes=0, seconds=0, milliseconds=0, microseconds=0, ), "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="json_col", column_class_name="JSON", column_class=JSON, params={ "default": "{}", "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="jsonb_col", column_class_name="JSONB", column_class=JSONB, params={ "default": "{}", "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="numeric_col", column_class_name="Numeric", column_class=Numeric, params={ "default": Decimal("0"), "digits": (5, 2), "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="real_col", column_class_name="Real", column_class=Real, params={ "default": 0.0, "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="double_precision_col", column_class_name="DoublePrecision", column_class=DoublePrecision, params={ "default": 0.0, "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="smallint_col", column_class_name="SmallInt", column_class=SmallInt, params={ "default": 0, "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="text_col", column_class_name="Text", column_class=Text, params={ "default": "", "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="timestamp_col", column_class_name="Timestamp", column_class=Timestamp, params={ "default": TimestampNow(), "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="timestamptz_col", column_class_name="Timestamptz", column_class=Timestamptz, params={ "default": TimestamptzNow(), "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="uuid_col", column_class_name="UUID", column_class=UUID, params={ "default": UUID4(), "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="varchar_col", column_class_name="Varchar", column_class=Varchar, params={ "length": 255, "default": "", "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="unique_col", column_class_name="Varchar", column_class=Varchar, params={ "length": 255, "default": "", "null": False, "primary_key": False, "unique": True, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="null_col", column_class_name="Varchar", column_class=Varchar, params={ "length": 255, "default": "", "null": True, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="MegaTable", tablename="mega_table", column_name="not_null_col", column_class_name="Varchar", column_class=Varchar, params={ "length": 255, "default": "", "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) manager.add_column( table_class_name="SmallTable", tablename="small_table", column_name="varchar_col", column_class_name="Varchar", column_class=Varchar, params={ "length": 255, "default": "", "null": False, "primary_key": False, "unique": False, "index": False, "index_method": IndexMethod.btree, "choices": None, }, ) return manager
async def forwards(): manager = MigrationManager(migration_id=ID, app_name="questions") manager.add_table("Answer", tablename="answer") manager.add_column( table_class_name="Answer", tablename="answer", column_name="content", column_class_name="Text", params={ "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Answer", tablename="answer", column_name="created_at", column_class_name="Timestamp", params={ "default": TimestampNow(), "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Answer", tablename="answer", column_name="answer_like", column_class_name="Integer", params={ "default": 0, "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Answer", tablename="answer", column_name="is_accepted_answer", column_class_name="Boolean", params={ "default": False, "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Answer", tablename="answer", column_name="ans_user", column_class_name="ForeignKey", params={ "references": User, "on_delete": OnDelete.cascade, "on_update": OnUpdate.cascade, "default": None, "null": True, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Answer", tablename="answer", column_name="question", column_class_name="ForeignKey", params={ "references": Question, "on_delete": OnDelete.cascade, "on_update": OnUpdate.cascade, "default": None, "null": True, "primary": False, "key": False, "unique": False, "index": False, }, ) return manager
async def forwards(): manager = MigrationManager(migration_id=ID, app_name="blog") manager.add_table("Post", tablename="post") manager.add_column( table_class_name="Post", tablename="post", column_name="title", column_class_name="Varchar", params={ "length": 255, "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Post", tablename="post", column_name="content", column_class_name="Text", params={ "default": "", "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Post", tablename="post", column_name="published", column_class_name="Boolean", params={ "default": False, "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) manager.add_column( table_class_name="Post", tablename="post", column_name="created_on", column_class_name="Timestamp", params={ "default": TimestampNow(), "null": False, "primary": False, "key": False, "unique": False, "index": False, }, ) return manager