# How to prevent same migration timestamps when scripting?

**URL:** <https://www.ruby-forum.com/t/how-to-prevent-same-migration-timestamps-when-scripting/145756>\
**Category:** Rails\
**Created:** [August 13, 2008, 6:31pm UTC](https://www.ruby-forum.com/t/how-to-prevent-same-migration-timestamps-when-scripting/145756 "2008-08-13T18:31:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DarkTatka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e19adc/32.png) [@DarkTatka](https://www.ruby-forum.com/u/DarkTatka)\
**Post date:** [August 13, 2008, 6:31pm UTC](https://www.ruby-forum.com/t/how-to-prevent-same-migration-timestamps-when-scripting/145756/1 "2008-08-13T18:31:23Z")

</div>

Hi,  
recently i had a need to generate lots of migrations and models from  
csv fixtures. I wrote a generator script.  
The problem is, it runs so “fast” that all migrations are of the same  
timestamp, so i cant migrate.

#main generator file  
…  
def manifest  
record do |m|  
prepared\_migrations.each do |key, value|  
value.shift  
m.migration\_template ‘lib/migration\_template.rb’, ‘db/  
migrate’,  
:migration\_file\_name =\> “create\_#{key.downcase}”,  
:assigns =\> {:table =\> key, :colums =\> value, }  
end  
end  
end  
…

# migration\_tempate.rb

class \<%=“Create#{table.capitalize}”.underscore.camelize %\> \<  
ActiveRecord::Migration  
def self.up  
create\_table :\<%= table.downcase %\> do |t|\<%- colums.each do |  
value| -%\>  
t.string :\<%= value%\>\<%- end -%\>  
end  
end  
end

Prepared migrations is a hash “table\_name” =\> [“column\_name”,  
“column\_name”, … ]  
is there a “take a break” option in migrations? or do i have to  
migrate into one BIG migration?  
best regards  
DarkTatka

---

<div class="post-metadata">

**Author:** ![DarkTatka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e19adc/32.png) [@DarkTatka](https://www.ruby-forum.com/u/DarkTatka)\
**Post date:** [August 13, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/how-to-prevent-same-migration-timestamps-when-scripting/145756/2 "2008-08-13T18:49:36Z")

</div>

> ```
> prepared_migrations.each do |key, value|
> 
> ```
> 
> # migration\_tempate.rb
> 
> Prepared migrations is a hash “table\_name” =\> [“column\_name”,  
> “column\_name”, … ]  
> is there a “take a break” option in migrations? or do i have to  
> migrate into one BIG migration?

How many is a lot? If it’s not that many and you don’t do it that  
often just sleep for one second up in your loop when you’re generating  
the files. Or fake it by checking the file name and if there is one  
with the same timestamp add a second.

Or better yet, start with Time.now.to\_i and then for each migration  
add one to that value and use that…

-philip

---

<div class="post-metadata">

**Author:** ![DarkTatka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e19adc/32.png) [@DarkTatka](https://www.ruby-forum.com/u/DarkTatka)\
**Post date:** [August 13, 2008, 7:21pm UTC](https://www.ruby-forum.com/t/how-to-prevent-same-migration-timestamps-when-scripting/145756/3 "2008-08-13T19:21:48Z")

</div>

On 13 Aug 2008, at 17:30, DarkTatka wrote:

> Hi,  
> recently i had a need to generate lots of migrations and models from  
> csv fixtures. I wrote a generator script.  
> The problem is, it runs so “fast” that all migrations are of the same  
> timestamp, so i cant migrate.  
> Evil thought: overwrite Time.now by increasing it by one second after  
> each migration is generated. mwahahahahaha.

Fred

---

<div class="post-metadata">

**Author:** ![DarkTatka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e19adc/32.png) [@DarkTatka](https://www.ruby-forum.com/u/DarkTatka)\
**Post date:** [August 13, 2008, 10:45pm UTC](https://www.ruby-forum.com/t/how-to-prevent-same-migration-timestamps-when-scripting/145756/4 "2008-08-13T22:45:31Z")

</div>

> I consider it a bit of a “hack”, but will be using it until i find  
> something better (or someone else here does)  
> Isn’t what your doing a bit of a “hack” anyway? So what’s your point,  
> hehe.

Please don’t take that the wrong way. I was just kidding around.  
However, the older incrementing migrations had other (maybe more serious  
issues) and I don’t think it was intended for multiple migrations to  
happen within the same second. Although that is a known limitation. So I  
wouldn’t consider generating your own time stamps any more of a “hack”  
than the problem you’re trying to solve. It sounds like a workable  
solution to me.

DarkTatka wrote:

> Philip,  
> thanks for sugestions, but when i insert “sleep 2” anywhere in the  
> record block,  
> it 1) takes incredibly long,  
> 2) migrations are still timestamped the same, since the record  
> block “records” with waiting,  
> but “plays” without it.  
> generating my own timestamps should work.  
> I consider it a bit of a “hack”, but will be using it until i find  
> something better (or someone else here does)
> 
> PS - lot means 100+, has to run once.

---

<div class="post-metadata">

**Author:** ![DarkTatka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e19adc/32.png) [@DarkTatka](https://www.ruby-forum.com/u/DarkTatka)\
**Post date:** [August 13, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/how-to-prevent-same-migration-timestamps-when-scripting/145756/5 "2008-08-13T19:28:51Z")

</div>

Philip,  
thanks for sugestions, but when i insert “sleep 2” anywhere in the  
record block,  
it 1) takes incredibly long,  
2) migrations are still timestamped the same, since the record  
block “records” with waiting,  
but “plays” without it.  
generating my own timestamps should work.  
I consider it a bit of a “hack”, but will be using it until i find  
something better (or someone else here does)

PS - lot means 100+, has to run once.
