diff --git a/worker/gen/go/db/mysql/db.go b/worker/gen/go/db/mysql/db.go new file mode 100644 index 0000000000..2155d3295c --- /dev/null +++ b/worker/gen/go/db/mysql/db.go @@ -0,0 +1,24 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.23.0 + +package mysql_queries + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New() *Queries { + return &Queries{} +} + +type Queries struct { +} diff --git a/worker/gen/go/db/mysql/models.go b/worker/gen/go/db/mysql/models.go new file mode 100644 index 0000000000..66f2ce7820 --- /dev/null +++ b/worker/gen/go/db/mysql/models.go @@ -0,0 +1,7 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.23.0 + +package mysql_queries + +import () diff --git a/worker/gen/go/db/mysql/querier.go b/worker/gen/go/db/mysql/querier.go new file mode 100644 index 0000000000..89297590e0 --- /dev/null +++ b/worker/gen/go/db/mysql/querier.go @@ -0,0 +1,18 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.23.0 + +package mysql_queries + +import ( + "context" +) + +type Querier interface { + GetDatabaseSchema(ctx context.Context, db DBTX) ([]*GetDatabaseSchemaRow, error) + GetDatabaseTableSchema(ctx context.Context, db DBTX, arg *GetDatabaseTableSchemaParams) ([]*GetDatabaseTableSchemaRow, error) + GetForeignKeyConstraints(ctx context.Context, db DBTX, tableSchema string) ([]*GetForeignKeyConstraintsRow, error) + GetTableConstraints(ctx context.Context, db DBTX, arg *GetTableConstraintsParams) ([]*GetTableConstraintsRow, error) +} + +var _ Querier = (*Queries)(nil) diff --git a/worker/gen/go/db/mysql/system.sql.go b/worker/gen/go/db/mysql/system.sql.go new file mode 100644 index 0000000000..822e367af0 --- /dev/null +++ b/worker/gen/go/db/mysql/system.sql.go @@ -0,0 +1,280 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.23.0 +// source: system.sql + +package mysql_queries + +import ( + "context" + "database/sql" +) + +const getDatabaseSchema = `-- name: GetDatabaseSchema :many +SELECT + c.table_schema, + c.table_name, + c.column_name, + c.ordinal_position, + COALESCE(c.column_default, 'NULL') as column_default, -- must coalesce because sqlc doesn't appear to work for system structs to output a *string + c.is_nullable, + c.data_type +FROM + information_schema.columns AS c + JOIN information_schema.tables AS t ON c.table_schema = t.table_schema + AND c.table_name = t.table_name +WHERE + c.table_schema NOT IN('sys', 'performance_schema', 'mysql') + AND t.table_type = 'BASE TABLE' +` + +type GetDatabaseSchemaRow struct { + TableSchema string + TableName string + ColumnName string + OrdinalPosition int32 + ColumnDefault string + IsNullable string + DataType string +} + +func (q *Queries) GetDatabaseSchema(ctx context.Context, db DBTX) ([]*GetDatabaseSchemaRow, error) { + rows, err := db.QueryContext(ctx, getDatabaseSchema) + if err != nil { + return nil, err + } + defer rows.Close() + var items []*GetDatabaseSchemaRow + for rows.Next() { + var i GetDatabaseSchemaRow + if err := rows.Scan( + &i.TableSchema, + &i.TableName, + &i.ColumnName, + &i.OrdinalPosition, + &i.ColumnDefault, + &i.IsNullable, + &i.DataType, + ); err != nil { + return nil, err + } + items = append(items, &i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getDatabaseTableSchema = `-- name: GetDatabaseTableSchema :many +SELECT + c.table_schema, + c.table_name, + c.column_name, + c.ordinal_position, + COALESCE(c.column_default, 'NULL') as column_default, -- must coalesce because sqlc doesn't appear to work for system structs to output a *string + c.is_nullable, + c.data_type +FROM + information_schema.columns AS c + JOIN information_schema.tables AS t ON c.table_schema = t.table_schema + AND c.table_name = t.table_name +WHERE + c.table_schema = ? AND t.table_name = ? + AND t.table_type = 'BASE TABLE' + ORDER BY c.ordinal_position ASC +` + +type GetDatabaseTableSchemaParams struct { + TableSchema string + TableName string +} + +type GetDatabaseTableSchemaRow struct { + TableSchema string + TableName string + ColumnName string + OrdinalPosition int32 + ColumnDefault string + IsNullable string + DataType string +} + +func (q *Queries) GetDatabaseTableSchema(ctx context.Context, db DBTX, arg *GetDatabaseTableSchemaParams) ([]*GetDatabaseTableSchemaRow, error) { + rows, err := db.QueryContext(ctx, getDatabaseTableSchema, arg.TableSchema, arg.TableName) + if err != nil { + return nil, err + } + defer rows.Close() + var items []*GetDatabaseTableSchemaRow + for rows.Next() { + var i GetDatabaseTableSchemaRow + if err := rows.Scan( + &i.TableSchema, + &i.TableName, + &i.ColumnName, + &i.OrdinalPosition, + &i.ColumnDefault, + &i.IsNullable, + &i.DataType, + ); err != nil { + return nil, err + } + items = append(items, &i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getForeignKeyConstraints = `-- name: GetForeignKeyConstraints :many +SELECT +rc.constraint_name +, +kcu.table_schema AS schema_name +, +kcu.table_name as table_name +, +kcu.column_name as column_name +, +kcu.referenced_table_schema AS foreign_schema_name +, +kcu.referenced_table_name AS foreign_table_name +, +kcu.referenced_column_name AS foreign_column_name +FROM + information_schema.referential_constraints rc +JOIN information_schema.key_column_usage kcu + ON + kcu.constraint_name = rc.constraint_name +WHERE + kcu.table_schema = ? +ORDER BY + rc.constraint_name, + kcu.ordinal_position +` + +type GetForeignKeyConstraintsRow struct { + ConstraintName string + SchemaName string + TableName string + ColumnName string + ForeignSchemaName string + ForeignTableName string + ForeignColumnName string +} + +func (q *Queries) GetForeignKeyConstraints(ctx context.Context, db DBTX, tableSchema string) ([]*GetForeignKeyConstraintsRow, error) { + rows, err := db.QueryContext(ctx, getForeignKeyConstraints, tableSchema) + if err != nil { + return nil, err + } + defer rows.Close() + var items []*GetForeignKeyConstraintsRow + for rows.Next() { + var i GetForeignKeyConstraintsRow + if err := rows.Scan( + &i.ConstraintName, + &i.SchemaName, + &i.TableName, + &i.ColumnName, + &i.ForeignSchemaName, + &i.ForeignTableName, + &i.ForeignColumnName, + ); err != nil { + return nil, err + } + items = append(items, &i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getTableConstraints = `-- name: GetTableConstraints :many +SELECT +kcu.constraint_name +, +kcu.table_schema AS db_schema +, +kcu.table_name as table_name +, +kcu.column_name as column_name +, +kcu.referenced_table_schema AS foreign_schema_name +, +kcu.referenced_table_name AS foreign_table_name +, +kcu.referenced_column_name AS foreign_column_name +, +rc.update_rule +, +rc.delete_rule +FROM information_schema.key_column_usage kcu +LEFT JOIN information_schema.referential_constraints rc + ON + kcu.constraint_name = rc.constraint_name +WHERE + kcu.table_schema = ? AND kcu.table_name = ? +` + +type GetTableConstraintsParams struct { + TableSchema string + TableName string +} + +type GetTableConstraintsRow struct { + ConstraintName string + DbSchema string + TableName string + ColumnName string + ForeignSchemaName string + ForeignTableName string + ForeignColumnName string + UpdateRule sql.NullString + DeleteRule sql.NullString +} + +func (q *Queries) GetTableConstraints(ctx context.Context, db DBTX, arg *GetTableConstraintsParams) ([]*GetTableConstraintsRow, error) { + rows, err := db.QueryContext(ctx, getTableConstraints, arg.TableSchema, arg.TableName) + if err != nil { + return nil, err + } + defer rows.Close() + var items []*GetTableConstraintsRow + for rows.Next() { + var i GetTableConstraintsRow + if err := rows.Scan( + &i.ConstraintName, + &i.DbSchema, + &i.TableName, + &i.ColumnName, + &i.ForeignSchemaName, + &i.ForeignTableName, + &i.ForeignColumnName, + &i.UpdateRule, + &i.DeleteRule, + ); err != nil { + return nil, err + } + items = append(items, &i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/worker/gen/go/db/postgresql/system.sql.go b/worker/gen/go/db/postgresql/system.sql.go index 20d52715f3..cd941edd0d 100644 --- a/worker/gen/go/db/postgresql/system.sql.go +++ b/worker/gen/go/db/postgresql/system.sql.go @@ -15,7 +15,7 @@ SELECT c.table_name, c.column_name, c.ordinal_position, - COALESCE(c.column_default, 'NULL') as column_default, + COALESCE(c.column_default, 'NULL') as column_default, -- must coalesce because sqlc doesn't appear to work for system structs to output a *string c.is_nullable, c.data_type FROM @@ -71,7 +71,7 @@ SELECT c.table_name, c.column_name, c.ordinal_position, - COALESCE(c.column_default, 'NULL') as column_default, + COALESCE(c.column_default, 'NULL') as column_default, -- must coalesce because sqlc doesn't appear to work for system structs to output a *string c.is_nullable, c.data_type FROM diff --git a/worker/sql/mysql/models/models.go b/worker/sql/mysql/models/models.go new file mode 100644 index 0000000000..2640e7f93e --- /dev/null +++ b/worker/sql/mysql/models/models.go @@ -0,0 +1 @@ +package models diff --git a/worker/sql/mysql/queries/system.sql b/worker/sql/mysql/queries/system.sql new file mode 100644 index 0000000000..7a69867124 --- /dev/null +++ b/worker/sql/mysql/queries/system.sql @@ -0,0 +1,86 @@ +-- name: GetDatabaseSchema :many +SELECT + c.table_schema, + c.table_name, + c.column_name, + c.ordinal_position, + COALESCE(c.column_default, 'NULL') as column_default, -- must coalesce because sqlc doesn't appear to work for system structs to output a *string + c.is_nullable, + c.data_type +FROM + information_schema.columns AS c + JOIN information_schema.tables AS t ON c.table_schema = t.table_schema + AND c.table_name = t.table_name +WHERE + c.table_schema NOT IN('sys', 'performance_schema', 'mysql') + AND t.table_type = 'BASE TABLE'; + +-- name: GetDatabaseTableSchema :many +SELECT + c.table_schema, + c.table_name, + c.column_name, + c.ordinal_position, + COALESCE(c.column_default, 'NULL') as column_default, -- must coalesce because sqlc doesn't appear to work for system structs to output a *string + c.is_nullable, + c.data_type +FROM + information_schema.columns AS c + JOIN information_schema.tables AS t ON c.table_schema = t.table_schema + AND c.table_name = t.table_name +WHERE + c.table_schema = ? AND t.table_name = ? + AND t.table_type = 'BASE TABLE' + ORDER BY c.ordinal_position ASC; + +-- name: GetTableConstraints :many +SELECT +kcu.constraint_name +, +kcu.table_schema AS db_schema +, +kcu.table_name as table_name +, +kcu.column_name as column_name +, +kcu.referenced_table_schema AS foreign_schema_name +, +kcu.referenced_table_name AS foreign_table_name +, +kcu.referenced_column_name AS foreign_column_name +, +rc.update_rule +, +rc.delete_rule +FROM information_schema.key_column_usage kcu +LEFT JOIN information_schema.referential_constraints rc + ON + kcu.constraint_name = rc.constraint_name +WHERE + kcu.table_schema = ? AND kcu.table_name = ?; + +-- name: GetForeignKeyConstraints :many +SELECT +rc.constraint_name +, +kcu.table_schema AS schema_name +, +kcu.table_name as table_name +, +kcu.column_name as column_name +, +kcu.referenced_table_schema AS foreign_schema_name +, +kcu.referenced_table_name AS foreign_table_name +, +kcu.referenced_column_name AS foreign_column_name +FROM + information_schema.referential_constraints rc +JOIN information_schema.key_column_usage kcu + ON + kcu.constraint_name = rc.constraint_name +WHERE + kcu.table_schema = ? +ORDER BY + rc.constraint_name, + kcu.ordinal_position; diff --git a/worker/sql/mysql/schema/system.sql b/worker/sql/mysql/schema/system.sql new file mode 100644 index 0000000000..5d8329c80c --- /dev/null +++ b/worker/sql/mysql/schema/system.sql @@ -0,0 +1,32 @@ +create schema information_schema; +CREATE TABLE information_schema.columns ( + table_schema text not null, + table_name text not null, + column_name text not null, + ordinal_position int not null, + column_default text null, + is_nullable text not null, + data_type text not null +); + +create table information_schema.tables ( + table_schema text not null, + table_name text not null +); + +create table information_schema.key_column_usage ( + constraint_name text not null, + table_schema text not null, + table_name text not null, + column_name text not null, + ordinal_position int not null, + referenced_table_schema text not null, + referenced_table_name text not null, + referenced_column_name text not null +); + +create table information_schema.referential_constraints ( + constraint_name text not null, + update_rule text not null, + delete_rule text not null +); diff --git a/worker/sqlc.yaml b/worker/sqlc.yaml index 15df9395b8..5a00e724a0 100644 --- a/worker/sqlc.yaml +++ b/worker/sqlc.yaml @@ -41,3 +41,16 @@ sql: go_type: string - column: information_schema.key_column_usage.column_name go_type: string + + - engine: "mysql" + queries: "sql/mysql/queries" + schema: "sql/mysql/schema" + gen: + go: + package: mysql_queries + out: "gen/go/db/mysql" + emit_interface: true + emit_methods_with_db_argument: true + emit_result_struct_pointers: true + emit_params_struct_pointers: true + emit_pointers_for_null_types: true