feat(medusa): Remove sqlite support (#4026)

This commit is contained in:
Oliver Windall Juhl
2023-05-17 12:13:36 +02:00
committed by GitHub
parent e2d29d35c4
commit a91987fab3
17 changed files with 148 additions and 415 deletions
@@ -363,15 +363,8 @@ export const newStarter = async (args) => {
}
const medusaConfig = getMedusaConfig(rootPath)
if (medusaConfig) {
let isPostgres = false
if (medusaConfig.projectConfig) {
const databaseType = medusaConfig.projectConfig.database_type
isPostgres = databaseType === "postgres"
}
if (!isPostgres && seed) {
await attemptSeed(rootPath)
}
if (medusaConfig && seed) {
await attemptSeed(rootPath)
}
}
-1
View File
@@ -58,7 +58,6 @@
"regenerator-runtime": "^0.13.11",
"resolve-cwd": "^3.0.0",
"semver": "^7.3.8",
"sqlite3": "^5.0.2",
"stack-trace": "^0.0.10",
"ulid": "^2.3.0",
"url": "^0.11.0",
+24 -28
View File
@@ -19,7 +19,7 @@ import inquirer from "inquirer"
import reporter from "../reporter"
import { getPackageManager, setPackageManager } from "../util/package-manager"
const removeUndefined = obj => {
const removeUndefined = (obj) => {
return Object.fromEntries(
Object.entries(obj)
.filter(([_, v]) => v != null)
@@ -50,21 +50,21 @@ const isAlreadyGitRepository = async () => {
try {
return await spawn(`git rev-parse --is-inside-work-tree`, {
stdio: `pipe`,
}).then(output => output.stdout === `true`)
}).then((output) => output.stdout === `true`)
} catch (err) {
return false
}
}
// Initialize newly cloned directory as a git repo
const gitInit = async rootPath => {
const gitInit = async (rootPath) => {
reporter.info(`Initialising git in ${rootPath}`)
return await spawn(`git init`, { cwd: rootPath })
}
// Create a .gitignore file if it is missing in the new directory
const maybeCreateGitIgnore = async rootPath => {
const maybeCreateGitIgnore = async (rootPath) => {
if (existsSync(sysPath.join(rootPath, `.gitignore`))) {
return
}
@@ -98,7 +98,7 @@ const createInitialGitCommit = async (rootPath, starterUrl) => {
}
// Executes `npm install` or `yarn install` in rootPath.
const install = async rootPath => {
const install = async (rootPath) => {
const prevDir = process.cwd()
reporter.info(`Installing packages...`)
@@ -128,7 +128,7 @@ const install = async rootPath => {
}
}
const ignored = path => !/^\.(git|hg)$/.test(sysPath.basename(path))
const ignored = (path) => !/^\.(git|hg)$/.test(sysPath.basename(path))
// Copy starter from file system.
const copy = async (starterPath, rootPath) => {
@@ -187,13 +187,13 @@ const clone = async (hostInfo, rootPath) => {
rootPath,
`--recursive`,
`--depth=1`,
].filter(arg => Boolean(arg))
].filter((arg) => Boolean(arg))
await execa(`git`, args, {})
.then(() => {
reporter.success(createAct, `Created starter directory layout`)
})
.catch(err => {
.catch((err) => {
reporter.failure(createAct, `Failed to clone repository`)
throw err
})
@@ -207,7 +207,7 @@ const clone = async (hostInfo, rootPath) => {
if (!isGit) await createInitialGitCommit(rootPath, url)
}
const getMedusaConfig = rootPath => {
const getMedusaConfig = (rootPath) => {
try {
const configPath = sysPath.join(rootPath, "medusa-config.js")
if (existsSync(configPath)) {
@@ -268,7 +268,7 @@ const getPaths = async (starterPath, rootPath) => {
return { starterPath, rootPath, selectedOtherStarter }
}
const successMessage = path => {
const successMessage = (path) => {
reporter.info(`Your new Medusa project is ready for you! To start developing run:
cd ${path}
@@ -284,7 +284,7 @@ const defaultDBCreds = {
host: "localhost",
}
const verifyPgCreds = async creds => {
const verifyPgCreds = async (creds) => {
const pool = new Pool(creds)
return new Promise((resolve, reject) => {
pool.query("SELECT NOW()", (err, res) => {
@@ -361,7 +361,7 @@ Do you wish to continue with these credentials?
message: `DB database`,
},
])
.then(async answers => {
.then(async (answers) => {
const collectedCreds = Object.assign({}, credentials, {
user: answers.user,
password: answers.password,
@@ -372,14 +372,14 @@ Do you wish to continue with these credentials?
switch (answers.continueWithDefault) {
case "Continue": {
const done = await verifyPgCreds(credentials).catch(_ => false)
const done = await verifyPgCreds(credentials).catch((_) => false)
if (done) {
return credentials
}
return false
}
case "Change credentials": {
const done = await verifyPgCreds(collectedCreds).catch(_ => false)
const done = await verifyPgCreds(collectedCreds).catch((_) => false)
if (done) {
return collectedCreds
}
@@ -412,7 +412,7 @@ const setupDB = async (dbName, dbCreds = {}) => {
.then(() => {
reporter.success(dbActivity, `Created database "${dbName}"`)
})
.catch(err => {
.catch((err) => {
if (err.name === "PDG_ERR::DuplicateDatabase") {
reporter.success(
dbActivity,
@@ -456,7 +456,7 @@ const setupEnvVars = async (
}
}
const runMigrations = async rootPath => {
const runMigrations = async (rootPath) => {
const migrationActivity = reporter.activity("Applying database migrations...")
const cliPath = sysPath.join(
@@ -472,7 +472,7 @@ const runMigrations = async rootPath => {
.then(() => {
reporter.success(migrationActivity, "Database migrations completed.")
})
.catch(err => {
.catch((err) => {
reporter.failure(
migrationActivity,
"Failed to migrate database you must complete migration manually before starting your server."
@@ -481,7 +481,7 @@ const runMigrations = async rootPath => {
})
}
const attemptSeed = async rootPath => {
const attemptSeed = async (rootPath) => {
const seedActivity = reporter.activity("Seeding database")
const pkgPath = sysPath.resolve(rootPath, "package.json")
@@ -499,7 +499,7 @@ const attemptSeed = async rootPath => {
.then(() => {
reporter.success(seedActivity, "Seed completed")
})
.catch(err => {
.catch((err) => {
reporter.failure(seedActivity, "Failed to complete seed; skipping")
console.error(err)
})
@@ -517,7 +517,7 @@ const attemptSeed = async rootPath => {
/**
* Main function that clones or copies the starter.
*/
export const newStarter = async args => {
export const newStarter = async (args) => {
track("CLI_NEW")
const {
@@ -614,33 +614,29 @@ medusa new ${rootPath} [url-to-starter]
const medusaConfig = getMedusaConfig(rootPath)
let isPostgres = false
if (medusaConfig && medusaConfig.projectConfig) {
const databaseType = medusaConfig.projectConfig.database_type
isPostgres = databaseType === "postgres"
}
track("CLI_NEW_LAYOUT_COMPLETED")
let creds = dbCredentials
if (isPostgres && !useDefaults && !skipDb && !skipEnv) {
if (!useDefaults && !skipDb && !skipEnv) {
creds = await interactiveDbCreds(rootPath, dbCredentials)
}
if (creds === null) {
reporter.info("Skipping automatic database setup")
} else {
if (!skipDb && isPostgres) {
if (!skipDb) {
track("CLI_NEW_SETUP_DB")
await setupDB(rootPath, creds)
}
if (!skipEnv) {
track("CLI_NEW_SETUP_ENV")
await setupEnvVars(rootPath, rootPath, creds, isPostgres)
await setupEnvVars(rootPath, rootPath, creds)
}
if (!skipMigrations && isPostgres) {
if (!skipMigrations) {
track("CLI_NEW_RUN_MIGRATIONS")
await runMigrations(rootPath)
}
@@ -45,21 +45,6 @@ Manage the content of your storefront with rich Content Management System (CMS)
DATABASE_URL=<YOUR_DB_URL>
```
3\. In `medusa-config.js`, enable PostgreSQL and remove the SQLite configurations:
```js
module.exports = {
projectConfig: {
// ...
database_url: DATABASE_URL,
database_type: "postgres",
// REMOVE OR COMMENT OUT THE BELOW:
// database_database: "./medusa-db.sql",
// database_type: "sqlite",
},
}
```
4\. Migrate the content types into Contentful with the following command:
```bash
+29 -32
View File
@@ -58,8 +58,7 @@ const seed = async function ({ directory, migrate, seedFile }: SeedOptions) {
const featureFlagRouter = featureFlagLoader(configModule)
const dbType = configModule.projectConfig.database_type
if (migrate && dbType !== "sqlite") {
if (migrate) {
const { coreMigrations } = getMigrations(directory, featureFlagRouter)
const { migrations: moduleMigrations } = getModuleSharedResources(
@@ -68,7 +67,7 @@ const seed = async function ({ directory, migrate, seedFile }: SeedOptions) {
)
const connectionOptions = {
type: configModule.projectConfig.database_type,
type: "postgres",
database: configModule.projectConfig.database_database,
schema: configModule.projectConfig.database_schema,
url: configModule.projectConfig.database_url,
@@ -171,6 +170,33 @@ const seed = async function ({ directory, migrate, seedFile }: SeedOptions) {
await shippingOptionService.withTransaction(tx).create(so)
}
const createProductCategory = async (
parameters,
parentCategoryId: string | null = null
) => {
// default to the categories being visible and public
parameters.is_active = parameters.is_active || true
parameters.is_internal = parameters.is_internal || false
parameters.parent_category_id = parentCategoryId
const categoryChildren = parameters.category_children || []
delete parameters.category_children
const category = await productCategoryService
.withTransaction(tx)
.create(parameters as CreateProductCategoryInput)
if (categoryChildren.length) {
for (const categoryChild of categoryChildren) {
await createProductCategory(categoryChild, category.id)
}
}
}
for (const c of categories) {
await createProductCategory(c)
}
for (const p of products) {
const variants = p.variants
delete p.variants
@@ -209,35 +235,6 @@ const seed = async function ({ directory, migrate, seedFile }: SeedOptions) {
}
}
}
const createProductCategory = async (
parameters,
parentCategoryId: string | null = null
) => {
// default to the categories being visible and public
parameters.is_active = parameters.is_active || true
parameters.is_internal = parameters.is_internal || false
parameters.parent_category_id = parentCategoryId
const categoryChildren = parameters.category_children || []
delete parameters.category_children
const category = await productCategoryService
.withTransaction(tx)
.create(parameters as CreateProductCategoryInput)
if (categoryChildren.length) {
for (const categoryChild of categoryChildren) {
await createProductCategory(categoryChild, category.id)
}
}
}
if (dbType !== "sqlite") {
for (const c of categories) {
await createProductCategory(c, null)
}
}
})
track("CLI_SEED_COMPLETED")
-6
View File
@@ -58,12 +58,6 @@ export default (rootDirectory: string): ConfigModule => {
)
}
if (!configModule?.projectConfig?.database_type) {
console.log(
`[medusa-config] ⚠️ database_type not found. fallback to default sqlite.`
)
}
return {
projectConfig: {
jwt_secret: jwt_secret ?? "supersecret",
+27 -8
View File
@@ -39,10 +39,8 @@ export default async ({
}: Options): Promise<DataSource> => {
const entities = container.resolve("db_entities")
const isSqlite = configModule.projectConfig.database_type === "sqlite"
dataSource = new DataSource({
type: configModule.projectConfig.database_type,
type: "postgres",
url: configModule.projectConfig.database_url,
database: configModule.projectConfig.database_database,
extra: configModule.projectConfig.database_extra || {},
@@ -54,12 +52,33 @@ export default async ({
(configModule.projectConfig.database_logging || false),
} as DataSourceOptions)
await dataSource.initialize()
try {
await dataSource.initialize()
} catch (err) {
// database name does not exist
if (err.code === "3D000") {
throw new Error(
`Specified database does not exist. Please create it and try again.\n${err.message}`
)
}
if (isSqlite) {
await dataSource.query(`PRAGMA foreign_keys = OFF`)
await dataSource.synchronize()
await dataSource.query(`PRAGMA foreign_keys = ON`)
throw err
}
// If migrations are not included in the config, we assume you are attempting to start the server
// Therefore, throw if the database is not migrated
if (!dataSource.migrations?.length) {
try {
await dataSource.query(`select * from migrations`)
} catch (err) {
if (err.code === "42P01") {
throw new Error(
`Migrations missing. Please run 'medusa migrations run' and try again.`
)
}
throw err
}
}
return dataSource
+4 -5
View File
@@ -9,15 +9,14 @@ import {
} from "typeorm"
import {
DbAwareColumn,
resolveDbGenerationStrategy,
resolveDbType,
resolveDbType
} from "../utils/db-aware-column"
import { BaseEntity } from "../interfaces/models/base-entity"
import { Cart } from "./cart"
import { Order } from "./order"
import { generateEntityId } from "../utils/generate-entity-id"
import { manualAutoIncrement } from "../utils/manual-auto-increment"
import { Cart } from "./cart"
import { Order } from "./order"
export enum DraftOrderStatus {
OPEN = "open",
@@ -31,7 +30,7 @@ export class DraftOrder extends BaseEntity {
@Index()
@Column()
@Generated(resolveDbGenerationStrategy("increment"))
@Generated("increment")
display_id: number
@Index()
+2 -2
View File
@@ -11,7 +11,7 @@ import {
OneToMany,
OneToOne,
} from "typeorm"
import { DbAwareColumn, resolveDbGenerationStrategy, resolveDbType, } from "../utils/db-aware-column"
import { DbAwareColumn, resolveDbType } from "../utils/db-aware-column"
import { FeatureFlagColumn, FeatureFlagDecorators, } from "../utils/feature-flag-decorators"
import { BaseEntity } from "../interfaces/models/base-entity"
@@ -86,7 +86,7 @@ export class Order extends BaseEntity {
@Index()
@Column()
@Generated(resolveDbGenerationStrategy("increment"))
@Generated("increment")
display_id: number
@Index()
@@ -1,52 +1,12 @@
import { Column, ColumnOptions, ColumnType } from "typeorm"
import path from "path"
import { getConfigFile } from "medusa-core-utils"
const pgSqliteTypeMapping: { [key: string]: ColumnType } = {
increment: "rowid",
timestamptz: "datetime",
jsonb: "simple-json",
enum: "text",
}
const pgSqliteGenerationMapping: {
[key: string]: "increment" | "uuid" | "rowid"
} = {
increment: "rowid",
}
let dbType: string
export function resolveDbType(pgSqlType: ColumnType): ColumnType {
if (!dbType) {
const { configModule } = getConfigFile(
path.resolve("."),
`medusa-config`
) as any
dbType = configModule?.projectConfig?.database_type || "postgres"
}
if (dbType === "sqlite" && (pgSqlType as string) in pgSqliteTypeMapping) {
return pgSqliteTypeMapping[pgSqlType.toString()]
}
return pgSqlType
}
export function resolveDbGenerationStrategy(
pgSqlType: "increment" | "uuid" | "rowid"
): "increment" | "uuid" | "rowid" {
if (!dbType) {
const { configModule } = getConfigFile(
path.resolve("."),
`medusa-config`
) as any
dbType = configModule?.projectConfig?.database_type || "postgres"
}
if (dbType === "sqlite" && pgSqlType in pgSqliteTypeMapping) {
return pgSqliteGenerationMapping[pgSqlType]
}
return pgSqlType
}
@@ -1,29 +1,5 @@
import { getConfigFile } from "medusa-core-utils"
import path from "path"
import { getConnection } from "typeorm"
export async function manualAutoIncrement(
tableName: string
): Promise<number | null> {
const { configModule } = getConfigFile(
path.resolve("."),
`medusa-config`
) as any
const dbType = configModule?.projectConfig?.database_type || "postgres"
if (dbType === "sqlite") {
const connection = getConnection()
const [rec] = await connection.query(
`SELECT MAX(rowid) as mr FROM "${tableName}"`
)
let mr = 0
if (rec && rec.mr) {
mr = rec.mr
}
return mr + 1
}
return null
}
+3 -1
View File
@@ -31,11 +31,13 @@ export type ProjectConfigOptions = {
cookie_secret?: string
database_url?: string
database_type: string
database_database?: string
database_schema?: string
database_logging: LoggerOptions
// @deprecated - only postgres is supported, so this config has no effect
database_type?: string
http_compression?: HttpCompressionOptions
database_extra?: Record<string, unknown> & {
@@ -1,52 +1,12 @@
import path from "path"
import { Column, ColumnOptions, ColumnType } from "typeorm"
import getConfigFile from "./get-config-file"
const pgSqliteTypeMapping: { [key: string]: ColumnType } = {
increment: "rowid",
timestamptz: "datetime",
jsonb: "simple-json",
enum: "text",
}
const pgSqliteGenerationMapping: {
[key: string]: "increment" | "uuid" | "rowid"
} = {
increment: "rowid",
}
let dbType: string
export function resolveDbType(pgSqlType: ColumnType): ColumnType {
if (!dbType) {
const { configModule } = getConfigFile(
path.resolve("."),
`medusa-config`
) as any
dbType = configModule?.projectConfig?.database_type || "postgres"
}
if (dbType === "sqlite" && (pgSqlType as string) in pgSqliteTypeMapping) {
return pgSqliteTypeMapping[pgSqlType.toString()]
}
return pgSqlType
}
export function resolveDbGenerationStrategy(
pgSqlType: "increment" | "uuid" | "rowid"
): "increment" | "uuid" | "rowid" {
if (!dbType) {
const { configModule } = getConfigFile(
path.resolve("."),
`medusa-config`
) as any
dbType = configModule?.projectConfig?.database_type || "postgres"
}
if (dbType === "sqlite" && pgSqlType in pgSqliteTypeMapping) {
return pgSqliteGenerationMapping[pgSqlType]
}
return pgSqlType
}