2020-03-03 18:47:38 +01:00
|
|
|
/*
|
2022-11-11 05:49:16 +01:00
|
|
|
* Copyright © 2019-2021 Musing Studio LLC.
|
2020-03-03 18:47:38 +01:00
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
|
|
|
|
2019-12-28 21:15:47 +01:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
2021-04-06 23:24:07 +02:00
|
|
|
wf_db "github.com/writefreely/writefreely/db"
|
2019-12-28 21:15:47 +01:00
|
|
|
)
|
|
|
|
|
2019-12-30 19:32:06 +01:00
|
|
|
func oauthSlack(db *datastore) error {
|
2019-12-28 21:15:47 +01:00
|
|
|
dialect := wf_db.DialectMySQL
|
|
|
|
if db.driverName == driverSQLite {
|
|
|
|
dialect = wf_db.DialectSQLite
|
|
|
|
}
|
|
|
|
return wf_db.RunTransactionWithOptions(context.Background(), db.DB, &sql.TxOptions{}, func(ctx context.Context, tx *sql.Tx) error {
|
|
|
|
builders := []wf_db.SQLBuilder{
|
|
|
|
dialect.
|
2019-12-31 17:48:08 +01:00
|
|
|
AlterTable("oauth_client_states").
|
2019-12-28 21:15:47 +01:00
|
|
|
AddColumn(dialect.
|
|
|
|
Column(
|
|
|
|
"provider",
|
|
|
|
wf_db.ColumnTypeVarChar,
|
2020-03-03 18:43:46 +01:00
|
|
|
wf_db.OptionalInt{Set: true, Value: 24}).SetDefault("")),
|
2020-03-03 18:40:56 +01:00
|
|
|
dialect.
|
|
|
|
AlterTable("oauth_client_states").
|
2019-12-28 21:15:47 +01:00
|
|
|
AddColumn(dialect.
|
|
|
|
Column(
|
|
|
|
"client_id",
|
|
|
|
wf_db.ColumnTypeVarChar,
|
2020-03-03 18:43:46 +01:00
|
|
|
wf_db.OptionalInt{Set: true, Value: 128}).SetDefault("")),
|
2019-12-30 19:32:06 +01:00
|
|
|
dialect.
|
2019-12-31 17:48:08 +01:00
|
|
|
AlterTable("oauth_users").
|
2019-12-30 19:32:06 +01:00
|
|
|
AddColumn(dialect.
|
|
|
|
Column(
|
|
|
|
"provider",
|
|
|
|
wf_db.ColumnTypeVarChar,
|
2020-03-03 18:43:46 +01:00
|
|
|
wf_db.OptionalInt{Set: true, Value: 24}).SetDefault("")),
|
2020-03-03 18:40:56 +01:00
|
|
|
dialect.
|
|
|
|
AlterTable("oauth_users").
|
2019-12-30 19:32:06 +01:00
|
|
|
AddColumn(dialect.
|
|
|
|
Column(
|
|
|
|
"client_id",
|
|
|
|
wf_db.ColumnTypeVarChar,
|
2020-03-03 18:43:46 +01:00
|
|
|
wf_db.OptionalInt{Set: true, Value: 128}).SetDefault("")),
|
2020-03-03 18:40:56 +01:00
|
|
|
dialect.
|
|
|
|
AlterTable("oauth_users").
|
2019-12-30 19:32:06 +01:00
|
|
|
AddColumn(dialect.
|
|
|
|
Column(
|
|
|
|
"access_token",
|
|
|
|
wf_db.ColumnTypeVarChar,
|
2020-03-03 18:43:46 +01:00
|
|
|
wf_db.OptionalInt{Set: true, Value: 512}).SetDefault("")),
|
2020-03-03 18:46:51 +01:00
|
|
|
dialect.CreateUniqueIndex("oauth_users_uk", "oauth_users", "user_id", "provider", "client_id"),
|
2019-12-28 21:15:47 +01:00
|
|
|
}
|
2020-03-03 18:40:56 +01:00
|
|
|
|
|
|
|
if dialect != wf_db.DialectSQLite {
|
|
|
|
// This updates the length of the `remote_user_id` column. It isn't needed for SQLite databases.
|
|
|
|
builders = append(builders, dialect.
|
|
|
|
AlterTable("oauth_users").
|
|
|
|
ChangeColumn("remote_user_id",
|
|
|
|
dialect.
|
|
|
|
Column(
|
|
|
|
"remote_user_id",
|
|
|
|
wf_db.ColumnTypeVarChar,
|
|
|
|
wf_db.OptionalInt{Set: true, Value: 128})))
|
|
|
|
}
|
2020-03-03 18:43:46 +01:00
|
|
|
|
2019-12-28 21:15:47 +01:00
|
|
|
for _, builder := range builders {
|
|
|
|
query, err := builder.ToSQL()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := tx.ExecContext(ctx, query); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|