Skip to content

Commit 5878002

Browse files
kieranajpacburdine
authored andcommitted
fix(setup): strip undesirable characters out of DB name (#486)
no issue - normalize default db name for mysql
1 parent 7b6c8cc commit 5878002

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/commands/config/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,13 @@ class ConfigCommand extends Command {
117117
}
118118

119119
if (!argv.dbname) {
120+
const sanitizedDirName = path.basename(process.cwd()).replace(/[^a-zA-Z0-9_]+/g, '_');
120121
prompts.push({
121122
type: 'input',
122123
name: 'dbname',
123124
message: 'Enter your Ghost database name:',
124-
default: this.instance.config.get('database.connection.database', `ghost_${this.system.environment}_${path.basename(process.cwd())}`)
125+
default: this.instance.config.get('database.connection.database', `ghost_${this.system.environment}_${sanitizedDirName}`),
126+
validate: (val) => !/[^a-zA-Z0-9_]/.test(val) || 'MySQL database names may consist of only alphanumeric characters and underscores.'
125127
});
126128
}
127129
}

test/unit/commands/config-spec.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,12 @@ describe('Unit: Command > Config', function () {
192192
expect(passprompt).to.be.ok;
193193
expect(passprompt.message).to.match(/skip to keep current/);
194194

195-
expect(result.find(prompt => prompt.name === 'dbname')).to.be.ok;
195+
const nameprompt = result.find(prompt => prompt.name === 'dbname');
196+
expect(nameprompt).to.be.ok;
197+
expect(nameprompt.validate('example123')).to.be.true;
198+
expect(nameprompt.validate('example123.com')).to.match(/consist of only alpha/);
199+
expect(nameprompt.validate('example-123')).to.match(/consist of only alpha/);
200+
expect(nameprompt.validate('example!!!')).to.match(/consist of only alpha/);
196201
});
197202

198203
it('doesn\'t return dbhost prompt if dbhost provided', function () {

0 commit comments

Comments
 (0)