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

feat(phpmyadmin): Add vip db phpmyadmin command #1526

Merged
merged 15 commits into from
Feb 1, 2024
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
94 changes: 94 additions & 0 deletions __tests__/commands/phpmyadmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-return */
/**
* External dependencies
*/
import { beforeEach, describe, expect, it, jest } from '@jest/globals';

/**
* Internal dependencies
*/
import { PhpMyAdminCommand } from '../../src/commands/phpmyadmin';
import API from '../../src/lib/api';
import { CommandTracker } from '../../src/lib/tracker';

const generatePMAAccessMutationMock = jest.fn( async () => {
return Promise.resolve( {
data: {
generatePHPMyAdminAccess: {
url: 'http://test-url.com',
},
},
} );
} );

const enablePMAMutationMock = jest.fn( async () => {
return Promise.resolve( {
data: {
enablePHPMyAdmin: {
success: true,
},
},
} );
} );

const pmaEnabledQueryMockTrue = jest.fn( async () => {
return Promise.resolve( {
data: {
app: {
environments: [
{
phpMyAdminStatus: {
status: 'enabled',
},
},
],
},
},
} );
} );

jest.mock( '../../src/lib/api' );
jest.mocked( API ).mockImplementation( () => {
return Promise.resolve( {
mutate: generatePMAAccessMutationMock,
query: pmaEnabledQueryMockTrue,
} as any );
} );

describe( 'commands/PhpMyAdminCommand', () => {
beforeEach( () => {} );

describe( '.run', () => {
const app = { id: 123 };
const env = { id: 456, jobs: [] };
const tracker = jest.fn() as CommandTracker;
const cmd = new PhpMyAdminCommand( app, env, tracker );
const openUrl = jest.spyOn( cmd, 'openUrl' );

beforeEach( () => {
openUrl.mockReset();
} );

it( 'should open the generated URL in browser', async () => {
await cmd.run();
expect( pmaEnabledQueryMockTrue ).toHaveBeenCalledWith( {
query: expect.anything(),
variables: {
appId: 123,
envId: 456,
},
fetchPolicy: 'network-only',
} );
expect( enablePMAMutationMock ).not.toHaveBeenCalled();
expect( generatePMAAccessMutationMock ).toHaveBeenCalledWith( {
mutation: expect.anything(),
variables: {
input: {
environmentId: 456,
},
},
} );
expect( openUrl ).toHaveBeenCalledWith( 'http://test-url.com' );
} );
} );
} );
2 changes: 2 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"vip-config-software": "dist/bin/vip-config-software.js",
"vip-config-software-get": "dist/bin/vip-config-software-get.js",
"vip-config-software-update": "dist/bin/vip-config-software-update.js",
"vip-db": "dist/bin/vip-db.js",
"vip-db-phpmyadmin": "dist/bin/vip-db-phpmyadmin.js",
"vip-dev-env": "dist/bin/vip-dev-env.js",
"vip-dev-env-create": "dist/bin/vip-dev-env-create.js",
"vip-dev-env-update": "dist/bin/vip-dev-env-update.js",
Expand Down
52 changes: 52 additions & 0 deletions src/bin/vip-db-phpmyadmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env node

/**
* External dependencies
*/

/**
* Internal dependencies
*/
import { PhpMyAdminCommand } from '../commands/phpmyadmin';
import { App, AppEnvironment } from '../graphqlTypes';
import command from '../lib/cli/command';
import { makeCommandTracker } from '../lib/tracker';

const examples = [
{
usage: 'vip db phpmyadmin @mysite.develop',
description: 'Open PhpMyAdmin console for the database of the @mysite.develop environment',
},
];

const appQuery = `
id,
environments{
id
appId
type
uniqueLabel
}
`;

void command( {
appContext: true,
appQuery,
envContext: true,
module: 'phpmyadmin',
requiredArgs: 0,
usage: 'vip db phpmyadmin',
} )
.examples( examples )
.argv( process.argv, async ( arg: string[], { app, env }: { app: App; env: AppEnvironment } ) => {
const trackerFn = makeCommandTracker( 'phpmyadmin', {
app: app.id,
env: env.uniqueLabel,
} );
await trackerFn( 'execute' );

const cmd = new PhpMyAdminCommand( app, env, trackerFn );
await cmd.run();

await trackerFn( 'success' );
} );
21 changes: 21 additions & 0 deletions src/bin/vip-db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

/**
* External dependencies
*/

/**
* Internal dependencies
*/
import command from '../lib/cli/command';
import { trackEvent } from '../lib/tracker';

void command( { usage: 'vip db' } )
.command( 'phpmyadmin', 'Open PhpMyAdmin console for your application database' )
.example(
'vip db phpmyadmin @mysite.develop',
'Open PhpMyAdmin console for your database of the @mysite.develop environment'
)
.argv( process.argv, async () => {
await trackEvent( 'vip_db_command_execute' );
} );
1 change: 1 addition & 0 deletions src/bin/vip.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const runCmd = async function () {
.command( 'logs', 'Get logs from your VIP applications' )
.command( 'search-replace', 'Perform search and replace tasks on files' )
.command( 'slowlogs', 'Get slowlogs from your VIP applications' )
.command( 'db', 'Run operations on your VIP application database' )
.command( 'sync', 'Sync production to a development environment' )
.command( 'whoami', 'Display details about the currently logged-in user' )
.command( 'validate', 'Validate your VIP application and environment' )
Expand Down
Loading
Loading