mirror of
https://github.com/immich-app/immich.git
synced 2025-07-08 10:44:15 -04:00
refactor: sql-tools (#19717)
This commit is contained in:
parent
484529e61e
commit
6044663e26
@ -1,4 +1,4 @@
|
||||
import { compareColumns } from 'src/sql-tools/diff/comparers/column.comparer';
|
||||
import { compareColumns } from 'src/sql-tools/comparers/column.comparer';
|
||||
import { DatabaseColumn, Reason } from 'src/sql-tools/types';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
@ -18,7 +18,7 @@ describe('compareColumns', () => {
|
||||
{
|
||||
tableName: 'table1',
|
||||
columnName: 'test',
|
||||
type: 'column.drop',
|
||||
type: 'ColumnDrop',
|
||||
reason: Reason.MissingInSource,
|
||||
},
|
||||
]);
|
||||
@ -29,7 +29,7 @@ describe('compareColumns', () => {
|
||||
it('should work', () => {
|
||||
expect(compareColumns.onMissing(testColumn)).toEqual([
|
||||
{
|
||||
type: 'column.add',
|
||||
type: 'ColumnAdd',
|
||||
column: testColumn,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
||||
@ -50,11 +50,11 @@ describe('compareColumns', () => {
|
||||
{
|
||||
columnName: 'test',
|
||||
tableName: 'table1',
|
||||
type: 'column.drop',
|
||||
type: 'ColumnDrop',
|
||||
reason,
|
||||
},
|
||||
{
|
||||
type: 'column.add',
|
||||
type: 'ColumnAdd',
|
||||
column: source,
|
||||
reason,
|
||||
},
|
||||
@ -69,7 +69,7 @@ describe('compareColumns', () => {
|
||||
{
|
||||
columnName: 'test',
|
||||
tableName: 'table1',
|
||||
type: 'column.alter',
|
||||
type: 'ColumnAlter',
|
||||
changes: {
|
||||
comment: 'new comment',
|
||||
},
|
@ -4,14 +4,14 @@ import { Comparer, DatabaseColumn, Reason, SchemaDiff } from 'src/sql-tools/type
|
||||
export const compareColumns: Comparer<DatabaseColumn> = {
|
||||
onMissing: (source) => [
|
||||
{
|
||||
type: 'column.add',
|
||||
type: 'ColumnAdd',
|
||||
column: source,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
||||
],
|
||||
onExtra: (target) => [
|
||||
{
|
||||
type: 'column.drop',
|
||||
type: 'ColumnDrop',
|
||||
tableName: target.tableName,
|
||||
columnName: target.name,
|
||||
reason: Reason.MissingInSource,
|
||||
@ -31,7 +31,7 @@ export const compareColumns: Comparer<DatabaseColumn> = {
|
||||
const items: SchemaDiff[] = [];
|
||||
if (source.nullable !== target.nullable) {
|
||||
items.push({
|
||||
type: 'column.alter',
|
||||
type: 'ColumnAlter',
|
||||
tableName: source.tableName,
|
||||
columnName: source.name,
|
||||
changes: {
|
||||
@ -43,7 +43,7 @@ export const compareColumns: Comparer<DatabaseColumn> = {
|
||||
|
||||
if (!isDefaultEqual(source, target)) {
|
||||
items.push({
|
||||
type: 'column.alter',
|
||||
type: 'ColumnAlter',
|
||||
tableName: source.tableName,
|
||||
columnName: source.name,
|
||||
changes: {
|
||||
@ -55,7 +55,7 @@ export const compareColumns: Comparer<DatabaseColumn> = {
|
||||
|
||||
if (source.comment !== target.comment) {
|
||||
items.push({
|
||||
type: 'column.alter',
|
||||
type: 'ColumnAlter',
|
||||
tableName: source.tableName,
|
||||
columnName: source.name,
|
||||
changes: {
|
||||
@ -72,11 +72,11 @@ export const compareColumns: Comparer<DatabaseColumn> = {
|
||||
const dropAndRecreateColumn = (source: DatabaseColumn, target: DatabaseColumn, reason: string): SchemaDiff[] => {
|
||||
return [
|
||||
{
|
||||
type: 'column.drop',
|
||||
type: 'ColumnDrop',
|
||||
tableName: target.tableName,
|
||||
columnName: target.name,
|
||||
reason,
|
||||
},
|
||||
{ type: 'column.add', column: source, reason },
|
||||
{ type: 'ColumnAdd', column: source, reason },
|
||||
];
|
||||
};
|
@ -1,9 +1,9 @@
|
||||
import { compareConstraints } from 'src/sql-tools/diff/comparers/constraint.comparer';
|
||||
import { DatabaseConstraint, DatabaseConstraintType, Reason } from 'src/sql-tools/types';
|
||||
import { compareConstraints } from 'src/sql-tools/comparers/constraint.comparer';
|
||||
import { ConstraintType, DatabaseConstraint, Reason } from 'src/sql-tools/types';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
const testConstraint: DatabaseConstraint = {
|
||||
type: DatabaseConstraintType.PRIMARY_KEY,
|
||||
type: ConstraintType.PRIMARY_KEY,
|
||||
name: 'test',
|
||||
tableName: 'table1',
|
||||
columnNames: ['column1'],
|
||||
@ -15,7 +15,7 @@ describe('compareConstraints', () => {
|
||||
it('should work', () => {
|
||||
expect(compareConstraints.onExtra(testConstraint)).toEqual([
|
||||
{
|
||||
type: 'constraint.drop',
|
||||
type: 'ConstraintDrop',
|
||||
constraintName: 'test',
|
||||
tableName: 'table1',
|
||||
reason: Reason.MissingInSource,
|
||||
@ -28,7 +28,7 @@ describe('compareConstraints', () => {
|
||||
it('should work', () => {
|
||||
expect(compareConstraints.onMissing(testConstraint)).toEqual([
|
||||
{
|
||||
type: 'constraint.add',
|
||||
type: 'ConstraintAdd',
|
||||
constraint: testConstraint,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
||||
@ -49,11 +49,11 @@ describe('compareConstraints', () => {
|
||||
{
|
||||
constraintName: 'test',
|
||||
tableName: 'table1',
|
||||
type: 'constraint.drop',
|
||||
type: 'ConstraintDrop',
|
||||
reason,
|
||||
},
|
||||
{
|
||||
type: 'constraint.add',
|
||||
type: 'ConstraintAdd',
|
||||
constraint: source,
|
||||
reason,
|
||||
},
|
@ -2,9 +2,9 @@ import { haveEqualColumns } from 'src/sql-tools/helpers';
|
||||
import {
|
||||
CompareFunction,
|
||||
Comparer,
|
||||
ConstraintType,
|
||||
DatabaseCheckConstraint,
|
||||
DatabaseConstraint,
|
||||
DatabaseConstraintType,
|
||||
DatabaseForeignKeyConstraint,
|
||||
DatabasePrimaryKeyConstraint,
|
||||
DatabaseUniqueConstraint,
|
||||
@ -15,14 +15,14 @@ import {
|
||||
export const compareConstraints: Comparer<DatabaseConstraint> = {
|
||||
onMissing: (source) => [
|
||||
{
|
||||
type: 'constraint.add',
|
||||
type: 'ConstraintAdd',
|
||||
constraint: source,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
||||
],
|
||||
onExtra: (target) => [
|
||||
{
|
||||
type: 'constraint.drop',
|
||||
type: 'ConstraintDrop',
|
||||
tableName: target.tableName,
|
||||
constraintName: target.name,
|
||||
reason: Reason.MissingInSource,
|
||||
@ -30,19 +30,19 @@ export const compareConstraints: Comparer<DatabaseConstraint> = {
|
||||
],
|
||||
onCompare: (source, target) => {
|
||||
switch (source.type) {
|
||||
case DatabaseConstraintType.PRIMARY_KEY: {
|
||||
case ConstraintType.PRIMARY_KEY: {
|
||||
return comparePrimaryKeyConstraint(source, target as DatabasePrimaryKeyConstraint);
|
||||
}
|
||||
|
||||
case DatabaseConstraintType.FOREIGN_KEY: {
|
||||
case ConstraintType.FOREIGN_KEY: {
|
||||
return compareForeignKeyConstraint(source, target as DatabaseForeignKeyConstraint);
|
||||
}
|
||||
|
||||
case DatabaseConstraintType.UNIQUE: {
|
||||
case ConstraintType.UNIQUE: {
|
||||
return compareUniqueConstraint(source, target as DatabaseUniqueConstraint);
|
||||
}
|
||||
|
||||
case DatabaseConstraintType.CHECK: {
|
||||
case ConstraintType.CHECK: {
|
||||
return compareCheckConstraint(source, target as DatabaseCheckConstraint);
|
||||
}
|
||||
|
||||
@ -123,11 +123,11 @@ const dropAndRecreateConstraint = (
|
||||
): SchemaDiff[] => {
|
||||
return [
|
||||
{
|
||||
type: 'constraint.drop',
|
||||
type: 'ConstraintDrop',
|
||||
tableName: target.tableName,
|
||||
constraintName: target.name,
|
||||
reason,
|
||||
},
|
||||
{ type: 'constraint.add', constraint: source, reason },
|
||||
{ type: 'ConstraintAdd', constraint: source, reason },
|
||||
];
|
||||
};
|
@ -1,4 +1,4 @@
|
||||
import { compareEnums } from 'src/sql-tools/diff/comparers/enum.comparer';
|
||||
import { compareEnums } from 'src/sql-tools/comparers/enum.comparer';
|
||||
import { DatabaseEnum, Reason } from 'src/sql-tools/types';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
@ -10,7 +10,7 @@ describe('compareEnums', () => {
|
||||
expect(compareEnums.onExtra(testEnum)).toEqual([
|
||||
{
|
||||
enumName: 'test',
|
||||
type: 'enum.drop',
|
||||
type: 'EnumDrop',
|
||||
reason: Reason.MissingInSource,
|
||||
},
|
||||
]);
|
||||
@ -21,7 +21,7 @@ describe('compareEnums', () => {
|
||||
it('should work', () => {
|
||||
expect(compareEnums.onMissing(testEnum)).toEqual([
|
||||
{
|
||||
type: 'enum.create',
|
||||
type: 'EnumCreate',
|
||||
enum: testEnum,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
||||
@ -40,11 +40,11 @@ describe('compareEnums', () => {
|
||||
expect(compareEnums.onCompare(source, target)).toEqual([
|
||||
{
|
||||
enumName: 'test',
|
||||
type: 'enum.drop',
|
||||
type: 'EnumDrop',
|
||||
reason: 'enum values has changed (foo,bar vs foo,bar,world)',
|
||||
},
|
||||
{
|
||||
type: 'enum.create',
|
||||
type: 'EnumCreate',
|
||||
enum: source,
|
||||
reason: 'enum values has changed (foo,bar vs foo,bar,world)',
|
||||
},
|
@ -3,14 +3,14 @@ import { Comparer, DatabaseEnum, Reason } from 'src/sql-tools/types';
|
||||
export const compareEnums: Comparer<DatabaseEnum> = {
|
||||
onMissing: (source) => [
|
||||
{
|
||||
type: 'enum.create',
|
||||
type: 'EnumCreate',
|
||||
enum: source,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
||||
],
|
||||
onExtra: (target) => [
|
||||
{
|
||||
type: 'enum.drop',
|
||||
type: 'EnumDrop',
|
||||
enumName: target.name,
|
||||
reason: Reason.MissingInSource,
|
||||
},
|
||||
@ -21,12 +21,12 @@ export const compareEnums: Comparer<DatabaseEnum> = {
|
||||
const reason = `enum values has changed (${source.values} vs ${target.values})`;
|
||||
return [
|
||||
{
|
||||
type: 'enum.drop',
|
||||
type: 'EnumDrop',
|
||||
enumName: source.name,
|
||||
reason,
|
||||
},
|
||||
{
|
||||
type: 'enum.create',
|
||||
type: 'EnumCreate',
|
||||
enum: source,
|
||||
reason,
|
||||
},
|
@ -1,4 +1,4 @@
|
||||
import { compareExtensions } from 'src/sql-tools/diff/comparers/extension.comparer';
|
||||
import { compareExtensions } from 'src/sql-tools/comparers/extension.comparer';
|
||||
import { Reason } from 'src/sql-tools/types';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
@ -10,7 +10,7 @@ describe('compareExtensions', () => {
|
||||
expect(compareExtensions.onExtra(testExtension)).toEqual([
|
||||
{
|
||||
extensionName: 'test',
|
||||
type: 'extension.drop',
|
||||
type: 'ExtensionDrop',
|
||||
reason: Reason.MissingInSource,
|
||||
},
|
||||
]);
|
||||
@ -21,7 +21,7 @@ describe('compareExtensions', () => {
|
||||
it('should work', () => {
|
||||
expect(compareExtensions.onMissing(testExtension)).toEqual([
|
||||
{
|
||||
type: 'extension.create',
|
||||
type: 'ExtensionCreate',
|
||||
extension: testExtension,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
@ -3,14 +3,14 @@ import { Comparer, DatabaseExtension, Reason } from 'src/sql-tools/types';
|
||||
export const compareExtensions: Comparer<DatabaseExtension> = {
|
||||
onMissing: (source) => [
|
||||
{
|
||||
type: 'extension.create',
|
||||
type: 'ExtensionCreate',
|
||||
extension: source,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
||||
],
|
||||
onExtra: (target) => [
|
||||
{
|
||||
type: 'extension.drop',
|
||||
type: 'ExtensionDrop',
|
||||
extensionName: target.name,
|
||||
reason: Reason.MissingInSource,
|
||||
},
|
@ -1,4 +1,4 @@
|
||||
import { compareFunctions } from 'src/sql-tools/diff/comparers/function.comparer';
|
||||
import { compareFunctions } from 'src/sql-tools/comparers/function.comparer';
|
||||
import { DatabaseFunction, Reason } from 'src/sql-tools/types';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
@ -14,7 +14,7 @@ describe('compareFunctions', () => {
|
||||
expect(compareFunctions.onExtra(testFunction)).toEqual([
|
||||
{
|
||||
functionName: 'test',
|
||||
type: 'function.drop',
|
||||
type: 'FunctionDrop',
|
||||
reason: Reason.MissingInSource,
|
||||
},
|
||||
]);
|
||||
@ -25,7 +25,7 @@ describe('compareFunctions', () => {
|
||||
it('should work', () => {
|
||||
expect(compareFunctions.onMissing(testFunction)).toEqual([
|
||||
{
|
||||
type: 'function.create',
|
||||
type: 'FunctionCreate',
|
||||
function: testFunction,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
||||
@ -43,7 +43,7 @@ describe('compareFunctions', () => {
|
||||
const target: DatabaseFunction = { ...testFunction, expression: 'SELECT 2' };
|
||||
expect(compareFunctions.onCompare(source, target)).toEqual([
|
||||
{
|
||||
type: 'function.create',
|
||||
type: 'FunctionCreate',
|
||||
reason: 'function expression has changed (SELECT 1 vs SELECT 2)',
|
||||
function: source,
|
||||
},
|
@ -3,14 +3,14 @@ import { Comparer, DatabaseFunction, Reason } from 'src/sql-tools/types';
|
||||
export const compareFunctions: Comparer<DatabaseFunction> = {
|
||||
onMissing: (source) => [
|
||||
{
|
||||
type: 'function.create',
|
||||
type: 'FunctionCreate',
|
||||
function: source,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
||||
],
|
||||
onExtra: (target) => [
|
||||
{
|
||||
type: 'function.drop',
|
||||
type: 'FunctionDrop',
|
||||
functionName: target.name,
|
||||
reason: Reason.MissingInSource,
|
||||
},
|
||||
@ -20,7 +20,7 @@ export const compareFunctions: Comparer<DatabaseFunction> = {
|
||||
const reason = `function expression has changed (${source.expression} vs ${target.expression})`;
|
||||
return [
|
||||
{
|
||||
type: 'function.create',
|
||||
type: 'FunctionCreate',
|
||||
function: source,
|
||||
reason,
|
||||
},
|
@ -1,4 +1,4 @@
|
||||
import { compareIndexes } from 'src/sql-tools/diff/comparers/index.comparer';
|
||||
import { compareIndexes } from 'src/sql-tools/comparers/index.comparer';
|
||||
import { DatabaseIndex, Reason } from 'src/sql-tools/types';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
@ -15,7 +15,7 @@ describe('compareIndexes', () => {
|
||||
it('should work', () => {
|
||||
expect(compareIndexes.onExtra(testIndex)).toEqual([
|
||||
{
|
||||
type: 'index.drop',
|
||||
type: 'IndexDrop',
|
||||
indexName: 'test',
|
||||
reason: Reason.MissingInSource,
|
||||
},
|
||||
@ -27,7 +27,7 @@ describe('compareIndexes', () => {
|
||||
it('should work', () => {
|
||||
expect(compareIndexes.onMissing(testIndex)).toEqual([
|
||||
{
|
||||
type: 'index.create',
|
||||
type: 'IndexCreate',
|
||||
index: testIndex,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
||||
@ -58,11 +58,11 @@ describe('compareIndexes', () => {
|
||||
expect(compareIndexes.onCompare(source, target)).toEqual([
|
||||
{
|
||||
indexName: 'test',
|
||||
type: 'index.drop',
|
||||
type: 'IndexDrop',
|
||||
reason: 'columns are different (column1 vs column1,column2)',
|
||||
},
|
||||
{
|
||||
type: 'index.create',
|
||||
type: 'IndexCreate',
|
||||
index: source,
|
||||
reason: 'columns are different (column1 vs column1,column2)',
|
||||
},
|
@ -4,14 +4,14 @@ import { Comparer, DatabaseIndex, Reason } from 'src/sql-tools/types';
|
||||
export const compareIndexes: Comparer<DatabaseIndex> = {
|
||||
onMissing: (source) => [
|
||||
{
|
||||
type: 'index.create',
|
||||
type: 'IndexCreate',
|
||||
index: source,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
||||
],
|
||||
onExtra: (target) => [
|
||||
{
|
||||
type: 'index.drop',
|
||||
type: 'IndexDrop',
|
||||
indexName: target.name,
|
||||
reason: Reason.MissingInSource,
|
||||
},
|
||||
@ -36,8 +36,8 @@ export const compareIndexes: Comparer<DatabaseIndex> = {
|
||||
|
||||
if (reason) {
|
||||
return [
|
||||
{ type: 'index.drop', indexName: target.name, reason },
|
||||
{ type: 'index.create', index: source, reason },
|
||||
{ type: 'IndexDrop', indexName: target.name, reason },
|
||||
{ type: 'IndexCreate', index: source, reason },
|
||||
];
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { compareParameters } from 'src/sql-tools/diff/comparers/parameter.comparer';
|
||||
import { compareParameters } from 'src/sql-tools/comparers/parameter.comparer';
|
||||
import { DatabaseParameter, Reason } from 'src/sql-tools/types';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
@ -15,7 +15,7 @@ describe('compareParameters', () => {
|
||||
it('should work', () => {
|
||||
expect(compareParameters.onExtra(testParameter)).toEqual([
|
||||
{
|
||||
type: 'parameter.reset',
|
||||
type: 'ParameterReset',
|
||||
databaseName: 'immich',
|
||||
parameterName: 'test',
|
||||
reason: Reason.MissingInSource,
|
||||
@ -28,7 +28,7 @@ describe('compareParameters', () => {
|
||||
it('should work', () => {
|
||||
expect(compareParameters.onMissing(testParameter)).toEqual([
|
||||
{
|
||||
type: 'parameter.set',
|
||||
type: 'ParameterSet',
|
||||
parameter: testParameter,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
@ -3,14 +3,14 @@ import { Comparer, DatabaseParameter, Reason } from 'src/sql-tools/types';
|
||||
export const compareParameters: Comparer<DatabaseParameter> = {
|
||||
onMissing: (source) => [
|
||||
{
|
||||
type: 'parameter.set',
|
||||
type: 'ParameterSet',
|
||||
parameter: source,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
||||
],
|
||||
onExtra: (target) => [
|
||||
{
|
||||
type: 'parameter.reset',
|
||||
type: 'ParameterReset',
|
||||
databaseName: target.databaseName,
|
||||
parameterName: target.name,
|
||||
reason: Reason.MissingInSource,
|
@ -1,4 +1,4 @@
|
||||
import { compareTables } from 'src/sql-tools/diff/comparers/table.comparer';
|
||||
import { compareTables } from 'src/sql-tools/comparers/table.comparer';
|
||||
import { DatabaseTable, Reason } from 'src/sql-tools/types';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
@ -16,7 +16,7 @@ describe('compareParameters', () => {
|
||||
it('should work', () => {
|
||||
expect(compareTables.onExtra(testTable)).toEqual([
|
||||
{
|
||||
type: 'table.drop',
|
||||
type: 'TableDrop',
|
||||
tableName: 'test',
|
||||
reason: Reason.MissingInSource,
|
||||
},
|
||||
@ -28,7 +28,7 @@ describe('compareParameters', () => {
|
||||
it('should work', () => {
|
||||
expect(compareTables.onMissing(testTable)).toEqual([
|
||||
{
|
||||
type: 'table.create',
|
||||
type: 'TableCreate',
|
||||
table: testTable,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
@ -1,47 +1,33 @@
|
||||
import { compareColumns } from 'src/sql-tools/diff/comparers/column.comparer';
|
||||
import { compareConstraints } from 'src/sql-tools/diff/comparers/constraint.comparer';
|
||||
import { compareIndexes } from 'src/sql-tools/diff/comparers/index.comparer';
|
||||
import { compareTriggers } from 'src/sql-tools/diff/comparers/trigger.comparer';
|
||||
import { compareColumns } from 'src/sql-tools/comparers/column.comparer';
|
||||
import { compareConstraints } from 'src/sql-tools/comparers/constraint.comparer';
|
||||
import { compareIndexes } from 'src/sql-tools/comparers/index.comparer';
|
||||
import { compareTriggers } from 'src/sql-tools/comparers/trigger.comparer';
|
||||
import { compare } from 'src/sql-tools/helpers';
|
||||
import { Comparer, DatabaseTable, Reason, SchemaDiff } from 'src/sql-tools/types';
|
||||
|
||||
const newTable = (name: string) => ({
|
||||
name,
|
||||
columns: [],
|
||||
indexes: [],
|
||||
constraints: [],
|
||||
triggers: [],
|
||||
synchronize: true,
|
||||
});
|
||||
|
||||
export const compareTables: Comparer<DatabaseTable> = {
|
||||
onMissing: (source) => [
|
||||
{
|
||||
type: 'table.create',
|
||||
type: 'TableCreate',
|
||||
table: source,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
||||
// TODO merge constraints into table create record when possible
|
||||
...compareTable(
|
||||
source,
|
||||
{
|
||||
name: source.name,
|
||||
columns: [],
|
||||
indexes: [],
|
||||
constraints: [],
|
||||
triggers: [],
|
||||
synchronize: true,
|
||||
},
|
||||
|
||||
{ columns: false },
|
||||
),
|
||||
...compareTable(source, newTable(source.name), { columns: false }),
|
||||
],
|
||||
onExtra: (target) => [
|
||||
...compareTable(
|
||||
...compareTable(newTable(target.name), target, { columns: false }),
|
||||
{
|
||||
name: target.name,
|
||||
columns: [],
|
||||
indexes: [],
|
||||
constraints: [],
|
||||
triggers: [],
|
||||
synchronize: true,
|
||||
},
|
||||
target,
|
||||
{ columns: false },
|
||||
),
|
||||
{
|
||||
type: 'table.drop',
|
||||
type: 'TableDrop',
|
||||
tableName: target.name,
|
||||
reason: Reason.MissingInSource,
|
||||
},
|
@ -1,4 +1,4 @@
|
||||
import { compareTriggers } from 'src/sql-tools/diff/comparers/trigger.comparer';
|
||||
import { compareTriggers } from 'src/sql-tools/comparers/trigger.comparer';
|
||||
import { DatabaseTrigger, Reason } from 'src/sql-tools/types';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
@ -17,7 +17,7 @@ describe('compareTriggers', () => {
|
||||
it('should work', () => {
|
||||
expect(compareTriggers.onExtra(testTrigger)).toEqual([
|
||||
{
|
||||
type: 'trigger.drop',
|
||||
type: 'TriggerDrop',
|
||||
tableName: 'table1',
|
||||
triggerName: 'test',
|
||||
reason: Reason.MissingInSource,
|
||||
@ -30,7 +30,7 @@ describe('compareTriggers', () => {
|
||||
it('should work', () => {
|
||||
expect(compareTriggers.onMissing(testTrigger)).toEqual([
|
||||
{
|
||||
type: 'trigger.create',
|
||||
type: 'TriggerCreate',
|
||||
trigger: testTrigger,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
||||
@ -47,42 +47,42 @@ describe('compareTriggers', () => {
|
||||
const source: DatabaseTrigger = { ...testTrigger, functionName: 'my_new_name' };
|
||||
const target: DatabaseTrigger = { ...testTrigger, functionName: 'my_old_name' };
|
||||
const reason = `function is different (my_new_name vs my_old_name)`;
|
||||
expect(compareTriggers.onCompare(source, target)).toEqual([{ type: 'trigger.create', trigger: source, reason }]);
|
||||
expect(compareTriggers.onCompare(source, target)).toEqual([{ type: 'TriggerCreate', trigger: source, reason }]);
|
||||
});
|
||||
|
||||
it('should detect a change in actions', () => {
|
||||
const source: DatabaseTrigger = { ...testTrigger, actions: ['delete'] };
|
||||
const target: DatabaseTrigger = { ...testTrigger, actions: ['delete', 'insert'] };
|
||||
const reason = `action is different (delete vs delete,insert)`;
|
||||
expect(compareTriggers.onCompare(source, target)).toEqual([{ type: 'trigger.create', trigger: source, reason }]);
|
||||
expect(compareTriggers.onCompare(source, target)).toEqual([{ type: 'TriggerCreate', trigger: source, reason }]);
|
||||
});
|
||||
|
||||
it('should detect a change in timing', () => {
|
||||
const source: DatabaseTrigger = { ...testTrigger, timing: 'before' };
|
||||
const target: DatabaseTrigger = { ...testTrigger, timing: 'after' };
|
||||
const reason = `timing method is different (before vs after)`;
|
||||
expect(compareTriggers.onCompare(source, target)).toEqual([{ type: 'trigger.create', trigger: source, reason }]);
|
||||
expect(compareTriggers.onCompare(source, target)).toEqual([{ type: 'TriggerCreate', trigger: source, reason }]);
|
||||
});
|
||||
|
||||
it('should detect a change in scope', () => {
|
||||
const source: DatabaseTrigger = { ...testTrigger, scope: 'row' };
|
||||
const target: DatabaseTrigger = { ...testTrigger, scope: 'statement' };
|
||||
const reason = `scope is different (row vs statement)`;
|
||||
expect(compareTriggers.onCompare(source, target)).toEqual([{ type: 'trigger.create', trigger: source, reason }]);
|
||||
expect(compareTriggers.onCompare(source, target)).toEqual([{ type: 'TriggerCreate', trigger: source, reason }]);
|
||||
});
|
||||
|
||||
it('should detect a change in new table reference', () => {
|
||||
const source: DatabaseTrigger = { ...testTrigger, referencingNewTableAs: 'new_table' };
|
||||
const target: DatabaseTrigger = { ...testTrigger, referencingNewTableAs: undefined };
|
||||
const reason = `new table reference is different (new_table vs undefined)`;
|
||||
expect(compareTriggers.onCompare(source, target)).toEqual([{ type: 'trigger.create', trigger: source, reason }]);
|
||||
expect(compareTriggers.onCompare(source, target)).toEqual([{ type: 'TriggerCreate', trigger: source, reason }]);
|
||||
});
|
||||
|
||||
it('should detect a change in old table reference', () => {
|
||||
const source: DatabaseTrigger = { ...testTrigger, referencingOldTableAs: 'old_table' };
|
||||
const target: DatabaseTrigger = { ...testTrigger, referencingOldTableAs: undefined };
|
||||
const reason = `old table reference is different (old_table vs undefined)`;
|
||||
expect(compareTriggers.onCompare(source, target)).toEqual([{ type: 'trigger.create', trigger: source, reason }]);
|
||||
expect(compareTriggers.onCompare(source, target)).toEqual([{ type: 'TriggerCreate', trigger: source, reason }]);
|
||||
});
|
||||
});
|
||||
});
|
@ -3,14 +3,14 @@ import { Comparer, DatabaseTrigger, Reason } from 'src/sql-tools/types';
|
||||
export const compareTriggers: Comparer<DatabaseTrigger> = {
|
||||
onMissing: (source) => [
|
||||
{
|
||||
type: 'trigger.create',
|
||||
type: 'TriggerCreate',
|
||||
trigger: source,
|
||||
reason: Reason.MissingInTarget,
|
||||
},
|
||||
],
|
||||
onExtra: (target) => [
|
||||
{
|
||||
type: 'trigger.drop',
|
||||
type: 'TriggerDrop',
|
||||
tableName: target.tableName,
|
||||
triggerName: target.name,
|
||||
reason: Reason.MissingInSource,
|
||||
@ -33,7 +33,7 @@ export const compareTriggers: Comparer<DatabaseTrigger> = {
|
||||
}
|
||||
|
||||
if (reason) {
|
||||
return [{ type: 'trigger.create', trigger: source, reason }];
|
||||
return [{ type: 'TriggerCreate', trigger: source, reason }];
|
||||
}
|
||||
|
||||
return [];
|
@ -1,4 +1,4 @@
|
||||
import { TriggerFunction, TriggerFunctionOptions } from 'src/sql-tools/from-code/decorators/trigger-function.decorator';
|
||||
import { TriggerFunction, TriggerFunctionOptions } from 'src/sql-tools/decorators/trigger-function.decorator';
|
||||
|
||||
export const AfterDeleteTrigger = (options: Omit<TriggerFunctionOptions, 'timing' | 'actions'>) =>
|
||||
TriggerFunction({
|
@ -1,4 +1,4 @@
|
||||
import { TriggerFunction, TriggerFunctionOptions } from 'src/sql-tools/from-code/decorators/trigger-function.decorator';
|
||||
import { TriggerFunction, TriggerFunctionOptions } from 'src/sql-tools/decorators/trigger-function.decorator';
|
||||
|
||||
export const AfterInsertTrigger = (options: Omit<TriggerFunctionOptions, 'timing' | 'actions'>) =>
|
||||
TriggerFunction({
|
@ -1,4 +1,4 @@
|
||||
import { TriggerFunction, TriggerFunctionOptions } from 'src/sql-tools/from-code/decorators/trigger-function.decorator';
|
||||
import { TriggerFunction, TriggerFunctionOptions } from 'src/sql-tools/decorators/trigger-function.decorator';
|
||||
|
||||
export const BeforeUpdateTrigger = (options: Omit<TriggerFunctionOptions, 'timing' | 'actions'>) =>
|
||||
TriggerFunction({
|
@ -1,4 +1,4 @@
|
||||
import { register } from 'src/sql-tools/from-code/register';
|
||||
import { register } from 'src/sql-tools/register';
|
||||
|
||||
export type CheckOptions = {
|
||||
name?: string;
|
@ -1,5 +1,5 @@
|
||||
import { register } from 'src/sql-tools/from-code/register';
|
||||
import { asOptions } from 'src/sql-tools/helpers';
|
||||
import { register } from 'src/sql-tools/register';
|
||||
import { ColumnStorage, ColumnType, DatabaseEnum } from 'src/sql-tools/types';
|
||||
|
||||
export type ColumnValue = null | boolean | string | number | object | Date | (() => string);
|
@ -1,5 +1,5 @@
|
||||
import { ColumnValue } from 'src/sql-tools/from-code/decorators/column.decorator';
|
||||
import { register } from 'src/sql-tools/from-code/register';
|
||||
import { ColumnValue } from 'src/sql-tools/decorators/column.decorator';
|
||||
import { register } from 'src/sql-tools/register';
|
||||
import { ParameterScope } from 'src/sql-tools/types';
|
||||
|
||||
export type ConfigurationParameterOptions = {
|
@ -1,4 +1,4 @@
|
||||
import { Column, ColumnOptions } from 'src/sql-tools/from-code/decorators/column.decorator';
|
||||
import { Column, ColumnOptions } from 'src/sql-tools/decorators/column.decorator';
|
||||
|
||||
export const CreateDateColumn = (options: ColumnOptions = {}): PropertyDecorator => {
|
||||
return Column({
|
@ -1,4 +1,4 @@
|
||||
import { register } from 'src/sql-tools/from-code/register';
|
||||
import { register } from 'src/sql-tools/register';
|
||||
|
||||
export type DatabaseOptions = {
|
||||
name?: string;
|
@ -1,4 +1,4 @@
|
||||
import { Column, ColumnOptions } from 'src/sql-tools/from-code/decorators/column.decorator';
|
||||
import { Column, ColumnOptions } from 'src/sql-tools/decorators/column.decorator';
|
||||
|
||||
export const DeleteDateColumn = (options: ColumnOptions = {}): PropertyDecorator => {
|
||||
return Column({
|
@ -1,5 +1,5 @@
|
||||
import { register } from 'src/sql-tools/from-code/register';
|
||||
import { asOptions } from 'src/sql-tools/helpers';
|
||||
import { register } from 'src/sql-tools/register';
|
||||
|
||||
export type ExtensionOptions = {
|
||||
name: string;
|
@ -1,5 +1,5 @@
|
||||
import { register } from 'src/sql-tools/from-code/register';
|
||||
import { asOptions } from 'src/sql-tools/helpers';
|
||||
import { register } from 'src/sql-tools/register';
|
||||
|
||||
export type ExtensionsOptions = {
|
||||
name: string;
|
@ -0,0 +1,16 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-function-type */
|
||||
import { ForeignKeyAction } from 'src/sql-tools//decorators/foreign-key-constraint.decorator';
|
||||
import { ColumnBaseOptions } from 'src/sql-tools/decorators/column.decorator';
|
||||
import { register } from 'src/sql-tools/register';
|
||||
|
||||
export type ForeignKeyColumnOptions = ColumnBaseOptions & {
|
||||
onUpdate?: ForeignKeyAction;
|
||||
onDelete?: ForeignKeyAction;
|
||||
constraintName?: string;
|
||||
};
|
||||
|
||||
export const ForeignKeyColumn = (target: () => Function, options: ForeignKeyColumnOptions): PropertyDecorator => {
|
||||
return (object: object, propertyName: string | symbol) => {
|
||||
register({ type: 'foreignKeyColumn', item: { object, propertyName, options, target } });
|
||||
};
|
||||
};
|
@ -1,4 +1,4 @@
|
||||
import { register } from 'src/sql-tools/from-code/register';
|
||||
import { register } from 'src/sql-tools/register';
|
||||
|
||||
export type ForeignKeyAction = 'CASCADE' | 'SET NULL' | 'SET DEFAULT' | 'RESTRICT' | 'NO ACTION';
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Column, ColumnOptions, ColumnValue } from 'src/sql-tools/from-code/decorators/column.decorator';
|
||||
import { Column, ColumnOptions, ColumnValue } from 'src/sql-tools/decorators/column.decorator';
|
||||
import { ColumnType } from 'src/sql-tools/types';
|
||||
|
||||
export type GeneratedColumnStrategy = 'uuid' | 'identity';
|
@ -1,5 +1,5 @@
|
||||
import { register } from 'src/sql-tools/from-code/register';
|
||||
import { asOptions } from 'src/sql-tools/helpers';
|
||||
import { register } from 'src/sql-tools/register';
|
||||
|
||||
export type IndexOptions = {
|
||||
name?: string;
|
22
server/src/sql-tools/decorators/index.ts
Normal file
22
server/src/sql-tools/decorators/index.ts
Normal file
@ -0,0 +1,22 @@
|
||||
export * from 'src/sql-tools/decorators/after-delete.decorator';
|
||||
export * from 'src/sql-tools/decorators/after-insert.decorator';
|
||||
export * from 'src/sql-tools/decorators/before-update.decorator';
|
||||
export * from 'src/sql-tools/decorators/check.decorator';
|
||||
export * from 'src/sql-tools/decorators/column.decorator';
|
||||
export * from 'src/sql-tools/decorators/configuration-parameter.decorator';
|
||||
export * from 'src/sql-tools/decorators/create-date-column.decorator';
|
||||
export * from 'src/sql-tools/decorators/database.decorator';
|
||||
export * from 'src/sql-tools/decorators/delete-date-column.decorator';
|
||||
export * from 'src/sql-tools/decorators/extension.decorator';
|
||||
export * from 'src/sql-tools/decorators/extensions.decorator';
|
||||
export * from 'src/sql-tools/decorators/foreign-key-column.decorator';
|
||||
export * from 'src/sql-tools/decorators/foreign-key-constraint.decorator';
|
||||
export * from 'src/sql-tools/decorators/generated-column.decorator';
|
||||
export * from 'src/sql-tools/decorators/index.decorator';
|
||||
export * from 'src/sql-tools/decorators/primary-column.decorator';
|
||||
export * from 'src/sql-tools/decorators/primary-generated-column.decorator';
|
||||
export * from 'src/sql-tools/decorators/table.decorator';
|
||||
export * from 'src/sql-tools/decorators/trigger-function.decorator';
|
||||
export * from 'src/sql-tools/decorators/trigger.decorator';
|
||||
export * from 'src/sql-tools/decorators/unique.decorator';
|
||||
export * from 'src/sql-tools/decorators/update-date-column.decorator';
|
@ -1,3 +1,3 @@
|
||||
import { Column, ColumnOptions } from 'src/sql-tools/from-code/decorators/column.decorator';
|
||||
import { Column, ColumnOptions } from 'src/sql-tools/decorators/column.decorator';
|
||||
|
||||
export const PrimaryColumn = (options: Omit<ColumnOptions, 'primary'> = {}) => Column({ ...options, primary: true });
|
@ -1,4 +1,4 @@
|
||||
import { GenerateColumnOptions, GeneratedColumn } from 'src/sql-tools/from-code/decorators/generated-column.decorator';
|
||||
import { GenerateColumnOptions, GeneratedColumn } from 'src/sql-tools/decorators/generated-column.decorator';
|
||||
|
||||
export const PrimaryGeneratedColumn = (options: Omit<GenerateColumnOptions, 'primary'> = {}) =>
|
||||
GeneratedColumn({ ...options, primary: true });
|
@ -1,5 +1,5 @@
|
||||
import { register } from 'src/sql-tools/from-code/register';
|
||||
import { asOptions } from 'src/sql-tools/helpers';
|
||||
import { register } from 'src/sql-tools/register';
|
||||
|
||||
export type TableOptions = {
|
||||
name?: string;
|
@ -1,4 +1,4 @@
|
||||
import { Trigger, TriggerOptions } from 'src/sql-tools/from-code/decorators/trigger.decorator';
|
||||
import { Trigger, TriggerOptions } from 'src/sql-tools/decorators/trigger.decorator';
|
||||
import { DatabaseFunction } from 'src/sql-tools/types';
|
||||
|
||||
export type TriggerFunctionOptions = Omit<TriggerOptions, 'functionName'> & { function: DatabaseFunction };
|
@ -1,4 +1,4 @@
|
||||
import { register } from 'src/sql-tools/from-code/register';
|
||||
import { register } from 'src/sql-tools/register';
|
||||
import { TriggerAction, TriggerScope, TriggerTiming } from 'src/sql-tools/types';
|
||||
|
||||
export type TriggerOptions = {
|
@ -1,4 +1,4 @@
|
||||
import { register } from 'src/sql-tools/from-code/register';
|
||||
import { register } from 'src/sql-tools/register';
|
||||
|
||||
export type UniqueOptions = {
|
||||
name?: string;
|
@ -1,4 +1,4 @@
|
||||
import { Column, ColumnOptions } from 'src/sql-tools/from-code/decorators/column.decorator';
|
||||
import { Column, ColumnOptions } from 'src/sql-tools/decorators/column.decorator';
|
||||
|
||||
export const UpdateDateColumn = (options: ColumnOptions = {}): PropertyDecorator => {
|
||||
return Column({
|
@ -1,85 +0,0 @@
|
||||
import { compareEnums } from 'src/sql-tools/diff/comparers/enum.comparer';
|
||||
import { compareExtensions } from 'src/sql-tools/diff/comparers/extension.comparer';
|
||||
import { compareFunctions } from 'src/sql-tools/diff/comparers/function.comparer';
|
||||
import { compareParameters } from 'src/sql-tools/diff/comparers/parameter.comparer';
|
||||
import { compareTables } from 'src/sql-tools/diff/comparers/table.comparer';
|
||||
import { compare } from 'src/sql-tools/helpers';
|
||||
import { schemaDiffToSql } from 'src/sql-tools/to-sql';
|
||||
import {
|
||||
DatabaseConstraintType,
|
||||
DatabaseSchema,
|
||||
SchemaDiff,
|
||||
SchemaDiffOptions,
|
||||
SchemaDiffToSqlOptions,
|
||||
} from 'src/sql-tools/types';
|
||||
|
||||
/**
|
||||
* Compute the difference between two database schemas
|
||||
*/
|
||||
export const schemaDiff = (source: DatabaseSchema, target: DatabaseSchema, options: SchemaDiffOptions = {}) => {
|
||||
const items = [
|
||||
...compare(source.parameters, target.parameters, options.parameters, compareParameters),
|
||||
...compare(source.extensions, target.extensions, options.extension, compareExtensions),
|
||||
...compare(source.functions, target.functions, options.functions, compareFunctions),
|
||||
...compare(source.enums, target.enums, options.enums, compareEnums),
|
||||
...compare(source.tables, target.tables, options.tables, compareTables),
|
||||
];
|
||||
|
||||
type SchemaName = SchemaDiff['type'];
|
||||
const itemMap: Record<SchemaName, SchemaDiff[]> = {
|
||||
'enum.create': [],
|
||||
'enum.drop': [],
|
||||
'extension.create': [],
|
||||
'extension.drop': [],
|
||||
'function.create': [],
|
||||
'function.drop': [],
|
||||
'table.create': [],
|
||||
'table.drop': [],
|
||||
'column.add': [],
|
||||
'column.alter': [],
|
||||
'column.drop': [],
|
||||
'constraint.add': [],
|
||||
'constraint.drop': [],
|
||||
'index.create': [],
|
||||
'index.drop': [],
|
||||
'trigger.create': [],
|
||||
'trigger.drop': [],
|
||||
'parameter.set': [],
|
||||
'parameter.reset': [],
|
||||
};
|
||||
|
||||
for (const item of items) {
|
||||
itemMap[item.type].push(item);
|
||||
}
|
||||
|
||||
const constraintAdds = itemMap['constraint.add'].filter((item) => item.type === 'constraint.add');
|
||||
|
||||
const orderedItems = [
|
||||
...itemMap['extension.create'],
|
||||
...itemMap['function.create'],
|
||||
...itemMap['parameter.set'],
|
||||
...itemMap['parameter.reset'],
|
||||
...itemMap['enum.create'],
|
||||
...itemMap['trigger.drop'],
|
||||
...itemMap['index.drop'],
|
||||
...itemMap['constraint.drop'],
|
||||
...itemMap['table.create'],
|
||||
...itemMap['column.alter'],
|
||||
...itemMap['column.add'],
|
||||
...constraintAdds.filter(({ constraint }) => constraint.type === DatabaseConstraintType.PRIMARY_KEY),
|
||||
...constraintAdds.filter(({ constraint }) => constraint.type === DatabaseConstraintType.FOREIGN_KEY),
|
||||
...constraintAdds.filter(({ constraint }) => constraint.type === DatabaseConstraintType.UNIQUE),
|
||||
...constraintAdds.filter(({ constraint }) => constraint.type === DatabaseConstraintType.CHECK),
|
||||
...itemMap['index.create'],
|
||||
...itemMap['trigger.create'],
|
||||
...itemMap['column.drop'],
|
||||
...itemMap['table.drop'],
|
||||
...itemMap['enum.drop'],
|
||||
...itemMap['function.drop'],
|
||||
];
|
||||
|
||||
return {
|
||||
items: orderedItems,
|
||||
asSql: (options?: SchemaDiffToSqlOptions) => schemaDiffToSql(orderedItems, options),
|
||||
};
|
||||
};
|
@ -1,15 +0,0 @@
|
||||
import { ColumnBaseOptions } from 'src/sql-tools/from-code/decorators/column.decorator';
|
||||
import { ForeignKeyAction } from 'src/sql-tools/from-code/decorators/foreign-key-constraint.decorator';
|
||||
import { register } from 'src/sql-tools/from-code/register';
|
||||
|
||||
export type ForeignKeyColumnOptions = ColumnBaseOptions & {
|
||||
onUpdate?: ForeignKeyAction;
|
||||
onDelete?: ForeignKeyAction;
|
||||
constraintName?: string;
|
||||
};
|
||||
|
||||
export const ForeignKeyColumn = (target: () => object, options: ForeignKeyColumnOptions): PropertyDecorator => {
|
||||
return (object: object, propertyName: string | symbol) => {
|
||||
register({ type: 'foreignKeyColumn', item: { object, propertyName, options, target } });
|
||||
};
|
||||
};
|
@ -1,36 +0,0 @@
|
||||
import { readdirSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { reset, schemaFromCode } from 'src/sql-tools/from-code';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe(schemaFromCode.name, () => {
|
||||
beforeEach(() => {
|
||||
reset();
|
||||
});
|
||||
|
||||
it('should work', () => {
|
||||
expect(schemaFromCode()).toEqual({
|
||||
name: 'postgres',
|
||||
schemaName: 'public',
|
||||
functions: [],
|
||||
enums: [],
|
||||
extensions: [],
|
||||
parameters: [],
|
||||
tables: [],
|
||||
warnings: [],
|
||||
});
|
||||
});
|
||||
|
||||
describe('test files', () => {
|
||||
const files = readdirSync('test/sql-tools', { withFileTypes: true });
|
||||
for (const file of files) {
|
||||
const filePath = join(file.parentPath, file.name);
|
||||
it(filePath, async () => {
|
||||
const module = await import(filePath);
|
||||
expect(module.description).toBeDefined();
|
||||
expect(module.schema).toBeDefined();
|
||||
expect(schemaFromCode(), module.description).toEqual(module.schema);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
@ -1,77 +0,0 @@
|
||||
import 'reflect-metadata';
|
||||
import { processCheckConstraints } from 'src/sql-tools/from-code/processors/check-constraint.processor';
|
||||
import { processColumns } from 'src/sql-tools/from-code/processors/column.processor';
|
||||
import { processConfigurationParameters } from 'src/sql-tools/from-code/processors/configuration-parameter.processor';
|
||||
import { processDatabases } from 'src/sql-tools/from-code/processors/database.processor';
|
||||
import { processEnums } from 'src/sql-tools/from-code/processors/enum.processor';
|
||||
import { processExtensions } from 'src/sql-tools/from-code/processors/extension.processor';
|
||||
import { processForeignKeyColumns } from 'src/sql-tools/from-code/processors/foreign-key-column.processor';
|
||||
import { processForeignKeyConstraints } from 'src/sql-tools/from-code/processors/foreign-key-constraint.processor';
|
||||
import { processFunctions } from 'src/sql-tools/from-code/processors/function.processor';
|
||||
import { processIndexes } from 'src/sql-tools/from-code/processors/index.processor';
|
||||
import { processPrimaryKeyConstraints } from 'src/sql-tools/from-code/processors/primary-key-contraint.processor';
|
||||
import { processTables } from 'src/sql-tools/from-code/processors/table.processor';
|
||||
import { processTriggers } from 'src/sql-tools/from-code/processors/trigger.processor';
|
||||
import { Processor, SchemaBuilder } from 'src/sql-tools/from-code/processors/type';
|
||||
import { processUniqueConstraints } from 'src/sql-tools/from-code/processors/unique-constraint.processor';
|
||||
import { getRegisteredItems, resetRegisteredItems } from 'src/sql-tools/from-code/register';
|
||||
import { DatabaseSchema } from 'src/sql-tools/types';
|
||||
|
||||
let initialized = false;
|
||||
let schema: DatabaseSchema;
|
||||
|
||||
export const reset = () => {
|
||||
initialized = false;
|
||||
resetRegisteredItems();
|
||||
};
|
||||
|
||||
const processors: Processor[] = [
|
||||
processDatabases,
|
||||
processConfigurationParameters,
|
||||
processEnums,
|
||||
processExtensions,
|
||||
processFunctions,
|
||||
processTables,
|
||||
processColumns,
|
||||
processForeignKeyColumns,
|
||||
processForeignKeyConstraints,
|
||||
processUniqueConstraints,
|
||||
processCheckConstraints,
|
||||
processPrimaryKeyConstraints,
|
||||
processIndexes,
|
||||
processTriggers,
|
||||
];
|
||||
|
||||
export type SchemaFromCodeOptions = {
|
||||
/** automatically create indexes on foreign key columns */
|
||||
createForeignKeyIndexes?: boolean;
|
||||
};
|
||||
export const schemaFromCode = (options: SchemaFromCodeOptions = {}) => {
|
||||
if (!initialized) {
|
||||
const globalOptions = {
|
||||
createForeignKeyIndexes: options.createForeignKeyIndexes ?? true,
|
||||
};
|
||||
|
||||
const builder: SchemaBuilder = {
|
||||
name: 'postgres',
|
||||
schemaName: 'public',
|
||||
tables: [],
|
||||
functions: [],
|
||||
enums: [],
|
||||
extensions: [],
|
||||
parameters: [],
|
||||
warnings: [],
|
||||
};
|
||||
|
||||
const items = getRegisteredItems();
|
||||
|
||||
for (const processor of processors) {
|
||||
processor(builder, items, globalOptions);
|
||||
}
|
||||
|
||||
schema = { ...builder, tables: builder.tables.map(({ metadata: _, ...table }) => table) };
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
return schema;
|
||||
};
|
@ -1,93 +0,0 @@
|
||||
import { ColumnOptions } from 'src/sql-tools/from-code/decorators/column.decorator';
|
||||
import { onMissingTable, resolveTable } from 'src/sql-tools/from-code/processors/table.processor';
|
||||
import { Processor, SchemaBuilder } from 'src/sql-tools/from-code/processors/type';
|
||||
import { addWarning, asMetadataKey, fromColumnValue } from 'src/sql-tools/helpers';
|
||||
import { DatabaseColumn } from 'src/sql-tools/types';
|
||||
|
||||
export const processColumns: Processor = (builder, items) => {
|
||||
for (const {
|
||||
type,
|
||||
item: { object, propertyName, options },
|
||||
} of items.filter((item) => item.type === 'column' || item.type === 'foreignKeyColumn')) {
|
||||
const table = resolveTable(builder, object.constructor);
|
||||
if (!table) {
|
||||
onMissingTable(builder, type === 'column' ? '@Column' : '@ForeignKeyColumn', object, propertyName);
|
||||
continue;
|
||||
}
|
||||
|
||||
const columnName = options.name ?? String(propertyName);
|
||||
const existingColumn = table.columns.find((column) => column.name === columnName);
|
||||
if (existingColumn) {
|
||||
// TODO log warnings if column name is not unique
|
||||
continue;
|
||||
}
|
||||
|
||||
const tableName = table.name;
|
||||
|
||||
let defaultValue = fromColumnValue(options.default);
|
||||
let nullable = options.nullable ?? false;
|
||||
|
||||
// map `{ default: null }` to `{ nullable: true }`
|
||||
if (defaultValue === null) {
|
||||
nullable = true;
|
||||
defaultValue = undefined;
|
||||
}
|
||||
|
||||
const isEnum = !!(options as ColumnOptions).enum;
|
||||
|
||||
const column: DatabaseColumn = {
|
||||
name: columnName,
|
||||
tableName,
|
||||
primary: options.primary ?? false,
|
||||
default: defaultValue,
|
||||
nullable,
|
||||
isArray: (options as ColumnOptions).array ?? false,
|
||||
length: options.length,
|
||||
type: isEnum ? 'enum' : options.type || 'character varying',
|
||||
enumName: isEnum ? (options as ColumnOptions).enum!.name : undefined,
|
||||
comment: options.comment,
|
||||
storage: options.storage,
|
||||
identity: options.identity,
|
||||
synchronize: options.synchronize ?? true,
|
||||
};
|
||||
|
||||
writeMetadata(object, propertyName, { name: column.name, options });
|
||||
|
||||
table.columns.push(column);
|
||||
}
|
||||
};
|
||||
|
||||
type ColumnMetadata = { name: string; options: ColumnOptions };
|
||||
|
||||
export const resolveColumn = (builder: SchemaBuilder, object: object, propertyName: string | symbol) => {
|
||||
const table = resolveTable(builder, object.constructor);
|
||||
if (!table) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const metadata = readMetadata(object, propertyName);
|
||||
if (!metadata) {
|
||||
return { table };
|
||||
}
|
||||
|
||||
const column = table.columns.find((column) => column.name === metadata.name);
|
||||
return { table, column };
|
||||
};
|
||||
|
||||
export const onMissingColumn = (
|
||||
builder: SchemaBuilder,
|
||||
context: string,
|
||||
object: object,
|
||||
propertyName?: symbol | string,
|
||||
) => {
|
||||
const label = object.constructor.name + (propertyName ? '.' + String(propertyName) : '');
|
||||
addWarning(builder, context, `Unable to find column (${label})`);
|
||||
};
|
||||
|
||||
const METADATA_KEY = asMetadataKey('table-metadata');
|
||||
|
||||
const writeMetadata = (object: object, propertyName: symbol | string, metadata: ColumnMetadata) =>
|
||||
Reflect.defineMetadata(METADATA_KEY, metadata, object, propertyName);
|
||||
|
||||
const readMetadata = (object: object, propertyName: symbol | string): ColumnMetadata | undefined =>
|
||||
Reflect.getMetadata(METADATA_KEY, object, propertyName);
|
@ -1,58 +0,0 @@
|
||||
import { TableOptions } from 'src/sql-tools/from-code/decorators/table.decorator';
|
||||
import { Processor, SchemaBuilder } from 'src/sql-tools/from-code/processors/type';
|
||||
import { addWarning, asMetadataKey, asSnakeCase } from 'src/sql-tools/helpers';
|
||||
|
||||
export const processTables: Processor = (builder, items) => {
|
||||
for (const {
|
||||
item: { options, object },
|
||||
} of items.filter((item) => item.type === 'table')) {
|
||||
const test = readMetadata(object);
|
||||
if (test) {
|
||||
throw new Error(
|
||||
`Table ${test.name} has already been registered. Does ${object.name} have two @Table() decorators?`,
|
||||
);
|
||||
}
|
||||
|
||||
const tableName = options.name || asSnakeCase(object.name);
|
||||
|
||||
writeMetadata(object, { name: tableName, options });
|
||||
|
||||
builder.tables.push({
|
||||
name: tableName,
|
||||
columns: [],
|
||||
constraints: [],
|
||||
indexes: [],
|
||||
triggers: [],
|
||||
synchronize: options.synchronize ?? true,
|
||||
metadata: { options, object },
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const resolveTable = (builder: SchemaBuilder, object: object) => {
|
||||
const metadata = readMetadata(object);
|
||||
if (!metadata) {
|
||||
return;
|
||||
}
|
||||
|
||||
return builder.tables.find((table) => table.name === metadata.name);
|
||||
};
|
||||
|
||||
export const onMissingTable = (
|
||||
builder: SchemaBuilder,
|
||||
context: string,
|
||||
object: object,
|
||||
propertyName?: symbol | string,
|
||||
) => {
|
||||
const label = object.constructor.name + (propertyName ? '.' + String(propertyName) : '');
|
||||
addWarning(builder, context, `Unable to find table (${label})`);
|
||||
};
|
||||
|
||||
const METADATA_KEY = asMetadataKey('table-metadata');
|
||||
|
||||
type TableMetadata = { name: string; options: TableOptions };
|
||||
|
||||
const readMetadata = (object: object): TableMetadata | undefined => Reflect.getMetadata(METADATA_KEY, object);
|
||||
|
||||
const writeMetadata = (object: object, metadata: TableMetadata): void =>
|
||||
Reflect.defineMetadata(METADATA_KEY, metadata, object);
|
@ -1,10 +0,0 @@
|
||||
import { SchemaFromCodeOptions } from 'src/sql-tools/from-code';
|
||||
import { TableOptions } from 'src/sql-tools/from-code/decorators/table.decorator';
|
||||
import { RegisterItem } from 'src/sql-tools/from-code/register-item';
|
||||
import { DatabaseSchema, DatabaseTable } from 'src/sql-tools/types';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
||||
export type TableWithMetadata = DatabaseTable & { metadata: { options: TableOptions; object: Function } };
|
||||
export type SchemaBuilder = Omit<DatabaseSchema, 'tables'> & { tables: TableWithMetadata[] };
|
||||
|
||||
export type Processor = (builder: SchemaBuilder, items: RegisterItem[], options: SchemaFromCodeOptions) => void;
|
@ -1,54 +0,0 @@
|
||||
import { Kysely } from 'kysely';
|
||||
import { PostgresJSDialect } from 'kysely-postgres-js';
|
||||
import { Sql } from 'postgres';
|
||||
import { readColumns } from 'src/sql-tools/from-database/readers/column.reader';
|
||||
import { readComments } from 'src/sql-tools/from-database/readers/comment.reader';
|
||||
import { readConstraints } from 'src/sql-tools/from-database/readers/constraint.reader';
|
||||
import { readExtensions } from 'src/sql-tools/from-database/readers/extension.reader';
|
||||
import { readFunctions } from 'src/sql-tools/from-database/readers/function.reader';
|
||||
import { readIndexes } from 'src/sql-tools/from-database/readers/index.reader';
|
||||
import { readName } from 'src/sql-tools/from-database/readers/name.reader';
|
||||
import { readParameters } from 'src/sql-tools/from-database/readers/parameter.reader';
|
||||
import { readTables } from 'src/sql-tools/from-database/readers/table.reader';
|
||||
import { readTriggers } from 'src/sql-tools/from-database/readers/trigger.reader';
|
||||
import { DatabaseReader } from 'src/sql-tools/from-database/readers/type';
|
||||
import { DatabaseSchema, LoadSchemaOptions, PostgresDB } from 'src/sql-tools/types';
|
||||
|
||||
const readers: DatabaseReader[] = [
|
||||
//
|
||||
readName,
|
||||
readParameters,
|
||||
readExtensions,
|
||||
readFunctions,
|
||||
readTables,
|
||||
readColumns,
|
||||
readIndexes,
|
||||
readConstraints,
|
||||
readTriggers,
|
||||
readComments,
|
||||
];
|
||||
|
||||
/**
|
||||
* Load the database schema from the database
|
||||
*/
|
||||
export const schemaFromDatabase = async (postgres: Sql, options: LoadSchemaOptions = {}): Promise<DatabaseSchema> => {
|
||||
const schema: DatabaseSchema = {
|
||||
name: 'immich',
|
||||
schemaName: options.schemaName || 'public',
|
||||
parameters: [],
|
||||
functions: [],
|
||||
enums: [],
|
||||
extensions: [],
|
||||
tables: [],
|
||||
warnings: [],
|
||||
};
|
||||
|
||||
const db = new Kysely<PostgresDB>({ dialect: new PostgresJSDialect({ postgres }) });
|
||||
for (const reader of readers) {
|
||||
await reader(schema, db);
|
||||
}
|
||||
|
||||
await db.destroy();
|
||||
|
||||
return schema;
|
||||
};
|
@ -1,3 +0,0 @@
|
||||
import { DatabaseClient, DatabaseSchema } from 'src/sql-tools/types';
|
||||
|
||||
export type DatabaseReader = (schema: DatabaseSchema, db: DatabaseClient) => Promise<void>;
|
@ -1,15 +1,6 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { ColumnValue } from 'src/sql-tools/from-code/decorators/column.decorator';
|
||||
import { SchemaBuilder } from 'src/sql-tools/from-code/processors/type';
|
||||
import {
|
||||
Comparer,
|
||||
DatabaseColumn,
|
||||
DiffOptions,
|
||||
SchemaDiff,
|
||||
TriggerAction,
|
||||
TriggerScope,
|
||||
TriggerTiming,
|
||||
} from 'src/sql-tools/types';
|
||||
import { ColumnValue } from 'src/sql-tools/decorators/column.decorator';
|
||||
import { Comparer, DatabaseColumn, IgnoreOptions, SchemaDiff } from 'src/sql-tools/types';
|
||||
|
||||
export const asMetadataKey = (name: string) => `sql-tools:${name}`;
|
||||
|
||||
@ -27,46 +18,6 @@ export const asOptions = <T extends { name?: string }>(options: string | T): T =
|
||||
};
|
||||
|
||||
export const sha1 = (value: string) => createHash('sha1').update(value).digest('hex');
|
||||
export const hasMask = (input: number, mask: number) => (input & mask) === mask;
|
||||
|
||||
export const parseTriggerType = (type: number) => {
|
||||
// eslint-disable-next-line unicorn/prefer-math-trunc
|
||||
const scope: TriggerScope = hasMask(type, 1 << 0) ? 'row' : 'statement';
|
||||
|
||||
let timing: TriggerTiming = 'after';
|
||||
const timingMasks: Array<{ mask: number; value: TriggerTiming }> = [
|
||||
{ mask: 1 << 1, value: 'before' },
|
||||
{ mask: 1 << 6, value: 'instead of' },
|
||||
];
|
||||
|
||||
for (const { mask, value } of timingMasks) {
|
||||
if (hasMask(type, mask)) {
|
||||
timing = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const actions: TriggerAction[] = [];
|
||||
const actionMasks: Array<{ mask: number; value: TriggerAction }> = [
|
||||
{ mask: 1 << 2, value: 'insert' },
|
||||
{ mask: 1 << 3, value: 'delete' },
|
||||
{ mask: 1 << 4, value: 'update' },
|
||||
{ mask: 1 << 5, value: 'truncate' },
|
||||
];
|
||||
|
||||
for (const { mask, value } of actionMasks) {
|
||||
if (hasMask(type, mask)) {
|
||||
actions.push(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (actions.length === 0) {
|
||||
throw new Error(`Unable to parse trigger type ${type}`);
|
||||
}
|
||||
|
||||
return { actions, timing, scope };
|
||||
};
|
||||
|
||||
export const fromColumnValue = (columnValue?: ColumnValue) => {
|
||||
if (columnValue === undefined) {
|
||||
@ -108,7 +59,7 @@ export const haveEqualColumns = (sourceColumns?: string[], targetColumns?: strin
|
||||
export const compare = <T extends { name: string; synchronize: boolean }>(
|
||||
sources: T[],
|
||||
targets: T[],
|
||||
options: DiffOptions | undefined,
|
||||
options: IgnoreOptions | undefined,
|
||||
comparer: Comparer<T>,
|
||||
) => {
|
||||
options = options || {};
|
||||
@ -144,7 +95,7 @@ export const compare = <T extends { name: string; synchronize: boolean }>(
|
||||
const isIgnored = (
|
||||
source: { synchronize?: boolean } | undefined,
|
||||
target: { synchronize?: boolean } | undefined,
|
||||
options: DiffOptions,
|
||||
options: IgnoreOptions,
|
||||
) => {
|
||||
return (options.ignoreExtra && !source) || (options.ignoreMissing && !target);
|
||||
};
|
||||
@ -214,20 +165,3 @@ export const asColumnComment = (tableName: string, columnName: string, comment:
|
||||
export const asColumnList = (columns: string[]) => columns.map((column) => `"${column}"`).join(', ');
|
||||
|
||||
export const asForeignKeyConstraintName = (table: string, columns: string[]) => asKey('FK_', table, [...columns]);
|
||||
|
||||
export const asIndexName = (table: string, columns?: string[], where?: string) => {
|
||||
const items: string[] = [];
|
||||
for (const columnName of columns ?? []) {
|
||||
items.push(columnName);
|
||||
}
|
||||
|
||||
if (where) {
|
||||
items.push(where);
|
||||
}
|
||||
|
||||
return asKey('IDX_', table, items);
|
||||
};
|
||||
|
||||
export const addWarning = (builder: SchemaBuilder, context: string, message: string) => {
|
||||
builder.warnings.push(`[${context}] ${message}`);
|
||||
};
|
||||
|
@ -1,22 +1,20 @@
|
||||
import { onMissingTable, resolveTable } from 'src/sql-tools/from-code/processors/table.processor';
|
||||
import { Processor } from 'src/sql-tools/from-code/processors/type';
|
||||
import { asKey } from 'src/sql-tools/helpers';
|
||||
import { DatabaseConstraintType } from 'src/sql-tools/types';
|
||||
import { ConstraintType, Processor } from 'src/sql-tools/types';
|
||||
|
||||
export const processCheckConstraints: Processor = (builder, items) => {
|
||||
for (const {
|
||||
item: { object, options },
|
||||
} of items.filter((item) => item.type === 'checkConstraint')) {
|
||||
const table = resolveTable(builder, object);
|
||||
const table = builder.getTableByObject(object);
|
||||
if (!table) {
|
||||
onMissingTable(builder, '@Check', object);
|
||||
builder.warnMissingTable('@Check', object);
|
||||
continue;
|
||||
}
|
||||
|
||||
const tableName = table.name;
|
||||
|
||||
table.constraints.push({
|
||||
type: DatabaseConstraintType.CHECK,
|
||||
type: ConstraintType.CHECK,
|
||||
name: options.name || asCheckConstraintName(tableName, options.expression),
|
||||
tableName,
|
||||
expression: options.expression,
|
55
server/src/sql-tools/processors/column.processor.ts
Normal file
55
server/src/sql-tools/processors/column.processor.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import { ColumnOptions } from 'src/sql-tools/decorators/column.decorator';
|
||||
import { fromColumnValue } from 'src/sql-tools/helpers';
|
||||
import { Processor } from 'src/sql-tools/types';
|
||||
|
||||
export const processColumns: Processor = (builder, items) => {
|
||||
for (const {
|
||||
type,
|
||||
item: { object, propertyName, options },
|
||||
} of items.filter((item) => item.type === 'column' || item.type === 'foreignKeyColumn')) {
|
||||
const table = builder.getTableByObject(object.constructor);
|
||||
if (!table) {
|
||||
builder.warnMissingTable(type === 'column' ? '@Column' : '@ForeignKeyColumn', object, propertyName);
|
||||
continue;
|
||||
}
|
||||
|
||||
const columnName = options.name ?? String(propertyName);
|
||||
const existingColumn = table.columns.find((column) => column.name === columnName);
|
||||
if (existingColumn) {
|
||||
// TODO log warnings if column name is not unique
|
||||
continue;
|
||||
}
|
||||
|
||||
let defaultValue = fromColumnValue(options.default);
|
||||
let nullable = options.nullable ?? false;
|
||||
|
||||
// map `{ default: null }` to `{ nullable: true }`
|
||||
if (defaultValue === null) {
|
||||
nullable = true;
|
||||
defaultValue = undefined;
|
||||
}
|
||||
|
||||
const isEnum = !!(options as ColumnOptions).enum;
|
||||
|
||||
builder.addColumn(
|
||||
table,
|
||||
{
|
||||
name: columnName,
|
||||
tableName: table.name,
|
||||
primary: options.primary ?? false,
|
||||
default: defaultValue,
|
||||
nullable,
|
||||
isArray: (options as ColumnOptions).array ?? false,
|
||||
length: options.length,
|
||||
type: isEnum ? 'enum' : options.type || 'character varying',
|
||||
enumName: isEnum ? (options as ColumnOptions).enum!.name : undefined,
|
||||
comment: options.comment,
|
||||
storage: options.storage,
|
||||
identity: options.identity,
|
||||
synchronize: options.synchronize ?? true,
|
||||
},
|
||||
options,
|
||||
propertyName,
|
||||
);
|
||||
}
|
||||
};
|
@ -1,12 +1,12 @@
|
||||
import { Processor } from 'src/sql-tools/from-code/processors/type';
|
||||
import { fromColumnValue } from 'src/sql-tools/helpers';
|
||||
import { Processor } from 'src/sql-tools/types';
|
||||
|
||||
export const processConfigurationParameters: Processor = (builder, items) => {
|
||||
for (const {
|
||||
item: { options },
|
||||
} of items.filter((item) => item.type === 'configurationParameter')) {
|
||||
builder.parameters.push({
|
||||
databaseName: builder.name,
|
||||
databaseName: builder.databaseName,
|
||||
name: options.name,
|
||||
value: fromColumnValue(options.value),
|
||||
scope: options.scope,
|
@ -1,10 +1,10 @@
|
||||
import { Processor } from 'src/sql-tools/from-code/processors/type';
|
||||
import { asSnakeCase } from 'src/sql-tools/helpers';
|
||||
import { Processor } from 'src/sql-tools/types';
|
||||
|
||||
export const processDatabases: Processor = (builder, items) => {
|
||||
for (const {
|
||||
item: { object, options },
|
||||
} of items.filter((item) => item.type === 'database')) {
|
||||
builder.name = options.name || asSnakeCase(object.name);
|
||||
builder.databaseName = options.name || asSnakeCase(object.name);
|
||||
}
|
||||
};
|
@ -1,4 +1,4 @@
|
||||
import { Processor } from 'src/sql-tools/from-code/processors/type';
|
||||
import { Processor } from 'src/sql-tools/types';
|
||||
|
||||
export const processEnums: Processor = (builder, items) => {
|
||||
for (const { item } of items.filter((item) => item.type === 'enum')) {
|
@ -1,4 +1,4 @@
|
||||
import { Processor } from 'src/sql-tools/from-code/processors/type';
|
||||
import { Processor } from 'src/sql-tools/types';
|
||||
|
||||
export const processExtensions: Processor = (builder, items) => {
|
||||
for (const {
|
@ -1,28 +1,25 @@
|
||||
import { onMissingColumn, resolveColumn } from 'src/sql-tools/from-code/processors/column.processor';
|
||||
import { onMissingTable, resolveTable } from 'src/sql-tools/from-code/processors/table.processor';
|
||||
import { Processor } from 'src/sql-tools/from-code/processors/type';
|
||||
import { asForeignKeyConstraintName, asKey } from 'src/sql-tools/helpers';
|
||||
import { DatabaseActionType, DatabaseConstraintType } from 'src/sql-tools/types';
|
||||
import { ActionType, ConstraintType, Processor } from 'src/sql-tools/types';
|
||||
|
||||
export const processForeignKeyColumns: Processor = (builder, items) => {
|
||||
for (const {
|
||||
item: { object, propertyName, options, target },
|
||||
} of items.filter((item) => item.type === 'foreignKeyColumn')) {
|
||||
const { table, column } = resolveColumn(builder, object, propertyName);
|
||||
const { table, column } = builder.getColumnByObjectAndPropertyName(object, propertyName);
|
||||
if (!table) {
|
||||
onMissingTable(builder, '@ForeignKeyColumn', object);
|
||||
builder.warnMissingTable('@ForeignKeyColumn', object);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!column) {
|
||||
// should be impossible since they are pre-created in `column.processor.ts`
|
||||
onMissingColumn(builder, '@ForeignKeyColumn', object, propertyName);
|
||||
builder.warnMissingColumn('@ForeignKeyColumn', object, propertyName);
|
||||
continue;
|
||||
}
|
||||
|
||||
const referenceTable = resolveTable(builder, target());
|
||||
const referenceTable = builder.getTableByObject(target());
|
||||
if (!referenceTable) {
|
||||
onMissingTable(builder, '@ForeignKeyColumn', object, propertyName);
|
||||
builder.warnMissingTable('@ForeignKeyColumn', object, propertyName);
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -41,11 +38,11 @@ export const processForeignKeyColumns: Processor = (builder, items) => {
|
||||
name,
|
||||
tableName: table.name,
|
||||
columnNames,
|
||||
type: DatabaseConstraintType.FOREIGN_KEY,
|
||||
type: ConstraintType.FOREIGN_KEY,
|
||||
referenceTableName: referenceTable.name,
|
||||
referenceColumnNames,
|
||||
onUpdate: options.onUpdate as DatabaseActionType,
|
||||
onDelete: options.onDelete as DatabaseActionType,
|
||||
onUpdate: options.onUpdate as ActionType,
|
||||
onDelete: options.onDelete as ActionType,
|
||||
synchronize: options.synchronize ?? true,
|
||||
});
|
||||
|
||||
@ -54,7 +51,7 @@ export const processForeignKeyColumns: Processor = (builder, items) => {
|
||||
name: options.uniqueConstraintName || asRelationKeyConstraintName(table.name, columnNames),
|
||||
tableName: table.name,
|
||||
columnNames,
|
||||
type: DatabaseConstraintType.UNIQUE,
|
||||
type: ConstraintType.UNIQUE,
|
||||
synchronize: options.synchronize ?? true,
|
||||
});
|
||||
}
|
@ -1,23 +1,20 @@
|
||||
import { onMissingTable, resolveTable } from 'src/sql-tools/from-code/processors/table.processor';
|
||||
import { Processor } from 'src/sql-tools/from-code/processors/type';
|
||||
import { addWarning, asForeignKeyConstraintName, asIndexName } from 'src/sql-tools/helpers';
|
||||
import { DatabaseActionType, DatabaseConstraintType } from 'src/sql-tools/types';
|
||||
import { asForeignKeyConstraintName } from 'src/sql-tools/helpers';
|
||||
import { ActionType, ConstraintType, Processor } from 'src/sql-tools/types';
|
||||
|
||||
export const processForeignKeyConstraints: Processor = (builder, items, config) => {
|
||||
for (const {
|
||||
item: { object, options },
|
||||
} of items.filter((item) => item.type === 'foreignKeyConstraint')) {
|
||||
const table = resolveTable(builder, object);
|
||||
const table = builder.getTableByObject(object);
|
||||
if (!table) {
|
||||
onMissingTable(builder, '@ForeignKeyConstraint', { name: 'referenceTable' });
|
||||
builder.warnMissingTable('@ForeignKeyConstraint', { name: 'referenceTable' });
|
||||
continue;
|
||||
}
|
||||
|
||||
const referenceTable = resolveTable(builder, options.referenceTable());
|
||||
const referenceTable = builder.getTableByObject(options.referenceTable());
|
||||
if (!referenceTable) {
|
||||
const referenceTableName = options.referenceTable()?.name;
|
||||
addWarning(
|
||||
builder,
|
||||
builder.warn(
|
||||
'@ForeignKeyConstraint.referenceTable',
|
||||
`Unable to find table` + (referenceTableName ? ` (${referenceTableName})` : ''),
|
||||
);
|
||||
@ -28,21 +25,18 @@ export const processForeignKeyConstraints: Processor = (builder, items, config)
|
||||
|
||||
for (const columnName of options.columns) {
|
||||
if (!table.columns.some(({ name }) => name === columnName)) {
|
||||
addWarning(
|
||||
builder,
|
||||
'@ForeignKeyConstraint.columns',
|
||||
`Unable to find column (${table.metadata.object.name}.${columnName})`,
|
||||
);
|
||||
const metadata = builder.getTableMetadata(table);
|
||||
builder.warn('@ForeignKeyConstraint.columns', `Unable to find column (${metadata.object.name}.${columnName})`);
|
||||
missingColumn = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (const columnName of options.referenceColumns || []) {
|
||||
if (!referenceTable.columns.some(({ name }) => name === columnName)) {
|
||||
addWarning(
|
||||
builder,
|
||||
const metadata = builder.getTableMetadata(referenceTable);
|
||||
builder.warn(
|
||||
'@ForeignKeyConstraint.referenceColumns',
|
||||
`Unable to find column (${referenceTable.metadata.object.name}.${columnName})`,
|
||||
`Unable to find column (${metadata.object.name}.${columnName})`,
|
||||
);
|
||||
missingColumn = true;
|
||||
}
|
||||
@ -58,14 +52,14 @@ export const processForeignKeyConstraints: Processor = (builder, items, config)
|
||||
const name = options.name || asForeignKeyConstraintName(table.name, options.columns);
|
||||
|
||||
table.constraints.push({
|
||||
type: DatabaseConstraintType.FOREIGN_KEY,
|
||||
type: ConstraintType.FOREIGN_KEY,
|
||||
name,
|
||||
tableName: table.name,
|
||||
columnNames: options.columns,
|
||||
referenceTableName: referenceTable.name,
|
||||
referenceColumnNames: referenceColumns,
|
||||
onUpdate: options.onUpdate as DatabaseActionType,
|
||||
onDelete: options.onDelete as DatabaseActionType,
|
||||
onUpdate: options.onUpdate as ActionType,
|
||||
onDelete: options.onDelete as ActionType,
|
||||
synchronize: options.synchronize ?? true,
|
||||
});
|
||||
|
||||
@ -75,7 +69,7 @@ export const processForeignKeyConstraints: Processor = (builder, items, config)
|
||||
|
||||
if (options.index || options.indexName || config.createForeignKeyIndexes) {
|
||||
table.indexes.push({
|
||||
name: options.indexName || asIndexName(table.name, options.columns),
|
||||
name: options.indexName || builder.asIndexName(table.name, options.columns),
|
||||
tableName: table.name,
|
||||
columnNames: options.columns,
|
||||
unique: false,
|
@ -1,4 +1,4 @@
|
||||
import { Processor } from 'src/sql-tools/from-code/processors/type';
|
||||
import { Processor } from 'src/sql-tools/types';
|
||||
|
||||
export const processFunctions: Processor = (builder, items) => {
|
||||
for (const { item } of items.filter((item) => item.type === 'function')) {
|
@ -1,20 +1,17 @@
|
||||
import { onMissingColumn, resolveColumn } from 'src/sql-tools/from-code/processors/column.processor';
|
||||
import { onMissingTable, resolveTable } from 'src/sql-tools/from-code/processors/table.processor';
|
||||
import { Processor } from 'src/sql-tools/from-code/processors/type';
|
||||
import { asIndexName } from 'src/sql-tools/helpers';
|
||||
import { Processor } from 'src/sql-tools/types';
|
||||
|
||||
export const processIndexes: Processor = (builder, items, config) => {
|
||||
for (const {
|
||||
item: { object, options },
|
||||
} of items.filter((item) => item.type === 'index')) {
|
||||
const table = resolveTable(builder, object);
|
||||
const table = builder.getTableByObject(object);
|
||||
if (!table) {
|
||||
onMissingTable(builder, '@Check', object);
|
||||
builder.warnMissingTable('@Check', object);
|
||||
continue;
|
||||
}
|
||||
|
||||
table.indexes.push({
|
||||
name: options.name || asIndexName(table.name, options.columns, options.where),
|
||||
name: options.name || builder.asIndexName(table.name, options.columns, options.where),
|
||||
tableName: table.name,
|
||||
unique: options.unique ?? false,
|
||||
expression: options.expression,
|
||||
@ -31,15 +28,15 @@ export const processIndexes: Processor = (builder, items, config) => {
|
||||
type,
|
||||
item: { object, propertyName, options },
|
||||
} of items.filter((item) => item.type === 'column' || item.type === 'foreignKeyColumn')) {
|
||||
const { table, column } = resolveColumn(builder, object, propertyName);
|
||||
const { table, column } = builder.getColumnByObjectAndPropertyName(object, propertyName);
|
||||
if (!table) {
|
||||
onMissingTable(builder, '@Column', object);
|
||||
builder.warnMissingTable('@Column', object);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!column) {
|
||||
// should be impossible since they are created in `column.processor.ts`
|
||||
onMissingColumn(builder, '@Column', object, propertyName);
|
||||
builder.warnMissingColumn('@Column', object, propertyName);
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -53,7 +50,7 @@ export const processIndexes: Processor = (builder, items, config) => {
|
||||
continue;
|
||||
}
|
||||
|
||||
const indexName = options.indexName || asIndexName(table.name, [column.name]);
|
||||
const indexName = options.indexName || builder.asIndexName(table.name, [column.name]);
|
||||
|
||||
const isIndexPresent = table.indexes.some((index) => index.name === indexName);
|
||||
if (isIndexPresent) {
|
32
server/src/sql-tools/processors/index.ts
Normal file
32
server/src/sql-tools/processors/index.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { processCheckConstraints } from 'src/sql-tools/processors/check-constraint.processor';
|
||||
import { processColumns } from 'src/sql-tools/processors/column.processor';
|
||||
import { processConfigurationParameters } from 'src/sql-tools/processors/configuration-parameter.processor';
|
||||
import { processDatabases } from 'src/sql-tools/processors/database.processor';
|
||||
import { processEnums } from 'src/sql-tools/processors/enum.processor';
|
||||
import { processExtensions } from 'src/sql-tools/processors/extension.processor';
|
||||
import { processForeignKeyColumns } from 'src/sql-tools/processors/foreign-key-column.processor';
|
||||
import { processForeignKeyConstraints } from 'src/sql-tools/processors/foreign-key-constraint.processor';
|
||||
import { processFunctions } from 'src/sql-tools/processors/function.processor';
|
||||
import { processIndexes } from 'src/sql-tools/processors/index.processor';
|
||||
import { processPrimaryKeyConstraints } from 'src/sql-tools/processors/primary-key-contraint.processor';
|
||||
import { processTables } from 'src/sql-tools/processors/table.processor';
|
||||
import { processTriggers } from 'src/sql-tools/processors/trigger.processor';
|
||||
import { processUniqueConstraints } from 'src/sql-tools/processors/unique-constraint.processor';
|
||||
import { Processor } from 'src/sql-tools/types';
|
||||
|
||||
export const processors: Processor[] = [
|
||||
processDatabases,
|
||||
processConfigurationParameters,
|
||||
processEnums,
|
||||
processExtensions,
|
||||
processFunctions,
|
||||
processTables,
|
||||
processColumns,
|
||||
processForeignKeyColumns,
|
||||
processForeignKeyConstraints,
|
||||
processUniqueConstraints,
|
||||
processCheckConstraints,
|
||||
processPrimaryKeyConstraints,
|
||||
processIndexes,
|
||||
processTriggers,
|
||||
];
|
@ -1,6 +1,5 @@
|
||||
import { Processor } from 'src/sql-tools/from-code/processors/type';
|
||||
import { asKey } from 'src/sql-tools/helpers';
|
||||
import { DatabaseConstraintType } from 'src/sql-tools/types';
|
||||
import { ConstraintType, Processor } from 'src/sql-tools/types';
|
||||
|
||||
export const processPrimaryKeyConstraints: Processor = (builder) => {
|
||||
for (const table of builder.tables) {
|
||||
@ -11,13 +10,15 @@ export const processPrimaryKeyConstraints: Processor = (builder) => {
|
||||
columnNames.push(column.name);
|
||||
}
|
||||
}
|
||||
|
||||
if (columnNames.length > 0) {
|
||||
const tableMetadata = builder.getTableMetadata(table);
|
||||
table.constraints.push({
|
||||
type: DatabaseConstraintType.PRIMARY_KEY,
|
||||
name: table.metadata.options.primaryConstraintName || asPrimaryKeyConstraintName(table.name, columnNames),
|
||||
type: ConstraintType.PRIMARY_KEY,
|
||||
name: tableMetadata.options.primaryConstraintName || asPrimaryKeyConstraintName(table.name, columnNames),
|
||||
tableName: table.name,
|
||||
columnNames,
|
||||
synchronize: table.metadata.options.synchronize ?? true,
|
||||
synchronize: tableMetadata.options.synchronize ?? true,
|
||||
});
|
||||
}
|
||||
}
|
28
server/src/sql-tools/processors/table.processor.ts
Normal file
28
server/src/sql-tools/processors/table.processor.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { asSnakeCase } from 'src/sql-tools/helpers';
|
||||
import { Processor } from 'src/sql-tools/types';
|
||||
|
||||
export const processTables: Processor = (builder, items) => {
|
||||
for (const {
|
||||
item: { options, object },
|
||||
} of items.filter((item) => item.type === 'table')) {
|
||||
const test = builder.getTableByObject(object);
|
||||
if (test) {
|
||||
throw new Error(
|
||||
`Table ${test.name} has already been registered. Does ${object.name} have two @Table() decorators?`,
|
||||
);
|
||||
}
|
||||
|
||||
builder.addTable(
|
||||
{
|
||||
name: options.name || asSnakeCase(object.name),
|
||||
columns: [],
|
||||
constraints: [],
|
||||
indexes: [],
|
||||
triggers: [],
|
||||
synchronize: options.synchronize ?? true,
|
||||
},
|
||||
options,
|
||||
object,
|
||||
);
|
||||
}
|
||||
};
|
@ -1,15 +1,14 @@
|
||||
import { TriggerOptions } from 'src/sql-tools/from-code/decorators/trigger.decorator';
|
||||
import { onMissingTable, resolveTable } from 'src/sql-tools/from-code/processors/table.processor';
|
||||
import { Processor } from 'src/sql-tools/from-code/processors/type';
|
||||
import { TriggerOptions } from 'src/sql-tools/decorators/trigger.decorator';
|
||||
import { asKey } from 'src/sql-tools/helpers';
|
||||
import { Processor } from 'src/sql-tools/types';
|
||||
|
||||
export const processTriggers: Processor = (builder, items) => {
|
||||
for (const {
|
||||
item: { object, options },
|
||||
} of items.filter((item) => item.type === 'trigger')) {
|
||||
const table = resolveTable(builder, object);
|
||||
const table = builder.getTableByObject(object);
|
||||
if (!table) {
|
||||
onMissingTable(builder, '@Trigger', object);
|
||||
builder.warnMissingTable('@Trigger', object);
|
||||
continue;
|
||||
}
|
||||
|
@ -1,16 +1,13 @@
|
||||
import { onMissingColumn, resolveColumn } from 'src/sql-tools/from-code/processors/column.processor';
|
||||
import { onMissingTable, resolveTable } from 'src/sql-tools/from-code/processors/table.processor';
|
||||
import { Processor } from 'src/sql-tools/from-code/processors/type';
|
||||
import { asKey } from 'src/sql-tools/helpers';
|
||||
import { DatabaseConstraintType } from 'src/sql-tools/types';
|
||||
import { ConstraintType, Processor } from 'src/sql-tools/types';
|
||||
|
||||
export const processUniqueConstraints: Processor = (builder, items) => {
|
||||
for (const {
|
||||
item: { object, options },
|
||||
} of items.filter((item) => item.type === 'uniqueConstraint')) {
|
||||
const table = resolveTable(builder, object);
|
||||
const table = builder.getTableByObject(object);
|
||||
if (!table) {
|
||||
onMissingTable(builder, '@Unique', object);
|
||||
builder.warnMissingTable('@Unique', object);
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -18,7 +15,7 @@ export const processUniqueConstraints: Processor = (builder, items) => {
|
||||
const columnNames = options.columns;
|
||||
|
||||
table.constraints.push({
|
||||
type: DatabaseConstraintType.UNIQUE,
|
||||
type: ConstraintType.UNIQUE,
|
||||
name: options.name || asUniqueConstraintName(tableName, columnNames),
|
||||
tableName,
|
||||
columnNames,
|
||||
@ -31,21 +28,21 @@ export const processUniqueConstraints: Processor = (builder, items) => {
|
||||
type,
|
||||
item: { object, propertyName, options },
|
||||
} of items.filter((item) => item.type === 'column' || item.type === 'foreignKeyColumn')) {
|
||||
const { table, column } = resolveColumn(builder, object, propertyName);
|
||||
const { table, column } = builder.getColumnByObjectAndPropertyName(object, propertyName);
|
||||
if (!table) {
|
||||
onMissingTable(builder, '@Column', object);
|
||||
builder.warnMissingTable('@Column', object);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!column) {
|
||||
// should be impossible since they are created in `column.processor.ts`
|
||||
onMissingColumn(builder, '@Column', object, propertyName);
|
||||
builder.warnMissingColumn('@Column', object, propertyName);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (type === 'column' && !options.primary && (options.unique || options.uniqueConstraintName)) {
|
||||
table.constraints.push({
|
||||
type: DatabaseConstraintType.UNIQUE,
|
||||
type: ConstraintType.UNIQUE,
|
||||
name: options.uniqueConstraintName || asUniqueConstraintName(table.name, [column.name]),
|
||||
tableName: table.name,
|
||||
columnNames: [column.name],
|
@ -1,29 +1,7 @@
|
||||
export { schemaDiff } from 'src/sql-tools/diff';
|
||||
export { schemaFromCode } from 'src/sql-tools/from-code';
|
||||
export * from 'src/sql-tools/from-code/decorators/after-delete.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/after-insert.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/before-update.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/check.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/column.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/configuration-parameter.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/create-date-column.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/database.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/delete-date-column.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/extension.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/extensions.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/foreign-key-column.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/foreign-key-constraint.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/generated-column.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/index.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/primary-column.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/primary-generated-column.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/table.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/trigger-function.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/trigger.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/unique.decorator';
|
||||
export * from 'src/sql-tools/from-code/decorators/update-date-column.decorator';
|
||||
export * from 'src/sql-tools/from-code/register-enum';
|
||||
export * from 'src/sql-tools/from-code/register-function';
|
||||
export { schemaFromDatabase } from 'src/sql-tools/from-database';
|
||||
export { schemaDiffToSql } from 'src/sql-tools/to-sql';
|
||||
export * from 'src/sql-tools/decorators';
|
||||
export * from 'src/sql-tools/register-enum';
|
||||
export * from 'src/sql-tools/register-function';
|
||||
export { schemaDiff, schemaDiffToSql } from 'src/sql-tools/schema-diff';
|
||||
export { schemaFromCode } from 'src/sql-tools/schema-from-code';
|
||||
export { schemaFromDatabase } from 'src/sql-tools/schema-from-database';
|
||||
export * from 'src/sql-tools/types';
|
||||
|
@ -1,7 +1,6 @@
|
||||
import { sql } from 'kysely';
|
||||
import { jsonArrayFrom } from 'kysely/helpers/postgres';
|
||||
import { DatabaseReader } from 'src/sql-tools/from-database/readers/type';
|
||||
import { ColumnType, DatabaseColumn } from 'src/sql-tools/types';
|
||||
import { ColumnType, DatabaseColumn, DatabaseReader } from 'src/sql-tools/types';
|
||||
|
||||
export const readColumns: DatabaseReader = async (schema, db) => {
|
||||
const columns = await db
|
@ -1,4 +1,4 @@
|
||||
import { DatabaseReader } from 'src/sql-tools/from-database/readers/type';
|
||||
import { DatabaseReader } from 'src/sql-tools/types';
|
||||
|
||||
export const readComments: DatabaseReader = async (schema, db) => {
|
||||
const comments = await db
|
@ -1,6 +1,5 @@
|
||||
import { sql } from 'kysely';
|
||||
import { DatabaseReader } from 'src/sql-tools/from-database/readers/type';
|
||||
import { DatabaseActionType, DatabaseConstraintType } from 'src/sql-tools/types';
|
||||
import { ActionType, ConstraintType, DatabaseReader } from 'src/sql-tools/types';
|
||||
|
||||
export const readConstraints: DatabaseReader = async (schema, db) => {
|
||||
const constraints = await db
|
||||
@ -60,7 +59,7 @@ export const readConstraints: DatabaseReader = async (schema, db) => {
|
||||
continue;
|
||||
}
|
||||
table.constraints.push({
|
||||
type: DatabaseConstraintType.PRIMARY_KEY,
|
||||
type: ConstraintType.PRIMARY_KEY,
|
||||
name: constraintName,
|
||||
tableName: constraint.table_name,
|
||||
columnNames: constraint.column_names,
|
||||
@ -79,7 +78,7 @@ export const readConstraints: DatabaseReader = async (schema, db) => {
|
||||
}
|
||||
|
||||
table.constraints.push({
|
||||
type: DatabaseConstraintType.FOREIGN_KEY,
|
||||
type: ConstraintType.FOREIGN_KEY,
|
||||
name: constraintName,
|
||||
tableName: constraint.table_name,
|
||||
columnNames: constraint.column_names,
|
||||
@ -95,7 +94,7 @@ export const readConstraints: DatabaseReader = async (schema, db) => {
|
||||
// unique constraint
|
||||
case 'u': {
|
||||
table.constraints.push({
|
||||
type: DatabaseConstraintType.UNIQUE,
|
||||
type: ConstraintType.UNIQUE,
|
||||
name: constraintName,
|
||||
tableName: constraint.table_name,
|
||||
columnNames: constraint.column_names as string[],
|
||||
@ -107,7 +106,7 @@ export const readConstraints: DatabaseReader = async (schema, db) => {
|
||||
// check constraint
|
||||
case 'c': {
|
||||
table.constraints.push({
|
||||
type: DatabaseConstraintType.CHECK,
|
||||
type: ConstraintType.CHECK,
|
||||
name: constraint.constraint_name,
|
||||
tableName: constraint.table_name,
|
||||
expression: constraint.expression.replace('CHECK ', ''),
|
||||
@ -122,23 +121,23 @@ export const readConstraints: DatabaseReader = async (schema, db) => {
|
||||
const asDatabaseAction = (action: string) => {
|
||||
switch (action) {
|
||||
case 'a': {
|
||||
return DatabaseActionType.NO_ACTION;
|
||||
return ActionType.NO_ACTION;
|
||||
}
|
||||
case 'c': {
|
||||
return DatabaseActionType.CASCADE;
|
||||
return ActionType.CASCADE;
|
||||
}
|
||||
case 'r': {
|
||||
return DatabaseActionType.RESTRICT;
|
||||
return ActionType.RESTRICT;
|
||||
}
|
||||
case 'n': {
|
||||
return DatabaseActionType.SET_NULL;
|
||||
return ActionType.SET_NULL;
|
||||
}
|
||||
case 'd': {
|
||||
return DatabaseActionType.SET_DEFAULT;
|
||||
return ActionType.SET_DEFAULT;
|
||||
}
|
||||
|
||||
default: {
|
||||
return DatabaseActionType.NO_ACTION;
|
||||
return ActionType.NO_ACTION;
|
||||
}
|
||||
}
|
||||
};
|
@ -1,4 +1,4 @@
|
||||
import { DatabaseReader } from 'src/sql-tools/from-database/readers/type';
|
||||
import { DatabaseReader } from 'src/sql-tools/types';
|
||||
|
||||
export const readExtensions: DatabaseReader = async (schema, db) => {
|
||||
const extensions = await db
|
@ -1,5 +1,5 @@
|
||||
import { sql } from 'kysely';
|
||||
import { DatabaseReader } from 'src/sql-tools/from-database/readers/type';
|
||||
import { DatabaseReader } from 'src/sql-tools/types';
|
||||
|
||||
export const readFunctions: DatabaseReader = async (schema, db) => {
|
||||
const routines = await db
|
@ -1,5 +1,5 @@
|
||||
import { sql } from 'kysely';
|
||||
import { DatabaseReader } from 'src/sql-tools/from-database/readers/type';
|
||||
import { DatabaseReader } from 'src/sql-tools/types';
|
||||
|
||||
export const readIndexes: DatabaseReader = async (schema, db) => {
|
||||
const indexes = await db
|
25
server/src/sql-tools/readers/index.ts
Normal file
25
server/src/sql-tools/readers/index.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { readColumns } from 'src/sql-tools/readers/column.reader';
|
||||
import { readComments } from 'src/sql-tools/readers/comment.reader';
|
||||
import { readConstraints } from 'src/sql-tools/readers/constraint.reader';
|
||||
import { readExtensions } from 'src/sql-tools/readers/extension.reader';
|
||||
import { readFunctions } from 'src/sql-tools/readers/function.reader';
|
||||
import { readIndexes } from 'src/sql-tools/readers/index.reader';
|
||||
import { readName } from 'src/sql-tools/readers/name.reader';
|
||||
import { readParameters } from 'src/sql-tools/readers/parameter.reader';
|
||||
import { readTables } from 'src/sql-tools/readers/table.reader';
|
||||
import { readTriggers } from 'src/sql-tools/readers/trigger.reader';
|
||||
import { DatabaseReader } from 'src/sql-tools/types';
|
||||
|
||||
export const readers: DatabaseReader[] = [
|
||||
//
|
||||
readName,
|
||||
readParameters,
|
||||
readExtensions,
|
||||
readFunctions,
|
||||
readTables,
|
||||
readColumns,
|
||||
readIndexes,
|
||||
readConstraints,
|
||||
readTriggers,
|
||||
readComments,
|
||||
];
|
@ -1,8 +1,8 @@
|
||||
import { QueryResult, sql } from 'kysely';
|
||||
import { DatabaseReader } from 'src/sql-tools/from-database/readers/type';
|
||||
import { DatabaseReader } from 'src/sql-tools/types';
|
||||
|
||||
export const readName: DatabaseReader = async (schema, db) => {
|
||||
const result = (await sql`SELECT current_database() as name`.execute(db)) as QueryResult<{ name: string }>;
|
||||
|
||||
schema.name = result.rows[0].name;
|
||||
schema.databaseName = result.rows[0].name;
|
||||
};
|
@ -1,6 +1,5 @@
|
||||
import { sql } from 'kysely';
|
||||
import { DatabaseReader } from 'src/sql-tools/from-database/readers/type';
|
||||
import { ParameterScope } from 'src/sql-tools/types';
|
||||
import { DatabaseReader, ParameterScope } from 'src/sql-tools/types';
|
||||
|
||||
export const readParameters: DatabaseReader = async (schema, db) => {
|
||||
const parameters = await db
|
||||
@ -13,7 +12,7 @@ export const readParameters: DatabaseReader = async (schema, db) => {
|
||||
schema.parameters.push({
|
||||
name: parameter.name,
|
||||
value: parameter.value,
|
||||
databaseName: schema.name,
|
||||
databaseName: schema.databaseName,
|
||||
scope: parameter.scope as ParameterScope,
|
||||
synchronize: true,
|
||||
});
|
@ -1,5 +1,5 @@
|
||||
import { sql } from 'kysely';
|
||||
import { DatabaseReader } from 'src/sql-tools/from-database/readers/type';
|
||||
import { DatabaseReader } from 'src/sql-tools/types';
|
||||
|
||||
export const readTables: DatabaseReader = async (schema, db) => {
|
||||
const tables = await db
|
@ -1,5 +1,4 @@
|
||||
import { DatabaseReader } from 'src/sql-tools/from-database/readers/type';
|
||||
import { parseTriggerType } from 'src/sql-tools/helpers';
|
||||
import { DatabaseReader, TriggerAction, TriggerScope, TriggerTiming } from 'src/sql-tools/types';
|
||||
|
||||
export const readTriggers: DatabaseReader = async (schema, db) => {
|
||||
const triggers = await db
|
||||
@ -44,3 +43,44 @@ export const readTriggers: DatabaseReader = async (schema, db) => {
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const hasMask = (input: number, mask: number) => (input & mask) === mask;
|
||||
|
||||
export const parseTriggerType = (type: number) => {
|
||||
// eslint-disable-next-line unicorn/prefer-math-trunc
|
||||
const scope: TriggerScope = hasMask(type, 1 << 0) ? 'row' : 'statement';
|
||||
|
||||
let timing: TriggerTiming = 'after';
|
||||
const timingMasks: Array<{ mask: number; value: TriggerTiming }> = [
|
||||
{ mask: 1 << 1, value: 'before' },
|
||||
{ mask: 1 << 6, value: 'instead of' },
|
||||
];
|
||||
|
||||
for (const { mask, value } of timingMasks) {
|
||||
if (hasMask(type, mask)) {
|
||||
timing = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const actions: TriggerAction[] = [];
|
||||
const actionMasks: Array<{ mask: number; value: TriggerAction }> = [
|
||||
{ mask: 1 << 2, value: 'insert' },
|
||||
{ mask: 1 << 3, value: 'delete' },
|
||||
{ mask: 1 << 4, value: 'update' },
|
||||
{ mask: 1 << 5, value: 'truncate' },
|
||||
];
|
||||
|
||||
for (const { mask, value } of actionMasks) {
|
||||
if (hasMask(type, mask)) {
|
||||
actions.push(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (actions.length === 0) {
|
||||
throw new Error(`Unable to parse trigger type ${type}`);
|
||||
}
|
||||
|
||||
return { actions, timing, scope };
|
||||
};
|
@ -1,4 +1,4 @@
|
||||
import { register } from 'src/sql-tools/from-code/register';
|
||||
import { register } from 'src/sql-tools/register';
|
||||
import { DatabaseEnum } from 'src/sql-tools/types';
|
||||
|
||||
export type EnumOptions = {
|
@ -1,4 +1,4 @@
|
||||
import { register } from 'src/sql-tools/from-code/register';
|
||||
import { register } from 'src/sql-tools/register';
|
||||
import { ColumnType, DatabaseFunction } from 'src/sql-tools/types';
|
||||
|
||||
export type FunctionOptions = {
|
@ -1,17 +1,17 @@
|
||||
import { CheckOptions } from 'src/sql-tools/from-code/decorators/check.decorator';
|
||||
import { ColumnOptions } from 'src/sql-tools/from-code/decorators/column.decorator';
|
||||
import { ConfigurationParameterOptions } from 'src/sql-tools/from-code/decorators/configuration-parameter.decorator';
|
||||
import { DatabaseOptions } from 'src/sql-tools/from-code/decorators/database.decorator';
|
||||
import { ExtensionOptions } from 'src/sql-tools/from-code/decorators/extension.decorator';
|
||||
import { ForeignKeyColumnOptions } from 'src/sql-tools/from-code/decorators/foreign-key-column.decorator';
|
||||
import { ForeignKeyConstraintOptions } from 'src/sql-tools/from-code/decorators/foreign-key-constraint.decorator';
|
||||
import { IndexOptions } from 'src/sql-tools/from-code/decorators/index.decorator';
|
||||
import { TableOptions } from 'src/sql-tools/from-code/decorators/table.decorator';
|
||||
import { TriggerOptions } from 'src/sql-tools/from-code/decorators/trigger.decorator';
|
||||
import { UniqueOptions } from 'src/sql-tools/from-code/decorators/unique.decorator';
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-function-type */
|
||||
import { CheckOptions } from 'src/sql-tools/decorators/check.decorator';
|
||||
import { ColumnOptions } from 'src/sql-tools/decorators/column.decorator';
|
||||
import { ConfigurationParameterOptions } from 'src/sql-tools/decorators/configuration-parameter.decorator';
|
||||
import { DatabaseOptions } from 'src/sql-tools/decorators/database.decorator';
|
||||
import { ExtensionOptions } from 'src/sql-tools/decorators/extension.decorator';
|
||||
import { ForeignKeyColumnOptions } from 'src/sql-tools/decorators/foreign-key-column.decorator';
|
||||
import { ForeignKeyConstraintOptions } from 'src/sql-tools/decorators/foreign-key-constraint.decorator';
|
||||
import { IndexOptions } from 'src/sql-tools/decorators/index.decorator';
|
||||
import { TableOptions } from 'src/sql-tools/decorators/table.decorator';
|
||||
import { TriggerOptions } from 'src/sql-tools/decorators/trigger.decorator';
|
||||
import { UniqueOptions } from 'src/sql-tools/decorators/unique.decorator';
|
||||
import { DatabaseEnum, DatabaseFunction } from 'src/sql-tools/types';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
||||
export type ClassBased<T> = { object: Function } & T;
|
||||
export type PropertyBased<T> = { object: object; propertyName: string | symbol } & T;
|
||||
export type RegisterItem =
|
||||
@ -26,6 +26,6 @@ export type RegisterItem =
|
||||
| { type: 'trigger'; item: ClassBased<{ options: TriggerOptions }> }
|
||||
| { type: 'extension'; item: ClassBased<{ options: ExtensionOptions }> }
|
||||
| { type: 'configurationParameter'; item: ClassBased<{ options: ConfigurationParameterOptions }> }
|
||||
| { type: 'foreignKeyColumn'; item: PropertyBased<{ options: ForeignKeyColumnOptions; target: () => object }> }
|
||||
| { type: 'foreignKeyColumn'; item: PropertyBased<{ options: ForeignKeyColumnOptions; target: () => Function }> }
|
||||
| { type: 'foreignKeyConstraint'; item: ClassBased<{ options: ForeignKeyConstraintOptions }> };
|
||||
export type RegisterItemType<T extends RegisterItem['type']> = Extract<RegisterItem, { type: T }>['item'];
|
@ -1,4 +1,4 @@
|
||||
import { RegisterItem } from 'src/sql-tools/from-code/register-item';
|
||||
import { RegisterItem } from 'src/sql-tools/register-item';
|
||||
|
||||
const items: RegisterItem[] = [];
|
||||
|
121
server/src/sql-tools/schema-builder.ts
Normal file
121
server/src/sql-tools/schema-builder.ts
Normal file
@ -0,0 +1,121 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-function-type */
|
||||
import { ColumnOptions, TableOptions } from 'src/sql-tools/decorators';
|
||||
import { asKey } from 'src/sql-tools/helpers';
|
||||
import {
|
||||
DatabaseColumn,
|
||||
DatabaseEnum,
|
||||
DatabaseExtension,
|
||||
DatabaseFunction,
|
||||
DatabaseParameter,
|
||||
DatabaseSchema,
|
||||
DatabaseTable,
|
||||
SchemaFromCodeOptions,
|
||||
} from 'src/sql-tools/types';
|
||||
|
||||
type TableMetadata = { options: TableOptions; object: Function; methodToColumn: Map<string | symbol, DatabaseColumn> };
|
||||
|
||||
export class SchemaBuilder {
|
||||
databaseName: string;
|
||||
schemaName: string;
|
||||
tables: DatabaseTable[] = [];
|
||||
functions: DatabaseFunction[] = [];
|
||||
enums: DatabaseEnum[] = [];
|
||||
extensions: DatabaseExtension[] = [];
|
||||
parameters: DatabaseParameter[] = [];
|
||||
warnings: string[] = [];
|
||||
|
||||
classToTable: WeakMap<Function, DatabaseTable> = new WeakMap();
|
||||
tableToMetadata: WeakMap<DatabaseTable, TableMetadata> = new WeakMap();
|
||||
|
||||
constructor(options: SchemaFromCodeOptions) {
|
||||
this.databaseName = options.databaseName ?? 'postgres';
|
||||
this.schemaName = options.schemaName ?? 'public';
|
||||
}
|
||||
|
||||
getTableByObject(object: Function) {
|
||||
return this.classToTable.get(object);
|
||||
}
|
||||
|
||||
getTableByName(name: string) {
|
||||
return this.tables.find((table) => table.name === name);
|
||||
}
|
||||
|
||||
getTableMetadata(table: DatabaseTable) {
|
||||
const metadata = this.tableToMetadata.get(table);
|
||||
if (!metadata) {
|
||||
throw new Error(`Table metadata not found for table: ${table.name}`);
|
||||
}
|
||||
return metadata;
|
||||
}
|
||||
|
||||
addTable(table: DatabaseTable, options: TableOptions, object: Function) {
|
||||
this.tables.push(table);
|
||||
this.classToTable.set(object, table);
|
||||
this.tableToMetadata.set(table, { options, object, methodToColumn: new Map() });
|
||||
}
|
||||
|
||||
getColumnByObjectAndPropertyName(
|
||||
object: object,
|
||||
propertyName: string | symbol,
|
||||
): { table?: DatabaseTable; column?: DatabaseColumn } {
|
||||
const table = this.getTableByObject(object.constructor);
|
||||
if (!table) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const tableMetadata = this.tableToMetadata.get(table);
|
||||
if (!tableMetadata) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const column = tableMetadata.methodToColumn.get(propertyName);
|
||||
|
||||
return { table, column };
|
||||
}
|
||||
|
||||
addColumn(table: DatabaseTable, column: DatabaseColumn, options: ColumnOptions, propertyName: string | symbol) {
|
||||
table.columns.push(column);
|
||||
const tableMetadata = this.getTableMetadata(table);
|
||||
tableMetadata.methodToColumn.set(propertyName, column);
|
||||
}
|
||||
|
||||
asIndexName(table: string, columns?: string[], where?: string) {
|
||||
const items: string[] = [];
|
||||
for (const columnName of columns ?? []) {
|
||||
items.push(columnName);
|
||||
}
|
||||
|
||||
if (where) {
|
||||
items.push(where);
|
||||
}
|
||||
|
||||
return asKey('IDX_', table, items);
|
||||
}
|
||||
|
||||
warn(context: string, message: string) {
|
||||
this.warnings.push(`[${context}] ${message}`);
|
||||
}
|
||||
|
||||
warnMissingTable(context: string, object: object, propertyName?: symbol | string) {
|
||||
const label = object.constructor.name + (propertyName ? '.' + String(propertyName) : '');
|
||||
this.warn(context, `Unable to find table (${label})`);
|
||||
}
|
||||
|
||||
warnMissingColumn(context: string, object: object, propertyName?: symbol | string) {
|
||||
const label = object.constructor.name + (propertyName ? '.' + String(propertyName) : '');
|
||||
this.warn(context, `Unable to find column (${label})`);
|
||||
}
|
||||
|
||||
build(): DatabaseSchema {
|
||||
return {
|
||||
databaseName: this.databaseName,
|
||||
schemaName: this.schemaName,
|
||||
tables: this.tables,
|
||||
functions: this.functions,
|
||||
enums: this.enums,
|
||||
extensions: this.extensions,
|
||||
parameters: this.parameters,
|
||||
warnings: this.warnings,
|
||||
};
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
import { schemaDiff } from 'src/sql-tools/diff';
|
||||
import { schemaDiff } from 'src/sql-tools/schema-diff';
|
||||
import {
|
||||
ActionType,
|
||||
ColumnType,
|
||||
DatabaseActionType,
|
||||
ConstraintType,
|
||||
DatabaseColumn,
|
||||
DatabaseConstraint,
|
||||
DatabaseConstraintType,
|
||||
DatabaseIndex,
|
||||
DatabaseSchema,
|
||||
DatabaseTable,
|
||||
@ -15,7 +15,7 @@ const fromColumn = (column: Partial<Omit<DatabaseColumn, 'tableName'>>): Databas
|
||||
const tableName = 'table1';
|
||||
|
||||
return {
|
||||
name: 'postgres',
|
||||
databaseName: 'postgres',
|
||||
schemaName: 'public',
|
||||
functions: [],
|
||||
enums: [],
|
||||
@ -49,7 +49,7 @@ const fromConstraint = (constraint?: DatabaseConstraint): DatabaseSchema => {
|
||||
const tableName = constraint?.tableName || 'table1';
|
||||
|
||||
return {
|
||||
name: 'postgres',
|
||||
databaseName: 'postgres',
|
||||
schemaName: 'public',
|
||||
functions: [],
|
||||
enums: [],
|
||||
@ -82,7 +82,7 @@ const fromIndex = (index?: DatabaseIndex): DatabaseSchema => {
|
||||
const tableName = index?.tableName || 'table1';
|
||||
|
||||
return {
|
||||
name: 'postgres',
|
||||
databaseName: 'postgres',
|
||||
schemaName: 'public',
|
||||
functions: [],
|
||||
enums: [],
|
||||
@ -155,7 +155,7 @@ const newSchema = (schema: {
|
||||
}
|
||||
|
||||
return {
|
||||
name: 'immich',
|
||||
databaseName: 'immich',
|
||||
schemaName: schema?.name || 'public',
|
||||
functions: [],
|
||||
enums: [],
|
||||
@ -166,14 +166,14 @@ const newSchema = (schema: {
|
||||
};
|
||||
};
|
||||
|
||||
describe('schemaDiff', () => {
|
||||
describe(schemaDiff.name, () => {
|
||||
it('should work', () => {
|
||||
const diff = schemaDiff(newSchema({ tables: [] }), newSchema({ tables: [] }));
|
||||
expect(diff.items).toEqual([]);
|
||||
});
|
||||
|
||||
describe('table', () => {
|
||||
describe('table.create', () => {
|
||||
describe('TableCreate', () => {
|
||||
it('should find a missing table', () => {
|
||||
const column: DatabaseColumn = {
|
||||
type: 'character varying',
|
||||
@ -190,7 +190,7 @@ describe('schemaDiff', () => {
|
||||
|
||||
expect(diff.items).toHaveLength(1);
|
||||
expect(diff.items[0]).toEqual({
|
||||
type: 'table.create',
|
||||
type: 'TableCreate',
|
||||
table: {
|
||||
name: 'table1',
|
||||
columns: [column],
|
||||
@ -204,7 +204,7 @@ describe('schemaDiff', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('table.drop', () => {
|
||||
describe('TableDrop', () => {
|
||||
it('should find an extra table', () => {
|
||||
const diff = schemaDiff(
|
||||
newSchema({ tables: [] }),
|
||||
@ -216,7 +216,7 @@ describe('schemaDiff', () => {
|
||||
|
||||
expect(diff.items).toHaveLength(1);
|
||||
expect(diff.items[0]).toEqual({
|
||||
type: 'table.drop',
|
||||
type: 'TableDrop',
|
||||
tableName: 'table1',
|
||||
reason: 'missing in source',
|
||||
});
|
||||
@ -238,7 +238,7 @@ describe('schemaDiff', () => {
|
||||
});
|
||||
|
||||
describe('column', () => {
|
||||
describe('column.add', () => {
|
||||
describe('ColumnAdd', () => {
|
||||
it('should find a new column', () => {
|
||||
const diff = schemaDiff(
|
||||
newSchema({
|
||||
@ -256,7 +256,7 @@ describe('schemaDiff', () => {
|
||||
|
||||
expect(diff.items).toEqual([
|
||||
{
|
||||
type: 'column.add',
|
||||
type: 'ColumnAdd',
|
||||
column: {
|
||||
tableName: 'table1',
|
||||
isArray: false,
|
||||
@ -271,7 +271,7 @@ describe('schemaDiff', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('column.drop', () => {
|
||||
describe('ColumnDrop', () => {
|
||||
it('should find an extra column', () => {
|
||||
const diff = schemaDiff(
|
||||
newSchema({
|
||||
@ -289,7 +289,7 @@ describe('schemaDiff', () => {
|
||||
|
||||
expect(diff.items).toEqual([
|
||||
{
|
||||
type: 'column.drop',
|
||||
type: 'ColumnDrop',
|
||||
tableName: 'table1',
|
||||
columnName: 'column2',
|
||||
reason: 'missing in source',
|
||||
@ -307,7 +307,7 @@ describe('schemaDiff', () => {
|
||||
|
||||
expect(diff.items).toEqual([
|
||||
{
|
||||
type: 'column.alter',
|
||||
type: 'ColumnAlter',
|
||||
tableName: 'table1',
|
||||
columnName: 'column1',
|
||||
changes: {
|
||||
@ -326,7 +326,7 @@ describe('schemaDiff', () => {
|
||||
|
||||
expect(diff.items).toEqual([
|
||||
{
|
||||
type: 'column.alter',
|
||||
type: 'ColumnAlter',
|
||||
tableName: 'table1',
|
||||
columnName: 'column1',
|
||||
changes: {
|
||||
@ -347,7 +347,7 @@ describe('schemaDiff', () => {
|
||||
|
||||
expect(diff.items).toEqual([
|
||||
{
|
||||
type: 'column.alter',
|
||||
type: 'ColumnAlter',
|
||||
tableName: 'table1',
|
||||
columnName: 'column1',
|
||||
changes: {
|
||||
@ -388,12 +388,12 @@ describe('schemaDiff', () => {
|
||||
});
|
||||
|
||||
describe('constraint', () => {
|
||||
describe('constraint.add', () => {
|
||||
describe('ConstraintAdd', () => {
|
||||
it('should detect a new constraint', () => {
|
||||
const diff = schemaDiff(
|
||||
fromConstraint({
|
||||
name: 'PK_test',
|
||||
type: DatabaseConstraintType.PRIMARY_KEY,
|
||||
type: ConstraintType.PRIMARY_KEY,
|
||||
tableName: 'table1',
|
||||
columnNames: ['id'],
|
||||
synchronize: true,
|
||||
@ -403,9 +403,9 @@ describe('schemaDiff', () => {
|
||||
|
||||
expect(diff.items).toEqual([
|
||||
{
|
||||
type: 'constraint.add',
|
||||
type: 'ConstraintAdd',
|
||||
constraint: {
|
||||
type: DatabaseConstraintType.PRIMARY_KEY,
|
||||
type: ConstraintType.PRIMARY_KEY,
|
||||
name: 'PK_test',
|
||||
columnNames: ['id'],
|
||||
tableName: 'table1',
|
||||
@ -417,13 +417,13 @@ describe('schemaDiff', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('constraint.drop', () => {
|
||||
describe('ConstraintDrop', () => {
|
||||
it('should detect an extra constraint', () => {
|
||||
const diff = schemaDiff(
|
||||
fromConstraint(),
|
||||
fromConstraint({
|
||||
name: 'PK_test',
|
||||
type: DatabaseConstraintType.PRIMARY_KEY,
|
||||
type: ConstraintType.PRIMARY_KEY,
|
||||
tableName: 'table1',
|
||||
columnNames: ['id'],
|
||||
synchronize: true,
|
||||
@ -432,7 +432,7 @@ describe('schemaDiff', () => {
|
||||
|
||||
expect(diff.items).toEqual([
|
||||
{
|
||||
type: 'constraint.drop',
|
||||
type: 'ConstraintDrop',
|
||||
tableName: 'table1',
|
||||
constraintName: 'PK_test',
|
||||
reason: 'missing in source',
|
||||
@ -444,7 +444,7 @@ describe('schemaDiff', () => {
|
||||
describe('primary key', () => {
|
||||
it('should skip identical primary key constraints', () => {
|
||||
const constraint: DatabaseConstraint = {
|
||||
type: DatabaseConstraintType.PRIMARY_KEY,
|
||||
type: ConstraintType.PRIMARY_KEY,
|
||||
name: 'PK_test',
|
||||
tableName: 'table1',
|
||||
columnNames: ['id'],
|
||||
@ -460,7 +460,7 @@ describe('schemaDiff', () => {
|
||||
describe('foreign key', () => {
|
||||
it('should skip identical foreign key constraints', () => {
|
||||
const constraint: DatabaseConstraint = {
|
||||
type: DatabaseConstraintType.FOREIGN_KEY,
|
||||
type: ConstraintType.FOREIGN_KEY,
|
||||
name: 'FK_test',
|
||||
tableName: 'table1',
|
||||
columnNames: ['parentId'],
|
||||
@ -476,7 +476,7 @@ describe('schemaDiff', () => {
|
||||
|
||||
it('should drop and recreate when the column changes', () => {
|
||||
const constraint: DatabaseConstraint = {
|
||||
type: DatabaseConstraintType.FOREIGN_KEY,
|
||||
type: ConstraintType.FOREIGN_KEY,
|
||||
name: 'FK_test',
|
||||
tableName: 'table1',
|
||||
columnNames: ['parentId'],
|
||||
@ -495,7 +495,7 @@ describe('schemaDiff', () => {
|
||||
constraintName: 'FK_test',
|
||||
reason: 'columns are different (parentId vs parentId2)',
|
||||
tableName: 'table1',
|
||||
type: 'constraint.drop',
|
||||
type: 'ConstraintDrop',
|
||||
},
|
||||
{
|
||||
constraint: {
|
||||
@ -508,20 +508,20 @@ describe('schemaDiff', () => {
|
||||
type: 'foreign-key',
|
||||
},
|
||||
reason: 'columns are different (parentId vs parentId2)',
|
||||
type: 'constraint.add',
|
||||
type: 'ConstraintAdd',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should drop and recreate when the ON DELETE action changes', () => {
|
||||
const constraint: DatabaseConstraint = {
|
||||
type: DatabaseConstraintType.FOREIGN_KEY,
|
||||
type: ConstraintType.FOREIGN_KEY,
|
||||
name: 'FK_test',
|
||||
tableName: 'table1',
|
||||
columnNames: ['parentId'],
|
||||
referenceTableName: 'table2',
|
||||
referenceColumnNames: ['id'],
|
||||
onDelete: DatabaseActionType.CASCADE,
|
||||
onDelete: ActionType.CASCADE,
|
||||
synchronize: true,
|
||||
};
|
||||
|
||||
@ -532,7 +532,7 @@ describe('schemaDiff', () => {
|
||||
constraintName: 'FK_test',
|
||||
reason: 'ON DELETE action is different (CASCADE vs NO ACTION)',
|
||||
tableName: 'table1',
|
||||
type: 'constraint.drop',
|
||||
type: 'ConstraintDrop',
|
||||
},
|
||||
{
|
||||
constraint: {
|
||||
@ -540,13 +540,13 @@ describe('schemaDiff', () => {
|
||||
name: 'FK_test',
|
||||
referenceColumnNames: ['id'],
|
||||
referenceTableName: 'table2',
|
||||
onDelete: DatabaseActionType.CASCADE,
|
||||
onDelete: ActionType.CASCADE,
|
||||
synchronize: true,
|
||||
tableName: 'table1',
|
||||
type: 'foreign-key',
|
||||
},
|
||||
reason: 'ON DELETE action is different (CASCADE vs NO ACTION)',
|
||||
type: 'constraint.add',
|
||||
type: 'ConstraintAdd',
|
||||
},
|
||||
]);
|
||||
});
|
||||
@ -555,7 +555,7 @@ describe('schemaDiff', () => {
|
||||
describe('unique', () => {
|
||||
it('should skip identical unique constraints', () => {
|
||||
const constraint: DatabaseConstraint = {
|
||||
type: DatabaseConstraintType.UNIQUE,
|
||||
type: ConstraintType.UNIQUE,
|
||||
name: 'UQ_test',
|
||||
tableName: 'table1',
|
||||
columnNames: ['id'],
|
||||
@ -571,7 +571,7 @@ describe('schemaDiff', () => {
|
||||
describe('check', () => {
|
||||
it('should skip identical check constraints', () => {
|
||||
const constraint: DatabaseConstraint = {
|
||||
type: DatabaseConstraintType.CHECK,
|
||||
type: ConstraintType.CHECK,
|
||||
name: 'CHK_test',
|
||||
tableName: 'table1',
|
||||
expression: 'column1 > 0',
|
||||
@ -586,7 +586,7 @@ describe('schemaDiff', () => {
|
||||
});
|
||||
|
||||
describe('index', () => {
|
||||
describe('index.create', () => {
|
||||
describe('IndexCreate', () => {
|
||||
it('should detect a new index', () => {
|
||||
const diff = schemaDiff(
|
||||
fromIndex({
|
||||
@ -601,7 +601,7 @@ describe('schemaDiff', () => {
|
||||
|
||||
expect(diff.items).toEqual([
|
||||
{
|
||||
type: 'index.create',
|
||||
type: 'IndexCreate',
|
||||
index: {
|
||||
name: 'IDX_test',
|
||||
columnNames: ['id'],
|
||||
@ -615,7 +615,7 @@ describe('schemaDiff', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('index.drop', () => {
|
||||
describe('IndexDrop', () => {
|
||||
it('should detect an extra index', () => {
|
||||
const diff = schemaDiff(
|
||||
fromIndex(),
|
||||
@ -630,7 +630,7 @@ describe('schemaDiff', () => {
|
||||
|
||||
expect(diff.items).toEqual([
|
||||
{
|
||||
type: 'index.drop',
|
||||
type: 'IndexDrop',
|
||||
indexName: 'IDX_test',
|
||||
reason: 'missing in source',
|
||||
},
|
||||
@ -650,12 +650,12 @@ describe('schemaDiff', () => {
|
||||
|
||||
expect(diff.items).toEqual([
|
||||
{
|
||||
type: 'index.drop',
|
||||
type: 'IndexDrop',
|
||||
indexName: 'IDX_test',
|
||||
reason: 'uniqueness is different (true vs false)',
|
||||
},
|
||||
{
|
||||
type: 'index.create',
|
||||
type: 'IndexCreate',
|
||||
index,
|
||||
reason: 'uniqueness is different (true vs false)',
|
||||
},
|
121
server/src/sql-tools/schema-diff.ts
Normal file
121
server/src/sql-tools/schema-diff.ts
Normal file
@ -0,0 +1,121 @@
|
||||
import { compareEnums } from 'src/sql-tools/comparers/enum.comparer';
|
||||
import { compareExtensions } from 'src/sql-tools/comparers/extension.comparer';
|
||||
import { compareFunctions } from 'src/sql-tools/comparers/function.comparer';
|
||||
import { compareParameters } from 'src/sql-tools/comparers/parameter.comparer';
|
||||
import { compareTables } from 'src/sql-tools/comparers/table.comparer';
|
||||
import { compare } from 'src/sql-tools/helpers';
|
||||
import { transformers } from 'src/sql-tools/transformers';
|
||||
import {
|
||||
ConstraintType,
|
||||
DatabaseSchema,
|
||||
SchemaDiff,
|
||||
SchemaDiffOptions,
|
||||
SchemaDiffToSqlOptions,
|
||||
} from 'src/sql-tools/types';
|
||||
|
||||
/**
|
||||
* Compute the difference between two database schemas
|
||||
*/
|
||||
export const schemaDiff = (source: DatabaseSchema, target: DatabaseSchema, options: SchemaDiffOptions = {}) => {
|
||||
const items = [
|
||||
...compare(source.parameters, target.parameters, options.parameters, compareParameters),
|
||||
...compare(source.extensions, target.extensions, options.extension, compareExtensions),
|
||||
...compare(source.functions, target.functions, options.functions, compareFunctions),
|
||||
...compare(source.enums, target.enums, options.enums, compareEnums),
|
||||
...compare(source.tables, target.tables, options.tables, compareTables),
|
||||
];
|
||||
|
||||
type SchemaName = SchemaDiff['type'];
|
||||
const itemMap: Record<SchemaName, SchemaDiff[]> = {
|
||||
EnumCreate: [],
|
||||
EnumDrop: [],
|
||||
ExtensionCreate: [],
|
||||
ExtensionDrop: [],
|
||||
FunctionCreate: [],
|
||||
FunctionDrop: [],
|
||||
TableCreate: [],
|
||||
TableDrop: [],
|
||||
ColumnAdd: [],
|
||||
ColumnAlter: [],
|
||||
ColumnDrop: [],
|
||||
ConstraintAdd: [],
|
||||
ConstraintDrop: [],
|
||||
IndexCreate: [],
|
||||
IndexDrop: [],
|
||||
TriggerCreate: [],
|
||||
TriggerDrop: [],
|
||||
ParameterSet: [],
|
||||
ParameterReset: [],
|
||||
};
|
||||
|
||||
for (const item of items) {
|
||||
itemMap[item.type].push(item);
|
||||
}
|
||||
|
||||
const constraintAdds = itemMap.ConstraintAdd.filter((item) => item.type === 'ConstraintAdd');
|
||||
|
||||
const orderedItems = [
|
||||
...itemMap.ExtensionCreate,
|
||||
...itemMap.FunctionCreate,
|
||||
...itemMap.ParameterSet,
|
||||
...itemMap.ParameterReset,
|
||||
...itemMap.EnumCreate,
|
||||
...itemMap.TriggerDrop,
|
||||
...itemMap.IndexDrop,
|
||||
...itemMap.ConstraintDrop,
|
||||
...itemMap.TableCreate,
|
||||
...itemMap.ColumnAlter,
|
||||
...itemMap.ColumnAdd,
|
||||
...constraintAdds.filter(({ constraint }) => constraint.type === ConstraintType.PRIMARY_KEY),
|
||||
...constraintAdds.filter(({ constraint }) => constraint.type === ConstraintType.FOREIGN_KEY),
|
||||
...constraintAdds.filter(({ constraint }) => constraint.type === ConstraintType.UNIQUE),
|
||||
...constraintAdds.filter(({ constraint }) => constraint.type === ConstraintType.CHECK),
|
||||
...itemMap.IndexCreate,
|
||||
...itemMap.TriggerCreate,
|
||||
...itemMap.ColumnDrop,
|
||||
...itemMap.TableDrop,
|
||||
...itemMap.EnumDrop,
|
||||
...itemMap.FunctionDrop,
|
||||
];
|
||||
|
||||
return {
|
||||
items: orderedItems,
|
||||
asSql: (options?: SchemaDiffToSqlOptions) => schemaDiffToSql(orderedItems, options),
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert schema diffs into SQL statements
|
||||
*/
|
||||
export const schemaDiffToSql = (items: SchemaDiff[], options: SchemaDiffToSqlOptions = {}): string[] => {
|
||||
return items.flatMap((item) => asSql(item).map((result) => result + withComments(options.comments, item)));
|
||||
};
|
||||
|
||||
const asSql = (item: SchemaDiff): string[] => {
|
||||
for (const transform of transformers) {
|
||||
const result = transform(item);
|
||||
if (!result) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return asArray(result);
|
||||
}
|
||||
|
||||
throw new Error(`Unhandled schema diff type: ${item.type}`);
|
||||
};
|
||||
|
||||
const withComments = (comments: boolean | undefined, item: SchemaDiff): string => {
|
||||
if (!comments) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return ` -- ${item.reason}`;
|
||||
};
|
||||
|
||||
const asArray = <T>(items: T | T[]): T[] => {
|
||||
if (Array.isArray(items)) {
|
||||
return items;
|
||||
}
|
||||
|
||||
return [items];
|
||||
};
|
46
server/src/sql-tools/schema-from-code.spec.ts
Normal file
46
server/src/sql-tools/schema-from-code.spec.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { readdirSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { schemaFromCode } from 'src/sql-tools/schema-from-code';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe(schemaFromCode.name, () => {
|
||||
it('should work', () => {
|
||||
expect(schemaFromCode({ reset: true })).toEqual({
|
||||
databaseName: 'postgres',
|
||||
schemaName: 'public',
|
||||
functions: [],
|
||||
enums: [],
|
||||
extensions: [],
|
||||
parameters: [],
|
||||
tables: [],
|
||||
warnings: [],
|
||||
});
|
||||
});
|
||||
|
||||
describe('test files', () => {
|
||||
const errorStubs = readdirSync('test/sql-tools/errors', { withFileTypes: true });
|
||||
for (const file of errorStubs) {
|
||||
const filePath = join(file.parentPath, file.name);
|
||||
it(filePath, async () => {
|
||||
const module = await import(filePath);
|
||||
expect(module.message).toBeDefined();
|
||||
expect(() => schemaFromCode({ reset: true })).toThrowError(module.message);
|
||||
});
|
||||
}
|
||||
|
||||
const stubs = readdirSync('test/sql-tools', { withFileTypes: true });
|
||||
for (const file of stubs) {
|
||||
if (file.isDirectory()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const filePath = join(file.parentPath, file.name);
|
||||
it(filePath, async () => {
|
||||
const module = await import(filePath);
|
||||
expect(module.description).toBeDefined();
|
||||
expect(module.schema).toBeDefined();
|
||||
expect(schemaFromCode({ reset: true }), module.description).toEqual(module.schema);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
29
server/src/sql-tools/schema-from-code.ts
Normal file
29
server/src/sql-tools/schema-from-code.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { processors } from 'src/sql-tools/processors';
|
||||
import { getRegisteredItems, resetRegisteredItems } from 'src/sql-tools/register';
|
||||
import { SchemaBuilder } from 'src/sql-tools/schema-builder';
|
||||
import { SchemaFromCodeOptions } from 'src/sql-tools/types';
|
||||
|
||||
/**
|
||||
* Load schema from code (decorators, etc)
|
||||
*/
|
||||
export const schemaFromCode = (options: SchemaFromCodeOptions = {}) => {
|
||||
try {
|
||||
const globalOptions = {
|
||||
createForeignKeyIndexes: options.createForeignKeyIndexes ?? true,
|
||||
};
|
||||
|
||||
const builder = new SchemaBuilder(options);
|
||||
const items = getRegisteredItems();
|
||||
for (const processor of processors) {
|
||||
processor(builder, items, globalOptions);
|
||||
}
|
||||
|
||||
const newSchema = builder.build();
|
||||
|
||||
return newSchema;
|
||||
} finally {
|
||||
if (options.reset) {
|
||||
resetRegisteredItems();
|
||||
}
|
||||
}
|
||||
};
|
33
server/src/sql-tools/schema-from-database.ts
Normal file
33
server/src/sql-tools/schema-from-database.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { Kysely } from 'kysely';
|
||||
import { PostgresJSDialect } from 'kysely-postgres-js';
|
||||
import { Sql } from 'postgres';
|
||||
import { readers } from 'src/sql-tools/readers';
|
||||
import { DatabaseSchema, PostgresDB, SchemaFromDatabaseOptions } from 'src/sql-tools/types';
|
||||
|
||||
/**
|
||||
* Load schema from a database url
|
||||
*/
|
||||
export const schemaFromDatabase = async (
|
||||
postgres: Sql,
|
||||
options: SchemaFromDatabaseOptions = {},
|
||||
): Promise<DatabaseSchema> => {
|
||||
const schema: DatabaseSchema = {
|
||||
databaseName: 'immich',
|
||||
schemaName: options.schemaName || 'public',
|
||||
parameters: [],
|
||||
functions: [],
|
||||
enums: [],
|
||||
extensions: [],
|
||||
tables: [],
|
||||
warnings: [],
|
||||
};
|
||||
|
||||
const db = new Kysely<PostgresDB>({ dialect: new PostgresJSDialect({ postgres }) });
|
||||
for (const reader of readers) {
|
||||
await reader(schema, db);
|
||||
}
|
||||
|
||||
await db.destroy();
|
||||
|
||||
return schema;
|
||||
};
|
@ -1,21 +0,0 @@
|
||||
import { schemaDiffToSql } from 'src/sql-tools';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe(schemaDiffToSql.name, () => {
|
||||
describe('comments', () => {
|
||||
it('should include the reason in a SQL comment', () => {
|
||||
expect(
|
||||
schemaDiffToSql(
|
||||
[
|
||||
{
|
||||
type: 'index.drop',
|
||||
indexName: 'IDX_test',
|
||||
reason: 'unknown',
|
||||
},
|
||||
],
|
||||
{ comments: true },
|
||||
),
|
||||
).toEqual([`DROP INDEX "IDX_test"; -- unknown`]);
|
||||
});
|
||||
});
|
||||
});
|
@ -1,59 +0,0 @@
|
||||
import { transformColumns } from 'src/sql-tools/to-sql/transformers/column.transformer';
|
||||
import { transformConstraints } from 'src/sql-tools/to-sql/transformers/constraint.transformer';
|
||||
import { transformEnums } from 'src/sql-tools/to-sql/transformers/enum.transformer';
|
||||
import { transformExtensions } from 'src/sql-tools/to-sql/transformers/extension.transformer';
|
||||
import { transformFunctions } from 'src/sql-tools/to-sql/transformers/function.transformer';
|
||||
import { transformIndexes } from 'src/sql-tools/to-sql/transformers/index.transformer';
|
||||
import { transformParameters } from 'src/sql-tools/to-sql/transformers/parameter.transformer';
|
||||
import { transformTables } from 'src/sql-tools/to-sql/transformers/table.transformer';
|
||||
import { transformTriggers } from 'src/sql-tools/to-sql/transformers/trigger.transformer';
|
||||
import { SqlTransformer } from 'src/sql-tools/to-sql/transformers/types';
|
||||
import { SchemaDiff, SchemaDiffToSqlOptions } from 'src/sql-tools/types';
|
||||
|
||||
/**
|
||||
* Convert schema diffs into SQL statements
|
||||
*/
|
||||
export const schemaDiffToSql = (items: SchemaDiff[], options: SchemaDiffToSqlOptions = {}): string[] => {
|
||||
return items.flatMap((item) => asSql(item).map((result) => result + withComments(options.comments, item)));
|
||||
};
|
||||
|
||||
const transformers: SqlTransformer[] = [
|
||||
transformColumns,
|
||||
transformConstraints,
|
||||
transformEnums,
|
||||
transformExtensions,
|
||||
transformFunctions,
|
||||
transformIndexes,
|
||||
transformParameters,
|
||||
transformTables,
|
||||
transformTriggers,
|
||||
];
|
||||
|
||||
const asSql = (item: SchemaDiff): string[] => {
|
||||
for (const transform of transformers) {
|
||||
const result = transform(item);
|
||||
if (!result) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return asArray(result);
|
||||
}
|
||||
|
||||
throw new Error(`Unhandled schema diff type: ${item.type}`);
|
||||
};
|
||||
|
||||
const withComments = (comments: boolean | undefined, item: SchemaDiff): string => {
|
||||
if (!comments) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return ` -- ${item.reason}`;
|
||||
};
|
||||
|
||||
const asArray = <T>(items: T | T[]): T[] => {
|
||||
if (Array.isArray(items)) {
|
||||
return items;
|
||||
}
|
||||
|
||||
return [items];
|
||||
};
|
@ -1,12 +1,12 @@
|
||||
import { transformColumns } from 'src/sql-tools/to-sql/transformers/column.transformer';
|
||||
import { transformColumns } from 'src/sql-tools/transformers/column.transformer';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe(transformColumns.name, () => {
|
||||
describe('column.add', () => {
|
||||
describe('ColumnAdd', () => {
|
||||
it('should work', () => {
|
||||
expect(
|
||||
transformColumns({
|
||||
type: 'column.add',
|
||||
type: 'ColumnAdd',
|
||||
column: {
|
||||
name: 'column1',
|
||||
tableName: 'table1',
|
||||
@ -23,7 +23,7 @@ describe(transformColumns.name, () => {
|
||||
it('should add a nullable column', () => {
|
||||
expect(
|
||||
transformColumns({
|
||||
type: 'column.add',
|
||||
type: 'ColumnAdd',
|
||||
column: {
|
||||
name: 'column1',
|
||||
tableName: 'table1',
|
||||
@ -40,7 +40,7 @@ describe(transformColumns.name, () => {
|
||||
it('should add a column with an enum type', () => {
|
||||
expect(
|
||||
transformColumns({
|
||||
type: 'column.add',
|
||||
type: 'ColumnAdd',
|
||||
column: {
|
||||
name: 'column1',
|
||||
tableName: 'table1',
|
||||
@ -58,7 +58,7 @@ describe(transformColumns.name, () => {
|
||||
it('should add a column that is an array type', () => {
|
||||
expect(
|
||||
transformColumns({
|
||||
type: 'column.add',
|
||||
type: 'ColumnAdd',
|
||||
column: {
|
||||
name: 'column1',
|
||||
tableName: 'table1',
|
||||
@ -73,11 +73,11 @@ describe(transformColumns.name, () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('column.alter', () => {
|
||||
describe('ColumnAlter', () => {
|
||||
it('should make a column nullable', () => {
|
||||
expect(
|
||||
transformColumns({
|
||||
type: 'column.alter',
|
||||
type: 'ColumnAlter',
|
||||
tableName: 'table1',
|
||||
columnName: 'column1',
|
||||
changes: { nullable: true },
|
||||
@ -89,7 +89,7 @@ describe(transformColumns.name, () => {
|
||||
it('should make a column non-nullable', () => {
|
||||
expect(
|
||||
transformColumns({
|
||||
type: 'column.alter',
|
||||
type: 'ColumnAlter',
|
||||
tableName: 'table1',
|
||||
columnName: 'column1',
|
||||
changes: { nullable: false },
|
||||
@ -101,7 +101,7 @@ describe(transformColumns.name, () => {
|
||||
it('should update the default value', () => {
|
||||
expect(
|
||||
transformColumns({
|
||||
type: 'column.alter',
|
||||
type: 'ColumnAlter',
|
||||
tableName: 'table1',
|
||||
columnName: 'column1',
|
||||
changes: { default: 'uuid_generate_v4()' },
|
||||
@ -111,11 +111,11 @@ describe(transformColumns.name, () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('column.drop', () => {
|
||||
describe('ColumnDrop', () => {
|
||||
it('should work', () => {
|
||||
expect(
|
||||
transformColumns({
|
||||
type: 'column.drop',
|
||||
type: 'ColumnDrop',
|
||||
tableName: 'table1',
|
||||
columnName: 'column1',
|
||||
reason: 'unknown',
|
@ -1,18 +1,18 @@
|
||||
import { asColumnComment, getColumnModifiers, getColumnType } from 'src/sql-tools/helpers';
|
||||
import { SqlTransformer } from 'src/sql-tools/to-sql/transformers/types';
|
||||
import { SqlTransformer } from 'src/sql-tools/transformers/types';
|
||||
import { ColumnChanges, DatabaseColumn, SchemaDiff } from 'src/sql-tools/types';
|
||||
|
||||
export const transformColumns: SqlTransformer = (item: SchemaDiff) => {
|
||||
switch (item.type) {
|
||||
case 'column.add': {
|
||||
case 'ColumnAdd': {
|
||||
return asColumnAdd(item.column);
|
||||
}
|
||||
|
||||
case 'column.alter': {
|
||||
case 'ColumnAlter': {
|
||||
return asColumnAlter(item.tableName, item.columnName, item.changes);
|
||||
}
|
||||
|
||||
case 'column.drop': {
|
||||
case 'ColumnDrop': {
|
||||
return asColumnDrop(item.tableName, item.columnName);
|
||||
}
|
||||
|
@ -1,16 +1,16 @@
|
||||
import { transformConstraints } from 'src/sql-tools/to-sql/transformers/constraint.transformer';
|
||||
import { DatabaseConstraintType } from 'src/sql-tools/types';
|
||||
import { transformConstraints } from 'src/sql-tools/transformers/constraint.transformer';
|
||||
import { ConstraintType } from 'src/sql-tools/types';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe(transformConstraints.name, () => {
|
||||
describe('constraint.add', () => {
|
||||
describe('ConstraintAdd', () => {
|
||||
describe('primary keys', () => {
|
||||
it('should work', () => {
|
||||
expect(
|
||||
transformConstraints({
|
||||
type: 'constraint.add',
|
||||
type: 'ConstraintAdd',
|
||||
constraint: {
|
||||
type: DatabaseConstraintType.PRIMARY_KEY,
|
||||
type: ConstraintType.PRIMARY_KEY,
|
||||
name: 'PK_test',
|
||||
tableName: 'table1',
|
||||
columnNames: ['id'],
|
||||
@ -26,9 +26,9 @@ describe(transformConstraints.name, () => {
|
||||
it('should work', () => {
|
||||
expect(
|
||||
transformConstraints({
|
||||
type: 'constraint.add',
|
||||
type: 'ConstraintAdd',
|
||||
constraint: {
|
||||
type: DatabaseConstraintType.FOREIGN_KEY,
|
||||
type: ConstraintType.FOREIGN_KEY,
|
||||
name: 'FK_test',
|
||||
tableName: 'table1',
|
||||
columnNames: ['parentId'],
|
||||
@ -48,9 +48,9 @@ describe(transformConstraints.name, () => {
|
||||
it('should work', () => {
|
||||
expect(
|
||||
transformConstraints({
|
||||
type: 'constraint.add',
|
||||
type: 'ConstraintAdd',
|
||||
constraint: {
|
||||
type: DatabaseConstraintType.UNIQUE,
|
||||
type: ConstraintType.UNIQUE,
|
||||
name: 'UQ_test',
|
||||
tableName: 'table1',
|
||||
columnNames: ['id'],
|
||||
@ -66,9 +66,9 @@ describe(transformConstraints.name, () => {
|
||||
it('should work', () => {
|
||||
expect(
|
||||
transformConstraints({
|
||||
type: 'constraint.add',
|
||||
type: 'ConstraintAdd',
|
||||
constraint: {
|
||||
type: DatabaseConstraintType.CHECK,
|
||||
type: ConstraintType.CHECK,
|
||||
name: 'CHK_test',
|
||||
tableName: 'table1',
|
||||
expression: '"id" IS NOT NULL',
|
||||
@ -81,11 +81,11 @@ describe(transformConstraints.name, () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('constraint.drop', () => {
|
||||
describe('ConstraintDrop', () => {
|
||||
it('should work', () => {
|
||||
expect(
|
||||
transformConstraints({
|
||||
type: 'constraint.drop',
|
||||
type: 'ConstraintDrop',
|
||||
tableName: 'table1',
|
||||
constraintName: 'PK_test',
|
||||
reason: 'unknown',
|
@ -1,14 +1,14 @@
|
||||
import { asColumnList } from 'src/sql-tools/helpers';
|
||||
import { SqlTransformer } from 'src/sql-tools/to-sql/transformers/types';
|
||||
import { DatabaseActionType, DatabaseConstraint, DatabaseConstraintType, SchemaDiff } from 'src/sql-tools/types';
|
||||
import { SqlTransformer } from 'src/sql-tools/transformers/types';
|
||||
import { ActionType, ConstraintType, DatabaseConstraint, SchemaDiff } from 'src/sql-tools/types';
|
||||
|
||||
export const transformConstraints: SqlTransformer = (item: SchemaDiff) => {
|
||||
switch (item.type) {
|
||||
case 'constraint.add': {
|
||||
case 'ConstraintAdd': {
|
||||
return asConstraintAdd(item.constraint);
|
||||
}
|
||||
|
||||
case 'constraint.drop': {
|
||||
case 'ConstraintDrop': {
|
||||
return asConstraintDrop(item.tableName, item.constraintName);
|
||||
}
|
||||
default: {
|
||||
@ -17,18 +17,18 @@ export const transformConstraints: SqlTransformer = (item: SchemaDiff) => {
|
||||
}
|
||||
};
|
||||
|
||||
const withAction = (constraint: { onDelete?: DatabaseActionType; onUpdate?: DatabaseActionType }) =>
|
||||
` ON UPDATE ${constraint.onUpdate ?? DatabaseActionType.NO_ACTION} ON DELETE ${constraint.onDelete ?? DatabaseActionType.NO_ACTION}`;
|
||||
const withAction = (constraint: { onDelete?: ActionType; onUpdate?: ActionType }) =>
|
||||
` ON UPDATE ${constraint.onUpdate ?? ActionType.NO_ACTION} ON DELETE ${constraint.onDelete ?? ActionType.NO_ACTION}`;
|
||||
|
||||
export const asConstraintAdd = (constraint: DatabaseConstraint): string | string[] => {
|
||||
const base = `ALTER TABLE "${constraint.tableName}" ADD CONSTRAINT "${constraint.name}"`;
|
||||
switch (constraint.type) {
|
||||
case DatabaseConstraintType.PRIMARY_KEY: {
|
||||
case ConstraintType.PRIMARY_KEY: {
|
||||
const columnNames = asColumnList(constraint.columnNames);
|
||||
return `${base} PRIMARY KEY (${columnNames});`;
|
||||
}
|
||||
|
||||
case DatabaseConstraintType.FOREIGN_KEY: {
|
||||
case ConstraintType.FOREIGN_KEY: {
|
||||
const columnNames = asColumnList(constraint.columnNames);
|
||||
const referenceColumnNames = asColumnList(constraint.referenceColumnNames);
|
||||
return (
|
||||
@ -38,12 +38,12 @@ export const asConstraintAdd = (constraint: DatabaseConstraint): string | string
|
||||
);
|
||||
}
|
||||
|
||||
case DatabaseConstraintType.UNIQUE: {
|
||||
case ConstraintType.UNIQUE: {
|
||||
const columnNames = asColumnList(constraint.columnNames);
|
||||
return `${base} UNIQUE (${columnNames});`;
|
||||
}
|
||||
|
||||
case DatabaseConstraintType.CHECK: {
|
||||
case ConstraintType.CHECK: {
|
||||
return `${base} CHECK (${constraint.expression});`;
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
import { SqlTransformer } from 'src/sql-tools/to-sql/transformers/types';
|
||||
import { SqlTransformer } from 'src/sql-tools/transformers/types';
|
||||
import { DatabaseEnum, SchemaDiff } from 'src/sql-tools/types';
|
||||
|
||||
export const transformEnums: SqlTransformer = (item: SchemaDiff) => {
|
||||
switch (item.type) {
|
||||
case 'enum.create': {
|
||||
case 'EnumCreate': {
|
||||
return asEnumCreate(item.enum);
|
||||
}
|
||||
|
||||
case 'enum.drop': {
|
||||
case 'EnumDrop': {
|
||||
return asEnumDrop(item.enumName);
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
import { transformExtensions } from 'src/sql-tools/to-sql/transformers/extension.transformer';
|
||||
import { transformExtensions } from 'src/sql-tools/transformers/extension.transformer';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe(transformExtensions.name, () => {
|
||||
describe('extension.drop', () => {
|
||||
describe('ExtensionDrop', () => {
|
||||
it('should work', () => {
|
||||
expect(
|
||||
transformExtensions({
|
||||
type: 'extension.drop',
|
||||
type: 'ExtensionDrop',
|
||||
extensionName: 'cube',
|
||||
reason: 'unknown',
|
||||
}),
|
||||
@ -14,11 +14,11 @@ describe(transformExtensions.name, () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('extension.create', () => {
|
||||
describe('ExtensionCreate', () => {
|
||||
it('should work', () => {
|
||||
expect(
|
||||
transformExtensions({
|
||||
type: 'extension.create',
|
||||
type: 'ExtensionCreate',
|
||||
extension: {
|
||||
name: 'cube',
|
||||
synchronize: true,
|
@ -1,13 +1,13 @@
|
||||
import { SqlTransformer } from 'src/sql-tools/to-sql/transformers/types';
|
||||
import { SqlTransformer } from 'src/sql-tools/transformers/types';
|
||||
import { DatabaseExtension, SchemaDiff } from 'src/sql-tools/types';
|
||||
|
||||
export const transformExtensions: SqlTransformer = (item: SchemaDiff) => {
|
||||
switch (item.type) {
|
||||
case 'extension.create': {
|
||||
case 'ExtensionCreate': {
|
||||
return asExtensionCreate(item.extension);
|
||||
}
|
||||
|
||||
case 'extension.drop': {
|
||||
case 'ExtensionDrop': {
|
||||
return asExtensionDrop(item.extensionName);
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
import { transformFunctions } from 'src/sql-tools/to-sql/transformers/function.transformer';
|
||||
import { transformFunctions } from 'src/sql-tools/transformers/function.transformer';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe(transformFunctions.name, () => {
|
||||
describe('function.drop', () => {
|
||||
describe('FunctionDrop', () => {
|
||||
it('should work', () => {
|
||||
expect(
|
||||
transformFunctions({
|
||||
type: 'function.drop',
|
||||
type: 'FunctionDrop',
|
||||
functionName: 'test_func',
|
||||
reason: 'unknown',
|
||||
}),
|
@ -1,13 +1,13 @@
|
||||
import { SqlTransformer } from 'src/sql-tools/to-sql/transformers/types';
|
||||
import { SqlTransformer } from 'src/sql-tools/transformers/types';
|
||||
import { DatabaseFunction, SchemaDiff } from 'src/sql-tools/types';
|
||||
|
||||
export const transformFunctions: SqlTransformer = (item: SchemaDiff) => {
|
||||
switch (item.type) {
|
||||
case 'function.create': {
|
||||
case 'FunctionCreate': {
|
||||
return asFunctionCreate(item.function);
|
||||
}
|
||||
|
||||
case 'function.drop': {
|
||||
case 'FunctionDrop': {
|
||||
return asFunctionDrop(item.functionName);
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
import { transformIndexes } from 'src/sql-tools/to-sql/transformers/index.transformer';
|
||||
import { transformIndexes } from 'src/sql-tools/transformers/index.transformer';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe(transformIndexes.name, () => {
|
||||
describe('index.create', () => {
|
||||
describe('IndexCreate', () => {
|
||||
it('should work', () => {
|
||||
expect(
|
||||
transformIndexes({
|
||||
type: 'index.create',
|
||||
type: 'IndexCreate',
|
||||
index: {
|
||||
name: 'IDX_test',
|
||||
tableName: 'table1',
|
||||
@ -22,7 +22,7 @@ describe(transformIndexes.name, () => {
|
||||
it('should create an unique index', () => {
|
||||
expect(
|
||||
transformIndexes({
|
||||
type: 'index.create',
|
||||
type: 'IndexCreate',
|
||||
index: {
|
||||
name: 'IDX_test',
|
||||
tableName: 'table1',
|
||||
@ -38,7 +38,7 @@ describe(transformIndexes.name, () => {
|
||||
it('should create an index with a custom expression', () => {
|
||||
expect(
|
||||
transformIndexes({
|
||||
type: 'index.create',
|
||||
type: 'IndexCreate',
|
||||
index: {
|
||||
name: 'IDX_test',
|
||||
tableName: 'table1',
|
||||
@ -54,7 +54,7 @@ describe(transformIndexes.name, () => {
|
||||
it('should create an index with a where clause', () => {
|
||||
expect(
|
||||
transformIndexes({
|
||||
type: 'index.create',
|
||||
type: 'IndexCreate',
|
||||
index: {
|
||||
name: 'IDX_test',
|
||||
tableName: 'table1',
|
||||
@ -71,7 +71,7 @@ describe(transformIndexes.name, () => {
|
||||
it('should create an index with a custom expression', () => {
|
||||
expect(
|
||||
transformIndexes({
|
||||
type: 'index.create',
|
||||
type: 'IndexCreate',
|
||||
index: {
|
||||
name: 'IDX_test',
|
||||
tableName: 'table1',
|
||||
@ -86,11 +86,11 @@ describe(transformIndexes.name, () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('index.drop', () => {
|
||||
describe('IndexDrop', () => {
|
||||
it('should work', () => {
|
||||
expect(
|
||||
transformIndexes({
|
||||
type: 'index.drop',
|
||||
type: 'IndexDrop',
|
||||
indexName: 'IDX_test',
|
||||
reason: 'unknown',
|
||||
}),
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user