1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
b1ackd0t 6f2ce82263
Don't throw an error if users status type exists
Signed-off-by: 0x6f736f646f <b1ackd0t@protonmail.com>

Signed-off-by: 0x6f736f646f <b1ackd0t@protonmail.com>
Co-authored-by: 0x6f736f646f <b1ackd0t@protonmail.com>
Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
2022-12-29 12:14:46 +01:00

100 lines
2.4 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package postgres
import (
"fmt"
_ "github.com/jackc/pgx/v5/stdlib" // required for SQL access
"github.com/jmoiron/sqlx"
migrate "github.com/rubenv/sql-migrate"
)
// Config defines the options that are used when connecting to a PostgreSQL instance
type Config struct {
Host string
Port string
User string
Pass string
Name string
SSLMode string
SSLCert string
SSLKey string
SSLRootCert string
}
// Connect creates a connection to the PostgreSQL instance and applies any
// unapplied database migrations. A non-nil error is returned to indicate
// failure.
func Connect(cfg Config) (*sqlx.DB, error) {
url := fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=%s sslcert=%s sslkey=%s sslrootcert=%s", cfg.Host, cfg.Port, cfg.User, cfg.Name, cfg.Pass, cfg.SSLMode, cfg.SSLCert, cfg.SSLKey, cfg.SSLRootCert)
db, err := sqlx.Open("pgx", url)
if err != nil {
return nil, err
}
if err := migrateDB(db); err != nil {
return nil, err
}
return db, nil
}
func migrateDB(db *sqlx.DB) error {
migrations := &migrate.MemoryMigrationSource{
Migrations: []*migrate.Migration{
{
Id: "users_1",
Up: []string{
`CREATE TABLE IF NOT EXISTS users (
email VARCHAR(254) PRIMARY KEY,
password CHAR(60) NOT NULL
)`,
},
Down: []string{"DROP TABLE users"},
},
{
Id: "users_2",
Up: []string{
`ALTER TABLE IF EXISTS users ADD COLUMN IF NOT EXISTS metadata JSONB`,
},
},
{
Id: "users_3",
Up: []string{
`CREATE EXTENSION IF NOT EXISTS "pgcrypto";
ALTER TABLE IF EXISTS users ADD COLUMN IF NOT EXISTS
id UUID NOT NULL DEFAULT gen_random_uuid()`,
},
},
{
Id: "users_4",
Up: []string{
`ALTER TABLE IF EXISTS users DROP CONSTRAINT users_pkey`,
`ALTER TABLE IF EXISTS users ADD CONSTRAINT users_email_key UNIQUE (email)`,
`ALTER TABLE IF EXISTS users ADD PRIMARY KEY (id)`,
},
},
{
Id: "users_5",
Up: []string{
`DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'user_status') THEN
CREATE TYPE user_status AS ENUM ('enabled', 'disabled');
END IF;
END$$;`,
`ALTER TABLE IF EXISTS users ADD COLUMN IF NOT EXISTS
status USER_STATUS NOT NULL DEFAULT 'enabled'`,
},
},
},
}
_, err := migrate.Exec(db.DB, "postgres", migrations, migrate.Up)
return err
}