How does spring session create the session table? - spring-session

I am using spring boot and trying to import spring session.
All I have done is add one single line to the application.yaml:
spring:
datasource:
password: xx
url: jdbc:postgresql://c:5432/dbname?schema=public
username: pg
session:
store-type: jdbc
Then I found that two tables spring_session and spring_session_attributes auto generated in my database. This is expected except one thing: these two tables are generated in a different table schema. However, the tables generated by jpa(hibernate) are being put in another schema.
I tired to dig into the source codes, however, I cannot find the codes which are calling the org/springframework/session/jdbc/xx-h2.sql to create the table.
What's the magic?

Related

Getting error when connecting with external redshift database in rails

Getting this error:
PG::InsufficientPrivilege: ERROR: permission denied to set parameter
"client_min_messages" to "warning" : SET client_min_messages TO
'warning' when connecting with redshift database.
enter image description here
My database.yml file setting looks like this
development:
adapter: postgresql
encoding: utf8
host: nacfhrcluster123.ctvpledrvuobs5.us-east-1.redshift.amazonaws.com
port: 5439
username: nacfhr123
password: NACFDChr12345!
database: devnacfhrdc
pool: 5
schema_search_path: 'beta'
timeout: 5000
min_messages: warning
Thank you guys for your responses. I resolved the issue by adding a gem with the name 'activerecord5-redshift-adapter' in my gemfile. So you have to add below line in your gem file.
gem 'activerecord5-redshift-adapter'
And then run bundle install again.
It is important to realise that Redshift IS NOT Postgres. There are many differences.
One difference is that the available parameters are much different, the only parameters that can be set on Redshift are:
analyze_threshold_percent
datestyle
extra_float_digits
query_group
search_path
statement_timeout
wlm_query_slot_count
You will need to alter your rails connection so that is does not request this parameter to be set.
I guess that means removing min_messages: warning
I hope this will help. As far as I know there is no active records available for Redshift and it don't it make sense as well as Redshift is not a typical RDBMS.
We have similar scenario where we use ROR for front end for one of analytics and planing application.
Here is our app scenario:
Front-end : ROR
Database : mysql(OLTP)
Datawarehouse : Redshift(OLAP)
This is how data flows back and forth between ROR and Redshift.
Insert/updates from ROR to Redshift(happens very rarely).
For any operations from ROR that impacts something on data warehouse, ROR executes PSQL commands with specific query, not via active records.
SELECT from Redshift(happens frequently)
Similarly, for getting data from DW for various reason done via select query and redirection to file using PSQL command, then it import it into OLTP using myssqlimport.
Link for psql, mysqlimport tools that we use.
Comment if you have further specific followup questions.

Grails database migration gorm diff yields no changes

