Liquibase: setDefaultSchema is not working - mysql

I am creating Schema-Based Multi Tenant application with spring boot(2.5.2) , mySQL 8.0 and Liquibae 4.4.1.
I expect that all my tenant tables and liquibase internal tables should be created inside a tenant schema which I specify before running the migration.
liquibase.setDefaultSchema(tenantSchema); --sets the schema for changesets
liquibase.setLiquibaseSchema(tenantSchema); -- sets schema for liquibase internal tables
I can see that liquibase internal tables are getting created inside tenant schema correctly .
But liquibase is always executing my changesets against public schema. Looks like setting the deafult schema prior to liquibase migration has no effect. I am using sql-based changesets and I don't want to switch to xml.
I found a workaround but it is risky . I am looking for a better solution.
Workaround:
Before running migration, I save current tenant's schema in a jvm argumanet as below
System.setProperty("changeSetSchemaName", tenant.getSchema());
Then in my change set , I read this schema value and explicitly set the schema name as below
--liquibase formatted sql
--changeset author-id:alter_xxx_table
use ${changeSetSchemaName} --switch to tenant schema
alter table table_name add column new_column data_type; --execute sql
use master; --switch back to master schema
--rollback ....
This does work but I am forced to change every single changeset. If I forget to do this then some unexpected stuff will happen. I can add unit test to parse the changesets and verify that schema statements are used before and after the sql (I will have to do this in case I dont get any solution) .
It will be great if someone knows a risk-free solution for my problem.

You can use these two arguments liquibaseSchemaName and defaultSchemaName. liquibaseSchemaName is for internal liquibase tables and defaultSchemaName specifies the default schema name to use for the database connection.

Related

Export part of table definition using phpMyAdmin

Is there a way to export part of MySQL table definition using phpMyAdmin?
I need to transfer 2 columns from one table to other DB, so I expect 2 "ALTER TABLE" SQL commands to be created.
Currently, there is no built in way to do this, but Schema Sync - a MySQL Schema Versioning and Migration Utility can be used:
Schema Sync will generate the SQL necessary to migrate the schema of a source database to a target database (patch script), as well as a the SQL necessary to undo the changes after you apply them (revert script).

How can I update mysql database schema in Symfony2

Can anybody help me? How can i update mysql database schema (entity, doctrine yml etc) after inserting new integer field type?
I added manually a new field into mysql, I get semantical error like this :
Site\SiteBundle\Entity\Yorum has no field or association named yayin
using cmd or a console
go to the root of the project
and type
php app/console doctrine:schema:update --force
and remember to clear up your cache
NOTE: You shouldn't do the changes directly into mysql, add the field into the entity and remember to put the right annotations with the field type, length, etc, when you do the schema:update, that would update the sql schema and you'd probably avoid getting this kind of error messages
Check the docs for more info on this subject
Do you have any relations between two tables?
Because the error
Site\SiteBundle\Entity\Yorum has no field or association named yayin
This error is thrown when there is relation between tables

What changes I need to do on the restored DB and how?

I have restored a DB from "mixed authentication" MS SQL Server into "windows authentication only" SQL Server.
My application is configured for windows authentication which uses 'dbo' for accessing DB.
But in the restored DB , all the tables, views are NOT owned by 'dbo'
Im not really sure if i understood the question, but i guess when you accessing database, you are/was creating the tables. Most possibly your statements was like:
Create Table TableName (...)
To create tables in specific schema, you should add schema prefix (or change the default schema of user you are using):
Create Table dbo.TableName (...)
To move tables to other schema, see Alter Schema.

How to accomplish "MySQL cross database reference" with PostgreSQL

We will migrate the database from mysql to postgresql in our product(through java). So we need to change the mysql query to postgresql query in java application. How to create the table i.e., databasename.tablename in postgresql.
For mysql, we can directly create the table e.g create table information.employee.
Here database name is "information" and table name is "employee" . Is it possible to achieve same query in postgresql.
I searched google it says cross database reference is not possible. Please help me.
I saw pg_class table it contains the table names in the specific database, like wise databse and tables relationships are stored in any other table.
This is normally done using schemas rather than databases, which is more or less like how MySQL organizes it anyway.
Instead of
create database xyz
use
create schema xyz
When you create tables, create them:
create table xyz.myTable
you will need to update your search path to see them on the psql command line tool, or if you want to query them without using the schema explicitly. The default schema is public, so when you create a table without a schema name, it ends up in public. If you modify your search_path as below, the default schema becomes the first in the list: xyz.
set search_path=xyz,public,pg_catalog;
and you must not have spaces in that statement. You can do it globally for a user/role too:
alter role webuser set search_path=xyz,public,pg_catalog;
Also, don't forget that postgresql string matches are case sensitive by default (this one catches people out a lot).
If you want to have different physical locations for the files for each schema, you can do that with tablespaces. If you have a look at the postgresql documentation page, they have info on how to do it, it's pretty easy.
database in MySQL == schema in PostgreSQL. So you will most probably want to migrate all your mysql dbs into one postgres db. Then you will be able to do "cross-database" queries.
See my answer to this question: Relationship between catalog, schema, user, and database instance

How to setup heterogeneous replication with tungsten?

Recently i am working on a replication between heterogeneous dbs with Tungsten Replicator. We have a mysql master and an oracle slave. According to the docs such a setup should work. I am using tungsten-replicator-2.0.5. I call
$TUNGSTEN_HOME/tools/configure \
--verbose \
--home-directory=$INSTALL_HOME \
--cluster-hosts=$MA_HOST,$SL_HOST \
on the master node for creating a basic installation on both nodes. Note: using the installer (as recommended) fails, due to the heterogeneous setup, since the installer fails to find a mysql instance on the slave node. The replicator instances are configured by adding static-$SERVICENAME.properties to the conf directory and modifying conf/services.properties (replicator.host=$HOSTNAME, replicator.masterListenPortStart=12112, replicator.rmi_port=20000).
Launching the replicators resulted in an ORA-01850 when issuing an update statement against trep_commit_seqno in the tungsten schema, due to a missing 'timestamp' keyword in the SQL-Statement. Just in order to get beyond this error, i altered datatype of update_timestamp and extract_timestamp to varchar. The replicators are now starting up und some simple inserts where replicated but when the test script issues a
DROP TABLE IF EXISTS table1;
replication fails due to an ORA-00933, because of the 'IF EXISTS' clause. I am not sure if this is an error in my configuration or if tungsten in general has problems with the differences in DDL statements between those two products. Has somebody successfully set up a similar replication using tungsten?
The Tungsten docuemntation has some useful guidance. In particular, this point from the "Advanced Principles of Operation" is relevant: "Also, DDL statements beyond the simplest CREATE TABLE expressions are rarely at all portable. ". In your case, DROP TABLE IF EXISTS table1; is not valid Oracle DDL.
Read it here.
For anybody who is interested: Up to now, Tungsten does not perform any transformation of ddl statements in a heterogeneous environment (as MithunSasidharan wrote). Now i wrote a custom filter, that skips ddl statements using regular expressions. For synchronizing the schema defition, we will use Apache DdlUtils, which serves quite well for transforming a schema definition between mysql and oracle. I assume it works for other vendors similarly well. Thanks.