/migrations/versions/59729e468045_add_view_column_to_setting_table.py

https://github.com/ngoduykhanh/PowerDNS-Admin · Python · 92 lines · 60 code · 11 blank · 21 comment · 0 complexity · 01b6ccdf75c0a2794fb48070a59aa238 MD5 · raw file

  1. """Add view column to setting table
  2. Revision ID: 59729e468045
  3. Revises: 787bdba9e147
  4. Create Date: 2018-08-17 16:17:31.058782
  5. """
  6. from alembic import op
  7. import sqlalchemy as sa
  8. # revision identifiers, used by Alembic.
  9. revision = '59729e468045'
  10. down_revision = '787bdba9e147'
  11. branch_labels = None
  12. depends_on = None
  13. def update_data():
  14. setting_table = sa.sql.table('setting',
  15. sa.sql.column('id', sa.Integer),
  16. sa.sql.column('name', sa.String),
  17. sa.sql.column('value', sa.String),
  18. sa.sql.column('view', sa.String)
  19. )
  20. # just update previous records which have id <= 7
  21. op.execute(
  22. setting_table.update().where(setting_table.c.id <= 7).values({'view': 'basic'})
  23. )
  24. # add more new settings
  25. op.bulk_insert(setting_table,
  26. [
  27. {'id': 8, 'name': 'pretty_ipv6_ptr', 'value': 'False', 'view': 'basic'},
  28. {'id': 9, 'name': 'dnssec_admins_only', 'value': 'False', 'view': 'basic'},
  29. {'id': 10, 'name': 'bg_domain_updates', 'value': 'False', 'view': 'basic'},
  30. {'id': 11, 'name': 'site_name', 'value': 'PowerDNS-Admin', 'view': 'basic'},
  31. {'id': 12, 'name': 'pdns_api_url', 'value': '', 'view': 'pdns'},
  32. {'id': 13, 'name': 'pdns_api_key', 'value': '', 'view': 'pdns'},
  33. {'id': 14, 'name': 'pdns_version', 'value': '4.1.1', 'view': 'pdns'},
  34. {'id': 15, 'name': 'local_db_enabled', 'value': 'True', 'view': 'authentication'},
  35. {'id': 16, 'name': 'signup_enabled', 'value': 'True', 'view': 'authentication'},
  36. {'id': 17, 'name': 'ldap_enabled', 'value': 'False', 'view': 'authentication'},
  37. {'id': 18, 'name': 'ldap_type', 'value': 'ldap', 'view': 'authentication'},
  38. {'id': 19, 'name': 'ldap_uri', 'value': '', 'view': 'authentication'},
  39. {'id': 20, 'name': 'ldap_base_dn', 'value': '', 'view': 'authentication'},
  40. {'id': 21, 'name': 'ldap_admin_username', 'value': '', 'view': 'authentication'},
  41. {'id': 22, 'name': 'ldap_admin_password', 'value': '', 'view': 'authentication'},
  42. {'id': 23, 'name': 'ldap_filter_basic', 'value': '', 'view': 'authentication'},
  43. {'id': 24, 'name': 'ldap_filter_username', 'value': '', 'view': 'authentication'},
  44. {'id': 25, 'name': 'ldap_sg_enabled', 'value': 'False', 'view': 'authentication'},
  45. {'id': 26, 'name': 'ldap_admin_group', 'value': '', 'view': 'authentication'},
  46. {'id': 27, 'name': 'ldap_user_group', 'value': '', 'view': 'authentication'},
  47. {'id': 28, 'name': 'github_oauth_enabled', 'value': 'False', 'view': 'authentication'},
  48. {'id': 29, 'name': 'github_oauth_key', 'value': '', 'view': 'authentication'},
  49. {'id': 30, 'name': 'github_oauth_secret', 'value': '', 'view': 'authentication'},
  50. {'id': 31, 'name': 'github_oauth_scope', 'value': 'email', 'view': 'authentication'},
  51. {'id': 32, 'name': 'github_oauth_api_url', 'value': 'https://api.github.com/user', 'view': 'authentication'},
  52. {'id': 33, 'name': 'github_oauth_token_url', 'value': 'https://github.com/login/oauth/access_token', 'view': 'authentication'},
  53. {'id': 34, 'name': 'github_oauth_authorize_url', 'value': 'https://github.com/login/oauth/authorize', 'view': 'authentication'},
  54. {'id': 35, 'name': 'google_oauth_enabled', 'value': 'False', 'view': 'authentication'},
  55. {'id': 36, 'name': 'google_oauth_client_id', 'value': '', 'view': 'authentication'},
  56. {'id': 37, 'name': 'google_oauth_client_secret', 'value': '', 'view': 'authentication'},
  57. {'id': 38, 'name': 'google_token_url', 'value': 'https://accounts.google.com/o/oauth2/token', 'view': 'authentication'},
  58. {'id': 39, 'name': 'google_token_params', 'value': "{'scope': 'email profile'}", 'view': 'authentication'},
  59. {'id': 40, 'name': 'google_authorize_url', 'value': 'https://accounts.google.com/o/oauth2/auth', 'view': 'authentication'},
  60. {'id': 41, 'name': 'google_base_url', 'value': 'https://www.googleapis.com/oauth2/v1/', 'view': 'authentication'},
  61. ]
  62. )
  63. def upgrade():
  64. # ### commands auto generated by Alembic - please adjust! ###
  65. op.add_column('setting', sa.Column('view', sa.String(length=64), nullable=True))
  66. # ### end Alembic commands ###
  67. # update data for new schema
  68. update_data()
  69. def downgrade():
  70. # ### commands auto generated by Alembic - please adjust! ###
  71. ## NOTE:
  72. ## - Drop action does not work on sqlite3
  73. ## - This action touches the `setting` table which loaded in views.py
  74. ## during app initlization, so the downgrade function won't work
  75. ## unless we temporary remove importing `views` from `app/__init__.py`
  76. op.drop_column('setting', 'view')
  77. # delete added records in previous version
  78. op.execute("DELETE FROM setting WHERE id > 7")
  79. # ### end Alembic commands ###