A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

89 lines
2.3 KiB

  1. /*
  2. * Copyright © 2019-2021 Musing Studio LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package migrations
  11. import (
  12. "context"
  13. "database/sql"
  14. wf_db "github.com/writefreely/writefreely/db"
  15. )
  16. func oauthSlack(db *datastore) error {
  17. dialect := wf_db.DialectMySQL
  18. if db.driverName == driverSQLite {
  19. dialect = wf_db.DialectSQLite
  20. }
  21. return wf_db.RunTransactionWithOptions(context.Background(), db.DB, &sql.TxOptions{}, func(ctx context.Context, tx *sql.Tx) error {
  22. builders := []wf_db.SQLBuilder{
  23. dialect.
  24. AlterTable("oauth_client_states").
  25. AddColumn(dialect.
  26. Column(
  27. "provider",
  28. wf_db.ColumnTypeVarChar,
  29. wf_db.OptionalInt{Set: true, Value: 24}).SetDefault("")),
  30. dialect.
  31. AlterTable("oauth_client_states").
  32. AddColumn(dialect.
  33. Column(
  34. "client_id",
  35. wf_db.ColumnTypeVarChar,
  36. wf_db.OptionalInt{Set: true, Value: 128}).SetDefault("")),
  37. dialect.
  38. AlterTable("oauth_users").
  39. AddColumn(dialect.
  40. Column(
  41. "provider",
  42. wf_db.ColumnTypeVarChar,
  43. wf_db.OptionalInt{Set: true, Value: 24}).SetDefault("")),
  44. dialect.
  45. AlterTable("oauth_users").
  46. AddColumn(dialect.
  47. Column(
  48. "client_id",
  49. wf_db.ColumnTypeVarChar,
  50. wf_db.OptionalInt{Set: true, Value: 128}).SetDefault("")),
  51. dialect.
  52. AlterTable("oauth_users").
  53. AddColumn(dialect.
  54. Column(
  55. "access_token",
  56. wf_db.ColumnTypeVarChar,
  57. wf_db.OptionalInt{Set: true, Value: 512}).SetDefault("")),
  58. dialect.CreateUniqueIndex("oauth_users_uk", "oauth_users", "user_id", "provider", "client_id"),
  59. }
  60. if dialect != wf_db.DialectSQLite {
  61. // This updates the length of the `remote_user_id` column. It isn't needed for SQLite databases.
  62. builders = append(builders, dialect.
  63. AlterTable("oauth_users").
  64. ChangeColumn("remote_user_id",
  65. dialect.
  66. Column(
  67. "remote_user_id",
  68. wf_db.ColumnTypeVarChar,
  69. wf_db.OptionalInt{Set: true, Value: 128})))
  70. }
  71. for _, builder := range builders {
  72. query, err := builder.ToSQL()
  73. if err != nil {
  74. return err
  75. }
  76. if _, err := tx.ExecContext(ctx, query); err != nil {
  77. return err
  78. }
  79. }
  80. return nil
  81. })
  82. }