006_migrate_timestamps_and_charfields.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """Peewee migrations -- 006_migrate_timestamps_and_charfields.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. # Alter the tables with timestamps
  30. migrator.change_fields(
  31. "chatidtag",
  32. timestamp=pw.BigIntegerField(),
  33. )
  34. migrator.change_fields(
  35. "document",
  36. timestamp=pw.BigIntegerField(),
  37. )
  38. migrator.change_fields(
  39. "modelfile",
  40. timestamp=pw.BigIntegerField(),
  41. )
  42. migrator.change_fields(
  43. "prompt",
  44. timestamp=pw.BigIntegerField(),
  45. )
  46. migrator.change_fields(
  47. "user",
  48. timestamp=pw.BigIntegerField(),
  49. )
  50. # Alter the tables with varchar to text where necessary
  51. migrator.change_fields(
  52. "auth",
  53. password=pw.TextField(),
  54. )
  55. migrator.change_fields(
  56. "chat",
  57. title=pw.TextField(),
  58. )
  59. migrator.change_fields(
  60. "document",
  61. title=pw.TextField(),
  62. filename=pw.TextField(),
  63. )
  64. migrator.change_fields(
  65. "prompt",
  66. title=pw.TextField(),
  67. )
  68. migrator.change_fields(
  69. "user",
  70. profile_image_url=pw.TextField(),
  71. )
  72. def rollback(migrator: Migrator, database: pw.Database, *, fake=False):
  73. """Write your rollback migrations here."""
  74. if isinstance(database, pw.SqliteDatabase):
  75. # Alter the tables with timestamps
  76. migrator.change_fields(
  77. "chatidtag",
  78. timestamp=pw.DateField(),
  79. )
  80. migrator.change_fields(
  81. "document",
  82. timestamp=pw.DateField(),
  83. )
  84. migrator.change_fields(
  85. "modelfile",
  86. timestamp=pw.DateField(),
  87. )
  88. migrator.change_fields(
  89. "prompt",
  90. timestamp=pw.DateField(),
  91. )
  92. migrator.change_fields(
  93. "user",
  94. timestamp=pw.DateField(),
  95. )
  96. migrator.change_fields(
  97. "auth",
  98. password=pw.CharField(max_length=255),
  99. )
  100. migrator.change_fields(
  101. "chat",
  102. title=pw.CharField(),
  103. )
  104. migrator.change_fields(
  105. "document",
  106. title=pw.CharField(),
  107. filename=pw.CharField(),
  108. )
  109. migrator.change_fields(
  110. "prompt",
  111. title=pw.CharField(),
  112. )
  113. migrator.change_fields(
  114. "user",
  115. profile_image_url=pw.CharField(),
  116. )