Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable AutoMigrate when using non default sqlserver schemas #50

Merged
merged 9 commits into from
Dec 4, 2022
Merged

enable AutoMigrate when using non default sqlserver schemas #50

merged 9 commits into from
Dec 4, 2022

Conversation

themulle
Copy link
Contributor

Sqlserver supports multiple schemas per database. This feature works in gorm.io (using custom TableName/NamingStrategy) but fails at AutoMigrate.
This commit fixes the migrator to also support non-default schemas.

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

This pull request fixes the migrator to support "non user-default schemas" and to support the same table names in multiple schemas (e.g. dbo.testtables/testschema1.testtables/testschema2.testtables).
This pull request could be solved more elegant but this would require changes in gorm.io/migrator because the function "ColumnTypes" almost equal to the default migrator.

User Case Description

usecase1: We need to use multiple schemas for security purposes because this way access rights to the tables/schemas can be configured via the schema.
usecase2: Integrationtests are executed in the same database. The user default schema equals to the username which leads to ambigous table names if the schema isn't observed (e.g. user1.testtable1 vs. user2.testtable1)

Mueller Manuel (LWE) and others added 6 commits January 18, 2022 15:22
Sqlserver supports multiple schemas per database. This feature works in gorm (custom TableName/NamingStrategy) but fails at AutoMigrate.
This commit fixes the migrator to also support non-default schemas.
migrator_test.go Outdated
func (*Testtable5) TableName() string { return "testschema2.Testtables" }

func TestAutomigrateTablesWithoutDefaultSchema(t *testing.T) {
connectionString := os.Getenv("CONNECTION_STRING")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, Can you use the *DB from gorm/tests package?

@jinzhu jinzhu merged commit 07d0daa into go-gorm:master Dec 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants