SQLC Override Bool Type PostgreSQL - postgresql

I am using SQLC and my YAML config file contains the following type overrides for postgresql:
gen:
go:
emit_json_tags: true
package: "hmdb"
out: "hmdb"
overrides:
- db_type: "hstore"
nullable: true
go_type: "github.com/jackc/pgtype.Hstore"
- db_type: "text"
nullable: true
go_type:
import: "gopkg.in/guregu/null.v4"
package: "null"
type: "String"
- db_type: "timestamptz"
nullable: true
go_type:
import: "gopkg.in/guregu/null.v4"
package: "null"
type: "Time"
Everything here works, but if I add:
- db_type: "bool"
nullable: true
go_type:
import: "gopkg.in/guregu/null.v4"
package: "null"
type: "Bool"
I do not get the expected result. I have also tried boolean and bit to no avail, both with and without nullable.
I have an update query defined here:
-- name: SetUser :one
UPDATE users SET
username = coalesce(sqlc.narg(username), username),
email = coalesce(sqlc.narg('email'), email),
phone = coalesce(sqlc.narg('phone'), phone),
password = coalesce(sqlc.narg('password'), password),
mfatoken = coalesce(sqlc.narg('mfatoken'), mfatoken),
active = coalesce(sqlc.narg('active'), active)
WHERE id = $1 RETURNING *;
But the generated struct looks like:
type SetUserParams struct {
ID uuid.UUID `json:"id"`
Username null.String `json:"username"`
Email null.String `json:"email"`
Phone null.String `json:"phone"`
Password null.String `json:"password"`
MFAToken null.String `json:"mfatoken"`
Active sql.NullBool `json:"active"`
}
I want to use null.Bool instead of sql.NullBool, is this possible?

Create your schema.yaml like this:
CREATE TABLE users (
...
active pg_catalog.bool
)
In sqlc.yaml the entry should look something like this:
- db_type: "pg_catalog.bool"
nullable: true
go_type:
import: "gopkg.in/guregu/null.v4"
package: "null"
type: "Bool"
Then after sqlc generate it would look like this:
type SetUserParams struct {
...
Active null.Bool `json:"active"`
}
So it uses null.Bool instead of sql.NullBool.

Related

How to add indexes to already created table in Liquibase

There is already created table 'A'
'id' 'b_id' 'name'
1 2 someName
Now I want to add unique index to id and b_id columns
How to do it in yml format
based on documentation, something like this:
changeSet:
id: addUniqueConstraint-example
author: liquibase-docs
changes:
- addUniqueConstraint:
catalogName: cat
clustered: false
columnNames: id, b_id
constraintName: const_name
deferrable: true
disabled: false
forIndexName:
initiallyDeferred: true
schemaName: [yourschema]
tableName: [yourtablename]
tablespace:
validate: true

Cannot pass dynamic query parameters when using `express-openapi-validator`

The idea is taken from here stack-overflow
After adding a parameter that is supposed do allow dynamic query parameters, it gives error.
Query Example:
/pets:
get:
description: |
Returns all pets
operationId: findPets
parameters:
- name: params
in: query
required: false
schema:
type: object
# If the parameter values are of specific type, e.g. string:
# additionalProperties:
# type: string
# If the parameter values can be of different types
# (e.g. string, number, boolean, ...)
additionalProperties: true
# `style: form` and `explode: true` is the default serialization method
# for query parameters, so these keywords can be omitted
style: form
explode: true
responses:
'200':
description: pet response
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
Executing a query, returns
{"message":"Cannot convert undefined or null to object"}
To Reproduce
Clone this repository
run npm install
run npm start
run curl http://localhost:3000/v1/pets\?type\=dog\&limit\=10\&test\=query
Expected behavior
It must allow all the query strings
This was the bug in the express-openapi-validator package.
It is now fixed in v4.4.2
To test out the functionality, see this example project

How to define and test for a null string in AWS CloudFormation template?

I have a CloudFormation template that accepts this parameter
TargetGroupName:
Type: String
Description: 'Parameter to override target group name'
Default: ''
However, instead of an empty string as default, how can I define is as a Null string, like this? I know Null is NOT a valid keyword, but I wanted to illustrate what I wanted to do.
TargetGroupName:
Type: String
Description: 'Parameter to override target group name'
Default: Null
And then, how can I set a condition to test for the Null string, like this?
Conditions:
CreateTargetGroup:
!Not [ !Equals [ !Ref TargetGroupName, Null ] ]
Of course the keyword Null throws a CloudFormation script validation exception since it's NOT a valid keyword.
You can do it like this:
Parameters:
TargetGroupName:
Description: 'Parameter to override target group name'
Type: String
Default: ""
Condition:
IsTargetGroupNameEmpty: !Equals [!Ref "TargetGroupName", ""]
And then in every resource that you don't want to create just pass this line:
!If [ IsTargetGroupNameEmpty, [ !Ref <RESOURCE_NAME> ], !Ref "AWS::NoValue" ]
Please take a look on this docs:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html#cfn-pseudo-param-novalue

