010_migrate_modelfiles_to_models.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """Peewee migrations -- 009_add_models.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. import json
  26. with suppress(ImportError):
  27. import playhouse.postgres_ext as pw_pext
  28. def migrate(migrator: Migrator, database: pw.Database, *, fake=False):
  29. """Write your migrations here."""
  30. # Fetch data from 'modelfile' table and insert into 'model' table
  31. migrate_modelfile_to_model(migrator, database)
  32. # Drop the 'modelfile' table
  33. migrator.remove_model("modelfile")
  34. def migrate_modelfile_to_model(migrator: Migrator, database: pw.Database):
  35. ModelFile = migrator.orm["modelfile"]
  36. Model = migrator.orm["model"]
  37. modelfiles = ModelFile.select()
  38. for modelfile in modelfiles:
  39. # Extract and transform data in Python
  40. modelfile.modelfile = json.loads(modelfile.modelfile)
  41. meta = json.dumps(
  42. {
  43. "description": modelfile.modelfile.get("desc"),
  44. "profile_image_url": modelfile.modelfile.get("imageUrl"),
  45. "ollama": {"modelfile": modelfile.modelfile.get("content")},
  46. "suggestion_prompts": modelfile.modelfile.get("suggestionPrompts"),
  47. "categories": modelfile.modelfile.get("categories"),
  48. "user": {**modelfile.modelfile.get("user", {}), "community": True},
  49. }
  50. )
  51. # Insert the processed data into the 'model' table
  52. Model.create(
  53. id=modelfile.tag_name,
  54. user_id=modelfile.user_id,
  55. name=modelfile.modelfile.get("title"),
  56. meta=meta,
  57. params="{}",
  58. created_at=modelfile.timestamp,
  59. updated_at=modelfile.timestamp,
  60. )
  61. def rollback(migrator: Migrator, database: pw.Database, *, fake=False):
  62. """Write your rollback migrations here."""
  63. recreate_modelfile_table(migrator, database)
  64. move_data_back_to_modelfile(migrator, database)
  65. migrator.remove_model("model")
  66. def recreate_modelfile_table(migrator: Migrator, database: pw.Database):
  67. query = """
  68. CREATE TABLE IF NOT EXISTS modelfile (
  69. user_id TEXT,
  70. tag_name TEXT,
  71. modelfile JSON,
  72. timestamp BIGINT
  73. )
  74. """
  75. migrator.sql(query)
  76. def move_data_back_to_modelfile(migrator: Migrator, database: pw.Database):
  77. Model = migrator.orm["model"]
  78. Modelfile = migrator.orm["modelfile"]
  79. models = Model.select()
  80. for model in models:
  81. # Extract and transform data in Python
  82. meta = json.loads(model.meta)
  83. modelfile_data = {
  84. "title": model.name,
  85. "desc": meta.get("description"),
  86. "imageUrl": meta.get("profile_image_url"),
  87. "content": meta.get("ollama", {}).get("modelfile"),
  88. "suggestionPrompts": meta.get("suggestion_prompts"),
  89. "categories": meta.get("categories"),
  90. "user": {k: v for k, v in meta.get("user", {}).items() if k != "community"},
  91. }
  92. # Insert the processed data back into the 'modelfile' table
  93. Modelfile.create(
  94. user_id=model.user_id,
  95. tag_name=model.id,
  96. modelfile=modelfile_data,
  97. timestamp=model.created_at,
  98. )