Skip to content

Commit

Permalink
drizzle kit cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodriguespn committed Oct 1, 2024
1 parent 56fd347 commit aacc5fe
Show file tree
Hide file tree
Showing 28 changed files with 1,317 additions and 9,129 deletions.
112 changes: 0 additions & 112 deletions drizzle-kit/src/cli/commands/introspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
} from '../../snapshotsDiffer';
import { prepareOutFolder } from '../../utils';
import type { Casing, Prefix } from '../validations/common';
import { LibSQLCredentials } from '../validations/libsql';
import type { MysqlCredentials } from '../validations/mysql';
import type { PostgresCredentials } from '../validations/postgres';
import { SingleStoreCredentials } from '../validations/singlestore';
Expand Down Expand Up @@ -470,117 +469,6 @@ export const introspectSqlite = async (
process.exit(0);
};

export const introspectLibSQL = async (
casing: Casing,
out: string,
breakpoints: boolean,
credentials: LibSQLCredentials,
tablesFilter: string[],
prefix: Prefix,
) => {
const { connectToLibSQL } = await import('../connections');
const db = await connectToLibSQL(credentials);

const matchers = tablesFilter.map((it) => {
return new Minimatch(it);
});

const filter = (tableName: string) => {
if (matchers.length === 0) return true;

let flags: boolean[] = [];

for (let matcher of matchers) {
if (matcher.negate) {
if (!matcher.match(tableName)) {
flags.push(false);
}
}

if (matcher.match(tableName)) {
flags.push(true);
}
}

if (flags.length > 0) {
return flags.every(Boolean);
}
return false;
};

const progress = new IntrospectProgress();
const res = await renderWithTask(
progress,
fromSqliteDatabase(db, filter, (stage, count, status) => {
progress.update(stage, count, status);
}),
);

const schema = { id: originUUID, prevId: '', ...res } as SQLiteSchema;
const ts = sqliteSchemaToTypeScript(schema, casing);
const relationsTs = relationsToTypeScript(schema, casing);

// check orm and orm-pg api version

const schemaFile = join(out, 'schema.ts');
writeFileSync(schemaFile, ts.file);
const relationsFile = join(out, 'relations.ts');
writeFileSync(relationsFile, relationsTs.file);
console.log();

const { snapshots, journal } = prepareOutFolder(out, 'sqlite');

if (snapshots.length === 0) {
const { sqlStatements, _meta } = await applySqliteSnapshotsDiff(
squashSqliteScheme(drySQLite),
squashSqliteScheme(schema),
tablesResolver,
columnsResolver,
drySQLite,
schema,
);

writeResult({
cur: schema,
sqlStatements,
journal,
_meta,
outFolder: out,
breakpoints,
type: 'introspect',
prefixMode: prefix,
});
} else {
render(
`[${
chalk.blue(
'i',
)
}] No SQL generated, you already have migrations in project`,
);
}

render(
`[${
chalk.green(
'✓',
)
}] You schema file is ready ➜ ${chalk.bold.underline.blue(schemaFile)} 🚀`,
);
render(
`[${
chalk.green(
'✓',
)
}] You relations file is ready ➜ ${
chalk.bold.underline.blue(
relationsFile,
)
} 🚀`,
);
process.exit(0);
};

const withCasing = (value: string, casing: Casing) => {
if (casing === 'preserve') {
return value;
Expand Down
93 changes: 1 addition & 92 deletions drizzle-kit/src/cli/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { MySqlSchema, mysqlSchema, squashMysqlScheme } from '../../serializer/my
import { PgSchema, pgSchema, squashPgScheme } from '../../serializer/pgSchema';
import { SQLiteSchema, sqliteSchema, squashSqliteScheme } from '../../serializer/sqliteSchema';
import {
applyLibSQLSnapshotsDiff,
applyMysqlSnapshotsDiff,
applyPgSnapshotsDiff,
applySingleStoreSnapshotsDiff,
Expand Down Expand Up @@ -397,7 +396,7 @@ export const prepareAndMigrateMysql = async (config: GenerateConfig) => {
};

// Not needed for now
function mySingleStoreSchemaSuggestions(
function singleStoreSchemaSuggestions(
curSchema: TypeOf<typeof singlestoreSchema>,
prevSchema: TypeOf<typeof singlestoreSchema>,
) {
Expand Down Expand Up @@ -599,65 +598,6 @@ export const prepareAndMigrateSqlite = async (config: GenerateConfig) => {
}
};

export const prepareAndMigrateLibSQL = async (config: GenerateConfig) => {
const outFolder = config.out;
const schemaPath = config.schema;

try {
assertV1OutFolder(outFolder);

const { snapshots, journal } = prepareMigrationFolder(outFolder, 'sqlite');
const { prev, cur, custom } = await prepareSqliteMigrationSnapshot(
snapshots,
schemaPath,
);

const validatedPrev = sqliteSchema.parse(prev);
const validatedCur = sqliteSchema.parse(cur);

if (config.custom) {
writeResult({
cur: custom,
sqlStatements: [],
journal,
outFolder,
name: config.name,
breakpoints: config.breakpoints,
bundle: config.bundle,
type: 'custom',
prefixMode: config.prefix,
});
return;
}

const squashedPrev = squashSqliteScheme(validatedPrev);
const squashedCur = squashSqliteScheme(validatedCur);

const { sqlStatements, _meta } = await applyLibSQLSnapshotsDiff(
squashedPrev,
squashedCur,
tablesResolver,
columnsResolver,
validatedPrev,
validatedCur,
);

writeResult({
cur,
sqlStatements,
journal,
_meta,
outFolder,
name: config.name,
breakpoints: config.breakpoints,
bundle: config.bundle,
prefixMode: config.prefix,
});
} catch (e) {
console.error(e);
}
};

export const prepareSQLitePush = async (
schemaPath: string | string[],
snapshot: SQLiteSchema,
Expand Down Expand Up @@ -689,37 +629,6 @@ export const prepareSQLitePush = async (
};
};

export const prepareLibSQLPush = async (
schemaPath: string | string[],
snapshot: SQLiteSchema,
) => {
const { prev, cur } = await prepareSQLiteDbPushSnapshot(snapshot, schemaPath);

const validatedPrev = sqliteSchema.parse(prev);
const validatedCur = sqliteSchema.parse(cur);

const squashedPrev = squashSqliteScheme(validatedPrev, 'push');
const squashedCur = squashSqliteScheme(validatedCur, 'push');

const { sqlStatements, statements, _meta } = await applyLibSQLSnapshotsDiff(
squashedPrev,
squashedCur,
tablesResolver,
columnsResolver,
validatedPrev,
validatedCur,
'push',
);

return {
sqlStatements,
statements,
squashedPrev,
squashedCur,
meta: _meta,
};
};

const freeeeeeze = (obj: any) => {
Object.freeze(obj);
for (let key in obj) {
Expand Down
Loading

0 comments on commit aacc5fe

Please sign in to comment.