Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
add foreign key support
Browse files Browse the repository at this point in the history
  • Loading branch information
TimurRin committed Mar 18, 2024
1 parent baff895 commit 507cd8c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ function getColumnQueryPart(columnName, column) {
columnQuery.push(`"${columnName}"`);
if (column.type === "ID") {
columnQuery.push("INTEGER PRIMARY KEY AUTOINCREMENT");
} else if (column.type === "FOREIGN") {
columnQuery.push("INTEGER");
} else {
columnQuery.push(column.type.toUpperCase());
// if (column.primaryKey) {
Expand Down Expand Up @@ -75,6 +77,7 @@ function getTableCreationSqlQuery(name, columns) {

const primaryKeys = [];
const uniques = [];
const foreigns = [];

for (const [columnName, column] of Object.entries(columns)) {
if (column.type !== "ID" && column.primaryKey) {
Expand All @@ -85,6 +88,10 @@ function getTableCreationSqlQuery(name, columns) {
uniques.push(`"${columnName}"`);
}

if (column.type === "FOREIGN" && column.table) {
foreigns.push({ column: columnName, table: column.table });
}

columnsQuery.push(getColumnQueryPart(columnName, column));
}

Expand All @@ -96,6 +103,14 @@ function getTableCreationSqlQuery(name, columns) {
columnsQuery.push(`UNIQUE(${uniques.join(", ")})`);
}

if (foreigns.length > 0) {
for (const foreign of foreigns) {
columnsQuery.push(
`FOREIGN KEY ("${foreign.column}") REFERENCES "${foreign.table}"("id")`
);
}
}

return `CREATE TABLE "${name}" (${columnsQuery.join(", ")});`;
}

Expand Down
32 changes: 32 additions & 0 deletions migrate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,38 @@ describe("Creating new table", function () {
});
});

describe("Creating two tables and link foreign key", function () {
it("should create a table 'species'", function () {
createBaseMigration();
});
it("should create a table 'people'", function () {
migrations.createTable("people", {
id: { type: "ID" },
name: { type: "TEXT", notNull: true, default: "Unnamed person" },
talisman: { type: "FOREIGN", table: "species" },
});
});
it("should return correct SQL query", function () {
const queries = migrations.getMigrationsSqlQueries({});
assert.strictEqual(queries[0].query, "BEGIN TRANSACTION;");
assert.strictEqual(
queries[1].query,
`INSERT INTO "migrations" ("revision", "dw_version", "date_migrated") VALUES (?, ?, ?);`
);
assert.strictEqual(queries[1].args.length, 3);
assert.strictEqual(
queries[2].query,
`CREATE TABLE "species" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "name" TEXT NOT NULL DEFAULT 'Unnamed species', "origin" TEXT, "population" INTEGER);`
);
assert.strictEqual(
queries[3].query,
`CREATE TABLE "people" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "name" TEXT NOT NULL DEFAULT 'Unnamed person', "talisman" INTEGER, FOREIGN KEY ("talisman") REFERENCES "species"("id"));`
);
assert.strictEqual(queries[4].query, "COMMIT TRANSACTION;");
assert.strictEqual(queries[5].query, "VACUUM;");
});
});

describe("Creating new column", function () {
it("should create a table 'species'", function () {
createBaseMigration();
Expand Down

0 comments on commit 507cd8c

Please sign in to comment.