001_initial_schema.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. """Peewee migrations -- 001_initial_schema.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. @migrator.create_model
  30. class Auth(pw.Model):
  31. id = pw.CharField(max_length=255, unique=True)
  32. email = pw.CharField(max_length=255)
  33. password = pw.CharField(max_length=255)
  34. active = pw.BooleanField()
  35. class Meta:
  36. table_name = "auth"
  37. @migrator.create_model
  38. class Chat(pw.Model):
  39. id = pw.CharField(max_length=255, unique=True)
  40. user_id = pw.CharField(max_length=255)
  41. title = pw.CharField()
  42. chat = pw.TextField()
  43. timestamp = pw.DateField()
  44. class Meta:
  45. table_name = "chat"
  46. @migrator.create_model
  47. class ChatIdTag(pw.Model):
  48. id = pw.CharField(max_length=255, unique=True)
  49. tag_name = pw.CharField(max_length=255)
  50. chat_id = pw.CharField(max_length=255)
  51. user_id = pw.CharField(max_length=255)
  52. timestamp = pw.DateField()
  53. class Meta:
  54. table_name = "chatidtag"
  55. @migrator.create_model
  56. class Document(pw.Model):
  57. id = pw.AutoField()
  58. collection_name = pw.CharField(max_length=255, unique=True)
  59. name = pw.CharField(max_length=255, unique=True)
  60. title = pw.CharField()
  61. filename = pw.CharField()
  62. content = pw.TextField(null=True)
  63. user_id = pw.CharField(max_length=255)
  64. timestamp = pw.DateField()
  65. class Meta:
  66. table_name = "document"
  67. @migrator.create_model
  68. class Modelfile(pw.Model):
  69. id = pw.AutoField()
  70. tag_name = pw.CharField(max_length=255, unique=True)
  71. user_id = pw.CharField(max_length=255)
  72. modelfile = pw.TextField()
  73. timestamp = pw.DateField()
  74. class Meta:
  75. table_name = "modelfile"
  76. @migrator.create_model
  77. class Prompt(pw.Model):
  78. id = pw.AutoField()
  79. command = pw.CharField(max_length=255, unique=True)
  80. user_id = pw.CharField(max_length=255)
  81. title = pw.CharField()
  82. content = pw.TextField()
  83. timestamp = pw.DateField()
  84. class Meta:
  85. table_name = "prompt"
  86. @migrator.create_model
  87. class Tag(pw.Model):
  88. id = pw.CharField(max_length=255, unique=True)
  89. name = pw.CharField(max_length=255)
  90. user_id = pw.CharField(max_length=255)
  91. data = pw.TextField(null=True)
  92. class Meta:
  93. table_name = "tag"
  94. @migrator.create_model
  95. class User(pw.Model):
  96. id = pw.CharField(max_length=255, unique=True)
  97. name = pw.CharField(max_length=255)
  98. email = pw.CharField(max_length=255)
  99. role = pw.CharField(max_length=255)
  100. profile_image_url = pw.CharField(max_length=255)
  101. timestamp = pw.DateField()
  102. class Meta:
  103. table_name = "user"
  104. def rollback(migrator: Migrator, database: pw.Database, *, fake=False):
  105. """Write your rollback migrations here."""
  106. migrator.remove_model("user")
  107. migrator.remove_model("tag")
  108. migrator.remove_model("prompt")
  109. migrator.remove_model("modelfile")
  110. migrator.remove_model("document")
  111. migrator.remove_model("chatidtag")
  112. migrator.remove_model("chat")
  113. migrator.remove_model("auth")