001_initial_schema.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. # We perform different migrations for SQLite and other databases
  30. # This is because SQLite is very loose with enforcing its schema, and trying to migrate other databases like SQLite
  31. # will require per-database SQL queries.
  32. # Instead, we assume that because external DB support was added at a later date, it is safe to assume a newer base
  33. # schema instead of trying to migrate from an older schema.
  34. if isinstance(database, pw.SqliteDatabase):
  35. migrate_sqlite(migrator, database, fake=fake)
  36. else:
  37. migrate_external(migrator, database, fake=fake)
  38. def migrate_sqlite(migrator: Migrator, database: pw.Database, *, fake=False):
  39. @migrator.create_model
  40. class Auth(pw.Model):
  41. id = pw.CharField(max_length=255, unique=True)
  42. email = pw.CharField(max_length=255)
  43. password = pw.CharField(max_length=255)
  44. active = pw.BooleanField()
  45. class Meta:
  46. table_name = "auth"
  47. @migrator.create_model
  48. class Chat(pw.Model):
  49. id = pw.CharField(max_length=255, unique=True)
  50. user_id = pw.CharField(max_length=255)
  51. title = pw.CharField()
  52. chat = pw.TextField()
  53. timestamp = pw.BigIntegerField()
  54. class Meta:
  55. table_name = "chat"
  56. @migrator.create_model
  57. class ChatIdTag(pw.Model):
  58. id = pw.CharField(max_length=255, unique=True)
  59. tag_name = pw.CharField(max_length=255)
  60. chat_id = pw.CharField(max_length=255)
  61. user_id = pw.CharField(max_length=255)
  62. timestamp = pw.BigIntegerField()
  63. class Meta:
  64. table_name = "chatidtag"
  65. @migrator.create_model
  66. class Document(pw.Model):
  67. id = pw.AutoField()
  68. collection_name = pw.CharField(max_length=255, unique=True)
  69. name = pw.CharField(max_length=255, unique=True)
  70. title = pw.CharField()
  71. filename = pw.CharField()
  72. content = pw.TextField(null=True)
  73. user_id = pw.CharField(max_length=255)
  74. timestamp = pw.BigIntegerField()
  75. class Meta:
  76. table_name = "document"
  77. @migrator.create_model
  78. class Modelfile(pw.Model):
  79. id = pw.AutoField()
  80. tag_name = pw.CharField(max_length=255, unique=True)
  81. user_id = pw.CharField(max_length=255)
  82. modelfile = pw.TextField()
  83. timestamp = pw.BigIntegerField()
  84. class Meta:
  85. table_name = "modelfile"
  86. @migrator.create_model
  87. class Prompt(pw.Model):
  88. id = pw.AutoField()
  89. command = pw.CharField(max_length=255, unique=True)
  90. user_id = pw.CharField(max_length=255)
  91. title = pw.CharField()
  92. content = pw.TextField()
  93. timestamp = pw.BigIntegerField()
  94. class Meta:
  95. table_name = "prompt"
  96. @migrator.create_model
  97. class Tag(pw.Model):
  98. id = pw.CharField(max_length=255, unique=True)
  99. name = pw.CharField(max_length=255)
  100. user_id = pw.CharField(max_length=255)
  101. data = pw.TextField(null=True)
  102. class Meta:
  103. table_name = "tag"
  104. @migrator.create_model
  105. class User(pw.Model):
  106. id = pw.CharField(max_length=255, unique=True)
  107. name = pw.CharField(max_length=255)
  108. email = pw.CharField(max_length=255)
  109. role = pw.CharField(max_length=255)
  110. profile_image_url = pw.CharField(max_length=255)
  111. timestamp = pw.BigIntegerField()
  112. class Meta:
  113. table_name = "user"
  114. def migrate_external(migrator: Migrator, database: pw.Database, *, fake=False):
  115. @migrator.create_model
  116. class Auth(pw.Model):
  117. id = pw.CharField(max_length=255, unique=True)
  118. email = pw.CharField(max_length=255)
  119. password = pw.TextField()
  120. active = pw.BooleanField()
  121. class Meta:
  122. table_name = "auth"
  123. @migrator.create_model
  124. class Chat(pw.Model):
  125. id = pw.CharField(max_length=255, unique=True)
  126. user_id = pw.CharField(max_length=255)
  127. title = pw.TextField()
  128. chat = pw.TextField()
  129. timestamp = pw.BigIntegerField()
  130. class Meta:
  131. table_name = "chat"
  132. @migrator.create_model
  133. class ChatIdTag(pw.Model):
  134. id = pw.CharField(max_length=255, unique=True)
  135. tag_name = pw.CharField(max_length=255)
  136. chat_id = pw.CharField(max_length=255)
  137. user_id = pw.CharField(max_length=255)
  138. timestamp = pw.BigIntegerField()
  139. class Meta:
  140. table_name = "chatidtag"
  141. @migrator.create_model
  142. class Document(pw.Model):
  143. id = pw.AutoField()
  144. collection_name = pw.CharField(max_length=255, unique=True)
  145. name = pw.CharField(max_length=255, unique=True)
  146. title = pw.TextField()
  147. filename = pw.TextField()
  148. content = pw.TextField(null=True)
  149. user_id = pw.CharField(max_length=255)
  150. timestamp = pw.BigIntegerField()
  151. class Meta:
  152. table_name = "document"
  153. @migrator.create_model
  154. class Modelfile(pw.Model):
  155. id = pw.AutoField()
  156. tag_name = pw.CharField(max_length=255, unique=True)
  157. user_id = pw.CharField(max_length=255)
  158. modelfile = pw.TextField()
  159. timestamp = pw.BigIntegerField()
  160. class Meta:
  161. table_name = "modelfile"
  162. @migrator.create_model
  163. class Prompt(pw.Model):
  164. id = pw.AutoField()
  165. command = pw.CharField(max_length=255, unique=True)
  166. user_id = pw.CharField(max_length=255)
  167. title = pw.TextField()
  168. content = pw.TextField()
  169. timestamp = pw.BigIntegerField()
  170. class Meta:
  171. table_name = "prompt"
  172. @migrator.create_model
  173. class Tag(pw.Model):
  174. id = pw.CharField(max_length=255, unique=True)
  175. name = pw.CharField(max_length=255)
  176. user_id = pw.CharField(max_length=255)
  177. data = pw.TextField(null=True)
  178. class Meta:
  179. table_name = "tag"
  180. @migrator.create_model
  181. class User(pw.Model):
  182. id = pw.CharField(max_length=255, unique=True)
  183. name = pw.CharField(max_length=255)
  184. email = pw.CharField(max_length=255)
  185. role = pw.CharField(max_length=255)
  186. profile_image_url = pw.TextField()
  187. timestamp = pw.BigIntegerField()
  188. class Meta:
  189. table_name = "user"
  190. def rollback(migrator: Migrator, database: pw.Database, *, fake=False):
  191. """Write your rollback migrations here."""
  192. migrator.remove_model("user")
  193. migrator.remove_model("tag")
  194. migrator.remove_model("prompt")
  195. migrator.remove_model("modelfile")
  196. migrator.remove_model("document")
  197. migrator.remove_model("chatidtag")
  198. migrator.remove_model("chat")
  199. migrator.remove_model("auth")