Drop or Delete Schema Postgresql via controller Rails - ruby-on-rails-3

I have multi-tenant application rails with postgresql,
i want to drop schema (schema name = subdomain) and delete or table on schema.
primitive code on controller, wkwkwk.
accounts_controller.rb
def destroy
#account = Account.find(params[:id])
conn = ActiveRecord::Base.connection
conn.execute("DROP SCHEMA "+#account.subdomain)
end
error message
ActiveRecord::StatementInvalid in AccountsController#destroy
PG::Error: ERROR: cannot drop schema subdomain1 because other objects depend on it
DETAIL: table articles depends on schema subdomain1
table gambarinfos depends on schema subdomain1
table pages depends on schema subdomain1
table redactor_assets depends on schema subdomain1
table schema_migrations depends on schema subdomain1
table usersekolahs depends on schema subdomain1
HINT: Use DROP ... CASCADE to drop the dependent objects too.
: DROP SCHEMA subdomain1
any ideas?
thx

problem solved with
add CASCADE to conn.execute("DROP SCHEMA "+#account.subdomain+" CASCADE")

Related

What happens if I don´t define a specific schema to the database?

If a specific schema is not defined in a database, where are the database objects going to be stored? Is that a good or a bad thing? Why?
Quote from the manual
In the previous sections we created tables without specifying any schema names. By default such tables (and other objects) are automatically put into a schema named “public”. Every new database contains such a schema
If no schema is defined when creating a table, the first (existing) schema that is found in the schema search path will be used to store the table.
In psql
create database sch_test;
CREATE DATABASE
\c sch_test
You are now connected to database "sch_test" as user "postgres".
--Show available schemas
\dn
List of schemas
Name | Owner
--------+----------
public | postgres
drop schema public ;
DROP SCHEMA
\dn
List of schemas
Name | Owner
------+-------
(0 rows)
show search_path ;
search_path
-----------------
"$user", public
create table tbl(id integer);
ERROR: no schema has been selected to create in
LINE 1: create table tbl(id integer);
create table test.tbl(id integer);
ERROR: schema "test" does not exist
LINE 1: create table test.tbl(id integer);
Just to show that an object may not be created if a schema does not exist. Bottom line is an object(table, function, etc) needs to be created in a schema. If there is none available for search_path to find or you specifically point at one that does not exist the object creation will fail.

POSTGRES - Risk of dropping foreign data wrapper/schema

We had one of the devs create a foreign data wrapper with these commands:
CREATE SERVER serverName FOREIGN DATA WRAPPER postgres_fdw OPTIONS (xxxx);
CREATE USER MAPPING FOR user SERVER foreign_db OPTIONS (user 'xxxx', password 'xxxx');
CREATE SCHEMA foreign_db;
IMPORT FOREIGN SCHEMA public FROM SERVER serverName INTO foreign_db;
To drop this schema the suggestion was to run:
DROP SCHEMA if exists foreign_db cascade;
DROP USER mapping if exists for user server foreign_db;
DROP SERVER if exists serverName;
In the spec I see this for CASCADE:
Automatically drop objects (tables, functions, etc.) that are
contained in the schema, and in turn all objects that depend on those
objects
what concerns me is this line:
and in turn all objects that depend on those objects
My question is there a possibility of dropping anything outside of foreign_db schema and if yes, how can I check it?
Thank you.
It is possible that the command drops something outside the schema. Consider this:
create schema example;
create table example.my_table (id int);
create view public.my_view as select * from example.my_table;
If the schema is dropped with the cascade option, public.my_view will also be dropped. However, the behavior is logical and desirable.
You can check this executing these commands one by one:
begin;
drop schema example cascade;
rollback;
The schema will not be dropped and after drop... you should get something like this:
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to table example.my_tabledrop cascades to view my_view
Alternatively, you can use the system catalog pg_depend, see this answer How to list tables affected by cascading delete.

Synonym support on PostgreSQL

How to create and use Synonyms on PostgreSQL as in Oracle. Do I need to create some DB link or any thing else. I could not find any good official doc on this topic.
Edit 1
Actually as of now i have an application which has two separate modules which connects with two different oracle databases; One modules need to access tables of other so for which we use synonyms over db link in oracle. Now we are migrating application to postgresql, so we need synonyms.
Edit 2
When i say two different oracle databases it means it can be two different oracle instances or two schemas of same db, it is configurable in application and application must support both modes.
PostgreSQL version: 9.6.3
Approach 1:-
Finally i got it working using foreign data wrapper postgres_fdw as below
I have two databases named dba and dbb. dbb has a table users and i need to access it in dba
CREATE SERVER myserver FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'localhost', dbname 'dbb', port '5432');
CREATE USER MAPPING FOR postgres
SERVER myserver
OPTIONS (user 'user', password 'password');
CREATE FOREIGN TABLE users (
username char(1))
SERVER myserver
OPTIONS (schema_name 'public', table_name 'users');
CREATE FOREIGN TABLE users (users char(1));
Now i can execute all select/update queries in dba.
Approach 2:-
Can be achieved by creating two schemas in same db, below are the steps:
create two schemas ex app_schema, common_schema.
Grant access:
GRANT CREATE,USAGE ON SCHEMA app_schema TO myuser;
GRANT CREATE,USAGE ON SCHEMA common_schema TO myuser;
Now set search path of user as below
alter user myuser set search_path to app_schema,common_schema;
Now tables in common_schema will be visible to myuser. For example let say we have a table user in common_schema and table app in app_schema then below queries will be running easily:
select * from user;
select * from app;
This is similar to synonyms in oracle.
Note- Above queries will work PostgreSQL 9.5.3+
I think you don't need synonyms in Postgres the way you need them in Oracle because unlike Oracle there is a clear distinction between a user and a schema in Postgres. It's not a 1:1 relationship and multiple users can easily use multiple schemas without the need to fully qualify the objects by exploiting Postgres' "search path" feature -  mydb.public.mytable.
If the tables are supposed to be in a different database in PostgreSQL as well, you'd create a foreign table using a foreign data wrapper.
If you used the Oracle synonym just to avoid having to write atable#dblink, you don't have to do anything in PostgreSQL, because foreign tables look and feel just like local tables in PostgreSQL.
If you use the synonym for some other purposes, you can either set search_path to include the schema where the target table is, or you can create a simple view that just selects everything from the target table.

Db2 drop schema contents along with schema at once

Is there a query in db2 9.7 control center wherein I can't DELETE(DROP) all the contents of my schema (including the schema) at once?
My other option is to drop/delete the objects first and then DROP schema..
But I want to DROP THE ENTIRE SCHEMA WITH ALL OBJECTS at once.
DROP SCHEMA <schema_name> CASCADE/RESTRICT didn't work for me.
The ADMIN_DROP_SCHEMA procedure is what you're looking for.
The ADMIN_DROP_SCHEMA procedure is used to drop a specific schema and all objects contained in it.
http://publib.boulder.ibm.com/infocenter/db2luw/v9/topic/com.ibm.db2.udb.admin.doc/doc/r0022036.htm
First drop all the tables in the schema.
Then try to delete the schema using
DROP SCHEMA SCHEMA_NAME RESTRICT
webchain.in have sample java program, explains how to delete the schema using java program
in case drop schema fails after dropping all the tables with the error SQLCODE=-551, SQLSTATE=42501, try command
grant dbadm on database to USER_NAME

Dropping a schema and all of its contents in DB2 8.x

How would I drop a schema and all of its contents, using SQL, in DB2 8.x without knowing what the content is?
I do not have a schema to drop at hand, but the infocenter (http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/admin/t0005230.htm) says that a DROP SCHEMA [schema name] shall work on DB2 8.x
EDIT: Ok, the Infocenter also says: "Before dropping a schema, all objects that were in that schema must be dropped themselves or moved to another schema"
So, need to drop all the objects in the schema first. The objects (tables, views, triggers, procedures, indexes...) can be listed quering the catalog views in SYSIBM schema.
E.g. to get all tables that belong to schema, run:
select table_name from sysibm.tables where table_schema = '[your schema name]'
Check the other sysibm views to get all objects that belong into a schema. the views are: sysibm.views, sysibm.triggers, sysibm.routines, sysibm.indexes, ... - consult IBM's Infocenter (the link above) for details.
use the comand centre-GUI to drop all contents of the schema, then DROP SCHEMA