Creating Servers#
Summary#
How to create a database that does not exist initially.
Create Database Problem#
Suppose we have the following database definition:
database mydb
{
driver: 'sqlserver';
connString: 'Server=myserver;user=sandy;Password=sandy;autocommit=1;Database=mydb';
create: '/create.sql';
}
We would like to create this database, so we run this command:
create database mydb;
But we get an error with output that looks like this:
mydb: Creating database.
mydb: Running '/create.sql'.
mydb: Error. (18456, b"Login failed for user 'sandy'.DB-Lib error message 20018, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (192.168.10.170)\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (192.168.10.170)\n")
Here we get a general Login failed for user 'sandy'
error, which is not very descriptive.
This could be caused by several factors, one of which is the database does not exist.
If the database does not exist, then you will NEVER be able to log in, especially in the case of SQL Server.
If you remove the Database
parameter from the connection string, as shown here:
Warning
This is NOT a recommended solution.
1database mydb
2{
3 driver: 'sqlserver';
4 connString: 'Server=myserver;user=sandy;Password=sandy;autocommit=1'; // No database specified.
5 create: '/create.sql';
6}
You can then create the database, and your problem will be solved. However, this causes another problem: your connection string doesn’t specify a database anymore, so you won’t be able to update your database later because the database parameter was removed.
The SQL Current solution to this problem is to use a server connection string and a script hint. A server connection string is simply a connection string that does not have a database parameter. It only points to the server. You end up defining a connection string for the database and a connection string for the server. SQL Current provides two options for this.
Solution 1.
Define property serverConnString
in the database definition and add a create
script hint:
1database mydb
2{
3 driver: 'sqlserver';
4 connString: 'Server=myserver;user=sandy;Password=sandy;autocommit=1;Database=mydb';
5 serverConnString: 'Server=myserver;user=sandy;Password=sandy;autocommit=1;Database=mydb';
6 create: '/create.sql' (serverConnString);
7}
SQL Current will use the serverConnString
value.
Otherwise, connString
will be used.
Solution 2.
Define a server. Add a create
property script hint that references myserver
.
1server myserver
2{
3 driver: 'sqlserver';
4 connString: 'Server=myserver;user=sandy;Password=sandy;autocommit=1';
5}
6
7database mydb
8{
9 driver: 'sqlserver';
10 connString: 'Server=myserver;user=sandy;Password=sandy;autocommit=1;Database=mydb';
11 create: '/create.sql' (myserver);
12}
SQL Current will use the myserver
connection string for the script defined by create
.