Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if ! sudo mariadb -e "CREATE DATABASE IF NOT EXISTS {{ $name }} CHARACTER SET '{{ $charset }}' COLLATE '{{ $collation }}'"; then
if ! sudo mariadb -e "CREATE DATABASE IF NOT EXISTS \`{{ $name }}\` CHARACTER SET '{{ $charset }}' COLLATE '{{ $collation }}'"; then
echo 'VITO_SSH_ERROR' && exit 1
fi

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if ! sudo mariadb -e "DROP DATABASE IF EXISTS {{ $name }}"; then
if ! sudo mariadb -e "DROP DATABASE IF EXISTS \`{{ $name }}\`"; then
echo 'VITO_SSH_ERROR' && exit 1
fi

Expand Down
4 changes: 2 additions & 2 deletions resources/views/ssh/services/database/mariadb/link.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
@endphp

# Revoke all privileges first to ensure clean state
if ! sudo mariadb -e "REVOKE ALL PRIVILEGES ON {{ $database }}.* FROM '{{ $username }}'@'{{ $host }}'"; then
if ! sudo mariadb -e "REVOKE ALL PRIVILEGES ON \`{{ $database }}\`.* FROM '{{ $username }}'@'{{ $host }}'"; then
# Ignore error if user has no privileges yet
true
fi

# Grant the specific privileges
if ! sudo mariadb -e "GRANT {{ $grants }} ON {{ $database }}.* TO '{{ $username }}'@'{{ $host }}'"; then
if ! sudo mariadb -e "GRANT {{ $grants }} ON \`{{ $database }}\`.* TO '{{ $username }}'@'{{ $host }}'"; then
echo 'VITO_SSH_ERROR' && exit 1
fi

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if ! sudo mysql -e "CREATE DATABASE IF NOT EXISTS {{ $name }} CHARACTER SET '{{ $charset }}' COLLATE '{{ $collation }}'"; then
if ! sudo mysql -e "CREATE DATABASE IF NOT EXISTS \`{{ $name }}\` CHARACTER SET '{{ $charset }}' COLLATE '{{ $collation }}'"; then
echo 'VITO_SSH_ERROR' && exit 1
fi

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if ! sudo mysql -e "DROP DATABASE IF EXISTS {{ $name }}"; then
if ! sudo mysql -e "DROP DATABASE IF EXISTS \`{{ $name }}\`"; then
echo 'VITO_SSH_ERROR' && exit 1
fi

Expand Down
4 changes: 2 additions & 2 deletions resources/views/ssh/services/database/mysql/link.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
@endphp

# Revoke all privileges first to ensure clean state
if ! sudo mysql -e "REVOKE ALL PRIVILEGES ON {{ $database }}.* FROM '{{ $username }}'@'{{ $host }}'"; then
if ! sudo mysql -e "REVOKE ALL PRIVILEGES ON \`{{ $database }}\`.* FROM '{{ $username }}'@'{{ $host }}'"; then
# Ignore error if user has no privileges yet
true
fi

# Grant the specific privileges
if ! sudo mysql -e "GRANT {{ $grants }} ON {{ $database }}.* TO '{{ $username }}'@'{{ $host }}'"; then
if ! sudo mysql -e "GRANT {{ $grants }} ON \`{{ $database }}\`.* TO '{{ $username }}'@'{{ $host }}'"; then
echo 'VITO_SSH_ERROR' && exit 1
fi

Expand Down
42 changes: 42 additions & 0 deletions tests/Feature/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@
]);
});

test('create database with a hyphen in the name', function () {
$this->actingAs($this->user);

SSH::fake();

$this->post(route('databases.store', $this->server), [
'name' => 'my-database',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
])->assertSessionDoesntHaveErrors();

$this->assertDatabaseHas('databases', [
'name' => 'my-database',
'status' => DatabaseStatus::READY,
]);

SSH::assertExecutedContains('CREATE DATABASE IF NOT EXISTS \`my-database\`');
});

test('create database with user', function () {
$this->actingAs($this->user);

Expand Down Expand Up @@ -129,6 +148,29 @@
]);
});

test('delete database with a hyphen in the name', function () {
$this->actingAs($this->user);

SSH::fake();

/** @var Database $database */
$database = Database::factory()->create([
'server_id' => $this->server,
'name' => 'my-database',
]);

$this->delete(route('databases.destroy', [
'server' => $this->server,
'database' => $database,
]))->assertSessionDoesntHaveErrors();

$this->assertSoftDeleted('databases', [
'id' => $database->id,
]);

SSH::assertExecutedContains('DROP DATABASE IF EXISTS \`my-database\`');
});

test('delete database keeps linked user databases a list', function () {
$this->actingAs($this->user);

Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/DatabaseUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,28 @@
SSH::assertNotExecutedContains('ALTER DEFAULT PRIVILEGES');
});

test('mysql link quotes a database name with a hyphen', function () {
$this->actingAs($this->user);

SSH::fake();

Database::factory()->create(['server_id' => $this->server, 'name' => 'my-database']);

$databaseUser = DatabaseUser::factory()->create([
'server_id' => $this->server,
'username' => 'app',
'host' => 'localhost',
]);

$this->put(route('database-users.link', [
'server' => $this->server,
'databaseUser' => $databaseUser,
]), ['databases' => ['my-database']])->assertSessionDoesntHaveErrors();

SSH::assertExecutedContains('REVOKE ALL PRIVILEGES ON \`my-database\`.*');
SSH::assertExecutedContains('GRANT ALL PRIVILEGES ON \`my-database\`.*');
});

function vitoPestFeatureDatabaseUserTestUsePostgresql(): void
{
test()->server->services()->where('type', Mysql::type())->delete();
Expand Down
Loading