Duplicate postgres schema - postgresql-9.6

I'm working on multi-tenant system in which I need to work on different tenats. in some cases I need to create new schema which contains some tables and default data. for that I just want to duplicate or copy public schema with diff. name Is there any way to duplicate or copy it.
I did work around on this problem but I want a solution to copy schema not to create schema and regenrate table and data

Take a back up of the schema you want along with the data In a plain text SQL file using pgadmin. And then create new schema and execute the SQL file under the new schema.

Related

How to export the DDL of domains(Database schema) of new version for update

I have developed a application with Grails earlier.Now as per new requirement there is a need to modify the existing domain classes as well as adding a new classes and changing / establishing new relationship between the as well.
Now the new requirement has been implemented and I am going to deploy to production environment. however, the DBA want a script change the production database DDL. DBA is not allowing the auto create / update of database schema while bootstrapping the application.
I know how to export DDL of for creating tables. but that script will drop tables which means all data will be lost.
But I don't know how to export DDL for DDL-update (no drop tables/recreate tables). Anybody has good suggestion ?
You can not expect the existing data to get stored according to the new database schema as is.
For example, you have a table Sample with the contactNumber field with the nullable : true constraint in your existing schema and in your new schema this constraint has been changed to nullable : true & unique : true.
In such cases database will fail to keep the existing data intact or adapt to new schema.
To preserve the existing data, you may have to go through a tedious process like -
Take backup of the existing database.
Make a note of the modification you have made to the existing Domain classes.
Find out which modifications may lead to failure / data loss.
Drop the earlier database schema & Deploy the new application and let it create the database schema.
Write a script or utility which will process & store the data from database backup according to new database schema.Make sure the utility you have written has the capalibility to handle the modification(constraint, field added, field removed) done to the database schema.

Manually Editing an EF7 Migration Class & Snapshot

The EF7 migration add command (to date, beta5) compares the model classes defined in the DbContext against the current model snapshot, creates a new migration class, and updates the model snapshot.
I need to modify the migration to make it generate different DDL SQL. As an example, EF7 uses sequences for SQL Server auto-increment values, and I would like it to use identity. However it could be any other reason. The migration remove command would physically delete the migration files and revert the model snapshot, so it's useless in this case.
There are 3 files that contain related code that look like they need to be edited:
The primary migration class: The Up and Down methods should be modified.
The DbContextModelSnapshot file contains Annotations that need to be modified
The secondary migration partial class: The badly named [migration].Designer.cs file also contains a model snapshot for the migration. I'm assuming this snapshot needs to match the model snapshot in item 2, but am not certain. The only information I have about the purpose of it is from Brice's blog , which says "It’s there in case you or a provider need to inspect the model for additional information during a migration."
Specific questions:
Do the two model snapshots need to be kept in sync in order to correctly perform migrations?
Is modifying 3 separate files the only way to edit the migration? (Although depending on the changes, the model snapshots may not have to be touched in some cases.)
Is there some EF command that would regenerate just the model snapshots, but not the migration methods?
Specific answers:
Do the two model snapshots need to be kept in sync in order to correctly perform migrations?
No, the snapshot in the migration is a last resort for provider writers. For example, SQLite can't rename columns so it could use migration's model snapshot to do a table rebuild for this operation. 99% of the time, it won't ever be used.
Is modifying 3 separate files the only way to edit the migration?
Most of the time, you should only ever edit the main migration file. In rare cases, you may need to edit the model snapshot if you're working in a team environment and you encounter a merge conflict. You can ignore the designer file; it just captures some metadata about the migration.
You may not have to edit anything if you configure your model correctly. For example, to use identity instead of sequences, override DbContext.OnModelCreating() and add modelBuilder.ForSqlServer().UseIdentity().
Is there some EF command that would regenerate just the model snapshots, but not the migration methods?
No, it shouldn't be needed since you almost never edit these files.

Hstore and multitenancy

