New Model & tokens/credits updates
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
# Generated manually for AI Model & Cost Configuration System
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('billing', '0018_update_operation_choices'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
# Step 1: Create AIModelConfig table using raw SQL
|
||||
migrations.RunSQL(
|
||||
sql="""
|
||||
CREATE TABLE "igny8_ai_model_config" (
|
||||
"id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
|
||||
"model_name" varchar(100) NOT NULL UNIQUE,
|
||||
"provider" varchar(50) NOT NULL,
|
||||
"model_type" varchar(20) NOT NULL,
|
||||
"cost_per_1k_input_tokens" numeric(10, 6) NOT NULL,
|
||||
"cost_per_1k_output_tokens" numeric(10, 6) NOT NULL,
|
||||
"tokens_per_credit" numeric(10, 2) NOT NULL,
|
||||
"display_name" varchar(200) NOT NULL,
|
||||
"is_active" boolean NOT NULL DEFAULT true,
|
||||
"is_default" boolean NOT NULL DEFAULT false,
|
||||
"created_at" timestamp with time zone NOT NULL DEFAULT NOW(),
|
||||
"updated_at" timestamp with time zone NOT NULL DEFAULT NOW()
|
||||
);
|
||||
CREATE INDEX "igny8_ai_model_config_model_name_75645c19_like"
|
||||
ON "igny8_ai_model_config" ("model_name" varchar_pattern_ops);
|
||||
""",
|
||||
reverse_sql='DROP TABLE "igny8_ai_model_config";',
|
||||
),
|
||||
|
||||
# Step 2: Modify CreditUsageLog table
|
||||
migrations.RunSQL(
|
||||
sql="""
|
||||
-- Add model_name column (copy of old model_used for backward compat)
|
||||
ALTER TABLE igny8_credit_usage_logs ADD COLUMN model_name varchar(100);
|
||||
UPDATE igny8_credit_usage_logs SET model_name = model_used;
|
||||
|
||||
-- Make old model_used nullable
|
||||
ALTER TABLE igny8_credit_usage_logs ALTER COLUMN model_used DROP NOT NULL;
|
||||
|
||||
-- Add cost tracking fields
|
||||
ALTER TABLE igny8_credit_usage_logs ADD COLUMN cost_usd_input numeric(10, 6) NULL;
|
||||
ALTER TABLE igny8_credit_usage_logs ADD COLUMN cost_usd_output numeric(10, 6) NULL;
|
||||
ALTER TABLE igny8_credit_usage_logs ADD COLUMN cost_usd_total numeric(10, 6) NULL;
|
||||
|
||||
-- Add model_config FK
|
||||
ALTER TABLE igny8_credit_usage_logs ADD COLUMN model_config_id bigint NULL;
|
||||
ALTER TABLE igny8_credit_usage_logs
|
||||
ADD CONSTRAINT "igny8_credit_usage_l_model_config_id_fk_igny8_ai_"
|
||||
FOREIGN KEY (model_config_id) REFERENCES igny8_ai_model_config(id)
|
||||
DEFERRABLE INITIALLY DEFERRED;
|
||||
CREATE INDEX "igny8_credit_usage_logs_model_config_id_idx"
|
||||
ON "igny8_credit_usage_logs" ("model_config_id");
|
||||
|
||||
-- Rename old model_used to avoid conflicts
|
||||
ALTER TABLE igny8_credit_usage_logs RENAME COLUMN model_used TO model_used_old;
|
||||
""",
|
||||
reverse_sql="""
|
||||
ALTER TABLE igny8_credit_usage_logs RENAME COLUMN model_used_old TO model_used;
|
||||
ALTER TABLE igny8_credit_usage_logs DROP CONSTRAINT "igny8_credit_usage_l_model_config_id_fk_igny8_ai_";
|
||||
DROP INDEX "igny8_credit_usage_logs_model_config_id_idx";
|
||||
ALTER TABLE igny8_credit_usage_logs DROP COLUMN model_config_id;
|
||||
ALTER TABLE igny8_credit_usage_logs DROP COLUMN cost_usd_total;
|
||||
ALTER TABLE igny8_credit_usage_logs DROP COLUMN cost_usd_output;
|
||||
ALTER TABLE igny8_credit_usage_logs DROP COLUMN cost_usd_input;
|
||||
ALTER TABLE igny8_credit_usage_logs DROP COLUMN model_name;
|
||||
""",
|
||||
),
|
||||
|
||||
# Step 3: Modify CreditCostConfig table
|
||||
migrations.RunSQL(
|
||||
sql="""
|
||||
ALTER TABLE igny8_credit_cost_config ADD COLUMN default_model_id bigint NULL;
|
||||
ALTER TABLE igny8_credit_cost_config
|
||||
ADD CONSTRAINT "igny8_credit_cost_co_default_model_id_fk_igny8_ai_"
|
||||
FOREIGN KEY (default_model_id) REFERENCES igny8_ai_model_config(id)
|
||||
DEFERRABLE INITIALLY DEFERRED;
|
||||
CREATE INDEX "igny8_credit_cost_config_default_model_id_idx"
|
||||
ON "igny8_credit_cost_config" ("default_model_id");
|
||||
""",
|
||||
reverse_sql="""
|
||||
ALTER TABLE igny8_credit_cost_config DROP CONSTRAINT "igny8_credit_cost_co_default_model_id_fk_igny8_ai_";
|
||||
DROP INDEX "igny8_credit_cost_config_default_model_id_idx";
|
||||
ALTER TABLE igny8_credit_cost_config DROP COLUMN default_model_id;
|
||||
""",
|
||||
),
|
||||
|
||||
# Step 4: Update model state (tell Django about the new structure)
|
||||
migrations.SeparateDatabaseAndState(
|
||||
state_operations=[
|
||||
migrations.CreateModel(
|
||||
name='AIModelConfig',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('model_name', models.CharField(help_text='Technical model identifier', max_length=100, unique=True)),
|
||||
('provider', models.CharField(choices=[('openai', 'OpenAI'), ('anthropic', 'Anthropic'), ('runware', 'Runware'), ('other', 'Other')], max_length=50)),
|
||||
('model_type', models.CharField(choices=[('text', 'Text Generation'), ('image', 'Image Generation'), ('embedding', 'Embeddings')], max_length=20)),
|
||||
('cost_per_1k_input_tokens', models.DecimalField(decimal_places=6, help_text='Cost in USD per 1,000 input tokens', max_digits=10)),
|
||||
('cost_per_1k_output_tokens', models.DecimalField(decimal_places=6, help_text='Cost in USD per 1,000 output tokens', max_digits=10)),
|
||||
('tokens_per_credit', models.DecimalField(decimal_places=2, help_text='How many tokens equal 1 credit', max_digits=10)),
|
||||
('display_name', models.CharField(help_text='Human-readable model name', max_length=200)),
|
||||
('is_active', models.BooleanField(default=True)),
|
||||
('is_default', models.BooleanField(default=False)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
],
|
||||
options={
|
||||
'db_table': 'igny8_ai_model_config',
|
||||
'ordering': ['provider', 'model_name'],
|
||||
},
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='creditusagelog',
|
||||
name='model_name',
|
||||
field=models.CharField(blank=True, max_length=100),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='creditusagelog',
|
||||
name='cost_usd_input',
|
||||
field=models.DecimalField(blank=True, decimal_places=6, max_digits=10, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='creditusagelog',
|
||||
name='cost_usd_output',
|
||||
field=models.DecimalField(blank=True, decimal_places=6, max_digits=10, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='creditusagelog',
|
||||
name='cost_usd_total',
|
||||
field=models.DecimalField(blank=True, decimal_places=6, max_digits=10, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='creditusagelog',
|
||||
name='model_config',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='usage_logs', to='billing.aimodelconfig', db_column='model_config_id'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='creditcostconfig',
|
||||
name='unit',
|
||||
field=models.CharField(choices=[('per_operation', 'Per Operation'), ('per_item', 'Per Item'), ('per_100_tokens', 'Per 100 Tokens'), ('per_1000_tokens', 'Per 1000 Tokens')], default='per_operation', max_length=50),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='creditcostconfig',
|
||||
name='default_model',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='cost_configs', to='billing.aimodelconfig'),
|
||||
),
|
||||
],
|
||||
database_operations=[], # Already done with RunSQL above
|
||||
),
|
||||
]
|
||||
Reference in New Issue
Block a user