Liquibase ignorable comments - liquibase

Is there a way we can add comments in liquibase file which are not parsed by the program?
We are using the text format for the changes.sql and this is how it looks
--changeset Sapan.Parikh:MyUniqueAlphaNumericId5
--comment: Table created for liquibase testing purpose with non numeric id
--6:10 PM 11/10/2015
create table liqui_test11 (
id int primary key,
name varchar(255)
);
create table liqui_test9 (
id int primary key,
name varchar(255)
);
create table liqui_test10 (
id int primary key,
name varchar(255)
);
Our organization has been using similar change log for years and while migrating to Liquibase we want to be able to do two things.
Add dashes or hashes after each changeset.
And after every production build we add a comment at the end of the changes file.
For instance
--changeset Sapan.Parikh:MyUniqueAlphaNumericId5
--comment: Table created for liquibase testing purpose with non numeric id
--6:10 PM 11/10/2015
create table liqui_test11 (
id int primary key,
name varchar(255)
);
-----------------------------------------------------------------
--changeset Sapan.Parikh:MyUniqueAlphaNumericId4
--comment: Table created for liquibase testing purpose with non numeric id
--6:10 PM 11/10/2015
create table liqui_test12 (
id int primary key,
name varchar(255)
);
###------------------Build 10.0.1 Made-------------------
Now if we add just dashes- or # the luqibase task is breaking and DB upgrade does not happen. Is there a way to accommodate comments which are not parsed by liquibase engine?

You can just persist your comments and strip them right before executing liquibase
- can be done easily using sed

Related

Golang-migrate returns "no change"

I'm starting learn Go and SQL. I tryed to make initial migration using golang-migrate in my go project. Database is postgresql
This is migration file:
CREATE TABLE users
(
id serial not null unique,
name varchar(255) not null,
username varchar(255) not null unique,
password_hash varchar(255) not null,
)
CREATE TABLE todo_lists
(
id serial not null unique,
title varchar(255) not null,
description varchar(255),
);
CREATE TABLE users_lists
(
id serial not null unique,
user_id int references users (id) on delete cascade not null,
list_id int references todo_lists (id) on delete cascade not null,
);
CREATE TABLE todo_items
(
id serial not null unique,
title varchar(255) not null,
description varchar(255),
done boolean not null default false,
);
CREATE TABLE lists_items
(
id serial not null unique,
item_id int references todo_items (id) on delete cascade not null,
list_id int references todo_lists (id) on delete cascade not null,
);
The command I use:
migrate -path ./schema -database 'postgres://postgres:root#localhost:5432/to_do?sslmode=disable' up
And bash returns:
no change (without any error)
Where can be problem?
I put together a small guide to help you solve your issue. Please be sure to follow along and you would be good to go!
Run Postgres
To run the Postgres instance I used to test my solution I used the following command:
docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=postgres postgres
This command spins up an instance of Postgres with these two things:
It sets up the port mapping (the port 5432 on your machine is mapped to the port 5432 of your Docker container).
It sets the password for the postgres user to postgres (just for the sake of the demo).
Create the migration scripts
To create the first migration script, I used the following command:
migrate create -ext sql -dir db/migrations -seq create_users_table
Thanks to this command, I was able to create the folder path db/migrations and two files within it (one for the up migration and one for the down one).
Fill the files with the code
The next step was to fill the above-created files with the CREATE and DROP statements. Let's start with the up one.
000001_create_users_table.up.sql file
CREATE TABLE IF NOT EXISTS users
(
id serial not null unique,
name varchar(255) not null,
username varchar(255) not null,
password_hash varchar(255) not null
);
CREATE TABLE IF NOT EXISTS todo_lists
(
id serial not null unique,
title varchar(255) not null,
description varchar(255)
);
CREATE TABLE IF NOT EXISTS users_lists
(
id serial not null unique,
user_id int references users (id) on delete cascade not null,
list_id int references todo_lists (id) on delete cascade not null
);
CREATE TABLE IF NOT EXISTS todo_items
(
id serial not null unique,
title varchar(255) not null,
description varchar(255),
done boolean not null default false
);
CREATE TABLE IF NOT EXISTS lists_items
(
id serial not null unique,
item_id int references todo_items (id) on delete cascade not null,
list_id int references todo_lists (id) on delete cascade not null
);
To make the migration idempotent I added the IF NOT EXISTS check. Take this as a best practice when you're about to write migrations.
000001_create_users_table.down.sql file
DROP TABLE IF EXISTS users_lists;
DROP TABLE IF EXISTS lists_items;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS todo_lists;
DROP TABLE IF EXISTS todo_items;
The same applies here with the IF EXISTS check. Pay attention to the order in which you're deleting stuff as you can easily get into error due to objects' dependencies.
Run migrations
To run this migration, be sure that the to_do DB is created.
To apply the migration run:
migrate -database 'postgres://postgres:postgres#localhost:5432/to_do?sslmode=disable' -path ./db/migrations up
With this, you'll get this output: 1/u create_users_table (44.392422ms).
If you run this twice, the second output will be: no change.
When you want to undo the migration, you've to run the following statement:
migrate -database 'postgres://postgres:postgres#localhost:5432/to_do?sslmode=disable' -path ./db/migrations down
This will undo all of the migrations applied so far. For a deeper understanding, please refer to the official doc: https://github.com/golang-migrate/migrate#cli-usage.
Let me know if this solves your issue or if you need anything else!
I resolve this problem by deleting migration table and correcting sql file (delete ',' in every string of last columns)