I'm bulding an app where users can add custom fields and submit data. I'm using Postgresql so I thought that Hstore would be the perfect solution for this. Also because it allows to query the custom data that the users may introduce. My problem is that Hstore can only be installed into one schema and I'm using apartment gem for multitenancy. So each User has one schema in the database.
The data into the Hstore column is private, so I don't want other users to get access to it. How can I acomplish this? I prefer to store this data into the same user's schema. Is there another solution?
Craig is telling you to install hstore in its own schema. That means doing something along these lines.
create schema hs;
create extension hstore with schema hs;
This has nothing to do with how and where you store hstore data.
If I had a personal schema named "mike", I would build tables in the "mike" schema with hstore data types like this.
create schema mike;
create table mike.test (
some_column_name hs.hstore
);
You can avoid having to use the "hs" schema name (as in "hs".hstore) by putting that schema in the search path. If you were doing a "normal" client/server application, you'd probably want to set it at the database level.
alter database your_database_name set search_path TO mike, hs, public;
But in your multi-tenant architecture, which has one schema per tenant, you'd probably want one database role per tenant, and you'd probably want to set the search path for each role. (I'm not familiar with the "apartment" gem; I presume it creates one role per tenant and one schema per tenant. Verify that by checking the database schema.)
alter role one_role_name set search_path to one_role_name, hs, public;
I'd also want to verify sensible privileges on the schemas. For example, in a multi-tenant architecture like yours, I'd want to verify that only "mike" has privileges in the "mike" schema.
The first schema in the search path becomes the default schema for new objects. On the other hand, database objects in that schema can hide database objects of the same name in other schemas. Keep that in the back of your mind.
Finally, thousands of rows is a tiny database. Performance problems will probably have nothing to do with your database search path.

symfony migration create table and insert record

I want to create a new table and then insert a specific record in a symfony 1.4 project. I can do the data insert manually, but I would like to make use of the migration infrastructure so that when different existing instances are upgraded this row will be created when the migrations are run.
Here is the sequence of migrations:
Create a new table email.
Insert a row into the table email.
(Note that these are two separate consecutive migrations.)
The problem is that in this migration there is no chance to generate the base model classes, so attempting to insert the row using the normal Doctrine commands will fail.
I could use a naked SQL INSERT commands, but that seems like an admission of defeat. Is there another, more Doctrine-friendly way to do the data insert?
There is a way to do migrations. Do the following :
Update your schema.yml to include the new table
Create a new task or fixture file with the new entry
Run symfony doctrine:generate-migrations-diff - this will create the migration file
Run symfony doctrine:build --all-classes --and-migrate
The last line will update the Database - ie create the new table and also create the base model classes. Here are the options (related to classes on the build command)
--all-classes Build all classes
--model Build model classes
--forms Build form classes
--filters Build filter classes
The just run the task to insert the new DB entry or use the new fixtures (using symfony doctrine:data-load --append <filename>) file to create the entry in the DB.

How do define the schema that a rails model is set to?

For instance, when I generate an Event model, the table automatically sets to the public schema. How do I specify it to get set to a different schema?
Furthermore, how do you alter the schema of an existing table? Perhaps move it to a different schema?
Thank you!
Disclaimer: I don't know rails, so I'm going to give very postgresql-oriented answers here. For the first part of your question, there is quite possibly a much better way to do this, by making rails specify the schema when creating tables.
In PostgreSQL, tables are searched for in schemas according to the search_path setting. This is set by default to "$user",public. Tables are created in the first schema found in the search path that exists. So if you connect as "my_user", it will try to create tables in "my_user", and fall back to creating them in "public" if "my_user" doesn't exist.
So one approach is to update the "search_path" setting used for the user you connect to the database to make schema changes. For example you can say ALTER USER my_user SET search_path = my_app, public. If you then create a "my_app" schema then subsequent CREATE TABLE foo(...) commands executed by "my_user" will put the new table into "my_app".
You can change the schema of a table using ALTER TABLE foo SET SCHEMA my_app.
Create a migration to generate your new schema. ActiveRecord can't update you schema to you it's the pattern system. You can try sequel or DataMapper if you want update you schema from your code.

Resources