005_add_updated_at.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Peewee migrations -- 002_add_local_sharing.py.
  2. Some examples (model - class or model name)::
  3. > Model = migrator.orm['table_name'] # Return model in current state by name
  4. > Model = migrator.ModelClass # Return model in current state by name
  5. > migrator.sql(sql) # Run custom SQL
  6. > migrator.run(func, *args, **kwargs) # Run python function with the given args
  7. > migrator.create_model(Model) # Create a model (could be used as decorator)
  8. > migrator.remove_model(model, cascade=True) # Remove a model
  9. > migrator.add_fields(model, **fields) # Add fields to a model
  10. > migrator.change_fields(model, **fields) # Change fields
  11. > migrator.remove_fields(model, *field_names, cascade=True)
  12. > migrator.rename_field(model, old_field_name, new_field_name)
  13. > migrator.rename_table(model, new_table_name)
  14. > migrator.add_index(model, *col_names, unique=False)
  15. > migrator.add_not_null(model, *field_names)
  16. > migrator.add_default(model, field_name, default)
  17. > migrator.add_constraint(model, name, sql)
  18. > migrator.drop_index(model, *col_names)
  19. > migrator.drop_not_null(model, *field_names)
  20. > migrator.drop_constraints(model, *constraints)
  21. """
  22. from contextlib import suppress
  23. import peewee as pw
  24. from peewee_migrate import Migrator
  25. with suppress(ImportError):
  26. import playhouse.postgres_ext as pw_pext
  27. def migrate(migrator: Migrator, database: pw.Database, *, fake=False):
  28. """Write your migrations here."""
  29. # Adding fields created_at and updated_at to the 'chat' table
  30. migrator.add_fields(
  31. "chat",
  32. created_at=pw.DateTimeField(null=True), # Allow null for transition
  33. updated_at=pw.DateTimeField(null=True), # Allow null for transition
  34. )
  35. # Populate the new fields from an existing 'timestamp' field
  36. migrator.sql(
  37. "UPDATE chat SET created_at = timestamp, updated_at = timestamp WHERE timestamp IS NOT NULL"
  38. )
  39. # Now that the data has been copied, remove the original 'timestamp' field
  40. migrator.remove_fields("chat", "timestamp")
  41. # Update the fields to be not null now that they are populated
  42. migrator.change_fields(
  43. "chat",
  44. created_at=pw.DateTimeField(null=False),
  45. updated_at=pw.DateTimeField(null=False),
  46. )
  47. def rollback(migrator: Migrator, database: pw.Database, *, fake=False):
  48. """Write your rollback migrations here."""
  49. # Recreate the timestamp field initially allowing null values for safe transition
  50. migrator.add_fields("chat", timestamp=pw.DateTimeField(null=True))
  51. # Copy the earliest created_at date back into the new timestamp field
  52. # This assumes created_at was originally a copy of timestamp
  53. migrator.sql("UPDATE chat SET timestamp = created_at")
  54. # Remove the created_at and updated_at fields
  55. migrator.remove_fields("chat", "created_at", "updated_at")
  56. # Finally, alter the timestamp field to not allow nulls if that was the original setting
  57. migrator.change_fields("chat", timestamp=pw.DateTimeField(null=False))