H2 refuses to create auto_increment for Postgres emulated database

I created an in memory H2 database with JDBC URL
jdbc:h2:~/test;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH
The H2 web console refuses to let me do an auto_increment. I've seen serial for Postgres, but that doesn't work either.
At it's simplest, it hates:
create table test(id bigint auto_increment);
Syntax error in SQL statement "create table test(id bigint [*]auto_increment)"; expected "ARRAY, INVISIBLE, VISIBLE, NOT NULL, NULL, AS, DEFAULT, GENERATED, ON UPDATE, NOT NULL, NULL, DEFAULT ON NULL, NULL_TO_DEFAULT, SEQUENCE, SELECTIVITY, COMMENT, CONSTRAINT, COMMENT, PRIMARY KEY, UNIQUE, NOT NULL, NULL, CHECK, REFERENCES, ,, )"; SQL statement:
create table test(id bigint auto_increment) [42001-214] 42001/42001 (Help)
Why do I care:
My code base was failing with NULL not allowed for column "REV". I'm using JPA/Hibernate + Liquibase. In order to try the suggestions at
Hibernate Envers + Liquibase: NULL not allowed for column "REV"
I'm trying to add an auto_increment to my Liquibase changelog file.
You can use the SQL Standard's generation clause GENERATED ALWAYS AS IDENTITY. For example:
create table test (
id bigint generated always as identity,
name varchar(10)
);
See PostgreSQL Example.
It works the same way in H2. For example:
create table test(id bigint generated always as identity, name varchar(10));
insert into test (name) values ('Chicago') ;
select * from test;
Result:
ID NAME
-- -------
1 Chicago

Why is the column not altering when I try to convert it to UUID?

