-
Notifications
You must be signed in to change notification settings - Fork 65
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
Implement custom migration table name #48
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,11 @@ export async function migrate( | |
migrationsDirectory: string, | ||
config: Config = {}, | ||
): Promise<Array<Migration>> { | ||
const migrationTableName = | ||
typeof config.migrationTableName === "string" | ||
? config.migrationTableName | ||
: "migrations" | ||
|
||
const log = | ||
config.logger != null | ||
? config.logger | ||
|
@@ -32,13 +37,16 @@ export async function migrate( | |
if (typeof migrationsDirectory !== "string") { | ||
throw new Error("Must pass migrations directory as a string") | ||
} | ||
const intendedMigrations = await load(migrationsDirectory, log) | ||
|
||
const intendedMigrations = await load(migrationsDirectory, log, { | ||
migrationTableName, | ||
}) | ||
|
||
if ("client" in dbConfig) { | ||
// we have been given a client to use, it should already be connected | ||
return withAdvisoryLock( | ||
log, | ||
runMigrations(intendedMigrations, log), | ||
runMigrations(intendedMigrations, log, migrationTableName), | ||
)(dbConfig.client) | ||
} | ||
|
||
|
@@ -59,17 +67,22 @@ export async function migrate( | |
|
||
const runWith = withConnection( | ||
log, | ||
withAdvisoryLock(log, runMigrations(intendedMigrations, log)), | ||
withAdvisoryLock( | ||
log, | ||
runMigrations(intendedMigrations, log, migrationTableName), | ||
), | ||
) | ||
|
||
return runWith(client) | ||
} | ||
|
||
function runMigrations(intendedMigrations: Array<Migration>, log: Logger) { | ||
function runMigrations( | ||
intendedMigrations: Array<Migration>, | ||
log: Logger, | ||
migrationTableName: string, | ||
) { | ||
return async (client: BasicPgClient) => { | ||
try { | ||
const migrationTableName = "migrations" | ||
|
||
log("Starting migrations") | ||
|
||
const appliedMigrations = await fetchAppliedMigrationFromDB( | ||
|
@@ -190,13 +203,13 @@ function logResult(completedMigrations: Array<Migration>, log: Logger) { | |
} | ||
|
||
/** Check whether table exists in postgres - http://stackoverflow.com/a/24089729 */ | ||
async function doesTableExist(client: BasicPgClient, tableName: string) { | ||
const result = await client.query(SQL`SELECT EXISTS ( | ||
SELECT 1 | ||
FROM pg_catalog.pg_class c | ||
WHERE c.relname = ${tableName} | ||
AND c.relkind = 'r' | ||
);`) | ||
|
||
return result.rows.length > 0 && result.rows[0].exists | ||
async function doesTableExist( | ||
client: BasicPgClient, | ||
migrationTableName: string, | ||
) { | ||
const result = await client.query(SQL` | ||
SELECT to_regclass(${migrationTableName}) as matching_tables | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed to |
||
`) | ||
|
||
return result.rows.length > 0 && result.rows[0].matching_tables !== null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = { | ||
generateSql: ({migrationTableName}) => ` | ||
CREATE TABLE IF NOT EXISTS ${migrationTableName} ( | ||
id integer PRIMARY KEY, | ||
name varchar(100) UNIQUE NOT NULL, | ||
hash varchar(40) NOT NULL, -- sha1 hex encoded hash of the file name and contents, to ensure it hasn't been altered since applying the migration | ||
executed_at timestamp DEFAULT current_timestamp | ||
)`, | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this test satisfy what you were asking for in #33 (comment)?