Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@contember/schema-migrations

Package Overview
Dependencies
Maintainers
5
Versions
263
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@contember/schema-migrations - npm Package Compare versions

Comparing version 0.10.2 to 0.11.0-alpha.0

9

dist/src/MigrationCreator.d.ts
import { MigrationFilesManager } from './MigrationFilesManager';
import { Schema } from '@contember/schema';
import { SchemaVersionBuilder } from './SchemaVersionBuilder';
import { SchemaDiffer } from './SchemaDiffer';

@@ -8,11 +7,11 @@ import { Migration } from './Migration';

private readonly migrationFilesManager;
private readonly schemaVersionBuilder;
private readonly schemaDiffer;
constructor(migrationFilesManager: MigrationFilesManager, schemaVersionBuilder: SchemaVersionBuilder, schemaDiffer: SchemaDiffer);
constructor(migrationFilesManager: MigrationFilesManager, schemaDiffer: SchemaDiffer);
createEmpty(migrationName: string): Promise<string>;
prepareDiff(newSchema: Schema, migrationName: string): Promise<{
prepareMigration(initialSchema: Schema, newSchema: Schema, migrationName: string): Promise<{
migration: Migration;
initialSchema: Schema;
} | null>;
saveDiff(migration: Migration): Promise<string>;
saveMigration(migration: Migration): Promise<string>;
removeMigration(name: string): Promise<void>;
private createMigration;

@@ -19,0 +18,0 @@ static createContent({ modifications, formatVersion }: Migration): string;

@@ -7,5 +7,4 @@ "use strict";

class MigrationCreator {
constructor(migrationFilesManager, schemaVersionBuilder, schemaDiffer) {
constructor(migrationFilesManager, schemaDiffer) {
this.migrationFilesManager = migrationFilesManager;
this.schemaVersionBuilder = schemaVersionBuilder;
this.schemaDiffer = schemaDiffer;

@@ -17,7 +16,6 @@ }

const jsonDiff = MigrationCreator.createContent(migration);
return await this.migrationFilesManager.createFile(jsonDiff, migration.version, 'json');
return await this.migrationFilesManager.createFile(jsonDiff, migration.version);
}
async prepareDiff(newSchema, migrationName) {
async prepareMigration(initialSchema, newSchema, migrationName) {
await this.migrationFilesManager.createDirIfNotExist();
const initialSchema = await this.schemaVersionBuilder.buildSchema();
const modifications = this.schemaDiffer.diffSchemas(initialSchema, newSchema);

@@ -30,10 +28,13 @@ if (modifications.length === 0) {

}
async saveDiff(migration) {
async saveMigration(migration) {
const jsonDiff = MigrationCreator.createContent(migration);
const filename = await this.migrationFilesManager.createFile(jsonDiff, migration.version, 'json');
const filename = await this.migrationFilesManager.createFile(jsonDiff, migration.name);
return filename;
}
async removeMigration(name) {
await this.migrationFilesManager.removeFile(name);
}
createMigration(modifications, name) {
const version = MigrationVersionHelper_1.MigrationVersionHelper.createVersion(name);
return { formatVersion: ModificationVersions_1.VERSION_LATEST, modifications, version, name };
const [version, fullName] = MigrationVersionHelper_1.MigrationVersionHelper.createVersion(name);
return { formatVersion: ModificationVersions_1.VERSION_LATEST, modifications, version, name: fullName };
}

@@ -40,0 +41,0 @@ static createContent({ modifications, formatVersion }) {

declare class MigrationFilesManager {
readonly directory: string;
constructor(directory: string);
createFile(content: string, version: string, extension: string): Promise<string>;
createFile(content: string, name: string): Promise<string>;
removeFile(name: string): Promise<void>;
moveFile(oldName: string, newName: string): Promise<void>;
createDirIfNotExist(): Promise<void>;
listFiles(extension: string): Promise<string[]>;
listFiles(): Promise<string[]>;
private tryReadDir;
readFiles(extension: string, predicate?: (version: string) => boolean): Promise<MigrationFilesManager.MigrationFile[]>;
readFiles(predicate?: (version: string) => boolean): Promise<MigrationFilesManager.MigrationFile[]>;
static createForProject(projectsDirectory: string, projectSlug: string): MigrationFilesManager;
private formatPath;
}

@@ -11,0 +14,0 @@ declare namespace MigrationFilesManager {

@@ -29,2 +29,3 @@ "use strict";

const fsWrite = util_1.promisify(fs.writeFile);
const fsRemove = util_1.promisify(fs.unlink);
const fsRealpath = util_1.promisify(fs.realpath);

@@ -34,2 +35,3 @@ const mkdir = util_1.promisify(fs.mkdir);

const readDir = util_1.promisify(fs.readdir);
const mvFile = util_1.promisify(fs.rename);
class MigrationFilesManager {

@@ -39,8 +41,14 @@ constructor(directory) {

}
async createFile(content, version, extension) {
const filename = `${version}.${extension}`;
const path = `${this.directory}/${filename}`;
async createFile(content, name) {
const path = this.formatPath(name);
await fsWrite(path, content, { encoding: 'utf8' });
return await fsRealpath(path);
}
async removeFile(name) {
const path = this.formatPath(name);
await fsRemove(path);
}
async moveFile(oldName, newName) {
await mvFile(this.formatPath(oldName), this.formatPath(newName));
}
async createDirIfNotExist() {

@@ -56,6 +64,6 @@ try {

}
async listFiles(extension) {
async listFiles() {
const files = await this.tryReadDir();
const filteredFiles = await Promise.all(files
.filter(file => file.endsWith(`.${extension}`))
.filter(file => file.endsWith(`.json`))
.filter(async (file) => {

@@ -77,4 +85,4 @@ return (await lstatFile(`${this.directory}/${file}`)).isFile();

}
async readFiles(extension, predicate) {
let files = await this.listFiles(extension);
async readFiles(predicate) {
let files = await this.listFiles();
if (predicate) {

@@ -94,4 +102,9 @@ files = files.filter(filename => predicate(MigrationVersionHelper_1.MigrationVersionHelper.extractVersion(filename)));

}
formatPath(version) {
const filename = `${version}.json`;
const path = `${this.directory}/${filename}`;
return path;
}
}
exports.MigrationFilesManager = MigrationFilesManager;
//# sourceMappingURL=MigrationFilesManager.js.map

@@ -14,3 +14,3 @@ "use strict";

async getMigrations() {
return (await this.migrationFilesManager.readFiles('json')).map(({ filename, content }) => {
return (await this.migrationFilesManager.readFiles()).map(({ filename, content }) => {
const parsed = JSON.parse(content);

@@ -17,0 +17,0 @@ return {

export declare class MigrationVersionHelper {
static prefixLength: number;
static extractVersion(filename: string): string;
static createVersion(name: string): string;
static createVersion(name: string, increment?: number): [string, string];
static extractName(filename: string): string;

@@ -6,0 +6,0 @@ private static createTimePrefix;

@@ -8,5 +8,6 @@ "use strict";

}
static createVersion(name) {
name = MigrationVersionHelper.normalizeMigrationLabel(name);
return `${MigrationVersionHelper.createTimePrefix()}-${name}`;
static createVersion(name, increment = 0) {
const normalizedName = MigrationVersionHelper.normalizeMigrationLabel(name);
const version = MigrationVersionHelper.createTimePrefix(increment);
return [version, `${version}-${normalizedName}`];
}

@@ -16,4 +17,5 @@ static extractName(filename) {

}
static createTimePrefix() {
static createTimePrefix(increment) {
const now = new Date();
now.setSeconds(now.getSeconds() + increment);
const year = now.getFullYear();

@@ -20,0 +22,0 @@ const month = (now.getMonth() + 1).toString().padStart(2, '0');

@@ -11,4 +11,4 @@ import { Schema } from '@contember/schema';

continue(schema: Schema, previousVersion: string | null, targetVersion: string): Promise<Schema>;
private doBuild;
buildSchemaAdvanced(initialSchema: Schema, condition: (version: string) => boolean): Promise<Schema>;
}
//# sourceMappingURL=SchemaVersionBuilder.d.ts.map

@@ -11,11 +11,11 @@ "use strict";

async buildSchema(targetVersion) {
return this.doBuild(schema_utils_1.emptySchema, version => !targetVersion || version <= targetVersion);
return this.buildSchemaAdvanced(schema_utils_1.emptySchema, version => !targetVersion || version <= targetVersion);
}
async buildSchemaUntil(targetVersion) {
return this.doBuild(schema_utils_1.emptySchema, version => version < targetVersion);
return this.buildSchemaAdvanced(schema_utils_1.emptySchema, version => version < targetVersion);
}
async continue(schema, previousVersion, targetVersion) {
return this.doBuild(schema, version => version <= targetVersion && version > (previousVersion || ''));
return this.buildSchemaAdvanced(schema, version => version <= targetVersion && version > (previousVersion || ''));
}
async doBuild(initialSchema, condition) {
async buildSchemaAdvanced(initialSchema, condition) {
return (await this.migrationsResolver.getMigrations())

@@ -22,0 +22,0 @@ .filter(({ version }) => condition(version))

{
"name": "@contember/schema-migrations",
"version": "0.10.2",
"version": "0.11.0-alpha.0",
"license": "Apache-2.0",

@@ -11,6 +11,6 @@ "main": "dist/src/index.js",

"dependencies": {
"@contember/database-migrations": "^0.10.2",
"@contember/engine-common": "^0.10.2",
"@contember/schema": "^0.10.2",
"@contember/schema-utils": "^0.10.2",
"@contember/database-migrations": "^0.11.0-alpha.0",
"@contember/engine-common": "^0.11.0-alpha.0",
"@contember/schema": "^0.11.0-alpha.0",
"@contember/schema-utils": "^0.11.0-alpha.0",
"fast-deep-equal": "^3.1.3",

@@ -20,3 +20,3 @@ "rfc6902": "^4.0.1"

"devDependencies": {
"@contember/schema-definition": "^0.10.2",
"@contember/schema-definition": "^0.11.0-alpha.0",
"uvu": "^0.4.1"

@@ -23,0 +23,0 @@ },

@@ -12,3 +12,2 @@ import { MigrationFilesManager } from './MigrationFilesManager'

private readonly migrationFilesManager: MigrationFilesManager,
private readonly schemaVersionBuilder: SchemaVersionBuilder,
private readonly schemaDiffer: SchemaDiffer,

@@ -22,6 +21,7 @@ ) {}

return await this.migrationFilesManager.createFile(jsonDiff, migration.version, 'json')
return await this.migrationFilesManager.createFile(jsonDiff, migration.version)
}
async prepareDiff(
async prepareMigration(
initialSchema: Schema,
newSchema: Schema,

@@ -32,4 +32,2 @@ migrationName: string,

const initialSchema = await this.schemaVersionBuilder.buildSchema()
const modifications = this.schemaDiffer.diffSchemas(initialSchema, newSchema)

@@ -45,11 +43,15 @@ if (modifications.length === 0) {

async saveDiff(migration: Migration): Promise<string> {
async saveMigration(migration: Migration): Promise<string> {
const jsonDiff = MigrationCreator.createContent(migration)
const filename = await this.migrationFilesManager.createFile(jsonDiff, migration.version, 'json')
const filename = await this.migrationFilesManager.createFile(jsonDiff, migration.name)
return filename
}
async removeMigration(name: string): Promise<void> {
await this.migrationFilesManager.removeFile(name)
}
private createMigration(modifications: Migration.Modification[], name: string): Migration {
const version = MigrationVersionHelper.createVersion(name)
return { formatVersion: VERSION_LATEST, modifications, version, name }
const [version, fullName] = MigrationVersionHelper.createVersion(name)
return { formatVersion: VERSION_LATEST, modifications, version, name: fullName }
}

@@ -56,0 +58,0 @@

@@ -8,2 +8,3 @@ import { MigrationVersionHelper } from './MigrationVersionHelper'

const fsWrite = promisify(fs.writeFile)
const fsRemove = promisify(fs.unlink)
const fsRealpath = promisify(fs.realpath)

@@ -13,2 +14,3 @@ const mkdir = promisify(fs.mkdir)

const readDir = promisify(fs.readdir)
const mvFile = promisify(fs.rename)

@@ -18,5 +20,4 @@ class MigrationFilesManager {

public async createFile(content: string, version: string, extension: string): Promise<string> {
const filename = `${version}.${extension}`
const path = `${this.directory}/${filename}`
public async createFile(content: string, name: string): Promise<string> {
const path = this.formatPath(name)
await fsWrite(path, content, { encoding: 'utf8' })

@@ -26,2 +27,11 @@ return await fsRealpath(path)

public async removeFile(name: string) {
const path = this.formatPath(name)
await fsRemove(path)
}
public async moveFile(oldName: string, newName: string) {
await mvFile(this.formatPath(oldName), this.formatPath(newName))
}
public async createDirIfNotExist(): Promise<void> {

@@ -37,3 +47,3 @@ try {

public async listFiles(extension: string): Promise<string[]> {
public async listFiles(): Promise<string[]> {
const files: string[] = await this.tryReadDir()

@@ -43,3 +53,3 @@

files
.filter(file => file.endsWith(`.${extension}`))
.filter(file => file.endsWith(`.json`))
.filter(async file => {

@@ -63,7 +73,4 @@ return (await lstatFile(`${this.directory}/${file}`)).isFile()

public async readFiles(
extension: string,
predicate?: (version: string) => boolean,
): Promise<MigrationFilesManager.MigrationFile[]> {
let files = await this.listFiles(extension)
public async readFiles(predicate?: (version: string) => boolean): Promise<MigrationFilesManager.MigrationFile[]> {
let files = await this.listFiles()
if (predicate) {

@@ -85,2 +92,8 @@ files = files.filter(filename => predicate(MigrationVersionHelper.extractVersion(filename)))

}
private formatPath(version: string) {
const filename = `${version}.json`
const path = `${this.directory}/${filename}`
return path
}
}

@@ -87,0 +100,0 @@

@@ -14,3 +14,3 @@ import { MigrationFilesManager } from './MigrationFilesManager'

public async getMigrations(): Promise<Migration[]> {
return (await this.migrationFilesManager.readFiles('json')).map(({ filename, content }) => {
return (await this.migrationFilesManager.readFiles()).map(({ filename, content }) => {
const parsed = JSON.parse(content)

@@ -17,0 +17,0 @@ return {

@@ -8,5 +8,6 @@ export class MigrationVersionHelper {

public static createVersion(name: string): string {
name = MigrationVersionHelper.normalizeMigrationLabel(name)
return `${MigrationVersionHelper.createTimePrefix()}-${name}`
public static createVersion(name: string, increment: number = 0): [string, string] {
const normalizedName = MigrationVersionHelper.normalizeMigrationLabel(name)
const version = MigrationVersionHelper.createTimePrefix(increment)
return [version, `${version}-${normalizedName}`]
}

@@ -18,4 +19,5 @@

private static createTimePrefix(): string {
private static createTimePrefix(increment: number): string {
const now = new Date()
now.setSeconds(now.getSeconds() + increment)
const year = now.getFullYear()

@@ -22,0 +24,0 @@ const month = (now.getMonth() + 1).toString().padStart(2, '0')

@@ -13,14 +13,14 @@ import { Schema } from '@contember/schema'

async buildSchema(targetVersion?: string): Promise<Schema> {
return this.doBuild(emptySchema, version => !targetVersion || version <= targetVersion)
return this.buildSchemaAdvanced(emptySchema, version => !targetVersion || version <= targetVersion)
}
async buildSchemaUntil(targetVersion: string): Promise<Schema> {
return this.doBuild(emptySchema, version => version < targetVersion)
return this.buildSchemaAdvanced(emptySchema, version => version < targetVersion)
}
async continue(schema: Schema, previousVersion: string | null, targetVersion: string): Promise<Schema> {
return this.doBuild(schema, version => version <= targetVersion && version > (previousVersion || ''))
return this.buildSchemaAdvanced(schema, version => version <= targetVersion && version > (previousVersion || ''))
}
private async doBuild(initialSchema: Schema, condition: (version: string) => boolean): Promise<Schema> {
public async buildSchemaAdvanced(initialSchema: Schema, condition: (version: string) => boolean): Promise<Schema> {
return (await this.migrationsResolver.getMigrations())

@@ -27,0 +27,0 @@ .filter(({ version }) => condition(version))

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc