005_add_updated_at.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. if isinstance(database, pw.SqliteDatabase):
  30. migrate_sqlite(migrator, database, fake=fake)
  31. else:
  32. migrate_external(migrator, database, fake=fake)
  33. def migrate_sqlite(migrator: Migrator, database: pw.Database, *, fake=False):
  34. # Adding fields created_at and updated_at to the 'chat' table
  35. migrator.add_fields(
  36. "chat",
  37. created_at=pw.DateTimeField(null=True), # Allow null for transition
  38. updated_at=pw.DateTimeField(null=True), # Allow null for transition
  39. )
  40. # Populate the new fields from an existing 'timestamp' field
  41. migrator.sql(
  42. "UPDATE chat SET created_at = timestamp, updated_at = timestamp WHERE timestamp IS NOT NULL"
  43. )
  44. # Now that the data has been copied, remove the original 'timestamp' field
  45. migrator.remove_fields("chat", "timestamp")
  46. # Update the fields to be not null now that they are populated
  47. migrator.change_fields(
  48. "chat",
  49. created_at=pw.DateTimeField(null=False),
  50. updated_at=pw.DateTimeField(null=False),
  51. )
  52. def migrate_external(migrator: Migrator, database: pw.Database, *, fake=False):
  53. # Adding fields created_at and updated_at to the 'chat' table
  54. migrator.add_fields(
  55. "chat",
  56. created_at=pw.BigIntegerField(null=True), # Allow null for transition
  57. updated_at=pw.BigIntegerField(null=True), # Allow null for transition
  58. )
  59. # Populate the new fields from an existing 'timestamp' field
  60. migrator.sql(
  61. "UPDATE chat SET created_at = timestamp, updated_at = timestamp WHERE timestamp IS NOT NULL"
  62. )
  63. # Now that the data has been copied, remove the original 'timestamp' field
  64. migrator.remove_fields("chat", "timestamp")
  65. # Update the fields to be not null now that they are populated
  66. migrator.change_fields(
  67. "chat",
  68. created_at=pw.BigIntegerField(null=False),
  69. updated_at=pw.BigIntegerField(null=False),
  70. )
  71. def rollback(migrator: Migrator, database: pw.Database, *, fake=False):
  72. """Write your rollback migrations here."""
  73. if isinstance(database, pw.SqliteDatabase):
  74. rollback_sqlite(migrator, database, fake=fake)
  75. else:
  76. rollback_external(migrator, database, fake=fake)
  77. def rollback_sqlite(migrator: Migrator, database: pw.Database, *, fake=False):
  78. # Recreate the timestamp field initially allowing null values for safe transition
  79. migrator.add_fields("chat", timestamp=pw.DateTimeField(null=True))
  80. # Copy the earliest created_at date back into the new timestamp field
  81. # This assumes created_at was originally a copy of timestamp
  82. migrator.sql("UPDATE chat SET timestamp = created_at")
  83. # Remove the created_at and updated_at fields
  84. migrator.remove_fields("chat", "created_at", "updated_at")
  85. # Finally, alter the timestamp field to not allow nulls if that was the original setting
  86. migrator.change_fields("chat", timestamp=pw.DateTimeField(null=False))
  87. def rollback_external(migrator: Migrator, database: pw.Database, *, fake=False):
  88. # Recreate the timestamp field initially allowing null values for safe transition
  89. migrator.add_fields("chat", timestamp=pw.BigIntegerField(null=True))
  90. # Copy the earliest created_at date back into the new timestamp field
  91. # This assumes created_at was originally a copy of timestamp
  92. migrator.sql("UPDATE chat SET timestamp = created_at")
  93. # Remove the created_at and updated_at fields
  94. migrator.remove_fields("chat", "created_at", "updated_at")
  95. # Finally, alter the timestamp field to not allow nulls if that was the original setting
  96. migrator.change_fields("chat", timestamp=pw.BigIntegerField(null=False))