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

Extract many import-sql functionality to a separate file #1979

Merged
merged 2 commits into from
Aug 2, 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
49 changes: 21 additions & 28 deletions src/commands/dev-env-import-sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ import {
validateDependencies,
} from '../lib/dev-environment/dev-environment-cli';
import {
exec,
getEnvironmentPath,
resolveImportPath,
exec,
} from '../lib/dev-environment/dev-environment-core';
import {
addAdminUser,
flushCache,
reIndexSearch,
} from '../lib/dev-environment/dev-environment-database';
import { bootstrapLando, isEnvUp } from '../lib/dev-environment/dev-environment-lando';
import UserError from '../lib/user-error';
import { makeTempDir } from '../lib/utils';
import { validate as validateSQL, validateImportFileExtension } from '../lib/validations/sql';

export interface DevEnvImportSQLOptions {
skipReindex?: string;
searchReplace?: string;
skipReindex?: string | boolean;
searchReplace?: string[];
inPlace: boolean;
skipValidate: boolean;
quiet: boolean;
Expand Down Expand Up @@ -84,9 +89,8 @@ export class DevEnvImportSQLCommand {
}

const fd = await fs.promises.open( resolvedPath, 'r' );
const importArg = [ 'db', '--disable-auto-rehash' ].concat(
this.options.quiet ? '--silent' : []
);
const importArg = this.getImportArgs();

const origIsTTY = process.stdin.isTTY;

try {
Expand All @@ -111,38 +115,27 @@ export class DevEnvImportSQLCommand {
fs.unlinkSync( resolvedPath );
}

const cacheArg = [ 'wp', 'cache', 'flush', '--skip-plugins', '--skip-themes' ].concat(
this.options.quiet ? '--quiet' : []
);
await exec( lando, this.slug, cacheArg );
await flushCache( lando, this.slug, this.options.quiet );

if (
undefined === this.options.skipReindex ||
! processBooleanOption( this.options.skipReindex )
) {
try {
await exec( lando, this.slug, [ 'wp', 'cli', 'has-command', 'vip-search' ] );
await exec( lando, this.slug, [
'wp',
'vip-search',
'index',
'--setup',
'--network-wide',
'--skip-confirm',
] );
await reIndexSearch( lando, this.slug );
} catch {
// Exception means they don't have vip-search enabled.
}
}

const addUserArg = [
'wp',
'dev-env-add-admin',
'--username=vipgo',
'--password=password',
'--skip-plugins',
'--skip-themes',
].concat( this.options.quiet ? '--quiet' : [] );
await exec( lando, this.slug, addUserArg );
await addAdminUser( lando, this.slug );
}

public getImportArgs() {
const importArg = [ 'db', '--disable-auto-rehash' ].concat(
this.options.quiet ? '--silent' : []
);

return importArg;
}
}
34 changes: 34 additions & 0 deletions src/lib/dev-environment/dev-environment-database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Lando from 'lando';

import { exec } from './dev-environment-core';

export const addAdminUser = async ( lando: Lando, slug: string, quiet?: boolean ) => {
const addUserArg = [
'wp',
'dev-env-add-admin',
'--username=vipgo',
'--password=password',
'--skip-plugins',
'--skip-themes',
].concat( quiet ? [ '--quiet' ] : [] );
await exec( lando, slug, addUserArg );
};

export const reIndexSearch = async ( lando: Lando, slug: string ) => {
await exec( lando, slug, [ 'wp', 'cli', 'has-command', 'vip-search' ] );
await exec( lando, slug, [
'wp',
'vip-search',
'index',
'--setup',
'--network-wide',
'--skip-confirm',
] );
};

export const flushCache = async ( lando: Lando, slug: string, quiet?: boolean ) => {
const cacheArg = [ 'wp', 'cache', 'flush', '--skip-plugins', '--skip-themes' ].concat(
quiet ? [ '--quiet' ] : []
);
await exec( lando, slug, cacheArg );
};