<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Moundher Bouroumana</title>
    <description>The latest articles on DEV Community by Moundher Bouroumana (@moundher_bouroumana_e81f6).</description>
    <link>https://dev.to/moundher_bouroumana_e81f6</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2780371%2Fa911d67e-513f-4024-bb03-5193f6649da6.png</url>
      <title>DEV Community: Moundher Bouroumana</title>
      <link>https://dev.to/moundher_bouroumana_e81f6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/moundher_bouroumana_e81f6"/>
    <language>en</language>
    <item>
      <title>django-schema-history package</title>
      <dc:creator>Moundher Bouroumana</dc:creator>
      <pubDate>Sat, 15 Feb 2025 22:21:40 +0000</pubDate>
      <link>https://dev.to/moundher_bouroumana_e81f6/django-schema-history-package-li7</link>
      <guid>https://dev.to/moundher_bouroumana_e81f6/django-schema-history-package-li7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Managing database schema changes is a crucial aspect of Django development. Keeping track of modifications such as adding, removing, or altering fields can be challenging, especially in collaborative projects. To simplify this process, we introduce &lt;strong&gt;Django-Schema-History&lt;/strong&gt;, a package that automatically records schema changes for your Django models.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use Django-Schema-History?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Automatic tracking&lt;/strong&gt;: Monitors and logs changes in your database schema.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easy integration&lt;/strong&gt;: Works seamlessly with Django's migration system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Historical insights&lt;/strong&gt;: Provides a structured record of schema modifications for debugging and auditing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Admin integration&lt;/strong&gt;: Allows reviewing schema changes directly in the Django admin panel.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;To install the package, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;django-schema-history
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, add it to your &lt;code&gt;INSTALLED_APPS&lt;/code&gt; in &lt;code&gt;settings.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;INSTALLED_APPS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="bp"&gt;...&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;schema_history&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run migrations to create the necessary tables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py migrate schema_history
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How It Works
&lt;/h2&gt;

&lt;p&gt;Django-Schema-History captures schema changes by analyzing migration operations. It detects modifications such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AddField&lt;/strong&gt;: When a new field is added.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RemoveField&lt;/strong&gt;: When an existing field is removed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AlterField&lt;/strong&gt;: When a field is modified.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These changes are stored in the &lt;code&gt;SchemaChange&lt;/code&gt; model, which records:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The model name&lt;/li&gt;
&lt;li&gt;The field name&lt;/li&gt;
&lt;li&gt;The type of change (added, removed, or modified)&lt;/li&gt;
&lt;li&gt;The timestamp of the change&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;After installing the package, schema changes will be tracked automatically. You can access them via Django’s shell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py track_migration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example Output
&lt;/h2&gt;

&lt;p&gt;Suppose you add a new field &lt;code&gt;email&lt;/code&gt; to the &lt;code&gt;User&lt;/code&gt; model and run migrations. The following entry will be recorded:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;Field&lt;/th&gt;
&lt;th&gt;Change Type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;User&lt;/td&gt;
&lt;td&gt;email&lt;/td&gt;
&lt;td&gt;added_field&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Similarly, if you remove a field or modify an existing one, the changes will be logged accordingly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Contributing
&lt;/h2&gt;

&lt;p&gt;Contributions are welcome! If you find issues or want to suggest improvements:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Fork the repository&lt;/li&gt;
&lt;li&gt;Create a new branch (&lt;code&gt;feature-name&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Submit a pull request&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Django-Schema-History makes tracking database changes effortless. Whether you're working solo or in a team, having a historical record of schema modifications can be invaluable. Install it today and simplify your Django development workflow!&lt;/p&gt;




&lt;p&gt;For more details, visit our GitHub repository: [&lt;a href="https://github.com/Moundher122/django-schema-history" rel="noopener noreferrer"&gt;https://github.com/Moundher122/django-schema-history&lt;/a&gt;]&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
