Creating a plan/applying a plan fails on schemas other than `public`
I've just tried using pgschema with a schema that is not public.
Unfortunately, this does not work as expected; I've encountered the following issues:
- Creating a plan seems to write the
publicschema into theCREATEstatements - seemingly ignoring--schema - Applying said plan will then only create the tables in the
publicschema - seemingly ignoring--schema - I've tried it out with the tables already present in another schema and the plan always wants to drop all tables and create them anew - not sure what's going on there.
Below are the logs of following the Quickstart once without a schema specified (success) and once with a schema specified (failure).
Am I doing something wrong, or did I stumble upon a bug? 🙂
Success (public schema)
$ docker run --name some-postgres -e POSTGRES_PASSWORD=bar -e POSTGRES_USER=foo -e POSTGRES_DB=my_db -p 5433:5432 -d postgres
5cfc6835b8db38d0916fa1b621f94e4f40976e6e2c56761daa17855983458236
$ cat schema.sql
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
CREATE INDEX idx_users_email ON users(email);
$ PGPASSWORD=bar pgschema --host localhost --db my_db --user foo --port 5433 --file schema.sql plan --output-json plan.json --output-human stdout
Plan: 1 to add.
Summary by type:
tables: 1 to add
Tables:
+ users
+ idx_users_email (index)
Transaction: true
DDL to be executed:
--------------------------------------------------
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
email varchar(255) NOT NULL UNIQUE,
created_at timestamp DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_users_email ON users (email);
$ PGPASSWORD=bar pgschema apply --host localhost --db my_db --user foo --port 5433 --plan=plan.json
Plan: 1 to add.
Summary by type:
tables: 1 to add
Tables:
+ users
+ idx_users_email (index)
Transaction: true
DDL to be executed:
--------------------------------------------------
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
email varchar(255) NOT NULL UNIQUE,
created_at timestamp DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_users_email ON users (email);
Do you want to apply these changes? (yes/no): yes
Applying changes...
Changes applied successfully!
$ psql --host localhost --port 5433 --username foo --password my_db
Password:
psql (17.5, server 17.6 (Debian 17.6-1.pgdg13+1))
Type "help" for help.
my_db=# SELECT * FROM public.users
my_db-# ;
id | email | created_at
----+-------+------------
(0 rows)
my_db=#
\q
Failure (other schema)
$ cat schema.sql
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
CREATE INDEX idx_users_email ON users(email);
$ PGPASSWORD=bar pgschema --host localhost --db my_db --user foo --port 5433 --file schema.sql plan --output-json plan.json --output-human stdout --schema FOO
Error: failed to get current state from database: failed to build IR: schema 'FOO' does not exist in the database
failed to get current state from database: failed to build IR: schema 'FOO' does not exist in the database
$ psql --host localhost --port 5433 --username foo --password my_db
Password:
psql (17.5, server 17.6 (Debian 17.6-1.pgdg13+1))
Type "help" for help.
my_db=# CREATE SCHEMA IF NOT EXISTS foo;
CREATE SCHEMA
my_db=# SELECT schema_name FROM information_schema.schemata;
schema_name
--------------------
public
foo
information_schema
pg_catalog
pg_toast
(5 rows)
my_db=#
\q
$ PGPASSWORD=bar pgschema --host localhost --db my_db --user foo --port 5433 --file schema.sql plan --output-json plan.json --output-human stdout --schema foo
Plan: 1 to add.
Summary by type:
tables: 1 to add
Tables:
+ users
+ idx_users_email (index)
Transaction: true
DDL to be executed:
--------------------------------------------------
CREATE TABLE IF NOT EXISTS public.users (
id SERIAL PRIMARY KEY,
email varchar(255) NOT NULL UNIQUE,
created_at timestamp DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_users_email ON public.users (email);
$ PGPASSWORD=bar pgschema apply --host localhost --db my_db --user foo --port 5433 --plan=plan.json --schema foo
Plan: 1 to add.
Summary by type:
tables: 1 to add
Tables:
+ users
+ idx_users_email (index)
Transaction: true
DDL to be executed:
--------------------------------------------------
CREATE TABLE IF NOT EXISTS public.users (
id SERIAL PRIMARY KEY,
email varchar(255) NOT NULL UNIQUE,
created_at timestamp DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_users_email ON public.users (email);
Do you want to apply these changes? (yes/no): yes
Applying changes...
Changes applied successfully!
$ psql --host localhost --port 5433 --username foo --password my_db
Password:
psql (17.5, server 17.6 (Debian 17.6-1.pgdg13+1))
Type "help" for help.
my_db=# SELECT * FROM foo.users;
ERROR: relation "foo.users" does not exist
LINE 1: SELECT * FROM foo.users;
^
\q
Thanks for the detailed reporting! This is fixed in https://github.com/pgschema/pgschema/commit/743249cafd2138fbcd45f4759c6f4ac7c25ab2f2
It's included in the new 1.0.1 release.
Thanks for the quick fix!
Unfortunately, it only seems to work for schema names that contain only lowercase characters. However, if I have a schema like "GFJTA" it doesn't:
PGPASSWORD=bar pgschema apply --host localhost --db my_db --user foo --schema "GFJTA" --port 5432 --plan=plan.json
[...]
Error: failed to execute concatenated statements in group 2: ERROR: schema "gfjta" does not exist (SQLSTATE 3F000)
failed to execute concatenated statements in group 2: ERROR: schema "gfjta" does not exist (SQLSTATE 3F000)
However, plan seems to understand the uppercase.
Could you please open a separate issue? I will take a look