de95f5c77138_migration_serpapi_api_key.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """migration serpapi_api_key
  2. Revision ID: de95f5c77138
  3. Revises: 23db93619b9d
  4. Create Date: 2024-01-21 12:09:04.651394
  5. """
  6. from json import dumps, loads
  7. import sqlalchemy as sa
  8. from alembic import context, op
  9. # revision identifiers, used by Alembic.
  10. revision = 'de95f5c77138'
  11. down_revision = '23db93619b9d'
  12. branch_labels = None
  13. depends_on = None
  14. def upgrade():
  15. # ### commands auto generated by Alembic - please adjust! ###
  16. """
  17. 1. select all tool_providers
  18. 2. insert api_key to tool_provider_configs
  19. tool_providers
  20. - id
  21. - tenant_id
  22. - tool_name
  23. - encrypted_credentials
  24. {"api_key": "$KEY"}
  25. - created_at
  26. - updated_at
  27. tool_builtin_providers
  28. - id <- tool_providers.id
  29. - tenant_id <- tool_providers.tenant_id
  30. - user_id <- tenant_account_joins.account_id (tenant_account_joins.tenant_id = tool_providers.tenant_id and tenant_account_joins.role = 'owner')
  31. - encrypted_credentials <- tool_providers.encrypted_credentials
  32. {"serpapi_api_key": "$KEY"}
  33. - created_at <- tool_providers.created_at
  34. - updated_at <- tool_providers.updated_at
  35. """
  36. # in alembic's offline mode (with --sql option), skip data operations and output comments describing the migration to raw sql
  37. if context.is_offline_mode():
  38. print(f" /*{upgrade.__doc__}*/\n")
  39. return
  40. # select all tool_providers
  41. tool_providers = op.get_bind().execute(
  42. sa.text(
  43. "SELECT * FROM tool_providers WHERE tool_name = 'serpapi'"
  44. )
  45. ).fetchall()
  46. # insert api_key to tool_provider_configs
  47. for tool_provider in tool_providers:
  48. id = tool_provider['id']
  49. tenant_id = tool_provider['tenant_id']
  50. encrypted_credentials = tool_provider['encrypted_credentials']
  51. try:
  52. credentials = loads(encrypted_credentials)
  53. api_key = credentials['api_key']
  54. credentials['serpapi_api_key'] = api_key
  55. credentials.pop('api_key')
  56. encrypted_credentials = dumps(credentials)
  57. except Exception as e:
  58. print(e)
  59. continue
  60. # get user_id
  61. user_id = op.get_bind().execute(
  62. sa.text(
  63. "SELECT account_id FROM tenant_account_joins WHERE tenant_id = :tenant_id AND role = 'owner'"
  64. ),
  65. tenant_id=tenant_id
  66. ).fetchone()['account_id']
  67. created_at = tool_provider['created_at']
  68. updated_at = tool_provider['updated_at']
  69. # insert to tool_builtin_providers
  70. # check if exists
  71. exists = op.get_bind().execute(
  72. sa.text(
  73. "SELECT * FROM tool_builtin_providers WHERE tenant_id = :tenant_id AND provider = 'google'"
  74. ),
  75. tenant_id=tenant_id
  76. ).fetchone()
  77. if exists:
  78. continue
  79. op.get_bind().execute(
  80. sa.text(
  81. "INSERT INTO tool_builtin_providers (id, tenant_id, user_id, provider, encrypted_credentials, created_at, updated_at) VALUES (:id, :tenant_id, :user_id, :provider, :encrypted_credentials, :created_at, :updated_at)"
  82. ),
  83. id=id,
  84. tenant_id=tenant_id,
  85. user_id=user_id,
  86. provider='google',
  87. encrypted_credentials=encrypted_credentials,
  88. created_at=created_at,
  89. updated_at=updated_at
  90. )
  91. # ### end Alembic commands ###
  92. def downgrade():
  93. # ### commands auto generated by Alembic - please adjust! ###
  94. pass
  95. # ### end Alembic commands ###