groovy code to create database with no build id - database

At present, we are supposed to provide a tenant id to create net new database for a tenant . But there is no way to create net new databases for the default tenant. Initially it was agreed to create the default tenant database with "_system" but later it got changed and there is no support to create the default tenant db.
I need groovy code to create database with no tenant id.

I understand your question now. When you run your app, your app will create a database and with a suffix like "_100" added to the name of the database. You don't want the suffix added anymore.
When you run a Grails app, it does not create a database for you. You will have a database created ahead of time yourself.
If you said the app did create a database, your programmer might have a SQL script somewhere in your app.
You must have something like this in your source code .executeUpdate("CREATE DATABASE IF NOT EXISTS MyDatabaseName")
See if you can find this SQL and change it.

Related

SSDT - specify authorization user on schema create

I have a sqlproj XML for Azure SQL Database. I want to deploy database with several tables into specific schema which is also created using this build. How to specify what user should be used for CREATE SCHEMA [schema-name] AUTHORIZATION [user] ? By default it uses user dbo but I need to change it. How to specify it in the sqlproj?
Sounds like you are looking in a wrong place. All database schema definitions in SSDT, by default, are placed into the Security folder. In it, you will have your schema-name.sql file, and there you can adjust the authorization clause.
For the project to build, the owner of that schema should also be present in the project. Most likely, you will find it in the same folder.
It appeared the user (service principal) trying to deploy the solution to SQL Database was not set as an AAD admin on the SQL server. It seems a little bit strange to me, but it worked.

Azure Data Studio - Database Project - schema compare changes failed

I can successfully build my DB project in the Azure data studio with the SQL DB Projects extension. (not with SSDT but similar to this: https://www.sqlshack.com/two-ways-to-build-sql-database-projects-in-azure-data-studio/
When I right-click on the DB project and select "Update Project from database", it lists the delta between the online Azure SQL DB and the local db project.
The problem now is that when I either click on "Generate script" or on "apply", I receive an error.
Generate script: Performing script generation is not possible for this comparison result.
Performing script generation is not possible for this comparison result.
Apply schema compare changes: Object reference not set to an instance of an object.
Object reference not set to an instance of an object.
I already reinstalled the Azure Data Studio but with no success.
Any hint what I could do to fix this?
In our case these errors occur because it is added a Role Member to the Standard MSSQL Database Roles called db_datareader and db_datawriter. These roles (or objects as ADS calls them) do not exist in the local dacpac or ADS Database Project. It seems to map even if the roles are checked for update or not, so the Schema compare can't find the objects locally.
If the same applies to your project I will suggest checking if that user really needs to be a part of those two roles as they are not being mapped locally.
If you need the member in the role, a workaround is to exclude the Database Roles objects in the Schema Compare:
In Schema Compare, press Options.
In modal on the right, press the Include Object Types-tab
Find Database Roles and untick.
Press OK and re-compare.
I've set up an issue to the ADS-team, so hopefully the Schema Compare- and Database Project-responsibles can cook up a good solution on this problem.

EF Core, code-first, SQL Server, Blazor application: how to properly initialize the database?

My application have to create database and import data, this is done once the app is deployed.
I noticed that the database could be only created if my connection string specifies user sa, or other user with CREATE DATABASE privilege on master database. Well, the first time I created the database manually, then created a user for it, then used EF (via Package Manager Console) to create the structure.
Then I accidentally deleted the database, so I tried to recreate it with EF which failed with "create database failed" and that I need "create database" privilege on "master".
So I thought, well, I'll give it a try, I specify sa as the user. And no initial catalog, let EF Core create the database, right?
Here comes the surprise: it didn't create any databases, it created tables in my master database. This is definitely not what I wanted! How to specify the name of the database to use?
I know this question was asked before, with the answer "use the connection string name". NO - IT DOESN'T WORK in Blazor application with Identity. My connection has a name that is NOT USED, the database is not created, my tables goes to master. In this framework, the configuration sits in appsettings.json file. There is a special method to fetch connection strings, but their names are not special. It's just a configuration key as any other and I don't think its name is used for anything but finding the appropriate string in the file.
If I create the database manually, then create user, then grant privileges, then set the connection string to that user, with initial catalog set to my database - then it works, but there are several problems here. I try to do as much as possible within my application, I try to avoid manual actions as much as I can. I need the deployment process as simple as possible. First run on special privileges, to import data, second run on regular user privileges.
Then I'd like to make limit the concrete RDBMS dependence to the minimum. Let EF Core do everything it can automagically.
Is it even possible?
What is the best common practice?
Do you create databases and privileges manually, or can it be automated in EF?
How does EF create the database? Does it set any special options like default collation? What options should I set when creating the database manually?

Create a user for a known login on every new database created on Azure SQL?

Is there a good way to automate creating a new user and adding it as a DB owner on every new database created on an Azure-hosted SQL Server?
so basically create user + alter role db_owner add member (or sp_addrolemember) every time a new database is created on the server.
TL;DR on 'why' : I need to have every database on the server accessible (and editable, change schema objects, change data, etc) with a user attached to a specific login, relating to this issue.
I tried checking with ARM templates to see if there is a possibilty of editing templates to add users..But there is no option at present.Read comments section for more info..
If the logical server name is same for all the databases.. you could try using azure functions and loop each db and use execute non query method..
you can set to run this function every one hour or 12 hours and also it should check if that user status before creating

PostgreSQL: Can a role or user be created that is allowed to create databases, but only with specific names?

Use case:
I have a web app which drops and recreates a test database on every test run. I'd like to have it do this with a PG role which is not a superuser, and not one which can destroy or create any database willy nilly, but rather, just be allowed to drop and create a specific database name, such as foo_test. Is this possible?
You could create a function with the "SECURITY DEFINER" option that creates the database and sets permissions on it. It would need to be in a database the restricted role could connect to of course.
http://www.postgresql.org/docs/9.1/static/sql-createfunction.html
You might be able to do it all with an SQL function, but it's trivial with plpgsql.

Resources