Background
I have a relatively new Grails project using 3.0.14. I am looking to integrate liquibase for database migrations via the Database Migration plugin (2.0.0.RC4).
I have a large enough domain model so far that I have used the plugin to 'seed' an initial changelog. This is straight from the docs, and works as intended:
grails dbm-generate-gorm-changelog changelog.groovy
What I am now trying to test/get working is the dbm-gorm-diff command, which will take changes to the domain model and create a changelog that can be applied. This is where I am running into issues.
The Grails documentation suggest removing the dbCreate block from the datasource to ensure that Hibernate doesn't do the updating, and Liquibase can take over. Great, exactly what I want.
The Issue
When I remove dbCreate, Grails/hibernate still seems to update the database before the Database Migration plugin has a chance to do the diff. When doing the diff, it is already too late to see changes, so the changelogs do not contain the right data.
Config
dataSource:
pooled: true
jmxExport: true
driverClassName: org.h2.Driver
username: sa
password:
environments:
development:
dataSource:
dbCreate: verify
driverClassName: org.postgresql.Driver
dialect: org.hibernate.dialect.PostgreSQLDialect
url: jdbc:postgresql://127.0.0.1:5432/liquibase_test
username: dbuser
password: dbuser
logSql: false
formatSql: true
(I am aware that the dbCreate is set to verify. More on this later)
Steps Taken
Create a new postgres database - dbcreate -U dbuser liquibase_test
Run the initial changelog on the new database - grails dbm-update
Verify that the database is now up to date, and check that select * from databasechangelog equals the number of changes in changelog.groovy
Add a new simple domain class:
class TestDomain {
int testInt
}
Run the plugin to get the diff - grails dbm-gorm-diff add-simple-domain.groovy. The command fails with an exception:
:DataModel:dbmGormDiff
Command execution error: liquibase.command.CommandExecutionException: java.lang.NullPointerException
DataModel:dbmGormDiff FAILED
Now, remove the config dbCreate: verify from above, and run again
This completes successfully without exception, but there are issues:
the command created add-simple-domain.groovy, but it has no mention of the new domain class I just created. (It has index/sequences, but I think this is a known issue)
the new domain class has been added to the database(!?) (checked in PgAdmin)
the table databasechangelog still has the original row count, and even when interrogated no reference to the new domain class
So, I'm at a loss to explain what is going on. I can deal with the extra create/drop indexes & sequences, but I can't seem to get the liquibase stuff working. Can anyone shed some light on this for me?
Edit
I did some more digging into the NullPointer, and it seems to come from the class liquibase/ext/hibernate/snapshot/ForeignKeySnapshotGenerator.java:45, where the plugin is trying to construct a foreign key to the inherited table id field (using tablePerHierarchy false for this inheritance). I couldn't find anything that seemed related to this error after a decent search.
Edit #2
I have found an issue on Github for the tablePerHierarchy NPE: https://github.com/grails-plugins/grails-database-migration/issues/68
Update your application.yml (or application.groovy) configuration for your datasource:
dataSource:
dbCreate: none
Setting to "none" is not the same thing as removing dbCreate entirely - you need to set it explicitly to overwrite any defaults that are being set elsewhere.
"none" seems not to work for me when using JNDI datasources and still causes the ddl to run. I set it to "ignore" to be able to use db-migrations with JNDI datasources in Grails 3.0.x
I ended up getting this to work by setting hibernate.hbm2ddl.auto = 'none' in my application.groovy. Interestingly, when I tried to instead put this same config in my application.yml it had no effect.
I suspect there may be other forces at play here as I tried replicating the behaviour on a fresh Grails project without issue.
For the time being I have settled on using the hibernate property in the groovy file, though I am still curious as to why I couldn't get the config to work for me like a vanilla project.

Using existing SQL Server database with Ruby on Rails

I'm on the early stages on learning Ruby.I really don't have an idea on how to use an existing database filled with tables and data on ruby. Every guide, every article that I have or find on the internet is always creating a new one using the migration functions.
But which are the steps for using an existing database in SQL server on RoR?
You're in luck, friend. My first Rails project (7 years ago) was against a horribly set up SQL Server database.
Per the above, you need to set up your database.yml appropriately. But for an existing database, obviously it is unlikely that the table and column names conform to the Rails conventions. The good news is that you can override all of those defaults. Here is a non-exhaustive list of those directives:
In a model descended from AR::Base,
set_table_name 'actual_table_name'
set_primary_key 'actual_primary_key_name'
On the various association directives (has_one, has_many, belongs_to), there are :foreign_key keys that let you specify the name of the foreign keys.
Now, one of the things that MS SQL Server allows you to do which is TERRIBLE is that you can embed spaces into your column names. Fear not, you can still refer to these columns by name using write_attribute("badly named column") and read_attribute("badly named column"). You may also refer to them in various directives like so:
validates_length_of "Fax Number", :maximum => 17, :allow_nil => true
Finally, you may refer to the implied methods these devilishly named columns generate like so:
self.send('Fax Number=', new_fax_number)
Obviously, you can't refer to them as symbols, since spaces are disallowed in Ruby symbols.
Good luck, and next time I hope that you get to work with a real RDBMS, like Informix :).
First you have to setup your application to user sql server for databases connectivity.
you have to use gem for sql server in your gemfile and have to setup database.yml file accordingly.
In database.yml, in config folder put the name of same database In the Development part of this file.
development:
adapter:
database: db_name_dev
username:
password:
host: localhost
socket:
To use a existing server. In your database.yml you have to specify the host, port and the database name.
`database: <host>:<port>/<database_name>`
For eg
development:
adapter: mysql2
database: your.mysqlserver.com:1521/project_database
username: project_user
password: project_password

Ruby on Rails existing Sybase database

I am trying to connect to an existing Sybase db using Rails and populate a few selection lists. Here's what I have done so far:
1. Installed and configured FreeTDS
2. Installed TinyTDS gem
if I execute command tsql -S serverName -U userName, I'm able to query the data. I have my config/database.yml configured as such:
development:
adapter: sybase
host: <sybase_host>
port: <port_no>
username: <user>
password: <password>
database: <db>
I then tried generating a model via rails generate model sybase_db --skip-migration and editing the created app/models/sybase_db.rb file as follows:
class SybaseDb < ActiveRecord::Base
set_table_name "my_sybase_table"
end
When I try to run SybaseDb.new command in rails console, it doesn't seem to work. I'm pretty new to Rails so what am I doing wrong?
Thanks!
Everything looks right up to the part where you generate a model called sybase_db. A model in Rails is typically linked to a specific table in a database, not the whole database. Rails uses naming conventions to simplify the linking of tables and columns to models and attributes.
For example, if you have a model User with attributes name and email that is linked to a table in your database called users and having columns name and email then all sorts of wonderful Rails magic just works. You could start the rails console and execute User.all to produce a collection of all users in the database. Or you might do something like
> u = User.find_by_email 'joe#example.com'
=> #<User id: 123, email: "joe#example.com", name: "Joe"...>
> u.name
=> "Joe"
If you have an existing database, however, chances are you're going to have to explain in more detail to Rails how to map the names in Sybase to those in your Rails system. You have one case of this in set_table_name -- if your Sybase table was named t_user with a primary key of user_id but it had columns name and email then you could create a Rails model like
class User < ActiveRecord::Base
set_table_name "t_user"
set_primary_key "user_id"
end
Here's a discussion of this topic on Quora with a couple good links that you might use to go further.
Depending on how extensive your existing system is, you may find that all of the magic of Rails goes away. Rails helps us avoid all of this mapping this to that, and follows strong naming conventions to give us all sorts of wonderful coolness. If your existing system has strong and predictable naming conventions, and isn't terribly far off from Rails' way, you might be able to use Rails successfully.

Symfony project with models stored in multiple databases

I am writing Symfony project (using symfony 1.4 ver. with Propel as its ORM) where some data is stored in MySQL database and some other data is stored on another server in PostgreSQL database.
To be more precise I want to store some models in MySQL database and other models in PostgreSQL database at the same time and do it seamlessly without explicit database switching (I mean Propel will use proper database connection and SQL dialect to retrieve/store data). Models from MySQL part will not have relations with PostgreSQL.
Is it possible? If yes I also would like to know how to setup development environment (I want to access different MySQL/PostgreSQL DBs in developement and production environments).
UPD: I've found question on SO reagrding this problem: Multiple databases support in Symfony But i have to check if it works with recent versions of Symfony.
i work with symfony every day and in fact you can have 2 databases in order to store unrelated parts of the model. You need to set up both connection in you database.yml (i'm unfamiliar with posgress so you will have to figure out how to set it up correclty):
mysql_connection:
class: sfPropelDatabase
param:
phptype: mysql
classname: MysqlPropelPDO
dsn: ''
username: user
password: pass
encoding: UTF-8
pooling: true
postgress_connection:
class: sfPropelDatabase
param:
phptype: postgres
classname: PostgresPropelPDO
dsn: ''
username: user
password: pass
encoding: UTF-8
pooling: true
Once you have done that, we should get started with the schema.yml file or files (as you will be using 2 databases i would suggest to have 2 files, one for the mysql and another for the postgres database):
mysql_schema.yml file:
//This is how you tell witch connection you are using for this tables
connection: mysql_connection
classes:
CLassName:
tableName: table_name
columns:
id:
name:
type: varchar(255)
required: true
[...]
postgres_schema.yml file:
connection: postgress_connection
classes:
ClassName:
tableName: table_name
columns:
id:
name:
type: varchar(255)
required: true
[...]
Once you have finished setting up your schema files, you should be good to go, create all classes and start to have fun. Hope this helps
I believe you can!
Google has quite a few results for this, try
http://snippets.symfony-project.org/snippet/194
Thats based on an older version of propel/symfony, but from a quick look, I believe it's still valid. Plus there are recent comments suggesting it works.

Resources