I have a primary key column in my SQL table in PostgreSQL named "id". It is a "bigseries" column. I want to convert the column to a "UUID" column. It entered the below command in the terminal:
alter table people alter column id uuid;
and
alter table people alter column id uuid using (uuid_generate_v4());
but neither of them worked.
In both tries I got the error message
ERROR: syntax error at or near "uuid"
LINE 1: alter table people alter column id uuid using (uuid_generate...
What is the correct syntax?
First of all uuid_generate_v4() is a function which is provided by an extension called uuid-ossp. You should have install that extension by using;
CREATE EXTENSION uuid-ossp;
Postgresql 13 introduced a new function which does basically the same without installing extension. The function is called gen_random_uuid()
Suppose that we have a table like the one below;
CREATE TABLE people (
id bigserial primary key,
data text
);
The bigserial is not a real type. It's a macro which basically creates bigint column with default value and a sequence. The default value is next value of that sequence.
For your use case, to change data type, you first should drop the old default value. Then, alter the type and finally add new default value expression. Here is the sample:
ALTER TABLE people
ALTER id DROP DEFAULT,
ALTER id TYPE uuid using (gen_random_uuid() /* or uuid_generate_v4() */ ),
ALTER id SET DEFAULT gen_random_uuid() /* or uuid_generate_v4() */ ;
CREATE TABLE IF NOT EXISTS people (
id uuid NOT NULL CONSTRAINT people_pkey PRIMARY KEY,
address varchar,
city varchar(255),
country varchar(255),
email varchar(255),
phone varchar(255)
);
This is the correct syntax to create table in postgres SQL, it's better to do these constraints at beginning to avoid any error.
For using alter command you would do the following:
ALTER TABLE customer ADD COLUMN cid uuid PRIMARY KEY;
Most of errors that you could find while writing command either lower case or undefined correct the table name or column.

Creating a table in SQL using Oracle 11G

I am new to learning SQL and have been struggling to create a table for an assignment. These are the requirements:
Create a new table to track the Library location.
LIBRARY (lib_id, lib_name, lib_address, lib_city, lib_state, lib_zip)
LIB_ID is the library id – it is an auto generated number. (you should create a sequence number called lib_id_seq, start with 1001 and increment by 1.)
LIB_ID is the primary key.
LIB_NAME, LIB_ADDRESS, and LIB_CITY is between 1 and 35 characters.
LIB_STATE is 2 characters – default to TX.
LIB_ZIP is 5 numbers. Check for one of the following zip codes – 75081, 75080, 75082, 75079, 75078
And this is what I have written out so far:
CREATE TABLE LIBRARY
(
LIB_ID INT(4),
LIB_ADDRESS VARCHAR(35),
LIB_CITY VARCHAR(35),
LIB_STATE VARCHAR(2) DEFAULT ‘TX’,
LIB_ZIP INT(5) CHECK (Frequency IN ('75078', ‘75079', '75080', '75081', ‘75082’))
PRIMARY KEY(LIB_ID)
);
CREATE SEQUENCE LIB_ID_SEQ
START WITH 1001
INCREMENT BY 1;
I keep getting errors, but am not sure what I need to fix.
For oracle (Kid Tested unsure if SO approved)...
use varchar2 instead of varchar
use Number instead of int
added constraint syntax (named them)
adjusted apostrophe's (Removed) instead of whatever the heck you had in some of them :P (It's a numeric field shouldn't be using text apostrophes!)
personally I wouldn't name a table library as that's a reserved word
I woudln't use a numeric Zip code as we will never do math on a zipcode.
.
.
CREATE TABLE LIBRARY (
LIB_ID Number(4),
LIB_ADDRESS VARCHAR2(35),
LIB_CITY VARCHAR2(35),
LIB_STATE VARCHAR2(2) DEFAULT 'TX',
LIB_ZIP NUMBER(5),
CONSTRAINT Lib_ZIP_CON CHECK (LIB_ZIP IN (75078, 75079, 75080, 75081, 75082)),
CONSTRAINT LIB_ID_PK PRIMARY KEY(LIB_ID)
);
CREATE SEQUENCE LIB_ID_SEQ
START WITH 1001
INCREMENT BY 1;
This works for SQL Server. You need to modify the syntax accordingly for the concerned db.
CREATE TABLE LIBRARY
(
LIB_ID INTEGER PRIMARY KEY,
LIB_ADDRESS VARCHAR(35),
LIB_CITY VARCHAR(35),
LIB_STATE VARCHAR(2) DEFAULT 'TX',
LIB_ZIP INTEGER,
CHECK( LIB_ZIP IN ('75078', '75079', '75080', '75081', '75082') )
);
CREATE SEQUENCE LIB_ID_SEQ
START WITH 1001
INCREMENT BY 1;
For learning how to create tables and constraints check this link on w3schools as you seem to be a beginner.
http://www.w3schools.com/sql/sql_primarykey.asp

In H2 Database, add index while table creation in single query

I am trying to create table having different indexes with single query but H2 gives Error for example:
create table tbl_Cust
(
id int primary key auto_increment not null,
fid int,
c_name varchar(50),
INDEX (fid)
);
but this gives error as
Unknown data type: "("; SQL statement:
[Error Code: 50004]
[SQL State: HY004]
Due to this I have to run 2 different queries to create table with Index. First query to create table and then second query to add index with
create INDEX c_fid on tbl_Cust(fid);
Is there something wrong in my query or H2 simply does not support this creation of table with index in single query?
Interesting question. The solution is even more interesting, as it involves MySQL compatibility mode.
It's actually possible to perform the exact same command you wrote without any modification, provided you just add to your jdbc url the MySQL mode.
Example URL like this: jdbc:h2:mem:;mode=mysql
SQL remains:
create table tbl_Cust
(
id int primary key auto_increment not null,
fid int,
c_name varchar(50),
INDEX (fid)
);
Update count: 0
(15 ms)
Too bad I did not see this question earlier... Hopefully the solution might become handy one day to someone :-)
I could resolve the problem. According to
http://www.h2database.com/html/grammar.html#create_index
I modified the query. It works fine with my H2 server.
CREATE TABLE subscription_validator (
application_id int(11) NOT NULL,
api_id int(11) NOT NULL,
validator_id int(11) NOT NULL,
PRIMARY KEY (application_id,api_id),
CONSTRAINT subscription_validator_ibfk_1 FOREIGN KEY (validator_id) REFERENCES validator (id) ON UPDATE CASCADE
);
CREATE INDEX validator_id ON subscription_validator(validator_id);