Setting up spring boot project with liquibase

I have a spring boot 2 project(jhipster) and I am a little confused on how hibernate tables work with liquibase.
Right now I have a bunch of data classes with hibernate annotations and I would like to insert some static data for testing purposes. I have a bunch of questions in getting started.
Do I have to define liquibase changeSets to create tables when I already have the hibernate annotated data classes?
How do I run liquibase changeSets when configuring the database?
EDIT -----
How are the foreign key relationships named between the changeset and the domain(java) code? For example I have a Person table and it holds a reference to an Address table. How do I represent this relationship within the changeSet table?
The Person table doesn't hold an id of an Address, it holds the reference.
Spring boot has excellent integration with liquibase.
If you want liquibase to handle creation of the tables (DDL) and not hibernate:
you need to disable the hibernate auto-create flag.
set spring.jpa.hibernate.ddl-auto=none
(or) remove this property from application.yml file.
Just include following into pom.xml:
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
Below is the sample code to create tables and insert data into table.
File location:
src/main/resources/db/changelog/db.changelog-master.yaml
databaseChangeLog:
- changeSet:
id: 1
author: sgollapinni
changes:
- createTable:
tableName: person
columns:
- column:
name: id
type: int
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: first_name
type: varchar(255)
constraints:
nullable: false
- column:
name: last_name
type: varchar(255)
constraints:
nullable: false
- createTable:
tableName: address
columns:
- column:
name: id
type: int
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: city
type: varchar(255)
constraints:
nullable: false
- column:
name: person_id
type: varchar(255)
constraints:
nullable: false
foreignKeyName: fk_person_address
references: person(id)
- changeSet:
id: 2
author: sgollapinni
changes:
- insert:
tableName: person
columns:
- column:
name: first_name
value: Sunil
- column:
name: last_name
value: Kumar
- insert:
tableName: address
columns:
- column:
name: city
value: Bangalore
- column:
name: user_id
value: (Select id from person where name = 'Sunil')
Otherwise, if you want hibernate to handle the DDL and only you want to insert some static data for testing purposes, you can still do this using liquibase.
You can use changeSets to add the DML statements.
- changeSet:
id: 1
author: sgollapinni
changes:
- insert:
tableName: person
columns:
- column:
name: first_name
value: Sunil
- column:
name: last_name
value: Kumar
- insert:
tableName: address
columns:
- column:
name: city
value: Bangalore
- column:
name: user_id
value: (Select id from person where name = 'Sunil')
Hope it helps!

FactoryGirl gives wrong value on enum status field

I am trying to build a FactoryGirl factory for the Client.rb model:
Client.rb
enum status: [ :unregistered, :registered ]
has_many :quotation_requests
#Validations
validates :first_name,
presence: true,
length: {minimum: 2}
validates :last_name,
presence: true,
length: {minimum: 2}
validates :email, email: true
validates :status, presence: true
Factory:
FactoryGirl.define do
factory :client do
first_name "Peter"
last_name "Johnson"
sequence(:email) { |n| "peterjohnson#{n}#example.com" }
password "somepassword"
status "unregistered"
end
end
client_spec.rb
require 'rails_helper'
RSpec.describe Client, type: :model do
describe 'factory' do
it "has a valid factory" do
expect(FactoryGirl.build(:client).to be_valid
end
end
end
I get the following errorL
1) Client factory has a valid factory
Failure/Error: expect(FactoryGirl.build(:client, status: 'unregistered')).to be_valid
expected #<Client id: nil, email: "peterjohnson1#example.com", encrypted_password: "$2a$04$urndfdXNfKVqYB5t3kERZ.c.DUitIVXEZ6f19FNYZ2C...", first_name: "Peter", last_name: "Johnson", status: "0", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil> to be valid, but got errors: Status can't be blank
The error is that Status can't be blank.
I don't understand how this is possible as the factory is clearly assigning a value to the status attribute.
How can I get this factory to build a valid client object?
Rails 4.2
Using factory_girl 4.7.0
Using factory_girl_rails 4.7.0
This error was caused by the data type I used for the status attribute. I chose string instead of integer.
I solved the problem by running a new migration to change the data type of the status to integer.
class ChangeColumnTypeClientStatus < ActiveRecord::Migration
def change
change_column :clients, :status, :integer, default: 0
end
end
Now it works perfectly.
I think that you forgot the
let(:client) { FactoryGirl.create(:client) }
on your client_spec.rb
Where're you creating the client object?
Other issue may be that you assign on Factory:
status "unregistered"
instead of:
status :unregistered
as a symbol or due to is an enum maybe you should make
status 0 # :unregistered