2016-09-15 06:41:57 +05:30
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *conn) migrate() (int, error) {
|
|
|
|
_, err := c.Exec(`
|
|
|
|
create table if not exists migrations (
|
|
|
|
num integer not null,
|
2016-12-17 00:33:36 +05:30
|
|
|
at timestamptz not null
|
2016-09-15 06:41:57 +05:30
|
|
|
);
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("creating migration table: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
done := false
|
2020-02-21 16:43:38 +05:30
|
|
|
|
|
|
|
var flavorMigrations []migration
|
|
|
|
for _, m := range migrations {
|
|
|
|
if m.flavor == nil || m.flavor == c.flavor {
|
|
|
|
flavorMigrations = append(flavorMigrations, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-15 06:41:57 +05:30
|
|
|
for {
|
|
|
|
err := c.ExecTx(func(tx *trans) error {
|
|
|
|
// Within a transaction, perform a single migration.
|
|
|
|
var (
|
|
|
|
num sql.NullInt64
|
|
|
|
n int
|
|
|
|
)
|
|
|
|
if err := tx.QueryRow(`select max(num) from migrations;`).Scan(&num); err != nil {
|
|
|
|
return fmt.Errorf("select max migration: %v", err)
|
|
|
|
}
|
|
|
|
if num.Valid {
|
|
|
|
n = int(num.Int64)
|
|
|
|
}
|
2020-02-21 16:43:38 +05:30
|
|
|
if n >= len(flavorMigrations) {
|
2016-09-15 06:41:57 +05:30
|
|
|
done = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
migrationNum := n + 1
|
2020-02-21 16:43:38 +05:30
|
|
|
m := flavorMigrations[n]
|
2017-04-21 21:21:55 +05:30
|
|
|
for i := range m.stmts {
|
|
|
|
if _, err := tx.Exec(m.stmts[i]); err != nil {
|
|
|
|
return fmt.Errorf("migration %d statement %d failed: %v", migrationNum, i+1, err)
|
|
|
|
}
|
2016-09-15 06:41:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
q := `insert into migrations (num, at) values ($1, now());`
|
|
|
|
if _, err := tx.Exec(q, migrationNum); err != nil {
|
|
|
|
return fmt.Errorf("update migration table: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
if done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type migration struct {
|
2017-04-21 21:21:55 +05:30
|
|
|
stmts []string
|
2020-02-21 16:43:38 +05:30
|
|
|
|
|
|
|
// If flavor is nil the migration will take place for all database backend flavors.
|
|
|
|
// If specified, only for that corresponding flavor, in that case stmts can be written
|
|
|
|
// in the specific SQL dialect.
|
|
|
|
flavor *flavor
|
2016-09-15 06:41:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// All SQL flavors share migration strategies.
|
|
|
|
var migrations = []migration{
|
|
|
|
{
|
2017-04-21 21:21:55 +05:30
|
|
|
stmts: []string{`
|
2016-09-15 06:41:57 +05:30
|
|
|
create table client (
|
|
|
|
id text not null primary key,
|
|
|
|
secret text not null,
|
|
|
|
redirect_uris bytea not null, -- JSON array of strings
|
|
|
|
trusted_peers bytea not null, -- JSON array of strings
|
|
|
|
public boolean not null,
|
|
|
|
name text not null,
|
|
|
|
logo_url text not null
|
2017-04-21 21:21:55 +05:30
|
|
|
);`,
|
|
|
|
`
|
2016-09-15 06:41:57 +05:30
|
|
|
create table auth_request (
|
|
|
|
id text not null primary key,
|
|
|
|
client_id text not null,
|
|
|
|
response_types bytea not null, -- JSON array of strings
|
|
|
|
scopes bytea not null, -- JSON array of strings
|
|
|
|
redirect_uri text not null,
|
|
|
|
nonce text not null,
|
|
|
|
state text not null,
|
|
|
|
force_approval_prompt boolean not null,
|
2018-01-30 16:49:08 +05:30
|
|
|
|
2016-09-15 06:41:57 +05:30
|
|
|
logged_in boolean not null,
|
2018-01-30 16:49:08 +05:30
|
|
|
|
2016-09-15 06:41:57 +05:30
|
|
|
claims_user_id text not null,
|
|
|
|
claims_username text not null,
|
|
|
|
claims_email text not null,
|
|
|
|
claims_email_verified boolean not null,
|
|
|
|
claims_groups bytea not null, -- JSON array of strings
|
2018-01-30 16:49:08 +05:30
|
|
|
|
2016-09-15 06:41:57 +05:30
|
|
|
connector_id text not null,
|
2018-02-15 15:32:03 +05:30
|
|
|
connector_data bytea,
|
2018-01-30 16:49:08 +05:30
|
|
|
|
2016-12-17 00:33:36 +05:30
|
|
|
expiry timestamptz not null
|
2017-04-21 21:21:55 +05:30
|
|
|
);`,
|
|
|
|
`
|
2016-09-15 06:41:57 +05:30
|
|
|
create table auth_code (
|
|
|
|
id text not null primary key,
|
|
|
|
client_id text not null,
|
|
|
|
scopes bytea not null, -- JSON array of strings
|
|
|
|
nonce text not null,
|
|
|
|
redirect_uri text not null,
|
2018-01-30 16:49:08 +05:30
|
|
|
|
2016-09-15 06:41:57 +05:30
|
|
|
claims_user_id text not null,
|
|
|
|
claims_username text not null,
|
|
|
|
claims_email text not null,
|
|
|
|
claims_email_verified boolean not null,
|
|
|
|
claims_groups bytea not null, -- JSON array of strings
|
2018-01-30 16:49:08 +05:30
|
|
|
|
2016-09-15 06:41:57 +05:30
|
|
|
connector_id text not null,
|
2018-02-15 15:32:03 +05:30
|
|
|
connector_data bytea,
|
2018-01-30 16:49:08 +05:30
|
|
|
|
2016-12-17 00:33:36 +05:30
|
|
|
expiry timestamptz not null
|
2017-04-21 21:21:55 +05:30
|
|
|
);`,
|
|
|
|
`
|
2016-09-15 06:41:57 +05:30
|
|
|
create table refresh_token (
|
|
|
|
id text not null primary key,
|
|
|
|
client_id text not null,
|
|
|
|
scopes bytea not null, -- JSON array of strings
|
|
|
|
nonce text not null,
|
2018-01-30 16:49:08 +05:30
|
|
|
|
2016-09-15 06:41:57 +05:30
|
|
|
claims_user_id text not null,
|
|
|
|
claims_username text not null,
|
|
|
|
claims_email text not null,
|
|
|
|
claims_email_verified boolean not null,
|
|
|
|
claims_groups bytea not null, -- JSON array of strings
|
2018-01-30 16:49:08 +05:30
|
|
|
|
2018-02-15 15:32:03 +05:30
|
|
|
connector_id text not null,
|
|
|
|
connector_data bytea
|
2017-04-21 21:21:55 +05:30
|
|
|
);`,
|
|
|
|
`
|
2016-10-06 04:34:11 +05:30
|
|
|
create table password (
|
|
|
|
email text not null primary key,
|
|
|
|
hash bytea not null,
|
|
|
|
username text not null,
|
|
|
|
user_id text not null
|
2017-04-21 21:21:55 +05:30
|
|
|
);`,
|
|
|
|
`
|
2016-09-15 06:41:57 +05:30
|
|
|
-- keys is a weird table because we only ever expect there to be a single row
|
|
|
|
create table keys (
|
|
|
|
id text not null primary key,
|
|
|
|
verification_keys bytea not null, -- JSON array
|
|
|
|
signing_key bytea not null, -- JSON object
|
|
|
|
signing_key_pub bytea not null, -- JSON object
|
2016-12-17 00:33:36 +05:30
|
|
|
next_rotation timestamptz not null
|
2017-04-21 21:21:55 +05:30
|
|
|
);`,
|
|
|
|
},
|
2016-09-15 06:41:57 +05:30
|
|
|
},
|
2016-12-23 05:26:09 +05:30
|
|
|
{
|
2017-04-21 21:21:55 +05:30
|
|
|
stmts: []string{`
|
2016-12-23 05:26:09 +05:30
|
|
|
alter table refresh_token
|
2017-04-21 21:21:55 +05:30
|
|
|
add column token text not null default '';`,
|
|
|
|
`
|
2016-12-23 05:26:09 +05:30
|
|
|
alter table refresh_token
|
2017-04-21 21:21:55 +05:30
|
|
|
add column created_at timestamptz not null default '0001-01-01 00:00:00 UTC';`,
|
|
|
|
`
|
2016-12-23 05:26:09 +05:30
|
|
|
alter table refresh_token
|
2017-04-21 21:21:55 +05:30
|
|
|
add column last_used timestamptz not null default '0001-01-01 00:00:00 UTC';`,
|
|
|
|
},
|
2016-12-23 05:26:09 +05:30
|
|
|
},
|
2017-02-01 05:41:59 +05:30
|
|
|
{
|
2017-04-21 21:21:55 +05:30
|
|
|
stmts: []string{`
|
2017-02-01 05:41:59 +05:30
|
|
|
create table offline_session (
|
|
|
|
user_id text not null,
|
|
|
|
conn_id text not null,
|
|
|
|
refresh bytea not null,
|
|
|
|
PRIMARY KEY (user_id, conn_id)
|
2017-04-21 21:21:55 +05:30
|
|
|
);`,
|
|
|
|
},
|
2017-02-01 05:41:59 +05:30
|
|
|
},
|
2017-03-23 22:29:33 +05:30
|
|
|
{
|
2017-04-21 21:21:55 +05:30
|
|
|
stmts: []string{`
|
2017-03-23 22:29:33 +05:30
|
|
|
create table connector (
|
|
|
|
id text not null primary key,
|
|
|
|
type text not null,
|
|
|
|
name text not null,
|
|
|
|
resource_version text not null,
|
|
|
|
config bytea
|
2017-04-21 21:21:55 +05:30
|
|
|
);`,
|
|
|
|
},
|
2017-03-23 22:29:33 +05:30
|
|
|
},
|
2019-10-10 20:13:41 +05:30
|
|
|
{
|
|
|
|
stmts: []string{`
|
|
|
|
alter table auth_code
|
|
|
|
add column claims_preferred_username text not null default '';`,
|
|
|
|
`
|
|
|
|
alter table auth_request
|
|
|
|
add column claims_preferred_username text not null default '';`,
|
|
|
|
`
|
|
|
|
alter table refresh_token
|
|
|
|
add column claims_preferred_username text not null default '';`,
|
|
|
|
},
|
|
|
|
},
|
2018-02-15 15:32:03 +05:30
|
|
|
{
|
|
|
|
stmts: []string{`
|
|
|
|
alter table offline_session
|
2019-09-26 20:00:44 +05:30
|
|
|
add column connector_data bytea;
|
2018-02-15 15:32:03 +05:30
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
2020-02-21 16:44:42 +05:30
|
|
|
{
|
|
|
|
stmts: []string{`
|
|
|
|
alter table auth_request
|
|
|
|
modify column state varchar(4096);
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
flavor: &flavorMySQL,
|
|
|
|
},
|
2020-01-16 21:25:07 +05:30
|
|
|
{
|
|
|
|
stmts: []string{`
|
|
|
|
create table device_request (
|
|
|
|
user_code text not null primary key,
|
|
|
|
device_code text not null,
|
|
|
|
client_id text not null,
|
2020-02-04 20:37:18 +05:30
|
|
|
client_secret text ,
|
2020-01-16 21:25:07 +05:30
|
|
|
scopes bytea not null, -- JSON array of strings
|
|
|
|
expiry timestamptz not null
|
|
|
|
);`,
|
|
|
|
`
|
|
|
|
create table device_token (
|
|
|
|
device_code text not null primary key,
|
|
|
|
status text not null,
|
2020-06-03 00:09:30 +05:30
|
|
|
token bytea,
|
2020-01-29 00:44:30 +05:30
|
|
|
expiry timestamptz not null,
|
|
|
|
last_request timestamptz not null,
|
|
|
|
poll_interval integer not null
|
2020-01-16 21:25:07 +05:30
|
|
|
);`,
|
|
|
|
},
|
|
|
|
},
|
2016-09-15 06:41:57 +05:30
|
|
|
}
|