Monday, September 2, 2019

How to use git to connect to Github and Gitlab on the same machine

Well, easy problem to solve.

Just edit/create the SSH config file ~/.ssh/config and add the two providers

Host github
HostName github.com 
IdentityFile ~/.ssh/github

Host gitlab
HostName gilab.com 
IdentityFile ~/.ssh/gitlab
changing/defining your SSH keys for each provider.

Go to Github config area and Gitlab config area and add your PUBLIC key (normally something like id_rsa.pub) to the list of keys.

That's all, now you can work with both platforms.


Saturday, August 31, 2019

Setting up PostgreSQL DB with Java Spring Boot and Gradle

Let's start checking the dependencies, considering that I am using for this example spring boot 2.1.7.

Go to your build.gradle file and in the section dependencies be sure that you have the following:

compile 'org.springframework.boot:spring-boot-starter-jdbc'
compile 'org.postgresql:postgresql:42.2.2'

If you still don't have the resources.config.application.yml file, create it or edit it adding the following lines:

spring:
  application:
    name: appname
  profiles:
    active: dev

spring.datasource:
  driver-class-name: org.postgresql.Driver
  url: ${ENV_DATASOURCE_URL}
  username: ${ENV_DB_APP_USER}
  password: ${ENV_DB_APP_PASSWORD}

server:
  port: 8080
  servlet.context-path: /appname

logging.file: logs/${spring.application.name}.log

and create an environment variables file outside the project, for example, .env file in your project root folder. Don't forget to add the file to .gitignore. For example:

ENV_DB_HOST=development
ENV_DB_PORT=5432
ENV_DB_SCHEMA=schema
ENV_DB_APP_USER=username
ENV_DB_APP_PASSWORD=password

and run

$ export $(cat .env | xargs)

Now create a resolurces.schema.sql file with the schema that you want to create. For example:

CREATE TABLE clients
(
  id                    bigserial primary key,
  uuid                  varchar(255)             not null unique,
  name                  text                     not null,
  version               varchar(64)              null,
  template              json                     null,
  create_time           timestamp with time zone not null,
  update_time           timestamp with time zone not null
);

And... that's all! You are ready to start working with your PostgreSQL database.

You can include in your resources.application.properties file the following lines

spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
#spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.ddl-auto=none

The first remove a possible error launching your project. The second drop and creates the database using the schema.sql file that you defined previously. You should use this line only once and never in production. The third line deactivates this functionality. I recommend you to use update if you want to start using migrations or jump directly into Flyway to versioning your migrations.