go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/db/migration/predicates.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package migration 9 10 import ( 11 "context" 12 "database/sql" 13 "strings" 14 15 "go.charczuk.com/sdk/db" 16 ) 17 18 // PredicateTableExists returns if a table exists in a specific schema on the given connection. 19 func PredicateTableExists(ctx context.Context, c *db.Connection, tx *sql.Tx, tableName string) (bool, error) { 20 return c.Invoke(db.OptContext(ctx), db.OptTx(tx)).Query( 21 `SELECT 1 FROM pg_catalog.pg_tables WHERE tablename = $1`, 22 tableName, 23 ).Any() 24 } 25 26 // PredicateColumnExists returns if a column exists on a table in a specific schema on the given connection. 27 func PredicateColumnExists(ctx context.Context, c *db.Connection, tx *sql.Tx, tableName, columnName string) (bool, error) { 28 return c.Invoke(db.OptContext(ctx), db.OptTx(tx)).Query( 29 `SELECT 1 FROM information_schema.columns WHERE column_name = $1 AND table_name = $2`, 30 columnName, 31 tableName, 32 ).Any() 33 } 34 35 // PredicateConstraintExists returns if a constraint exists on a table in a specific schema on the given connection. 36 func PredicateConstraintExists(ctx context.Context, c *db.Connection, tx *sql.Tx, tableName, constraintName string) (bool, error) { 37 return c.Invoke(db.OptContext(ctx), db.OptTx(tx)).Query( 38 `SELECT 1 FROM information_schema.constraint_column_usage WHERE constraint_name = $1 AND table_name = $2`, 39 constraintName, 40 tableName, 41 ).Any() 42 } 43 44 // PredicateIndexExists returns if a index exists on a table in a specific schema on the given connection. 45 func PredicateIndexExists(ctx context.Context, c *db.Connection, tx *sql.Tx, tableName, indexName string) (bool, error) { 46 return c.Invoke(db.OptContext(ctx), db.OptTx(tx)).Query( 47 `SELECT 1 FROM pg_catalog.pg_indexes where indexname = $1 and tablename = $2`, 48 strings.ToLower(indexName), strings.ToLower(tableName)).Any() 49 } 50 51 // PredicateRoleExists returns if a role exists or not. 52 func PredicateRoleExists(ctx context.Context, c *db.Connection, tx *sql.Tx, roleName string) (bool, error) { 53 return c.Invoke(db.OptContext(ctx), db.OptTx(tx)).Query(`SELECT 1 FROM pg_catalog.pg_roles WHERE rolname ilike $1`, roleName).Any() 54 } 55 56 // PredicateAny returns if a statement has results. 57 func PredicateAny(ctx context.Context, c *db.Connection, tx *sql.Tx, selectStatement string, params ...interface{}) (bool, error) { 58 return c.Invoke(db.OptContext(ctx), db.OptTx(tx)).Query(selectStatement, params...).Any() 59 } 60 61 // PredicateNone returns if a statement doesnt have results. 62 func PredicateNone(ctx context.Context, c *db.Connection, tx *sql.Tx, selectStatement string, params ...interface{}) (bool, error) { 63 return c.Invoke(db.OptContext(ctx), db.OptTx(tx)).Query(selectStatement, params...).None() 64 } 65 66 // Not inverts the output of a predicate. 67 func Not(proceed bool, err error) (bool, error) { 68 return !proceed, err 69 }