feat(medusa): Remove sqlite support (#4026)
This commit is contained in:
1 parent
e2d29d35c4
commit
a91987fab3
17 files changed
+148
-415
No files matched your search
@@ -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")
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user