mirror of
https://github.com/writeas/writefreely
synced 2025-02-02 22:37:39 +01:00
Merge pull request #275 from writeas/fix-v4-migrations
Fix V4 + V5 SQLite migrations
This commit is contained in:
commit
bad970c60a
26
db/create.go
26
db/create.go
@ -1,3 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2019-2020 A Bunch Tell LLC.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -139,6 +149,15 @@ func (c *Column) SetDefault(value string) *Column {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Column) SetDefaultCurrentTimestamp() *Column {
|
||||||
|
def := "NOW()"
|
||||||
|
if c.Dialect == DialectSQLite {
|
||||||
|
def = "CURRENT_TIMESTAMP"
|
||||||
|
}
|
||||||
|
c.Default = OptionalString{Set: true, Value: def}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Column) SetType(t ColumnType) *Column {
|
func (c *Column) SetType(t ColumnType) *Column {
|
||||||
c.Type = t
|
c.Type = t
|
||||||
return c
|
return c
|
||||||
@ -168,7 +187,11 @@ func (c *Column) String() (string, error) {
|
|||||||
|
|
||||||
if c.Default.Set {
|
if c.Default.Set {
|
||||||
str.WriteString(" DEFAULT ")
|
str.WriteString(" DEFAULT ")
|
||||||
str.WriteString(c.Default.Value)
|
val := c.Default.Value
|
||||||
|
if val == "" {
|
||||||
|
val = "''"
|
||||||
|
}
|
||||||
|
str.WriteString(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.PrimaryKey {
|
if c.PrimaryKey {
|
||||||
@ -241,4 +264,3 @@ func (b *CreateTableSqlBuilder) ToSQL() (string, error) {
|
|||||||
|
|
||||||
return str.String(), nil
|
return str.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2019-2020 A Bunch Tell LLC.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
package migrations
|
package migrations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -15,21 +25,19 @@ func oauth(db *datastore) error {
|
|||||||
return wf_db.RunTransactionWithOptions(context.Background(), db.DB, &sql.TxOptions{}, func(ctx context.Context, tx *sql.Tx) error {
|
return wf_db.RunTransactionWithOptions(context.Background(), db.DB, &sql.TxOptions{}, func(ctx context.Context, tx *sql.Tx) error {
|
||||||
createTableUsersOauth, err := dialect.
|
createTableUsersOauth, err := dialect.
|
||||||
Table("oauth_users").
|
Table("oauth_users").
|
||||||
SetIfNotExists(true).
|
SetIfNotExists(false).
|
||||||
Column(dialect.Column("user_id", wf_db.ColumnTypeInteger, wf_db.UnsetSize)).
|
Column(dialect.Column("user_id", wf_db.ColumnTypeInteger, wf_db.UnsetSize)).
|
||||||
Column(dialect.Column("remote_user_id", wf_db.ColumnTypeInteger, wf_db.UnsetSize)).
|
Column(dialect.Column("remote_user_id", wf_db.ColumnTypeInteger, wf_db.UnsetSize)).
|
||||||
UniqueConstraint("user_id").
|
|
||||||
UniqueConstraint("remote_user_id").
|
|
||||||
ToSQL()
|
ToSQL()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
createTableOauthClientState, err := dialect.
|
createTableOauthClientState, err := dialect.
|
||||||
Table("oauth_client_states").
|
Table("oauth_client_states").
|
||||||
SetIfNotExists(true).
|
SetIfNotExists(false).
|
||||||
Column(dialect.Column("state", wf_db.ColumnTypeVarChar, wf_db.OptionalInt{Set: true, Value: 255})).
|
Column(dialect.Column("state", wf_db.ColumnTypeVarChar, wf_db.OptionalInt{Set: true, Value: 255})).
|
||||||
Column(dialect.Column("used", wf_db.ColumnTypeBool, wf_db.UnsetSize)).
|
Column(dialect.Column("used", wf_db.ColumnTypeBool, wf_db.UnsetSize)).
|
||||||
Column(dialect.Column("created_at", wf_db.ColumnTypeDateTime, wf_db.UnsetSize).SetDefault("NOW()")).
|
Column(dialect.Column("created_at", wf_db.ColumnTypeDateTime, wf_db.UnsetSize).SetDefaultCurrentTimestamp()).
|
||||||
UniqueConstraint("state").
|
UniqueConstraint("state").
|
||||||
ToSQL()
|
ToSQL()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1,3 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2019-2020 A Bunch Tell LLC.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
package migrations
|
package migrations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -20,39 +30,50 @@ func oauthSlack(db *datastore) error {
|
|||||||
Column(
|
Column(
|
||||||
"provider",
|
"provider",
|
||||||
wf_db.ColumnTypeVarChar,
|
wf_db.ColumnTypeVarChar,
|
||||||
wf_db.OptionalInt{Set: true, Value: 24,})).
|
wf_db.OptionalInt{Set: true, Value: 24}).SetDefault("")),
|
||||||
|
dialect.
|
||||||
|
AlterTable("oauth_client_states").
|
||||||
AddColumn(dialect.
|
AddColumn(dialect.
|
||||||
Column(
|
Column(
|
||||||
"client_id",
|
"client_id",
|
||||||
wf_db.ColumnTypeVarChar,
|
wf_db.ColumnTypeVarChar,
|
||||||
wf_db.OptionalInt{Set: true, Value: 128,})),
|
wf_db.OptionalInt{Set: true, Value: 128}).SetDefault("")),
|
||||||
dialect.
|
dialect.
|
||||||
|
AlterTable("oauth_users").
|
||||||
|
AddColumn(dialect.
|
||||||
|
Column(
|
||||||
|
"provider",
|
||||||
|
wf_db.ColumnTypeVarChar,
|
||||||
|
wf_db.OptionalInt{Set: true, Value: 24}).SetDefault("")),
|
||||||
|
dialect.
|
||||||
|
AlterTable("oauth_users").
|
||||||
|
AddColumn(dialect.
|
||||||
|
Column(
|
||||||
|
"client_id",
|
||||||
|
wf_db.ColumnTypeVarChar,
|
||||||
|
wf_db.OptionalInt{Set: true, Value: 128}).SetDefault("")),
|
||||||
|
dialect.
|
||||||
|
AlterTable("oauth_users").
|
||||||
|
AddColumn(dialect.
|
||||||
|
Column(
|
||||||
|
"access_token",
|
||||||
|
wf_db.ColumnTypeVarChar,
|
||||||
|
wf_db.OptionalInt{Set: true, Value: 512}).SetDefault("")),
|
||||||
|
dialect.CreateUniqueIndex("oauth_users_uk", "oauth_users", "user_id", "provider", "client_id"),
|
||||||
|
}
|
||||||
|
|
||||||
|
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").
|
AlterTable("oauth_users").
|
||||||
ChangeColumn("remote_user_id",
|
ChangeColumn("remote_user_id",
|
||||||
dialect.
|
dialect.
|
||||||
Column(
|
Column(
|
||||||
"remote_user_id",
|
"remote_user_id",
|
||||||
wf_db.ColumnTypeVarChar,
|
wf_db.ColumnTypeVarChar,
|
||||||
wf_db.OptionalInt{Set: true, Value: 128,})).
|
wf_db.OptionalInt{Set: true, Value: 128})))
|
||||||
AddColumn(dialect.
|
|
||||||
Column(
|
|
||||||
"provider",
|
|
||||||
wf_db.ColumnTypeVarChar,
|
|
||||||
wf_db.OptionalInt{Set: true, Value: 24,})).
|
|
||||||
AddColumn(dialect.
|
|
||||||
Column(
|
|
||||||
"client_id",
|
|
||||||
wf_db.ColumnTypeVarChar,
|
|
||||||
wf_db.OptionalInt{Set: true, Value: 128,})).
|
|
||||||
AddColumn(dialect.
|
|
||||||
Column(
|
|
||||||
"access_token",
|
|
||||||
wf_db.ColumnTypeVarChar,
|
|
||||||
wf_db.OptionalInt{Set: true, Value: 512,})),
|
|
||||||
dialect.DropIndex("remote_user_id", "oauth_users"),
|
|
||||||
dialect.DropIndex("user_id", "oauth_users"),
|
|
||||||
dialect.CreateUniqueIndex("oauth_users", "oauth_users", "user_id", "provider", "client_id"),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, builder := range builders {
|
for _, builder := range builders {
|
||||||
query, err := builder.ToSQL()
|
query, err := builder.ToSQL()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user